static int find_partition(BlockDriverState *bs, int partition,

                          off_t *offset, off_t *size)

{

    struct partition_record mbr[4];

    uint8_t data[512];

    int i;

    int ext_partnum = 4;

    int ret;



    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {

        errno = -ret;

        err(EXIT_FAILURE, "error while reading");

    }



    if (data[510] != 0x55 || data[511] != 0xaa) {

        errno = -EINVAL;

        return -1;

    }



    for (i = 0; i < 4; i++) {

        read_partition(&data[446 + 16 * i], &mbr[i]);



        if (!mbr[i].nb_sectors_abs)

            continue;



        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {

            struct partition_record ext[4];

            uint8_t data1[512];

            int j;



            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {

                errno = -ret;

                err(EXIT_FAILURE, "error while reading");

            }



            for (j = 0; j < 4; j++) {

                read_partition(&data1[446 + 16 * j], &ext[j]);

                if (!ext[j].nb_sectors_abs)

                    continue;



                if ((ext_partnum + j + 1) == partition) {

                    *offset = (uint64_t)ext[j].start_sector_abs << 9;

                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;

                    return 0;

                }

            }

            ext_partnum += 4;

        } else if ((i + 1) == partition) {

            *offset = (uint64_t)mbr[i].start_sector_abs << 9;

            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;

            return 0;

        }

    }



    errno = -ENOENT;

    return -1;

}

static int find_partition(BlockDriverState *bs, int partition,

                          off_t *offset, off_t *size)

{

    struct partition_record mbr[4];

    uint8_t data[512];

    int i;

    int ext_partnum = 4;

    int ret;



    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {

        errno = -ret;

        err(EXIT_FAILURE, "error while reading");

    }



    if (data[510] != 0x55 || data[511] != 0xaa) {

        errno = -EINVAL;

        return -1;

    }



    for (i = 0; i < 4; i++) {

        read_partition(&data[446 + 16 * i], &mbr[i]);



        if (!mbr[i].nb_sectors_abs)

            continue;



        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {

            struct partition_record ext[4];

            uint8_t data1[512];

            int j;



            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {

                errno = -ret;

                err(EXIT_FAILURE, "error while reading");

            }



            for (j = 0; j < 4; j++) {

                read_partition(&data1[446 + 16 * j], &ext[j]);

                if (!ext[j].nb_sectors_abs)

                    continue;



                if ((ext_partnum + j + 1) == partition) {

                    *offset = (uint64_t)ext[j].start_sector_abs << 9;

                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;

                    return 0;

                }

            }

            ext_partnum += 4;

        } else if ((i + 1) == partition) {

            *offset = (uint64_t)mbr[i].start_sector_abs << 9;

            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;

            return 0;

        }

    }



    errno = -ENOENT;

    return -1;

}

static int FUNC_0(BlockDriverState *VAR_0, int VAR_1,

                          off_t *VAR_2, off_t *VAR_3)

{

    struct partition_record VAR_4[4];

    uint8_t data[512];

    int VAR_5;

    int VAR_6 = 4;

    int VAR_7;



    if ((VAR_7 = bdrv_read(VAR_0, 0, data, 1)) < 0) {

        errno = -VAR_7;

        err(EXIT_FAILURE, "error while reading");

    }



    if (data[510] != 0x55 || data[511] != 0xaa) {

        errno = -EINVAL;

        return -1;

    }



    for (VAR_5 = 0; VAR_5 < 4; VAR_5++) {

        read_partition(&data[446 + 16 * VAR_5], &VAR_4[VAR_5]);



        if (!VAR_4[VAR_5].nb_sectors_abs)

            continue;



        if (VAR_4[VAR_5].system == 0xF || VAR_4[VAR_5].system == 0x5) {

            struct partition_record VAR_8[4];

            uint8_t data1[512];

            int VAR_9;



            if ((VAR_7 = bdrv_read(VAR_0, VAR_4[VAR_5].start_sector_abs, data1, 1)) < 0) {

                errno = -VAR_7;

                err(EXIT_FAILURE, "error while reading");

            }



            for (VAR_9 = 0; VAR_9 < 4; VAR_9++) {

                read_partition(&data1[446 + 16 * VAR_9], &VAR_8[VAR_9]);

                if (!VAR_8[VAR_9].nb_sectors_abs)

                    continue;



                if ((VAR_6 + VAR_9 + 1) == VAR_1) {

                    *VAR_2 = (uint64_t)VAR_8[VAR_9].start_sector_abs << 9;

                    *VAR_3 = (uint64_t)VAR_8[VAR_9].nb_sectors_abs << 9;

                    return 0;

                }

            }

            VAR_6 += 4;

        } else if ((VAR_5 + 1) == VAR_1) {

            *VAR_2 = (uint64_t)VAR_4[VAR_5].start_sector_abs << 9;

            *VAR_3 = (uint64_t)VAR_4[VAR_5].nb_sectors_abs << 9;

            return 0;

        }

    }



    errno = -ENOENT;

    return -1;

}

static int FUNC_0(BlockDriverState *VAR_0, int VAR_1,
off_t *VAR_2, off_t *VAR_3)
{ struct partition_record VAR_4[4]; uint8_t data[512]; int VAR_5; int VAR_6 = 4; int VAR_7; if ((VAR_7 = bdrv_read(VAR_0, 0, data, 1)) < 0) { errno = -VAR_7; err(EXIT_FAILURE, "error while reading"); } if (data[510] != 0x55 || data[511] != 0xaa) { errno = -EINVAL; return -1; } for (VAR_5 = 0; VAR_5 < 4; VAR_5++) { read_partition(&data[446 + 16 * VAR_5], &VAR_4[VAR_5]); if (!VAR_4[VAR_5].nb_sectors_abs)
continue; if (VAR_4[VAR_5].system == 0xF || VAR_4[VAR_5].system == 0x5) { struct partition_record VAR_8[4]; uint8_t data1[512]; int VAR_9; if ((VAR_7 = bdrv_read(VAR_0, VAR_4[VAR_5].start_sector_abs, data1, 1)) < 0) { errno = -VAR_7; err(EXIT_FAILURE, "error while reading"); } for (VAR_9 = 0; VAR_9 < 4; VAR_9++) { read_partition(&data1[446 + 16 * VAR_9], &VAR_8[VAR_9]); if (!VAR_8[VAR_9].nb_sectors_abs)
continue; if ((VAR_6 + VAR_9 + 1) == VAR_1) { *VAR_2 = (uint64_t)VAR_8[VAR_9].start_sector_abs << 9; *VAR_3 = (uint64_t)VAR_8[VAR_9].nb_sectors_abs << 9; return 0; } } VAR_6 += 4; } else if ((VAR_5 + 1) == VAR_1) { *VAR_2 = (uint64_t)VAR_4[VAR_5].start_sector_abs << 9; *VAR_3 = (uint64_t)VAR_4[VAR_5].nb_sectors_abs << 9; return 0; } } errno = -ENOENT; return -1; }