static int ram_save_block(QEMUFile *f, bool last_stage)

{

    RAMBlock *block = last_seen_block;

    ram_addr_t offset = last_offset;

    int bytes_sent = -1;

    MemoryRegion *mr;

    ram_addr_t current_addr;



    if (!block)

        block = QTAILQ_FIRST(&ram_list.blocks);



    do {

        mr = block->mr;

        if (migration_bitmap_test_and_reset_dirty(mr, offset)) {

            uint8_t *p;

            int cont = (block == last_sent_block) ?

                RAM_SAVE_FLAG_CONTINUE : 0;



            p = memory_region_get_ram_ptr(mr) + offset;



            if (is_dup_page(p)) {

                acct_info.dup_pages++;

                save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);

                qemu_put_byte(f, *p);

                bytes_sent = 1;

            } else if (migrate_use_xbzrle()) {

                current_addr = block->offset + offset;

                bytes_sent = save_xbzrle_page(f, p, current_addr, block,

                                              offset, cont, last_stage);

                if (!last_stage) {

                    p = get_cached_data(XBZRLE.cache, current_addr);

                }

            }



            /* either we didn't send yet (we may have had XBZRLE overflow) */

            if (bytes_sent == -1) {

                save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);

                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);

                bytes_sent = TARGET_PAGE_SIZE;

                acct_info.norm_pages++;

            }



            /* if page is unmodified, continue to the next */

            if (bytes_sent != 0) {

                last_sent_block = block;

                break;

            }

        }



        offset += TARGET_PAGE_SIZE;

        if (offset >= block->length) {

            offset = 0;

            block = QTAILQ_NEXT(block, next);

            if (!block)

                block = QTAILQ_FIRST(&ram_list.blocks);

        }

    } while (block != last_seen_block || offset != last_offset);



    last_seen_block = block;

    last_offset = offset;



    return bytes_sent;

}

static int ram_save_block(QEMUFile *f, bool last_stage)

{

    RAMBlock *block = last_seen_block;

    ram_addr_t offset = last_offset;

    int bytes_sent = -1;

    MemoryRegion *mr;

    ram_addr_t current_addr;



    if (!block)

        block = QTAILQ_FIRST(&ram_list.blocks);



    do {

        mr = block->mr;

        if (migration_bitmap_test_and_reset_dirty(mr, offset)) {

            uint8_t *p;

            int cont = (block == last_sent_block) ?

                RAM_SAVE_FLAG_CONTINUE : 0;



            p = memory_region_get_ram_ptr(mr) + offset;



            if (is_dup_page(p)) {

                acct_info.dup_pages++;

                save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_COMPRESS);

                qemu_put_byte(f, *p);

                bytes_sent = 1;

            } else if (migrate_use_xbzrle()) {

                current_addr = block->offset + offset;

                bytes_sent = save_xbzrle_page(f, p, current_addr, block,

                                              offset, cont, last_stage);

                if (!last_stage) {

                    p = get_cached_data(XBZRLE.cache, current_addr);

                }

            }



            

            if (bytes_sent == -1) {

                save_block_hdr(f, block, offset, cont, RAM_SAVE_FLAG_PAGE);

                qemu_put_buffer(f, p, TARGET_PAGE_SIZE);

                bytes_sent = TARGET_PAGE_SIZE;

                acct_info.norm_pages++;

            }



            

            if (bytes_sent != 0) {

                last_sent_block = block;

                break;

            }

        }



        offset += TARGET_PAGE_SIZE;

        if (offset >= block->length) {

            offset = 0;

            block = QTAILQ_NEXT(block, next);

            if (!block)

                block = QTAILQ_FIRST(&ram_list.blocks);

        }

    } while (block != last_seen_block || offset != last_offset);



    last_seen_block = block;

    last_offset = offset;



    return bytes_sent;

}

static int FUNC_0(QEMUFile *VAR_0, bool VAR_1)

