#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*/ *);
scalar_t__ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, struct timeval *);
int socket_wait_read(int socket, int timeout) {
  fd_set read_fd_set;
  struct timeval tv;
  struct timeval *tvp;
  if (timeout == -1) {
    tvp = NULL;
  } else {
    tvp = &tv;
  }
  FD_ZERO(&read_fd_set);
  FD_SET(socket, &read_fd_set);
  tv.tv_sec = 0;
  tv.tv_usec = timeout;
  if (select(socket + 1, &read_fd_set, NULL, NULL, tvp)) {
    return 1;
  } else {
    return 0;
  }
}


x86 assembly (clang, -O0):
<socket_wait_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1155 <socket_wait_read+0x25>
movq   $0x0,-0x28(%rbp)
jmp    115d <socket_wait_read+0x2d>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
call   1030 <FD_ZERO@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <FD_SET@plt>
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
add    $0x1,%edi
mov    -0x28(%rbp),%r8
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
je     11b0 <socket_wait_read+0x80>
movl   $0x1,-0x4(%rbp)
jmp    11b7 <socket_wait_read+0x87>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_wait_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
cmp    $0xffffffff,%esi
lea    0x8(%rsp),%rbx
cmove  %rax,%rbx
lea    0x4(%rsp),%r15
mov    %r15,%rdi
call   1030 <FD_ZERO@plt>
mov    %ebp,%edi
mov    %r15,%rsi
call   1050 <FD_SET@plt>
movq   $0x0,0x10(%rsp)
mov    %r14d,0x8(%rsp)
add    $0x1,%ebp
mov    %ebp,%edi
mov    %r15,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1040 <select@plt>
xor    %ecx,%ecx
test   %rax,%rax
setne  %cl
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define i32 @socket_wait_read(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 = icmp eq i32 %1, -1
%8 = select i1 %7, %struct.timeval* null, %struct.timeval* %4
%9 = call i32 @FD_ZERO(i32* %3)
%10 = call i32 @FD_SET(i32 %0, i32* %3)
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 %1, i32* %12
%13 = add nsw i32 %0, 1
%14 = call i64 @select(i32 %13, i32* %3, i32* null, i32* null, %struct.timeval* %8)
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i32
ret i32 %16
}