#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 member_0;
  int /*<<< orphan*/ member_1;
};
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 waitfor(int fd, int timeout) {
  fd_set rfds;
  struct timeval tv = {timeout, 0};
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  return select(fd + 1, &rfds, NULL, NULL, (timeout > 0) ? &tv : NULL);
}


x86 assembly (clang, -O0):
<waitfor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_SET@plt>
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1180 <waitfor+0x50>
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    118b <waitfor+0x5b>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    118b <waitfor+0x5b>
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%r8
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <select@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitfor>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    %esi,(%rsp)
movl   $0x0,0x4(%rsp)
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
add    $0x1,%ebp
xor    %eax,%eax
test   %ebx,%ebx
mov    %rsp,%r8
cmovle %rax,%r8
mov    %ebp,%edi
mov    %r14,%rsi
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 @waitfor(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.timeval
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
%8 = load i32, i32* %4
store i32 %8, i32* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i32 0, i32* %9
%10 = call i32 @FD_ZERO(i32* %5)
%11 = load i32, i32* %3
%12 = call i32 @FD_SET(i32 %11, i32* %5)
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
br label %19
18: ; preds = %2
br label %19
19: ; preds = %18, %17
%20 = phi %struct.timeval* [ %6, %17 ], [ null, %18 ]
%21 = call i32 @select(i32 %14, i32* %5, i32* null, i32* null, %struct.timeval* %20)
ret i32 %21
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define i32 @waitfor(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.timeval
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.timeval* %4 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %1, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i32 0, i32* %8
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = add nsw i32 %0, 1
%12 = icmp sgt i32 %1, 0
%13 = select i1 %12, %struct.timeval* %4, %struct.timeval* null
%14 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %13)
ret i32 %14
}