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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          struct timeval *);
void wait_for_fd(int fd, int write) {
  fd_set fds;
  struct timeval tv;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  tv.tv_sec = 30;
  tv.tv_usec = 0;
  if (write) {
    select(FD_SETSIZE, NULL, &fds, NULL, &tv);
  } else {
    select(FD_SETSIZE, &fds, NULL, NULL, &tv);
  }
}


x86 assembly (clang, -O0):
<wait_for_fd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1050 <FD_SET@plt>
movl   $0x1e,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
je     118e <wait_for_fd+0x5e>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0xc(%rbp),%rdx
lea    -0x20(%rbp),%r8
mov    %rcx,%rsi
call   1040 <select@plt>
jmp    11ab <wait_for_fd+0x7b>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    -0xc(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1040 <select@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_fd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <FD_SET@plt>
movl   $0x1e,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
test   %r14d,%r14d
je     1181 <wait_for_fd+0x51>
lea    0xc(%rsp),%rdx
lea    0x10(%rsp),%r8
xor    %esi,%esi
jmp    118d <wait_for_fd+0x5d>
lea    0xc(%rsp),%rsi
lea    0x10(%rsp),%r8
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define void @wait_for_fd(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 = call i32 @FD_ZERO(i32* %5)
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %5)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 30, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %11
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @FD_SETSIZE
%16 = call i32 @select(i32 %15, i32* null, i32* %5, i32* null, %struct.timeval* %6)
br label %20
17: ; preds = %2
%18 = load i32, i32* @FD_SETSIZE
%19 = call i32 @select(i32 %18, i32* %5, i32* null, i32* null, %struct.timeval* %6)
br label %20
20: ; preds = %17, %14
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@FD_SETSIZE = global i32 0
define void @wait_for_fd(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 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 30, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %10
%11 = icmp eq i32 %1, 0
%12 = load i32, i32* @FD_SETSIZE
br i1 %11, label %15, label %13
13: ; preds = %2
%14 = call i32 @select(i32 %12, i32* null, i32* %3, i32* null, %struct.timeval* %4)
br label %17
15: ; preds = %2
%16 = call i32 @select(i32 %12, i32* %3, i32* null, i32* null, %struct.timeval* %4)
br label %17
17: ; preds = %15, %13
ret void
}