#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
struct timeval {
  int tv_sec;
};
struct pollfd {
  int revents;
  int /*<<< orphan*/ fd;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int POLLERR;
int POLLHUP;
int POLLIN;
int POLLOUT;
int /*<<< orphan*/ __convert_fd_set(struct pollfd *, int /*<<< orphan*/ *, int,
                                    int *, int);
struct pollfd *calloc(int, int);
int /*<<< orphan*/ free(struct pollfd *);
int poll(struct pollfd *, int, int);
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
           struct timeval *timeout) {
  struct pollfd *pfds = calloc(nfds, sizeof(struct pollfd));
  if (!pfds) {
    return -1;
  }
  int found = 0;
  if (readfds) {
    __convert_fd_set(pfds, readfds, POLLIN, &found, nfds);
    FD_ZERO(readfds);
  }
  if (writefds) {
    __convert_fd_set(pfds, writefds, POLLOUT, &found, nfds);
    FD_ZERO(writefds);
  }
  if (exceptfds) {
    FD_ZERO(exceptfds);
  }
  int r = poll(pfds, found, timeout ? timeout->tv_sec * 1000 : -1);
  if (r < 0) {
    free(pfds);
    return r;
  }
  for (int i = 0; i < found; i++) {
    if (readfds && pfds[i].revents & POLLIN) {
      FD_SET(pfds[i].fd, readfds);
    }
    if (writefds && pfds[i].revents & POLLOUT) {
      FD_SET(pfds[i].fd, writefds);
    }
    if (exceptfds && pfds[i].revents & (POLLERR | POLLHUP)) {
      FD_SET(pfds[i].fd, exceptfds);
    }
  }
  free(pfds);
  return r;
}


