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

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ FD_SET(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ errno;
int /*<<< orphan*/ error_exit(char *);
int select(int /*<<< orphan*/, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int io_select(struct timeval *timeout) {
  int n;
  fd_set input;
  FD_ZERO(&input);
  FD_SET(0, &input);
  do {
    n = select(FD_SETSIZE, &input, NULL, NULL, timeout);
  } while (n == -1 && errno == EINTR);
  if (n < 0) {
    error_exit("Could not select from stdin");
  }
  return n;
}


x86 assembly (clang, -O0):
<io_select>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
lea    -0x10(%rbp),%rsi
call   1060 <FD_SET@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%r8
lea    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0xc(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0xc(%rbp)
mov    %al,-0x11(%rbp)
jne    11a9 <io_select+0x69>
mov    0x2e42(%rip),%rax
mov    (%rax),%rax
mov    0x2e30(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1160 <io_select+0x20>
cmpl   $0x0,-0xc(%rbp)
jge    11ca <io_select+0x8a>
lea    0xe3b(%rip),%rdi
call   1030 <error_exit@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<io_select>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0x4(%rsp),%r14
mov    %r14,%rdi
call   1040 <FD_ZERO@plt>
xor    %edi,%edi
mov    %r14,%rsi
call   1060 <FD_SET@plt>
mov    0x2e74(%rip),%r15
mov    0x2e65(%rip),%r12
mov    0x2e56(%rip),%r13
nopw   0x0(%rax,%rax,1)
mov    (%r15),%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1050 <select@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11a3 <io_select+0x63>
mov    0x0(%r13),%rax
cmp    %rax,(%r12)
je     1180 <io_select+0x40>
test   %ebp,%ebp
jns    11b3 <io_select+0x73>
lea    0xe52(%rip),%rdi
call   1030 <error_exit@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [28 x i8] c"Could not select from stdin\00"
define i32 @io_select(%struct.timeval* %0) {
%2 = alloca %struct.timeval*
%3 = alloca i32
%4 = alloca i32
store %struct.timeval* %0, %struct.timeval** %2
%5 = call i32 @FD_ZERO(i32* %4)
%6 = call i32 @FD_SET(i32 0, i32* %4)
br label %7
7: ; preds = %18, %1
%8 = load i32, i32* @FD_SETSIZE
%9 = load %struct.timeval*, %struct.timeval** %2
%10 = call i32 @select(i32 %8, i32* %4, i32* null, i32* null, %struct.timeval* %9)
store i32 %10, i32* %3
br label %11
11: ; preds = %7
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i64, i64* @errno
%16 = load i64, i64* @EINTR
%17 = icmp eq i64 %15, %16
br label %18
18: ; preds = %14, %11
%19 = phi i1 [ false, %11 ], [ %17, %14 ]
br i1 %19, label %7, label %20
20: ; preds = %18
%21 = load i32, i32* %3
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @error_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = load i32, i32* %3
ret i32 %26
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@FD_SETSIZE = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@.str = [28 x i8] c"Could not select from stdin\00"
define i32 @io_select(%struct.timeval* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @FD_ZERO(i32* %2)
%5 = call i32 @FD_SET(i32 0, i32* %2)
br label %6
6: ; preds = %6, %1
%7 = load i32, i32* @FD_SETSIZE
%8 = call i32 @select(i32 %7, i32* %2, i32* null, i32* null, %struct.timeval* %0)
%9 = icmp eq i32 %8, -1
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINTR
%12 = icmp eq i64 %10, %11
%13 = select i1 %9, i1 %12, i1 false
br i1 %13, label %6, label %14
14: ; preds = %6
%15 = icmp slt i32 %8, 0
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = call i32 @error_exit(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %14
ret i32 %8
}