#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*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int wait_for_fd(int direction, int fd, int msecs) {
  fd_set fds;
  int rc;
  struct timeval tv;
  tv.tv_sec = msecs / 1000;
  tv.tv_usec = (msecs % 1000) * 1000;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  if (direction)
    rc = select(fd + 1, NULL, &fds, NULL, &tv);
  else
    rc = select(fd + 1, &fds, NULL, NULL, &tv);
  return rc;
}


x86 assembly (clang, -O0):
<wait_for_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0x3e8,%edx,%eax
mov    %eax,-0x1c(%rbp)
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <FD_SET@plt>
cmpl   $0x0,-0x4(%rbp)
je     11a4 <wait_for_fd+0x74>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1040 <select@plt>
mov    %eax,-0x14(%rbp)
jmp    11c1 <wait_for_fd+0x91>
mov    -0x8(%rbp),%edi
add    $0x1,%edi
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
movslq %edx,%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
imul   $0x3e8,%eax,%eax
mov    %eax,0xc(%rsp)
lea    0x4(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebx
test   %r14d,%r14d
je     1199 <wait_for_fd+0x69>
lea    0x4(%rsp),%rdx
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %esi,%esi
jmp    11a7 <wait_for_fd+0x77>
lea    0x4(%rsp),%rsi
lea    0x8(%rsp),%r8
mov    %ebx,%edi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define i32 @wait_for_fd(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %6
%11 = sdiv i32 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* %6
%14 = srem i32 %13, 1000
%15 = mul nsw i32 %14, 1000
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @FD_ZERO(i32* %7)
%18 = load i32, i32* %5
%19 = call i32 @FD_SET(i32 %18, i32* %7)
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %3
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
%25 = call i32 @select(i32 %24, i32* null, i32* %7, i32* null, %struct.timeval* %9)
store i32 %25, i32* %8
br label %30
26: ; preds = %3
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
%29 = call i32 @select(i32 %28, i32* %7, i32* null, i32* null, %struct.timeval* %9)
store i32 %29, i32* %8
br label %30
30: ; preds = %26, %22
%31 = load i32, i32* %8
ret i32 %31
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @wait_for_fd(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = sdiv i32 %2, 1000
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 %8, i32* %9
%10 = srem i32 %2, 1000
%11 = mul nsw i32 %10, 1000
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i32 @FD_ZERO(i32* %4)
%14 = call i32 @FD_SET(i32 %1, i32* %4)
%15 = icmp eq i32 %0, 0
%16 = add nsw i32 %1, 1
br i1 %15, label %19, label %17
17: ; preds = %3
%18 = call i32 @select(i32 %16, i32* null, i32* %4, i32* null, %struct.timeval* %5)
br label %21
19: ; preds = %3
%20 = call i32 @select(i32 %16, i32* %4, i32* null, i32* null, %struct.timeval* %5)
br label %21
21: ; preds = %19, %17
%22 = phi i32 [ %18, %17 ], [ %20, %19 ]
ret i32 %22
}