#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;
  int tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ EINTR;
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int SocketWait(int fd, int rd, int wr, int timems) {
  fd_set rfds, wfds;
  struct timeval tv;
  FD_ZERO(&rfds);
  FD_ZERO(&wfds);
  if (rd)
    FD_SET(fd, &rfds);
  if (wr)
    FD_SET(fd, &wfds);
  tv.tv_sec = timems / 1000;
  tv.tv_usec = timems % 1000;
  for (;;)
    switch (select(fd + 1, &rfds, &wfds, NULL, &tv)) {
    case 0:
      return 0;
    case (-1):
      if (errno == EINTR)
        break;
      return 0;
    default:
      if (FD_ISSET(fd, &rfds))
        return 1;
      if (FD_ISSET(fd, &wfds))
        return 2;
      return 0;
    }
}


x86 assembly (clang, -O0):
<SocketWait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
cmpl   $0x0,-0xc(%rbp)
je     117c <SocketWait+0x3c>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x10(%rbp)
je     1192 <SocketWait+0x52>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %edx,-0x24(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x28(%rbp),%r8
call   1050 <select@plt>
mov    %eax,%ecx
mov    %ecx,-0x2c(%rbp)
sub    $0xffffffff,%eax
je     11f8 <SocketWait+0xb8>
jmp    11dc <SocketWait+0x9c>
mov    -0x2c(%rbp),%eax
test   %eax,%eax
jne    1221 <SocketWait+0xe1>
jmp    11ec <SocketWait+0xac>
movl   $0x0,-0x4(%rbp)
jmp    1274 <SocketWait+0x134>
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%eax
jne    1215 <SocketWait+0xd5>
jmp    126f <SocketWait+0x12f>
movl   $0x0,-0x4(%rbp)
jmp    1274 <SocketWait+0x134>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     1242 <SocketWait+0x102>
movl   $0x1,-0x4(%rbp)
jmp    1274 <SocketWait+0x134>
mov    -0x8(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     1263 <SocketWait+0x123>
movl   $0x2,-0x4(%rbp)
jmp    1274 <SocketWait+0x134>
movl   $0x0,-0x4(%rbp)
jmp    1274 <SocketWait+0x134>
jmp    11ae <SocketWait+0x6e>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SocketWait>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x4(%rsp),%rdi
call   1040 <FD_ZERO@plt>
mov    %rsp,%rdi
call   1040 <FD_ZERO@plt>
test   %ebp,%ebp
je     117a <SocketWait+0x3a>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1060 <FD_SET@plt>
test   %r14d,%r14d
je     1189 <SocketWait+0x49>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <FD_SET@plt>
movslq %r15d,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x8(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0xc(%rsp)
mov    %rbx,0x10(%rsp)
lea    0x1(%rbx),%ebp
lea    0x4(%rsp),%r15
mov    %rsp,%r12
lea    0x8(%rsp),%rbx
mov    0x2e14(%rip),%r13
mov    0x2e05(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1050 <select@plt>
cmp    $0xffffffff,%eax
jne    1204 <SocketWait+0xc4>
mov    0x0(%r13),%eax
cmp    (%r14),%eax
je     11e0 <SocketWait+0xa0>
xor    %eax,%eax
jmp    1239 <SocketWait+0xf9>
test   %eax,%eax
mov    0x10(%rsp),%rbx
je     1239 <SocketWait+0xf9>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    1239 <SocketWait+0xf9>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
add    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@errno = global i32 0
@EINTR = global i32 0
define i32 @SocketWait(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.timeval
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = call i32 @FD_ZERO(i32* %10)
%14 = call i32 @FD_ZERO(i32* %11)
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i32, i32* %6
%19 = call i32 @FD_SET(i32 %18, i32* %10)
br label %20
20: ; preds = %17, %4
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = load i32, i32* %6
%25 = call i32 @FD_SET(i32 %24, i32* %11)
br label %26
26: ; preds = %23, %20
%27 = load i32, i32* %9
%28 = sdiv i32 %27, 1000
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 0
store i32 %28, i32* %29
%30 = load i32, i32* %9
%31 = srem i32 %30, 1000
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %12, i32 0, i32 1
store i32 %31, i32* %32
br label %33
33: ; preds = %55, %26
%34 = load i32, i32* %6
%35 = add nsw i32 %34, 1
%36 = call i32 @select(i32 %35, i32* %10, i32* %11, i32* null, %struct.timeval* %12)
switch i32 %36, label %44 [
i32 0, label %37
i32 -1, label %38
]
37: ; preds = %33
store i32 0, i32* %5
br label %56
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = load i32, i32* @EINTR
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %43
42: ; preds = %38
br label %55
43: ; preds = %38
store i32 0, i32* %5
br label %56
44: ; preds = %33
%45 = load i32, i32* %6
%46 = call i32 @FD_ISSET(i32 %45, i32* %10)
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %44
store i32 1, i32* %5
br label %56
49: ; preds = %44
%50 = load i32, i32* %6
%51 = call i32 @FD_ISSET(i32 %50, i32* %11)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
store i32 2, i32* %5
br label %56
54: ; preds = %49
store i32 0, i32* %5
br label %56
55: ; preds = %42
br label %33
56: ; preds = %54, %53, %48, %43, %37
%57 = load i32, i32* %5
ret i32 %57
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@errno = global i32 0
@EINTR = global i32 0
define i32 @SocketWait(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = bitcast i32* %5 to i8*
%9 = bitcast i32* %6 to i8*
%10 = bitcast %struct.timeval* %7 to i8*
%11 = call i32 @FD_ZERO(i32* %5)
%12 = call i32 @FD_ZERO(i32* %6)
%13 = icmp eq i32 %1, 0
br i1 %13, label %16, label %14
14: ; preds = %4
%15 = call i32 @FD_SET(i32 %0, i32* %5)
br label %16
16: ; preds = %14, %4
%17 = icmp eq i32 %2, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = call i32 @FD_SET(i32 %0, i32* %6)
br label %20
20: ; preds = %18, %16
%21 = sdiv i32 %3, 1000
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 0
store i32 %21, i32* %22
%23 = srem i32 %3, 1000
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i64 0, i32 1
store i32 %23, i32* %24
%25 = add nsw i32 %0, 1
br label %26
26: ; preds = %28, %20
%27 = call i32 @select(i32 %25, i32* %5, i32* %6, i32* null, %struct.timeval* %7)
switch i32 %27, label %32 [
i32 0, label %39
i32 -1, label %28
]
28: ; preds = %26
%29 = load i32, i32* @errno
%30 = load i32, i32* @EINTR
%31 = icmp eq i32 %29, %30
br i1 %31, label %26, label %39
32: ; preds = %26
%33 = call i32 @FD_ISSET(i32 %0, i32* %5)
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %32
%36 = call i32 @FD_ISSET(i32 %0, i32* %6)
%37 = icmp eq i32 %36, 0
%38 = select i1 %37, i32 0, i32 2
br label %39
39: ; preds = %28, %26, %35, %32
%40 = phi i32 [ 1, %32 ], [ %38, %35 ], [ 0, %28 ], [ %27, %26 ]
ret i32 %40
}