static int qemu_rdma_write(QEMUFile *f, RDMAContext *rdma,

                           uint64_t block_offset, uint64_t offset,

                           uint64_t len)

{

    uint64_t current_addr = block_offset + offset;

    uint64_t index = rdma->current_index;

    uint64_t chunk = rdma->current_chunk;

    int ret;



    /* If we cannot merge it, we flush the current buffer first. */

    if (!qemu_rdma_buffer_mergable(rdma, current_addr, len)) {

        ret = qemu_rdma_write_flush(f, rdma);

        if (ret) {

            return ret;

        }

        rdma->current_length = 0;

        rdma->current_addr = current_addr;



        ret = qemu_rdma_search_ram_block(rdma, block_offset,

                                         offset, len, &index, &chunk);

        if (ret) {

            fprintf(stderr, "ram block search failed\n");

            return ret;

        }

        rdma->current_index = index;

        rdma->current_chunk = chunk;

    }



    /* merge it */

    rdma->current_length += len;



    /* flush it if buffer is too large */

    if (rdma->current_length >= RDMA_MERGE_MAX) {

        return qemu_rdma_write_flush(f, rdma);

    }



    return 0;

}

static int qemu_rdma_write(QEMUFile *f, RDMAContext *rdma,

                           uint64_t block_offset, uint64_t offset,

                           uint64_t len)

{

    uint64_t current_addr = block_offset + offset;

    uint64_t index = rdma->current_index;

    uint64_t chunk = rdma->current_chunk;

    int ret;



    

    if (!qemu_rdma_buffer_mergable(rdma, current_addr, len)) {

        ret = qemu_rdma_write_flush(f, rdma);

        if (ret) {

            return ret;

        }

        rdma->current_length = 0;

        rdma->current_addr = current_addr;



        ret = qemu_rdma_search_ram_block(rdma, block_offset,

                                         offset, len, &index, &chunk);

        if (ret) {

            fprintf(stderr, "ram block search failed\n");

            return ret;

        }

        rdma->current_index = index;

        rdma->current_chunk = chunk;

    }



    

    rdma->current_length += len;



    

    if (rdma->current_length >= RDMA_MERGE_MAX) {

        return qemu_rdma_write_flush(f, rdma);

    }



    return 0;

}

static int FUNC_0(QEMUFile *VAR_0, RDMAContext *VAR_1,

                           uint64_t VAR_2, uint64_t VAR_3,

                           uint64_t VAR_4)

{

    uint64_t current_addr = VAR_2 + VAR_3;

    uint64_t index = VAR_1->current_index;

    uint64_t chunk = VAR_1->current_chunk;

    int VAR_5;



    

    if (!qemu_rdma_buffer_mergable(VAR_1, current_addr, VAR_4)) {

        VAR_5 = qemu_rdma_write_flush(VAR_0, VAR_1);

        if (VAR_5) {

            return VAR_5;

        }

        VAR_1->current_length = 0;

        VAR_1->current_addr = current_addr;



        VAR_5 = qemu_rdma_search_ram_block(VAR_1, VAR_2,

                                         VAR_3, VAR_4, &index, &chunk);

        if (VAR_5) {

            fprintf(stderr, "ram block search failed\n");

            return VAR_5;

        }

        VAR_1->current_index = index;

        VAR_1->current_chunk = chunk;

    }



    

    VAR_1->current_length += VAR_4;



    

    if (VAR_1->current_length >= RDMA_MERGE_MAX) {

        return qemu_rdma_write_flush(VAR_0, VAR_1);

    }



    return 0;

}

static int FUNC_0(QEMUFile *VAR_0, RDMAContext *VAR_1,
uint64_t VAR_2, uint64_t VAR_3,
uint64_t VAR_4)
{ uint64_t current_addr = VAR_2 + VAR_3; uint64_t index = VAR_1->current_index; uint64_t chunk = VAR_1->current_chunk; int VAR_5; if (!qemu_rdma_buffer_mergable(VAR_1, current_addr, VAR_4)) { VAR_5 = qemu_rdma_write_flush(VAR_0, VAR_1); if (VAR_5) { return VAR_5; } VAR_1->current_length = 0; VAR_1->current_addr = current_addr; VAR_5 = qemu_rdma_search_ram_block(VAR_1, VAR_2,
VAR_3, VAR_4, &index, &chunk); if (VAR_5) { fprintf(stderr, "ram block search failed\n"); return VAR_5; } VAR_1->current_index = index; VAR_1->current_chunk = chunk; } VAR_1->current_length += VAR_4; if (VAR_1->current_length >= RDMA_MERGE_MAX) { return qemu_rdma_write_flush(VAR_0, VAR_1); } return 0; }