static int qemu_rdma_register_and_get_keys(RDMAContext *rdma,

        RDMALocalBlock *block, uintptr_t host_addr,

        uint32_t *lkey, uint32_t *rkey, int chunk,

        uint8_t *chunk_start, uint8_t *chunk_end)

{

    if (block->mr) {

        if (lkey) {

            *lkey = block->mr->lkey;

        }

        if (rkey) {

            *rkey = block->mr->rkey;

        }

        return 0;

    }



    /* allocate memory to store chunk MRs */

    if (!block->pmr) {

        block->pmr = g_malloc0(block->nb_chunks * sizeof(struct ibv_mr *));

    }



    /*

     * If 'rkey', then we're the destination, so grant access to the source.

     *

     * If 'lkey', then we're the source VM, so grant access only to ourselves.

     */

    if (!block->pmr[chunk]) {

        uint64_t len = chunk_end - chunk_start;



        trace_qemu_rdma_register_and_get_keys(len, chunk_start);



        block->pmr[chunk] = ibv_reg_mr(rdma->pd,

                chunk_start, len,

                (rkey ? (IBV_ACCESS_LOCAL_WRITE |

                        IBV_ACCESS_REMOTE_WRITE) : 0));



        if (!block->pmr[chunk]) {

            perror("Failed to register chunk!");

            fprintf(stderr, "Chunk details: block: %d chunk index %d"

                            " start %" PRIuPTR " end %" PRIuPTR

                            " host %" PRIuPTR

                            " local %" PRIuPTR " registrations: %d\n",

                            block->index, chunk, (uintptr_t)chunk_start,

                            (uintptr_t)chunk_end, host_addr,

                            (uintptr_t)block->local_host_addr,

                            rdma->total_registrations);

            return -1;

        }

        rdma->total_registrations++;

    }



    if (lkey) {

        *lkey = block->pmr[chunk]->lkey;

    }

    if (rkey) {

        *rkey = block->pmr[chunk]->rkey;

    }

    return 0;

}

static int qemu_rdma_register_and_get_keys(RDMAContext *rdma,

        RDMALocalBlock *block, uintptr_t host_addr,

        uint32_t *lkey, uint32_t *rkey, int chunk,

        uint8_t *chunk_start, uint8_t *chunk_end)

{

    if (block->mr) {

        if (lkey) {

            *lkey = block->mr->lkey;

        }

        if (rkey) {

            *rkey = block->mr->rkey;

        }

        return 0;

    }



    

    if (!block->pmr) {

        block->pmr = g_malloc0(block->nb_chunks * sizeof(struct ibv_mr *));

    }



    









    if (!block->pmr[chunk]) {

        uint64_t len = chunk_end - chunk_start;



        trace_qemu_rdma_register_and_get_keys(len, chunk_start);



        block->pmr[chunk] = ibv_reg_mr(rdma->pd,

                chunk_start, len,

                (rkey ? (IBV_ACCESS_LOCAL_WRITE |

                        IBV_ACCESS_REMOTE_WRITE) : 0));



        if (!block->pmr[chunk]) {

            perror("Failed to register chunk!");

            fprintf(stderr, "Chunk details: block: %d chunk index %d"

                            " start %" PRIuPTR " end %" PRIuPTR

                            " host %" PRIuPTR

                            " local %" PRIuPTR " registrations: %d\n",

                            block->index, chunk, (uintptr_t)chunk_start,

                            (uintptr_t)chunk_end, host_addr,

                            (uintptr_t)block->local_host_addr,

                            rdma->total_registrations);

            return -1;

        }

        rdma->total_registrations++;

    }



    if (lkey) {

        *lkey = block->pmr[chunk]->lkey;

    }

    if (rkey) {

        *rkey = block->pmr[chunk]->rkey;

    }

    return 0;

}

        block->pmr = g_malloc0(block->nb_chunks * sizeof(struct ibv_mr *));