x86 assembly (clang, -O0):
<select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11a3 <select+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    1381 <select+0x221>
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11dc <select+0x7c>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%r8d
lea    -0x34(%rbp),%rcx
call   1040 <__convert_fd_set@plt>
mov    -0x10(%rbp),%rdi
call   1060 <FD_ZERO@plt>
cmpq   $0x0,-0x18(%rbp)
je     120e <select+0xae>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    -0x8(%rbp),%r8d
lea    -0x34(%rbp),%rcx
call   1040 <__convert_fd_set@plt>
mov    -0x18(%rbp),%rdi
call   1060 <FD_ZERO@plt>
cmpq   $0x0,-0x20(%rbp)
je     1222 <select+0xc2>
mov    -0x20(%rbp),%rdi
call   1060 <FD_ZERO@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x40(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     124d <select+0xed>
mov    -0x28(%rbp),%rax
imul   $0x3e8,(%rax),%eax
mov    %eax,-0x4c(%rbp)
jmp    125a <select+0xfa>
mov    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
jmp    125a <select+0xfa>
mov    -0x40(%rbp),%esi
mov    -0x48(%rbp),%rdi
mov    -0x4c(%rbp),%edx
call   1070 <poll@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    128a <select+0x12a>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1381 <select+0x221>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    1372 <select+0x212>
cmpq   $0x0,-0x10(%rbp)
je     12da <select+0x17a>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2d16(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12da <select+0x17a>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
mov    -0x10(%rbp),%rsi
call   1080 <FD_SET@plt>
cmpq   $0x0,-0x18(%rbp)
je     1317 <select+0x1b7>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2d01(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1317 <select+0x1b7>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
mov    -0x18(%rbp),%rsi
call   1080 <FD_SET@plt>
cmpq   $0x0,-0x20(%rbp)
je     135f <select+0x1ff>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%eax
mov    0x2ca4(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2ca3(%rip),%rdx
or     (%rdx),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     135f <select+0x1ff>
mov    -0x30(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    0x4(%rax,%rcx,8),%edi
mov    -0x20(%rbp),%rsi
call   1080 <FD_SET@plt>
jmp    1364 <select+0x204>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1291 <select+0x131>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<select>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r13
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
mov    $0x8,%esi
call   1050 <calloc@plt>
test   %rax,%rax
je     11ff <select+0x9f>
mov    %rax,%rbx
movl   $0x0,(%rsp)
test   %r12,%r12
je     11b9 <select+0x59>
mov    0x2e32(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %ebp,%r8d
call   1040 <__convert_fd_set@plt>
mov    %r12,%rdi
call   1060 <FD_ZERO@plt>
test   %r15,%r15
je     11e0 <select+0x80>
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %ebp,%r8d
call   1040 <__convert_fd_set@plt>
mov    %r15,%rdi
call   1060 <FD_ZERO@plt>
test   %r14,%r14
je     11ed <select+0x8d>
mov    %r14,%rdi
call   1060 <FD_ZERO@plt>
mov    (%rsp),%esi
test   %r13,%r13
je     1209 <select+0xa9>
imul   $0x3e8,0x0(%r13),%edx
jmp    120e <select+0xae>
mov    $0xffffffff,%eax
jmp    12bc <select+0x15c>
mov    $0xffffffff,%edx
mov    %rbx,%rdi
call   1070 <poll@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     12b0 <select+0x150>
cmpl   $0x0,(%rsp)
jle    12b0 <select+0x150>
xor    %ebp,%ebp
mov    0x2d9b(%rip),%r13
jmp    124d <select+0xed>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
movslq (%rsp),%rax
cmp    %rax,%rbp
jge    12b0 <select+0x150>
test   %r12,%r12
je     1267 <select+0x107>
mov    0x0(%r13),%eax
test   %eax,(%rbx,%rbp,8)
je     1267 <select+0x107>
mov    0x4(%rbx,%rbp,8),%edi
mov    %r12,%rsi
call   1080 <FD_SET@plt>
test   %r15,%r15
je     1286 <select+0x126>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
test   %eax,(%rbx,%rbp,8)
je     1286 <select+0x126>
mov    0x4(%rbx,%rbp,8),%edi
mov    %r15,%rsi
call   1080 <FD_SET@plt>
test   %r14,%r14
je     1240 <select+0xe0>
mov    0x2d4e(%rip),%rax
mov    (%rax),%eax
mov    0x2d3d(%rip),%rcx
or     (%rcx),%eax
test   %eax,(%rbx,%rbp,8)
je     1240 <select+0xe0>
mov    0x4(%rbx,%rbp,8),%edi
mov    %r14,%rsi
call   1080 <FD_SET@plt>
jmp    1240 <select+0xe0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
define i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca %struct.timeval*
%12 = alloca %struct.pollfd*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %7
store i32* %1, i32** %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store %struct.timeval* %4, %struct.timeval** %11
%16 = load i32, i32* %7
%17 = call %struct.pollfd* @calloc(i32 %16, i32 8)
store %struct.pollfd* %17, %struct.pollfd** %12
%18 = load %struct.pollfd*, %struct.pollfd** %12
%19 = icmp ne %struct.pollfd* %18, null
br i1 %19, label %21, label %20
20: ; preds = %5
store i32 -1, i32* %6
br label %150
21: ; preds = %5
store i32 0, i32* %13
%22 = load i32*, i32** %8
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %32
24: ; preds = %21
%25 = load %struct.pollfd*, %struct.pollfd** %12
%26 = load i32*, i32** %8
%27 = load i32, i32* @POLLIN
%28 = load i32, i32* %7
%29 = call i32 @__convert_fd_set(%struct.pollfd* %25, i32* %26, i32 %27, i32* %13, i32 %28)
%30 = load i32*, i32** %8
%31 = call i32 @FD_ZERO(i32* %30)
br label %32
32: ; preds = %24, %21
%33 = load i32*, i32** %9
%34 = icmp ne i32* %33, null
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load %struct.pollfd*, %struct.pollfd** %12
%37 = load i32*, i32** %9
%38 = load i32, i32* @POLLOUT
%39 = load i32, i32* %7
%40 = call i32 @__convert_fd_set(%struct.pollfd* %36, i32* %37, i32 %38, i32* %13, i32 %39)
%41 = load i32*, i32** %9
%42 = call i32 @FD_ZERO(i32* %41)
br label %43
43: ; preds = %35, %32
%44 = load i32*, i32** %10
%45 = icmp ne i32* %44, null
br i1 %45, label %46, label %49
46: ; preds = %43
%47 = load i32*, i32** %10
%48 = call i32 @FD_ZERO(i32* %47)
br label %49
49: ; preds = %46, %43
%50 = load %struct.pollfd*, %struct.pollfd** %12
%51 = load i32, i32* %13
%52 = load %struct.timeval*, %struct.timeval** %11
%53 = icmp ne %struct.timeval* %52, null
br i1 %53, label %54, label %59
54: ; preds = %49
%55 = load %struct.timeval*, %struct.timeval** %11
%56 = getelementptr inbounds %struct.timeval, %struct.timeval* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = mul nsw i32 %57, 1000
br label %60
59: ; preds = %49
br label %60
60: ; preds = %59, %54
%61 = phi i32 [ %58, %54 ], [ -1, %59 ]
%62 = call i32 @poll(%struct.pollfd* %50, i32 %51, i32 %61)
store i32 %62, i32* %14
%63 = load i32, i32* %14
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %69
65: ; preds = %60
%66 = load %struct.pollfd*, %struct.pollfd** %12
%67 = call i32 @free(%struct.pollfd* %66)
%68 = load i32, i32* %14
store i32 %68, i32* %6
br label %150
69: ; preds = %60
store i32 0, i32* %15
br label %70
70: ; preds = %143, %69
%71 = load i32, i32* %15
%72 = load i32, i32* %13
%73 = icmp slt i32 %71, %72
br i1 %73, label %74, label %146
74: ; preds = %70
%75 = load i32*, i32** %8
%76 = icmp ne i32* %75, null
br i1 %76, label %77, label %96
77: ; preds = %74
%78 = load %struct.pollfd*, %struct.pollfd** %12
%79 = load i32, i32* %15
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %78, i64 %80
%82 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %81, i32 0, i32 0
%83 = load i32, i32* %82
%84 = load i32, i32* @POLLIN
%85 = and i32 %83, %84
%86 = icmp ne i32 %85, 0
br i1 %86, label %87, label %96
87: ; preds = %77
%88 = load %struct.pollfd*, %struct.pollfd** %12
%89 = load i32, i32* %15
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %88, i64 %90
%92 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %91, i32 0, i32 1
%93 = load i32, i32* %92
%94 = load i32*, i32** %8
%95 = call i32 @FD_SET(i32 %93, i32* %94)
br label %96
96: ; preds = %87, %77, %74
%97 = load i32*, i32** %9
%98 = icmp ne i32* %97, null
br i1 %98, label %99, label %118
99: ; preds = %96
%100 = load %struct.pollfd*, %struct.pollfd** %12
%101 = load i32, i32* %15
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %100, i64 %102
%104 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %103, i32 0, i32 0
%105 = load i32, i32* %104
%106 = load i32, i32* @POLLOUT
%107 = and i32 %105, %106
%108 = icmp ne i32 %107, 0
br i1 %108, label %109, label %118
109: ; preds = %99
%110 = load %struct.pollfd*, %struct.pollfd** %12
%111 = load i32, i32* %15
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %110, i64 %112
%114 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %113, i32 0, i32 1
%115 = load i32, i32* %114
%116 = load i32*, i32** %9
%117 = call i32 @FD_SET(i32 %115, i32* %116)
br label %118
118: ; preds = %109, %99, %96
%119 = load i32*, i32** %10
%120 = icmp ne i32* %119, null
br i1 %120, label %121, label %142
121: ; preds = %118
%122 = load %struct.pollfd*, %struct.pollfd** %12
%123 = load i32, i32* %15
%124 = sext i32 %123 to i64
%125 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %122, i64 %124
%126 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %125, i32 0, i32 0
%127 = load i32, i32* %126
%128 = load i32, i32* @POLLERR
%129 = load i32, i32* @POLLHUP
%130 = or i32 %128, %129
%131 = and i32 %127, %130
%132 = icmp ne i32 %131, 0
br i1 %132, label %133, label %142
133: ; preds = %121
%134 = load %struct.pollfd*, %struct.pollfd** %12
%135 = load i32, i32* %15
%136 = sext i32 %135 to i64
%137 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %134, i64 %136
%138 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %137, i32 0, i32 1
%139 = load i32, i32* %138
%140 = load i32*, i32** %10
%141 = call i32 @FD_SET(i32 %139, i32* %140)
br label %142
142: ; preds = %133, %121, %118
br label %143
143: ; preds = %142
%144 = load i32, i32* %15
%145 = add nsw i32 %144, 1
store i32 %145, i32* %15
br label %70
146: ; preds = %70
%147 = load %struct.pollfd*, %struct.pollfd** %12
%148 = call i32 @free(%struct.pollfd* %147)
%149 = load i32, i32* %14
store i32 %149, i32* %6
br label %150
150: ; preds = %146, %65, %20
%151 = load i32, i32* %6
ret i32 %151
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.pollfd = type { i32, i32 }
@POLLIN = global i32 0
@POLLOUT = global i32 0
@POLLERR = global i32 0
@POLLHUP = global i32 0
define i32 @select(i32 %0, i32* %1, i32* %2, i32* %3, %struct.timeval* %4) {
%6 = alloca i32
%7 = tail call %struct.pollfd* @calloc(i32 %0, i32 8)
%8 = icmp eq %struct.pollfd* %7, null
br i1 %8, label %83, label %9
9: ; preds = %5
%10 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%11 = icmp eq i32* %1, null
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* @POLLIN
%14 = call i32 @__convert_fd_set(%struct.pollfd* %7, i32* %1, i32 %13, i32* %6, i32 %0)
%15 = call i32 @FD_ZERO(i32* %1)
br label %16
16: ; preds = %12, %9
%17 = icmp eq i32* %2, null
br i1 %17, label %22, label %18
18: ; preds = %16
%19 = load i32, i32* @POLLOUT
%20 = call i32 @__convert_fd_set(%struct.pollfd* %7, i32* %2, i32 %19, i32* %6, i32 %0)
%21 = call i32 @FD_ZERO(i32* %2)
br label %22
22: ; preds = %18, %16
%23 = icmp eq i32* %3, null
br i1 %23, label %26, label %24
24: ; preds = %22
%25 = call i32 @FD_ZERO(i32* %3)
br label %26
26: ; preds = %24, %22
%27 = load i32, i32* %6
%28 = icmp eq %struct.timeval* %4, null
br i1 %28, label %33, label %29
29: ; preds = %26
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%31 = load i32, i32* %30
%32 = mul nsw i32 %31, 1000
br label %33
33: ; preds = %26, %29
%34 = phi i32 [ %32, %29 ], [ -1, %26 ]
%35 = call i32 @poll(%struct.pollfd* %7, i32 %27, i32 %34)
%36 = icmp sgt i32 %35, -1
%37 = load i32, i32* %6
%38 = icmp sgt i32 %37, 0
%39 = select i1 %36, i1 %38, i1 false
br i1 %39, label %40, label %81
40: ; preds = %33, %76
%41 = phi i64 [ %77, %76 ], [ 0, %33 ]
br i1 %11, label %52, label %42
42: ; preds = %40
%43 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 0
%44 = load i32, i32* %43
%45 = load i32, i32* @POLLIN
%46 = and i32 %45, %44
%47 = icmp eq i32 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %42
%49 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 1
%50 = load i32, i32* %49
%51 = call i32 @FD_SET(i32 %50, i32* %1)
br label %52
52: ; preds = %48, %42, %40
br i1 %17, label %63, label %53
53: ; preds = %52
%54 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* @POLLOUT
%57 = and i32 %56, %55
%58 = icmp eq i32 %57, 0
br i1 %58, label %63, label %59
59: ; preds = %53
%60 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 1
%61 = load i32, i32* %60
%62 = call i32 @FD_SET(i32 %61, i32* %2)
br label %63
63: ; preds = %59, %53, %52
br i1 %23, label %76, label %64
64: ; preds = %63
%65 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 0
%66 = load i32, i32* %65
%67 = load i32, i32* @POLLERR
%68 = load i32, i32* @POLLHUP
%69 = or i32 %68, %67
%70 = and i32 %69, %66
%71 = icmp eq i32 %70, 0
br i1 %71, label %76, label %72
72: ; preds = %64
%73 = getelementptr inbounds %struct.pollfd, %struct.pollfd* %7, i64 %41, i32 1
%74 = load i32, i32* %73
%75 = call i32 @FD_SET(i32 %74, i32* %3)
br label %76
76: ; preds = %63, %64, %72
%77 = add nuw nsw i64 %41, 1
%78 = load i32, i32* %6
%79 = sext i32 %78 to i64
%80 = icmp slt i64 %77, %79
br i1 %80, label %40, label %81
81: ; preds = %76, %33
%82 = call i32 @free(%struct.pollfd* %7)
br label %83
83: ; preds = %5, %81
%84 = phi i32 [ %35, %81 ], [ -1, %5 ]
ret i32 %84
}