static int raw_pwrite(BlockDriverState *bs, int64_t offset,

                      const uint8_t *buf, int count)

{

    BDRVRawState *s = bs->opaque;

    int size, ret, shift, sum;



    sum = 0;



    if (s->aligned_buf != NULL) {



        if (offset & 0x1ff) {

            /* align offset on a 512 bytes boundary */

            shift = offset & 0x1ff;

            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);

            if (ret < 0)

                return ret;



            size = 512 - shift;

            if (size > count)

                size = count;

            memcpy(s->aligned_buf + shift, buf, size);



            ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);

            if (ret < 0)

                return ret;



            buf += size;

            offset += size;

            count -= size;

            sum += size;



            if (count == 0)

                return sum;

        }

        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {



            while ((size = (count & ~0x1ff)) != 0) {



                if (size > ALIGNED_BUFFER_SIZE)

                    size = ALIGNED_BUFFER_SIZE;



                memcpy(s->aligned_buf, buf, size);



                ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);

                if (ret < 0)

                    return ret;



                buf += ret;

                offset += ret;

                count -= ret;

                sum += ret;

            }

            /* here, count < 512 because (count & ~0x1ff) == 0 */

            if (count) {

                ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);

                if (ret < 0)

                    return ret;

                 memcpy(s->aligned_buf, buf, count);



                 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);

                 if (ret < 0)

                     return ret;

                 if (count < ret)

                     ret = count;



                 sum += ret;

            }

            return sum;

        }

    }

    return raw_pwrite_aligned(bs, offset, buf, count) + sum;

}

static int raw_pwrite(BlockDriverState *bs, int64_t offset,

                      const uint8_t *buf, int count)

{

    BDRVRawState *s = bs->opaque;

    int size, ret, shift, sum;



    sum = 0;



    if (s->aligned_buf != NULL) {



        if (offset & 0x1ff) {

            

            shift = offset & 0x1ff;

            ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);

            if (ret < 0)

                return ret;



            size = 512 - shift;

            if (size > count)

                size = count;

            memcpy(s->aligned_buf + shift, buf, size);



            ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);

            if (ret < 0)

                return ret;



            buf += size;

            offset += size;

            count -= size;

            sum += size;



            if (count == 0)

                return sum;

        }

        if (count & 0x1ff || (uintptr_t) buf & 0x1ff) {



            while ((size = (count & ~0x1ff)) != 0) {



                if (size > ALIGNED_BUFFER_SIZE)

                    size = ALIGNED_BUFFER_SIZE;



                memcpy(s->aligned_buf, buf, size);



                ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size);

                if (ret < 0)

                    return ret;



                buf += ret;

                offset += ret;

                count -= ret;

                sum += ret;

            }

            

            if (count) {

                ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);

                if (ret < 0)

                    return ret;

                 memcpy(s->aligned_buf, buf, count);



                 ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);

                 if (ret < 0)

                     return ret;

                 if (count < ret)

                     ret = count;



                 sum += ret;

            }

            return sum;

        }

    }

    return raw_pwrite_aligned(bs, offset, buf, count) + sum;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,

                      const uint8_t *VAR_2, int VAR_3)

