static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,

                                int64_t pos, int size, int load)

{

    bool create;

    int fd, ret = 0, remaining = size;

    unsigned int data_len;

    uint64_t vmstate_oid;

    uint32_t vdi_index;

    uint64_t offset;



    fd = connect_to_sdog(s->addr, s->port);

    if (fd < 0) {

        return fd;

    }



    while (remaining) {

        vdi_index = pos / SD_DATA_OBJ_SIZE;

        offset = pos % SD_DATA_OBJ_SIZE;



        data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);



        vmstate_oid = vid_to_vmstate_oid(s->inode.vdi_id, vdi_index);



        create = (offset == 0);

        if (load) {

            ret = read_object(fd, (char *)data, vmstate_oid,

                              s->inode.nr_copies, data_len, offset,

                              s->cache_enabled);

        } else {

            ret = write_object(fd, (char *)data, vmstate_oid,

                               s->inode.nr_copies, data_len, offset, create,

                               s->cache_enabled);

        }



        if (ret < 0) {

            error_report("failed to save vmstate %s", strerror(errno));

            goto cleanup;

        }



        pos += data_len;

        data += data_len;

        remaining -= data_len;

    }

    ret = size;

cleanup:

    closesocket(fd);

    return ret;

}

static int do_load_save_vmstate(BDRVSheepdogState *s, uint8_t *data,

                                int64_t pos, int size, int load)

{

    bool create;

    int fd, ret = 0, remaining = size;

    unsigned int data_len;

    uint64_t vmstate_oid;

    uint32_t vdi_index;

    uint64_t offset;



    fd = connect_to_sdog(s->addr, s->port);

    if (fd < 0) {

        return fd;

    }



    while (remaining) {

        vdi_index = pos / SD_DATA_OBJ_SIZE;

        offset = pos % SD_DATA_OBJ_SIZE;



        data_len = MIN(remaining, SD_DATA_OBJ_SIZE - offset);



        vmstate_oid = vid_to_vmstate_oid(s->inode.vdi_id, vdi_index);



        create = (offset == 0);

        if (load) {

            ret = read_object(fd, (char *)data, vmstate_oid,

                              s->inode.nr_copies, data_len, offset,

                              s->cache_enabled);

        } else {

            ret = write_object(fd, (char *)data, vmstate_oid,

                               s->inode.nr_copies, data_len, offset, create,

                               s->cache_enabled);

        }



        if (ret < 0) {

            error_report("failed to save vmstate %s", strerror(errno));

            goto cleanup;

        }



        pos += data_len;

        data += data_len;

        remaining -= data_len;

    }

    ret = size;

cleanup:

    closesocket(fd);

    return ret;

}

static int FUNC_0(BDRVSheepdogState *VAR_0, uint8_t *VAR_1,

                                int64_t VAR_2, int VAR_3, int VAR_4)

{

    bool create;

    int VAR_5, VAR_6 = 0, VAR_7 = VAR_3;

    unsigned int VAR_8;

    uint64_t vmstate_oid;

    uint32_t vdi_index;

    uint64_t offset;



    VAR_5 = connect_to_sdog(VAR_0->addr, VAR_0->port);

    if (VAR_5 < 0) {

        return VAR_5;

    }



    while (VAR_7) {

        vdi_index = VAR_2 / SD_DATA_OBJ_SIZE;

        offset = VAR_2 % SD_DATA_OBJ_SIZE;



        VAR_8 = MIN(VAR_7, SD_DATA_OBJ_SIZE - offset);



        vmstate_oid = vid_to_vmstate_oid(VAR_0->inode.vdi_id, vdi_index);



        create = (offset == 0);

        if (VAR_4) {

            VAR_6 = read_object(VAR_5, (char *)VAR_1, vmstate_oid,

                              VAR_0->inode.nr_copies, VAR_8, offset,

                              VAR_0->cache_enabled);

        } else {

            VAR_6 = write_object(VAR_5, (char *)VAR_1, vmstate_oid,

                               VAR_0->inode.nr_copies, VAR_8, offset, create,

                               VAR_0->cache_enabled);

        }



        if (VAR_6 < 0) {

            error_report("failed to save vmstate %VAR_0", strerror(errno));

            goto cleanup;

        }



        VAR_2 += VAR_8;

        VAR_1 += VAR_8;

        VAR_7 -= VAR_8;

    }

    VAR_6 = VAR_3;

cleanup:

    closesocket(VAR_5);

    return VAR_6;

}

static int FUNC_0(BDRVSheepdogState *VAR_0, uint8_t *VAR_1,
int64_t VAR_2, int VAR_3, int VAR_4)
{ bool create; int VAR_5, VAR_6 = 0, VAR_7 = VAR_3; unsigned int VAR_8; uint64_t vmstate_oid; uint32_t vdi_index; uint64_t offset; VAR_5 = connect_to_sdog(VAR_0->addr, VAR_0->port); if (VAR_5 < 0) { return VAR_5; } while (VAR_7) { vdi_index = VAR_2 / SD_DATA_OBJ_SIZE; offset = VAR_2 % SD_DATA_OBJ_SIZE; VAR_8 = MIN(VAR_7, SD_DATA_OBJ_SIZE - offset); vmstate_oid = vid_to_vmstate_oid(VAR_0->inode.vdi_id, vdi_index); create = (offset == 0); if (VAR_4) { VAR_6 = read_object(VAR_5, (char *)VAR_1, vmstate_oid,
VAR_0->inode.nr_copies, VAR_8, offset,
VAR_0->cache_enabled); } else { VAR_6 = write_object(VAR_5, (char *)VAR_1, vmstate_oid,
VAR_0->inode.nr_copies, VAR_8, offset, create,
VAR_0->cache_enabled); } if (VAR_6 < 0) { error_report("failed to save vmstate %VAR_0", strerror(errno)); goto cleanup; } VAR_2 += VAR_8; VAR_1 += VAR_8; VAR_7 -= VAR_8; } VAR_6 = VAR_3; cleanup:
closesocket(VAR_5); return VAR_6; }