int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)

{

    TRACE("Setting NBD socket");



    if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {

        int serrno = errno;

        LOG("Failed to set NBD socket");

        errno = serrno;

        return -1;

    }



    TRACE("Setting block size to %lu", (unsigned long)blocksize);



    if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {

        int serrno = errno;

        LOG("Failed setting NBD block size");

        errno = serrno;

        return -1;

    }



        TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));



    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {

        int serrno = errno;

        LOG("Failed setting size (in blocks)");

        errno = serrno;

        return -1;

    }



    if (flags & NBD_FLAG_READ_ONLY) {

        int read_only = 1;

        TRACE("Setting readonly attribute");



        if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {

            int serrno = errno;

            LOG("Failed setting read-only attribute");

            errno = serrno;

            return -1;

        }

    }



    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0

        && errno != ENOTTY) {

        int serrno = errno;

        LOG("Failed setting flags");

        errno = serrno;

        return -1;

    }



    TRACE("Negotiation ended");



    return 0;

}

int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)

{

    TRACE("Setting NBD socket");



    if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {

        int serrno = errno;

        LOG("Failed to set NBD socket");

        errno = serrno;

        return -1;

    }



    TRACE("Setting block size to %lu", (unsigned long)blocksize);



    if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {

        int serrno = errno;

        LOG("Failed setting NBD block size");

        errno = serrno;

        return -1;

    }



        TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));



    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {

        int serrno = errno;

        LOG("Failed setting size (in blocks)");

        errno = serrno;

        return -1;

    }



    if (flags & NBD_FLAG_READ_ONLY) {

        int read_only = 1;

        TRACE("Setting readonly attribute");



        if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {

            int serrno = errno;

            LOG("Failed setting read-only attribute");

            errno = serrno;

            return -1;

        }

    }



    if (ioctl(fd, NBD_SET_FLAGS, flags) < 0

        && errno != ENOTTY) {

        int serrno = errno;

        LOG("Failed setting flags");

        errno = serrno;

        return -1;

    }



    TRACE("Negotiation ended");



    return 0;

}

int FUNC_0(int VAR_0, int VAR_1, uint32_t VAR_2, off_t VAR_3, size_t VAR_4)

{

    TRACE("Setting NBD socket");



    if (ioctl(VAR_0, NBD_SET_SOCK, VAR_1) < 0) {

        int VAR_7 = errno;

        LOG("Failed to set NBD socket");

        errno = VAR_7;

        return -1;

    }



    TRACE("Setting block VAR_3 to %lu", (unsigned long)VAR_4);



    if (ioctl(VAR_0, NBD_SET_BLKSIZE, VAR_4) < 0) {

        int VAR_7 = errno;

        LOG("Failed setting NBD block VAR_3");

        errno = VAR_7;

        return -1;

    }



        TRACE("Setting VAR_3 to %zd block(s)", (size_t)(VAR_3 / VAR_4));



    if (ioctl(VAR_0, NBD_SET_SIZE_BLOCKS, VAR_3 / VAR_4) < 0) {

        int VAR_7 = errno;

        LOG("Failed setting VAR_3 (in blocks)");

        errno = VAR_7;

        return -1;

    }



    if (VAR_2 & NBD_FLAG_READ_ONLY) {

        int VAR_6 = 1;

        TRACE("Setting readonly attribute");



        if (ioctl(VAR_0, BLKROSET, (unsigned long) &VAR_6) < 0) {

            int VAR_7 = errno;

            LOG("Failed setting read-only attribute");

            errno = VAR_7;

            return -1;

        }

    }



    if (ioctl(VAR_0, NBD_SET_FLAGS, VAR_2) < 0

        && errno != ENOTTY) {

        int VAR_7 = errno;

        LOG("Failed setting VAR_2");

        errno = VAR_7;

        return -1;

    }



    TRACE("Negotiation ended");



    return 0;

}

int FUNC_0(int VAR_0, int VAR_1, uint32_t VAR_2, off_t VAR_3, size_t VAR_4)
{ TRACE("Setting NBD socket"); if (ioctl(VAR_0, NBD_SET_SOCK, VAR_1) < 0) { int VAR_7 = errno; LOG("Failed to set NBD socket"); errno = VAR_7; return -1; } TRACE("Setting block VAR_3 to %lu", (unsigned long)VAR_4); if (ioctl(VAR_0, NBD_SET_BLKSIZE, VAR_4) < 0) { int VAR_7 = errno; LOG("Failed setting NBD block VAR_3"); errno = VAR_7; return -1; } TRACE("Setting VAR_3 to %zd block(s)", (size_t)(VAR_3 / VAR_4)); if (ioctl(VAR_0, NBD_SET_SIZE_BLOCKS, VAR_3 / VAR_4) < 0) { int VAR_7 = errno; LOG("Failed setting VAR_3 (in blocks)"); errno = VAR_7; return -1; } if (VAR_2 & NBD_FLAG_READ_ONLY) { int VAR_6 = 1; TRACE("Setting readonly attribute"); if (ioctl(VAR_0, BLKROSET, (unsigned long) &VAR_6) < 0) { int VAR_7 = errno; LOG("Failed setting read-only attribute"); errno = VAR_7; return -1; } } if (ioctl(VAR_0, NBD_SET_FLAGS, VAR_2) < 0
&& errno != ENOTTY) { int VAR_7 = errno; LOG("Failed setting VAR_2"); errno = VAR_7; return -1; } TRACE("Negotiation ended"); return 0; }