{

    BDRVRawState *s = VAR_0->opaque;

    int VAR_4, VAR_5, VAR_6, VAR_7;



    VAR_7 = 0;



    if (s->aligned_buf != NULL) {



        if (VAR_1 & 0x1ff) {

            

            VAR_6 = VAR_1 & 0x1ff;

            VAR_5 = raw_pread_aligned(VAR_0, VAR_1 - VAR_6, s->aligned_buf, 512);

            if (VAR_5 < 0)

                return VAR_5;



            VAR_4 = 512 - VAR_6;

            if (VAR_4 > VAR_3)

                VAR_4 = VAR_3;

            memcpy(s->aligned_buf + VAR_6, VAR_2, VAR_4);



            VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1 - VAR_6, s->aligned_buf, 512);

            if (VAR_5 < 0)

                return VAR_5;



            VAR_2 += VAR_4;

            VAR_1 += VAR_4;

            VAR_3 -= VAR_4;

            VAR_7 += VAR_4;



            if (VAR_3 == 0)

                return VAR_7;

        }

        if (VAR_3 & 0x1ff || (uintptr_t) VAR_2 & 0x1ff) {



            while ((VAR_4 = (VAR_3 & ~0x1ff)) != 0) {



                if (VAR_4 > ALIGNED_BUFFER_SIZE)

                    VAR_4 = ALIGNED_BUFFER_SIZE;



                memcpy(s->aligned_buf, VAR_2, VAR_4);



                VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1, s->aligned_buf, VAR_4);

                if (VAR_5 < 0)

                    return VAR_5;



                VAR_2 += VAR_5;

                VAR_1 += VAR_5;

                VAR_3 -= VAR_5;

                VAR_7 += VAR_5;

            }

            

            if (VAR_3) {

                VAR_5 = raw_pread_aligned(VAR_0, VAR_1, s->aligned_buf, 512);

                if (VAR_5 < 0)

                    return VAR_5;

                 memcpy(s->aligned_buf, VAR_2, VAR_3);



                 VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1, s->aligned_buf, 512);

                 if (VAR_5 < 0)

                     return VAR_5;

                 if (VAR_3 < VAR_5)

                     VAR_5 = VAR_3;



                 VAR_7 += VAR_5;

            }

            return VAR_7;

        }

    }

    return raw_pwrite_aligned(VAR_0, VAR_1, VAR_2, VAR_3) + VAR_7;

}

static int FUNC_0(BlockDriverState *VAR_0, int64_t VAR_1,
const uint8_t *VAR_2, int VAR_3)
{ BDRVRawState *s = VAR_0->opaque; int VAR_4, VAR_5, VAR_6, VAR_7; VAR_7 = 0; if (s->aligned_buf != NULL) { if (VAR_1 & 0x1ff) { VAR_6 = VAR_1 & 0x1ff; VAR_5 = raw_pread_aligned(VAR_0, VAR_1 - VAR_6, s->aligned_buf, 512); if (VAR_5 < 0)
return VAR_5; VAR_4 = 512 - VAR_6; if (VAR_4 > VAR_3)
VAR_4 = VAR_3; memcpy(s->aligned_buf + VAR_6, VAR_2, VAR_4); VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1 - VAR_6, s->aligned_buf, 512); if (VAR_5 < 0)
return VAR_5; VAR_2 += VAR_4; VAR_1 += VAR_4; VAR_3 -= VAR_4; VAR_7 += VAR_4; if (VAR_3 == 0)
return VAR_7; } if (VAR_3 & 0x1ff || (uintptr_t) VAR_2 & 0x1ff) { while ((VAR_4 = (VAR_3 & ~0x1ff)) != 0) { if (VAR_4 > ALIGNED_BUFFER_SIZE)
VAR_4 = ALIGNED_BUFFER_SIZE; memcpy(s->aligned_buf, VAR_2, VAR_4); VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1, s->aligned_buf, VAR_4); if (VAR_5 < 0)
return VAR_5; VAR_2 += VAR_5; VAR_1 += VAR_5; VAR_3 -= VAR_5; VAR_7 += VAR_5; } if (VAR_3) { VAR_5 = raw_pread_aligned(VAR_0, VAR_1, s->aligned_buf, 512); if (VAR_5 < 0)
return VAR_5; memcpy(s->aligned_buf, VAR_2, VAR_3); VAR_5 = raw_pwrite_aligned(VAR_0, VAR_1, s->aligned_buf, 512); if (VAR_5 < 0)
return VAR_5; if (VAR_3 < VAR_5)
VAR_5 = VAR_3; VAR_7 += VAR_5; } return VAR_7; } } return raw_pwrite_aligned(VAR_0, VAR_1, VAR_2, VAR_3) + VAR_7; }