int poll(struct pollfd *fds, nfds_t numfds, int timeout)

{

    fd_set read_set;

    fd_set write_set;

    fd_set exception_set;

    nfds_t i;

    int n;

    int rc;



#ifdef __MINGW32__

    if (numfds >= FD_SETSIZE) {

        errno = EINVAL;

        return -1;

    }

#endif



    FD_ZERO(&read_set);

    FD_ZERO(&write_set);

    FD_ZERO(&exception_set);



    n = -1;

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

        if (fds[i].fd < 0)

            continue;

#ifndef __MINGW32__

        if (fds[i].fd >= FD_SETSIZE) {

            errno = EINVAL;

            return -1;

        }

#endif



        if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);

        if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);

        if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);



        if (fds[i].fd > n)

            n = fds[i].fd;

    };



    if (n == -1)

        /* Hey!? Nothing to poll, in fact!!! */

        return 0;



    if (timeout < 0)

        rc = select(n+1, &read_set, &write_set, &exception_set, NULL);

    else {

        struct timeval    tv;



        tv.tv_sec = timeout / 1000;

        tv.tv_usec = 1000 * (timeout % 1000);

        rc = select(n+1, &read_set, &write_set, &exception_set, &tv);

    };



    if (rc < 0)

        return rc;



    for(i = 0; i < (nfds_t) n; i++) {

        fds[i].revents = 0;



        if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;

        if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;

        if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;

    };



    return rc;

}

int poll(struct pollfd *fds, nfds_t numfds, int timeout)

{

    fd_set read_set;

    fd_set write_set;

    fd_set exception_set;

    nfds_t i;

    int n;

    int rc;



#ifdef __MINGW32__

    if (numfds >= FD_SETSIZE) {

        errno = EINVAL;

        return -1;

    }

#endif



    FD_ZERO(&read_set);

    FD_ZERO(&write_set);

    FD_ZERO(&exception_set);



    n = -1;

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

        if (fds[i].fd < 0)

            continue;

#ifndef __MINGW32__

        if (fds[i].fd >= FD_SETSIZE) {

            errno = EINVAL;

            return -1;

        }

#endif



        if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);

        if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);

        if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);



        if (fds[i].fd > n)

            n = fds[i].fd;

    };



    if (n == -1)

        

        return 0;



    if (timeout < 0)

        rc = select(n+1, &read_set, &write_set, &exception_set, NULL);

    else {

        struct timeval    tv;



        tv.tv_sec = timeout / 1000;

        tv.tv_usec = 1000 * (timeout % 1000);

        rc = select(n+1, &read_set, &write_set, &exception_set, &tv);

    };



    if (rc < 0)

        return rc;



    for(i = 0; i < (nfds_t) n; i++) {

        fds[i].revents = 0;



        if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;

        if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;

        if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;

    };



    return rc;

}

int FUNC_0(struct pollfd *VAR_0, nfds_t VAR_1, int VAR_2)

{

    fd_set read_set;

    fd_set write_set;

    fd_set exception_set;

    nfds_t i;

    int VAR_3;

    int VAR_4;



#ifdef __MINGW32__

    if (VAR_1 >= FD_SETSIZE) {

        errno = EINVAL;

        return -1;

    }

#endif



    FD_ZERO(&read_set);

    FD_ZERO(&write_set);

    FD_ZERO(&exception_set);



    VAR_3 = -1;

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

        if (VAR_0[i].fd < 0)

            continue;

#ifndef __MINGW32__

        if (VAR_0[i].fd >= FD_SETSIZE) {

            errno = EINVAL;

            return -1;

        }

#endif



        if (VAR_0[i].events & POLLIN)  FD_SET(VAR_0[i].fd, &read_set);

        if (VAR_0[i].events & POLLOUT) FD_SET(VAR_0[i].fd, &write_set);

        if (VAR_0[i].events & POLLERR) FD_SET(VAR_0[i].fd, &exception_set);



        if (VAR_0[i].fd > VAR_3)

            VAR_3 = VAR_0[i].fd;

    };



    if (VAR_3 == -1)

        

        return 0;



    if (VAR_2 < 0)

        VAR_4 = select(VAR_3+1, &read_set, &write_set, &exception_set, NULL);

    else {

        struct timeval    VAR_5;



        VAR_5.tv_sec = VAR_2 / 1000;

        VAR_5.tv_usec = 1000 * (VAR_2 % 1000);

        VAR_4 = select(VAR_3+1, &read_set, &write_set, &exception_set, &VAR_5);

    };



    if (VAR_4 < 0)

        return VAR_4;



    for(i = 0; i < (nfds_t) VAR_3; i++) {

        VAR_0[i].revents = 0;



        if (FD_ISSET(VAR_0[i].fd, &read_set))      VAR_0[i].revents |= POLLIN;

        if (FD_ISSET(VAR_0[i].fd, &write_set))     VAR_0[i].revents |= POLLOUT;

        if (FD_ISSET(VAR_0[i].fd, &exception_set)) VAR_0[i].revents |= POLLERR;

    };



    return VAR_4;

}

int FUNC_0(struct pollfd *VAR_0, nfds_t VAR_1, int VAR_2)
{ fd_set read_set; fd_set write_set; fd_set exception_set; nfds_t i; int VAR_3; int VAR_4; #ifdef __MINGW32__
if (VAR_1 >= FD_SETSIZE) { errno = EINVAL; return -1; } #endif
FD_ZERO(&read_set); FD_ZERO(&write_set); FD_ZERO(&exception_set); VAR_3 = -1; for(i = 0; i < VAR_1; i++) { if (VAR_0[i].fd < 0)
continue; #ifndef __MINGW32__
if (VAR_0[i].fd >= FD_SETSIZE) { errno = EINVAL; return -1; } #endif
if (VAR_0[i].events & POLLIN)  FD_SET(VAR_0[i].fd, &read_set); if (VAR_0[i].events & POLLOUT) FD_SET(VAR_0[i].fd, &write_set); if (VAR_0[i].events & POLLERR) FD_SET(VAR_0[i].fd, &exception_set); if (VAR_0[i].fd > VAR_3)
VAR_3 = VAR_0[i].fd; }; if (VAR_3 == -1)
return 0; if (VAR_2 < 0)
VAR_4 = select(VAR_3+1, &read_set, &write_set, &exception_set, NULL); else { struct timeval    VAR_5; VAR_5.tv_sec = VAR_2 / 1000; VAR_5.tv_usec = 1000 * (VAR_2 % 1000); VAR_4 = select(VAR_3+1, &read_set, &write_set, &exception_set, &VAR_5); }; if (VAR_4 < 0)
return VAR_4; for(i = 0; i < (nfds_t) VAR_3; i++) { VAR_0[i].revents = 0; if (FD_ISSET(VAR_0[i].fd, &read_set))      VAR_0[i].revents |= POLLIN; if (FD_ISSET(VAR_0[i].fd, &write_set))     VAR_0[i].revents |= POLLOUT; if (FD_ISSET(VAR_0[i].fd, &exception_set)) VAR_0[i].revents |= POLLERR; }; return VAR_4; }