#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 ((retval = select(fd + 1, &rfds, &wfds, &efds, &tv)) > 0) {
    if (FD_ISSET(fd, &rfds))
      retmask |= 1;
    if (FD_ISSET(fd, &wfds))
      retmask |= 2;
    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    -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    1245 <aeWait+0x105>
mov    -0x8(%rbp),%edi
lea    -0x2c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     121b <aeWait+0xdb>
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     123a <aeWait+0xfa>
mov    -0x38(%rbp),%eax
or     $0x2,%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    124b <aeWait+0x10b>
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   %rbx
sub    $0x28,%rsp
mov    %rdx,%rcx
mov    %esi,%ebp
mov    %edi,%ebx
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,0x18(%rsp)
imul   $0x3e8,%rdx,%rax
sub    %rax,%rcx
imul   $0x3e8,%rcx,%rax
mov    %rax,0x20(%rsp)
lea    0x10(%rsp),%rdi
call   1040 <FD_ZERO@plt>
lea    0xc(%rsp),%rdi
call   1040 <FD_ZERO@plt>
lea    0x14(%rsp),%rdi
call   1040 <FD_ZERO@plt>
test   $0x1,%bpl
je     11b6 <aeWait+0x76>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1060 <FD_SET@plt>
test   $0x2,%bpl
je     11c8 <aeWait+0x88>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
lea    0x14(%rsp),%rcx
lea    0x18(%rsp),%r8
call   1050 <select@plt>
test   %eax,%eax
jle    1215 <aeWait+0xd5>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
mov    %rax,%rcx
lea    0x2(%rbp),%eax
test   %rcx,%rcx
cmove  %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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* %5
%39 = add nsw i32 %38, 1
%40 = call i32 @select(i32 %39, i32* %9, i32* %10, i32* %11, %struct.timeval* %8)
store i32 %40, i32* %13
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %58
42: ; preds = %37
%43 = load i32, i32* %5
%44 = call i64 @FD_ISSET(i32 %43, i32* %9)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = load i32, i32* %12
%48 = or i32 %47, 1
store i32 %48, i32* %12
br label %49
49: ; preds = %46, %42
%50 = load i32, i32* %5
%51 = call i64 @FD_ISSET(i32 %50, i32* %10)
%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, 2
store i32 %55, i32* %12
br label %56
56: ; preds = %53, %49
%57 = load i32, i32* %12
store i32 %57, i32* %4
br label %60
58: ; preds = %37
%59 = load i32, i32* %13
store i32 %59, i32* %4
br label %60
60: ; preds = %58, %56
%61 = load i32, i32* %4
ret i32 %61
}

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 = add nsw i32 %0, 1
%31 = call i32 @select(i32 %30, i32* %5, i32* %6, i32* %7, %struct.timeval* %4)
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = call i64 @FD_ISSET(i32 %0, i32* %5)
%35 = icmp ne i64 %34, 0
%36 = zext i1 %35 to i32
%37 = call i64 @FD_ISSET(i32 %0, i32* %6)
%38 = icmp eq i64 %37, 0
%39 = or i32 %36, 2
%40 = select i1 %38, i32 %36, i32 %39
br label %41
41: ; preds = %29, %33
%42 = phi i32 [ %40, %33 ], [ %31, %29 ]
ret i32 %42
}