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

/* Variables and functions */
scalar_t__ FD_ISSET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int aeWait(int fd, int mask, long long milliseconds) {
  struct timeval tv;
  fd_set rfds, wfds, efds;
  int retmask = 0, retval;
  tv.tv_sec = milliseconds / 1000;
  tv.tv_usec = (milliseconds % 1000) * 1000;
  FD_ZERO(&rfds);
  FD_ZERO(&wfds);
  FD_ZERO(&efds);
  if (mask & 1)
    FD_SET(fd, &rfds);
  if (mask & 2)
    FD_SET(fd, &wfds);
  if (mask & 4)
    FD_SET(fd, &efds);
  if ((retval = select(fd + 1, &rfds, &wfds, &efds, &tv)) > 0) {
    if (FD_ISSET(fd, &rfds))
      retmask |= 1;
    if (FD_ISSET(fd, &wfds))
      retmask |= 2;
    if (FD_ISSET(fd, &efds))
      retmask |= 4;
    return retmask;
  } else {
    return retval;
  }
}


x86 assembly (clang, -O0):
<aeWait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3e8,%ecx
cqto
idiv   %rcx
imul   $0x3e8,%rdx,%rax
mov    %rax,-0x20(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x30(%rbp),%rdi
call   1040 <FD_ZERO@plt>
lea    -0x34(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11ba <aeWait+0x7a>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0xc(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     11d5 <aeWait+0x95>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0xc(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     11f0 <aeWait+0xb0>
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x2c(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x34(%rbp),%rcx
lea    -0x28(%rbp),%r8
call   1050 <select@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jle    127f <aeWait+0x13f>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1236 <aeWait+0xf6>
mov    -0x38(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1255 <aeWait+0x115>
mov    -0x38(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x34(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1274 <aeWait+0x134>
mov    -0x38(%rbp),%eax
or     $0x4,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1285 <aeWait+0x145>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeWait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rcx
mov    %esi,%ebp
mov    %edi,%r14d
movabs $0x20c49ba5e353f7cf,%rdx
9b c4 20
mov    %rcx,%rax
imul   %rdx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x7,%rdx
add    %rax,%rdx
mov    %rdx,0x10(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rcx
imul   $0x3e8,%rcx,%rax
mov    %rax,0x18(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <FD_ZERO@plt>
lea    0x8(%rsp),%rdi
call   1040 <FD_ZERO@plt>
lea    0x4(%rsp),%rdi
call   1040 <FD_ZERO@plt>
test   $0x1,%bpl
jne    123d <aeWait+0xfd>
test   $0x2,%bpl
jne    1254 <aeWait+0x114>
test   $0x4,%bpl
je     11ce <aeWait+0x8e>
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1060 <FD_SET@plt>
lea    0x1(%r14),%edi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
lea    0x10(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
jle    1234 <aeWait+0xf4>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
lea    0x2(%rbp),%ebx
test   %rax,%rax
cmove  %ebp,%ebx
lea    0x4(%rsp),%rsi
mov    %r14d,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
lea    0x4(%rbx),%eax
test   %rcx,%rcx
cmove  %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
call   1060 <FD_SET@plt>
test   $0x2,%bpl
je     11bb <aeWait+0x7b>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
call   1060 <FD_SET@plt>
test   $0x4,%bpl
jne    11c1 <aeWait+0x81>
jmp    11ce <aeWait+0x8e>

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define i32 @aeWait(i32 %0, i32 %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.timeval
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i64 %2, i64* %7
store i32 0, i32* %12
%14 = load i64, i64* %7
%15 = sdiv i64 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i64 %15, i64* %16
%17 = load i64, i64* %7
%18 = srem i64 %17, 1000
%19 = mul nsw i64 %18, 1000
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 %19, i64* %20
%21 = call i32 @FD_ZERO(i32* %9)
%22 = call i32 @FD_ZERO(i32* %10)
%23 = call i32 @FD_ZERO(i32* %11)
%24 = load i32, i32* %6
%25 = and i32 %24, 1
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %3
%28 = load i32, i32* %5
%29 = call i32 @FD_SET(i32 %28, i32* %9)
br label %30
30: ; preds = %27, %3
%31 = load i32, i32* %6
%32 = and i32 %31, 2
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = load i32, i32* %5
%36 = call i32 @FD_SET(i32 %35, i32* %10)
br label %37
37: ; preds = %34, %30
%38 = load i32, i32* %6
%39 = and i32 %38, 4
%40 = icmp ne i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* %5
%43 = call i32 @FD_SET(i32 %42, i32* %11)
br label %44
44: ; preds = %41, %37
%45 = load i32, i32* %5
%46 = add nsw i32 %45, 1
%47 = call i32 @select(i32 %46, i32* %9, i32* %10, i32* %11, %struct.timeval* %8)
store i32 %47, i32* %13
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %72
49: ; preds = %44
%50 = load i32, i32* %5
%51 = call i64 @FD_ISSET(i32 %50, i32* %9)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = load i32, i32* %12
%55 = or i32 %54, 1
store i32 %55, i32* %12
br label %56
56: ; preds = %53, %49
%57 = load i32, i32* %5
%58 = call i64 @FD_ISSET(i32 %57, i32* %10)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %56
%61 = load i32, i32* %12
%62 = or i32 %61, 2
store i32 %62, i32* %12
br label %63
63: ; preds = %60, %56
%64 = load i32, i32* %5
%65 = call i64 @FD_ISSET(i32 %64, i32* %11)
%66 = icmp ne i64 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %63
%68 = load i32, i32* %12
%69 = or i32 %68, 4
store i32 %69, i32* %12
br label %70
70: ; preds = %67, %63
%71 = load i32, i32* %12
store i32 %71, i32* %4
br label %74
72: ; preds = %44
%73 = load i32, i32* %13
store i32 %73, i32* %4
br label %74
74: ; preds = %72, %70
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @aeWait(i32 %0, i32 %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast %struct.timeval* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = sdiv i64 %2, 1000
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i64 %12, i64* %13
%14 = srem i64 %2, 1000
%15 = mul nsw i64 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 %15, i64* %16
%17 = call i32 @FD_ZERO(i32* %5)
%18 = call i32 @FD_ZERO(i32* %6)
%19 = call i32 @FD_ZERO(i32* %7)
%20 = and i32 %1, 1
%21 = icmp eq i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %3
%23 = call i32 @FD_SET(i32 %0, i32* %5)
br label %24
24: ; preds = %22, %3
%25 = and i32 %1, 2
%26 = icmp eq i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @FD_SET(i32 %0, i32* %6)
br label %29
29: ; preds = %27, %24
%30 = and i32 %1, 4
%31 = icmp eq i32 %30, 0
br i1 %31, label %34, label %32
32: ; preds = %29
%33 = call i32 @FD_SET(i32 %0, i32* %7)
br label %34
34: ; preds = %32, %29
%35 = add nsw i32 %0, 1
%36 = call i32 @select(i32 %35, i32* %5, i32* %6, i32* %7, %struct.timeval* %4)
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %50
38: ; preds = %34
%39 = call i64 @FD_ISSET(i32 %0, i32* %5)
%40 = icmp ne i64 %39, 0
%41 = zext i1 %40 to i32
%42 = call i64 @FD_ISSET(i32 %0, i32* %6)
%43 = icmp eq i64 %42, 0
%44 = or i32 %41, 2
%45 = select i1 %43, i32 %41, i32 %44
%46 = call i64 @FD_ISSET(i32 %0, i32* %7)
%47 = icmp eq i64 %46, 0
%48 = or i32 %45, 4
%49 = select i1 %47, i32 %45, i32 %48
br label %50
50: ; preds = %34, %38
%51 = phi i32 [ %49, %38 ], [ %36, %34 ]
ret i32 %51
}