static int FUNC_0(RDMAContext *VAR_0,

        RDMALocalBlock *VAR_1, uintptr_t VAR_2,

        uint32_t *VAR_3, uint32_t *VAR_4, int VAR_5,

        uint8_t *VAR_6, uint8_t *VAR_7)

{

    if (VAR_1->mr) {

        if (VAR_3) {

            *VAR_3 = VAR_1->mr->VAR_3;

        }

        if (VAR_4) {

            *VAR_4 = VAR_1->mr->VAR_4;

        }

        return 0;

    }



    

    if (!VAR_1->pmr) {

        VAR_1->pmr = g_malloc0(VAR_1->nb_chunks * sizeof(struct ibv_mr *));

    }



    









    if (!VAR_1->pmr[VAR_5]) {

        uint64_t len = VAR_7 - VAR_6;



        trace_qemu_rdma_register_and_get_keys(len, VAR_6);



        VAR_1->pmr[VAR_5] = ibv_reg_mr(VAR_0->pd,

                VAR_6, len,

                (VAR_4 ? (IBV_ACCESS_LOCAL_WRITE |

                        IBV_ACCESS_REMOTE_WRITE) : 0));



        if (!VAR_1->pmr[VAR_5]) {

            perror("Failed to register VAR_5!");

            fprintf(stderr, "Chunk details: VAR_1: %d VAR_5 index %d"

                            " start %" PRIuPTR " end %" PRIuPTR

                            " host %" PRIuPTR

                            " local %" PRIuPTR " registrations: %d\n",

                            VAR_1->index, VAR_5, (uintptr_t)VAR_6,

                            (uintptr_t)VAR_7, VAR_2,

                            (uintptr_t)VAR_1->local_host_addr,

                            VAR_0->total_registrations);

            return -1;

        }

        VAR_0->total_registrations++;

    }



    if (VAR_3) {

        *VAR_3 = VAR_1->pmr[VAR_5]->VAR_3;

    }

    if (VAR_4) {

        *VAR_4 = VAR_1->pmr[VAR_5]->VAR_4;

    }

    return 0;

}

static int FUNC_0(RDMAContext *VAR_0,
RDMALocalBlock *VAR_1, uintptr_t VAR_2,
uint32_t *VAR_3, uint32_t *VAR_4, int VAR_5,
uint8_t *VAR_6, uint8_t *VAR_7)
{ if (VAR_1->mr) { if (VAR_3) { *VAR_3 = VAR_1->mr->VAR_3; } if (VAR_4) { *VAR_4 = VAR_1->mr->VAR_4; } return 0; } if (!VAR_1->pmr) { VAR_1->pmr = g_malloc0(VAR_1->nb_chunks * sizeof(struct ibv_mr *)); } if (!VAR_1->pmr[VAR_5]) { uint64_t len = VAR_7 - VAR_6; trace_qemu_rdma_register_and_get_keys(len, VAR_6); VAR_1->pmr[VAR_5] = ibv_reg_mr(VAR_0->pd,
VAR_6, len,
(VAR_4 ? (IBV_ACCESS_LOCAL_WRITE |
IBV_ACCESS_REMOTE_WRITE) : 0)); if (!VAR_1->pmr[VAR_5]) { perror("Failed to register VAR_5!"); fprintf(stderr, "Chunk details: VAR_1: %d VAR_5 index %d"
" start %" PRIuPTR " end %" PRIuPTR
" host %" PRIuPTR
" local %" PRIuPTR " registrations: %d\n",
VAR_1->index, VAR_5, (uintptr_t)VAR_6,
(uintptr_t)VAR_7, VAR_2,
(uintptr_t)VAR_1->local_host_addr,
VAR_0->total_registrations); return -1; } VAR_0->total_registrations++; } if (VAR_3) { *VAR_3 = VAR_1->pmr[VAR_5]->VAR_3; } if (VAR_4) { *VAR_4 = VAR_1->pmr[VAR_5]->VAR_4; } return 0; }