static int qemu_dup_flags(int fd, int flags)

{

    int ret;

    int serrno;

    int dup_flags;

    int setfl_flags;



#ifdef F_DUPFD_CLOEXEC

    ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);

#else

    ret = dup(fd);

    if (ret != -1) {

        qemu_set_cloexec(ret);

    }

#endif

    if (ret == -1) {

        goto fail;

    }



    dup_flags = fcntl(ret, F_GETFL);

    if (dup_flags == -1) {

        goto fail;

    }



    if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {

        errno = EINVAL;

        goto fail;

    }



    /* Set/unset flags that we can with fcntl */

    setfl_flags = O_APPEND | O_ASYNC | O_NONBLOCK;

#ifdef O_NOATIME

    setfl_flags |= O_NOATIME;

#endif

#ifdef O_DIRECT

    setfl_flags |= O_DIRECT;

#endif

    dup_flags &= ~setfl_flags;

    dup_flags |= (flags & setfl_flags);

    if (fcntl(ret, F_SETFL, dup_flags) == -1) {

        goto fail;

    }



    /* Truncate the file in the cases that open() would truncate it */

    if (flags & O_TRUNC ||

            ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {

        if (ftruncate(ret, 0) == -1) {

            goto fail;

        }

    }



    return ret;



fail:

    serrno = errno;

    if (ret != -1) {

        close(ret);

    }

    errno = serrno;

    return -1;

}

static int qemu_dup_flags(int fd, int flags)

{

    int ret;

    int serrno;

    int dup_flags;

    int setfl_flags;



#ifdef F_DUPFD_CLOEXEC

    ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);

#else

    ret = dup(fd);

    if (ret != -1) {

        qemu_set_cloexec(ret);

    }

#endif

    if (ret == -1) {

        goto fail;

    }



    dup_flags = fcntl(ret, F_GETFL);

    if (dup_flags == -1) {

        goto fail;

    }



    if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {

        errno = EINVAL;

        goto fail;

    }



    

    setfl_flags = O_APPEND | O_ASYNC | O_NONBLOCK;

#ifdef O_NOATIME

    setfl_flags |= O_NOATIME;

#endif

#ifdef O_DIRECT

    setfl_flags |= O_DIRECT;

#endif

    dup_flags &= ~setfl_flags;

    dup_flags |= (flags & setfl_flags);

    if (fcntl(ret, F_SETFL, dup_flags) == -1) {

        goto fail;

    }



    

    if (flags & O_TRUNC ||

            ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {

        if (ftruncate(ret, 0) == -1) {

            goto fail;

        }

    }



    return ret;



fail:

    serrno = errno;

    if (ret != -1) {

        close(ret);

    }

    errno = serrno;

    return -1;

}

static int FUNC_0(int VAR_0, int VAR_1)

{

    int VAR_2;

    int VAR_3;

    int VAR_4;

    int VAR_5;



#ifdef F_DUPFD_CLOEXEC

    VAR_2 = fcntl(VAR_0, F_DUPFD_CLOEXEC, 0);

#else

    VAR_2 = dup(VAR_0);

    if (VAR_2 != -1) {

        qemu_set_cloexec(VAR_2);

    }

#endif

    if (VAR_2 == -1) {

        goto fail;

    }



    VAR_4 = fcntl(VAR_2, F_GETFL);

    if (VAR_4 == -1) {

        goto fail;

    }



    if ((VAR_1 & O_SYNC) != (VAR_4 & O_SYNC)) {

        errno = EINVAL;

        goto fail;

    }



    

    VAR_5 = O_APPEND | O_ASYNC | O_NONBLOCK;

#ifdef O_NOATIME

    VAR_5 |= O_NOATIME;

#endif

#ifdef O_DIRECT

    VAR_5 |= O_DIRECT;

#endif

    VAR_4 &= ~VAR_5;

    VAR_4 |= (VAR_1 & VAR_5);

    if (fcntl(VAR_2, F_SETFL, VAR_4) == -1) {

        goto fail;

    }



    

    if (VAR_1 & O_TRUNC ||

            ((VAR_1 & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {

        if (ftruncate(VAR_2, 0) == -1) {

            goto fail;

        }

    }



    return VAR_2;



fail:

    VAR_3 = errno;

    if (VAR_2 != -1) {

        close(VAR_2);

    }

    errno = VAR_3;

    return -1;

}

static int FUNC_0(int VAR_0, int VAR_1)
{ int VAR_2; int VAR_3; int VAR_4; int VAR_5; #ifdef F_DUPFD_CLOEXEC
VAR_2 = fcntl(VAR_0, F_DUPFD_CLOEXEC, 0); #else
VAR_2 = dup(VAR_0); if (VAR_2 != -1) { qemu_set_cloexec(VAR_2); } #endif
if (VAR_2 == -1) { goto fail; } VAR_4 = fcntl(VAR_2, F_GETFL); if (VAR_4 == -1) { goto fail; } if ((VAR_1 & O_SYNC) != (VAR_4 & O_SYNC)) { errno = EINVAL; goto fail; } VAR_5 = O_APPEND | O_ASYNC | O_NONBLOCK; #ifdef O_NOATIME
VAR_5 |= O_NOATIME; #endif
#ifdef O_DIRECT
VAR_5 |= O_DIRECT; #endif
VAR_4 &= ~VAR_5; VAR_4 |= (VAR_1 & VAR_5); if (fcntl(VAR_2, F_SETFL, VAR_4) == -1) { goto fail; } if (VAR_1 & O_TRUNC ||
((VAR_1 & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) { if (ftruncate(VAR_2, 0) == -1) { goto fail; } } return VAR_2; fail:
VAR_3 = errno; if (VAR_2 != -1) { close(VAR_2); } errno = VAR_3; return -1; }