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

/* Variables and functions */
int /*<<< orphan*/ FD_ISSET(int, int /*<<< orphan*/ *);
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 wait_for_read(int s, long sec) {
  fd_set fds;
  int n, nfd;
  struct timeval tv;
  tv.tv_sec = sec;
  tv.tv_usec = 0;
  nfd = s;
  FD_ZERO(&fds);
  FD_SET(s, &fds);
  n = select(nfd + 1, &fds, 0, 0, &tv);
  switch (n) {
  case -1:
    return (-1);
  case 0:
    return (0);
  default:
    if (FD_ISSET(s, &fds))
      return (s);
    else
      return (-1);
  }
}


x86 assembly (clang, -O0):
<wait_for_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x14(%rbp),%rdi
mov    %rdi,-0x40(%rbp)
call   1040 <FD_ZERO@plt>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%edi
call   1060 <FD_SET@plt>
mov    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edi
add    $0x1,%edi
xor    %ecx,%ecx
lea    -0x30(%rbp),%r8
mov    %ecx,%edx
call   1050 <select@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     11bc <wait_for_read+0x7c>
jmp    11ac <wait_for_read+0x6c>
mov    -0x34(%rbp),%eax
test   %eax,%eax
je     11c8 <wait_for_read+0x88>
jmp    11d4 <wait_for_read+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fb <wait_for_read+0xbb>
movl   $0x0,-0x4(%rbp)
jmp    11fb <wait_for_read+0xbb>
mov    -0x8(%rbp),%edi
lea    -0x14(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%eax
je     11f4 <wait_for_read+0xb4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11fb <wait_for_read+0xbb>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_read>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsi,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
lea    0x1(%rbx),%edi
lea    0x8(%rsp),%r8
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0xffffffff,%eax
je     11a2 <wait_for_read+0x62>
test   %eax,%eax
jne    118f <wait_for_read+0x4f>
xor    %eax,%eax
jmp    11a2 <wait_for_read+0x62>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
call   1030 <FD_ISSET@plt>
cmp    $0x1,%eax
sbb    %eax,%eax
or     %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define i32 @wait_for_read(i32 %0, i64 %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 i64 %1, i64* %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 = add nsw i32 %0, 1
%12 = call i32 @select(i32 %11, i32* %3, i32 0, i32 0, %struct.timeval* %4)
switch i32 %12, label %14 [
i32 -1, label %18
i32 0, label %13
]
13: ; preds = %2
br label %18
14: ; preds = %2
%15 = call i32 @FD_ISSET(i32 %0, i32* %3)
%16 = icmp eq i32 %15, 0
%17 = select i1 %16, i32 -1, i32 %0
br label %18
18: ; preds = %14, %2, %13
%19 = phi i32 [ 0, %13 ], [ %12, %2 ], [ %17, %14 ]
ret i32 %19
}