#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_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *, int /*<<< orphan*/,
           struct timeval *);
int socket_wait(int fd, int is_read) {
  fd_set fds, *fdr = 0, *fdw = 0;
  struct timeval tv;
  int ret;
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  FD_ZERO(&fds);
  FD_SET(fd, &fds);
  if (is_read)
    fdr = &fds;
  else
    fdw = &fds;
  ret = select(fd + 1, fdr, fdw, 0, &tv);
  if (ret == -1)
    perror("select");
  return ret;
}


x86 assembly (clang, -O0):
<socket_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x5,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0xc(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
call   1060 <FD_SET@plt>
cmpl   $0x0,-0x8(%rbp)
je     1199 <socket_wait+0x59>
lea    -0xc(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11a1 <socket_wait+0x61>
lea    -0xc(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
call   1040 <select@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    11d3 <socket_wait+0x93>
lea    0xe32(%rip),%rdi
call   1050 <perror@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_wait>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1060 <FD_SET@plt>
xor    %eax,%eax
test   %r14d,%r14d
mov    $0x0,%edx
cmove  %rbx,%rdx
cmove  %rax,%rbx
add    $0x1,%ebp
lea    0x10(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1040 <select@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11ad <socket_wait+0x6d>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1050 <perror@plt>
jmp    11a2 <socket_wait+0x62>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
define i32 @socket_wait(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca %struct.timeval
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32* null, i32** %6
store i32* null, i32** %7
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
store i32 5, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
store i64 0, i64* %11
%12 = call i32 @FD_ZERO(i32* %5)
%13 = load i32, i32* %3
%14 = call i32 @FD_SET(i32 %13, i32* %5)
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %2
store i32* %5, i32** %6
br label %19
18: ; preds = %2
store i32* %5, i32** %7
br label %19
19: ; preds = %18, %17
%20 = load i32, i32* %3
%21 = add nsw i32 %20, 1
%22 = load i32*, i32** %6
%23 = load i32*, i32** %7
%24 = call i32 @select(i32 %21, i32* %22, i32* %23, i32 0, %struct.timeval* %8)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %19
%30 = load i32, i32* %9
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [7 x i8] c"select\00"
define i32 @socket_wait(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 5, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %8
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = icmp eq i32 %1, 0
%12 = select i1 %11, i32* %3, i32* null
%13 = select i1 %11, i32* null, i32* %3
%14 = add nsw i32 %0, 1
%15 = call i32 @select(i32 %14, i32* %13, i32* %12, i32 0, %struct.timeval* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
ret i32 %15
}