{

    RAMBlock *block = last_seen_block;

    ram_addr_t offset = last_offset;

    int VAR_2 = -1;

    MemoryRegion *mr;

    ram_addr_t current_addr;



    if (!block)

        block = QTAILQ_FIRST(&ram_list.blocks);



    do {

        mr = block->mr;

        if (migration_bitmap_test_and_reset_dirty(mr, offset)) {

            uint8_t *p;

            int VAR_3 = (block == last_sent_block) ?

                RAM_SAVE_FLAG_CONTINUE : 0;



            p = memory_region_get_ram_ptr(mr) + offset;



            if (is_dup_page(p)) {

                acct_info.dup_pages++;

                save_block_hdr(VAR_0, block, offset, VAR_3, RAM_SAVE_FLAG_COMPRESS);

                qemu_put_byte(VAR_0, *p);

                VAR_2 = 1;

            } else if (migrate_use_xbzrle()) {

                current_addr = block->offset + offset;

                VAR_2 = save_xbzrle_page(VAR_0, p, current_addr, block,

                                              offset, VAR_3, VAR_1);

                if (!VAR_1) {

                    p = get_cached_data(XBZRLE.cache, current_addr);

                }

            }



            

            if (VAR_2 == -1) {

                save_block_hdr(VAR_0, block, offset, VAR_3, RAM_SAVE_FLAG_PAGE);

                qemu_put_buffer(VAR_0, p, TARGET_PAGE_SIZE);

                VAR_2 = TARGET_PAGE_SIZE;

                acct_info.norm_pages++;

            }



            

            if (VAR_2 != 0) {

                last_sent_block = block;

                break;

            }

        }



        offset += TARGET_PAGE_SIZE;

        if (offset >= block->length) {

            offset = 0;

            block = QTAILQ_NEXT(block, next);

            if (!block)

                block = QTAILQ_FIRST(&ram_list.blocks);

        }

    } while (block != last_seen_block || offset != last_offset);



    last_seen_block = block;

    last_offset = offset;



    return VAR_2;

}

static int FUNC_0(QEMUFile *VAR_0, bool VAR_1)
{ RAMBlock *block = last_seen_block; ram_addr_t offset = last_offset; int VAR_2 = -1; MemoryRegion *mr; ram_addr_t current_addr; if (!block)
block = QTAILQ_FIRST(&ram_list.blocks); do { mr = block->mr; if (migration_bitmap_test_and_reset_dirty(mr, offset)) { uint8_t *p; int VAR_3 = (block == last_sent_block) ?
RAM_SAVE_FLAG_CONTINUE : 0; p = memory_region_get_ram_ptr(mr) + offset; if (is_dup_page(p)) { acct_info.dup_pages++; save_block_hdr(VAR_0, block, offset, VAR_3, RAM_SAVE_FLAG_COMPRESS); qemu_put_byte(VAR_0, *p); VAR_2 = 1; } else if (migrate_use_xbzrle()) { current_addr = block->offset + offset; VAR_2 = save_xbzrle_page(VAR_0, p, current_addr, block,
offset, VAR_3, VAR_1); if (!VAR_1) { p = get_cached_data(XBZRLE.cache, current_addr); } } if (VAR_2 == -1) { save_block_hdr(VAR_0, block, offset, VAR_3, RAM_SAVE_FLAG_PAGE); qemu_put_buffer(VAR_0, p, TARGET_PAGE_SIZE); VAR_2 = TARGET_PAGE_SIZE; acct_info.norm_pages++; } if (VAR_2 != 0) { last_sent_block = block; break; } } offset += TARGET_PAGE_SIZE; if (offset >= block->length) { offset = 0; block = QTAILQ_NEXT(block, next); if (!block)
block = QTAILQ_FIRST(&ram_list.blocks); } } while (block != last_seen_block || offset != last_offset); last_seen_block = block; last_offset = offset; return VAR_2; }