#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_usec;
  scalar_t__ tv_sec;
};
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 read_wait(int fd, int msec) {
  struct timeval tv;
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = msec;
  return select(fd + 1, &rfds, NULL, NULL, (msec != -1) ? &tv : NULL);
}


x86 assembly (clang, -O0):
<read_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x1c(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
je     1181 <read_wait+0x51>
lea    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    118c <read_wait+0x5c>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    118c <read_wait+0x5c>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%r8
lea    -0x1c(%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):
<read_wait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xc(%rsp),%r14
mov    %r14,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <FD_SET@plt>
movq   $0x0,0x18(%rsp)
mov    %ebx,0x10(%rsp)
add    $0x1,%ebp
xor    %eax,%eax
cmp    $0xffffffff,%ebx
lea    0x10(%rsp),%r8
cmove  %rax,%r8
mov    %ebp,%edi
mov    %r14,%rsi
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 }
define i32 @read_wait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 @FD_ZERO(i32* %6)
%8 = load i32, i32* %3
%9 = call i32 @FD_SET(i32 %8, i32* %6)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* %3
%14 = add nsw i32 %13, 1
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, -1
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* [ %5, %17 ], [ null, %18 ]
%21 = call i32 @select(i32 %14, i32* %6, i32* null, i32* null, %struct.timeval* %20)
ret i32 %21
}

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