#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*/ EXIT_SUCCESS;
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int FD_SETSIZE;
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(scalar_t__, char *, int);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int main(void) {
  printf("FD_SETSIZE=%d\n", FD_SETSIZE);
  printf("Wait 5 seconds for select() to time out, or press a key to cause it "
         "to return before it times out.\n");
  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(STDIN_FILENO, &rfds);
  struct timeval tv;
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  int retval = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
  if (retval == -1)
    perror("select()");
  else if (retval)
    printf("There is data to read from the file descriptor (%d of the fds are "
           "ready).\n",
           retval);
  else
    printf("We timed out waiting for there to be data to read from the file "
           "descriptor.\n");
  if (1) {
    char buf[1024];
    read(STDIN_FILENO, buf, 1024);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe6f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x8(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%rdi
lea    -0x8(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
lea    -0x8(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1060 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1212 <main+0xa2>
lea    0xe6b(%rip),%rdi
call   1070 <perror@plt>
jmp    1245 <main+0xd5>
cmpl   $0x0,-0x1c(%rbp)
je     1232 <main+0xc2>
mov    -0x1c(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1240 <main+0xd0>
lea    0xe8e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1245 <main+0xd5>
mov    0x2d84(%rip),%rax
mov    (%rax),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x428,%rsp
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe66(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xebb(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <FD_ZERO@plt>
mov    0x2e0f(%rip),%r14
mov    (%r14),%rdi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%r14),%rdi
add    $0x1,%rdi
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
cmp    $0xffffffff,%eax
jne    1226 <main+0xa6>
lea    0xe0e(%rip),%rdi
call   1080 <perror@plt>
mov    (%r14),%rdi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
test   %eax,%eax
jne    1238 <main+0xb8>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
jmp    1206 <main+0x86>
lea    0xdd9(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1206 <main+0x86>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [15 x i8] c"FD_SETSIZE=%d\0A\00"
@FD_SETSIZE = global i32 0
@.str.1 = [100 x i8] c"Wait 5 seconds for select() to time out, or press a key to cause it to return before it times out.\0A\00"
@STDIN_FILENO = global i64 0
@.str.2 = [9 x i8] c"select()\00"
@.str.3 = [75 x i8] c"There is data to read from the file descriptor (%d of the fds are ready).\0A\00"
@.str.4 = [77 x i8] c"We timed out waiting for there to be data to read from the file descriptor.\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca [1024 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @FD_SETSIZE
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @FD_ZERO(i32* %2)
%10 = load i64, i64* @STDIN_FILENO
%11 = call i32 @FD_SET(i64 %10, i32* %2)
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i32 5, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %13
%14 = load i64, i64* @STDIN_FILENO
%15 = add nsw i64 %14, 1
%16 = call i32 @select(i64 %15, i32* %2, i32* null, i32* null, %struct.timeval* %3)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %30
21: ; preds = %0
%22 = load i32, i32* %4
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.3, i64 0, i64 0), i32 %25)
br label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([77 x i8], [77 x i8]* @.str.4, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %30
30: ; preds = %29, %19
%31 = load i64, i64* @STDIN_FILENO
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%33 = call i32 @read(i64 %31, i8* %32, i32 1024)
%34 = load i32, i32* @EXIT_SUCCESS
%35 = call i32 @exit(i32 %34)
unreachable
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [15 x i8] c"FD_SETSIZE=%d\0A\00"
@FD_SETSIZE = global i32 0
@STDIN_FILENO = global i64 0
@.str.2 = [9 x i8] c"select()\00"
@.str.3 = [75 x i8] c"There is data to read from the file descriptor (%d of the fds are ready).\0A\00"
@EXIT_SUCCESS = global i32 0
@str = [99 x i8] c"Wait 5 seconds for select() to time out, or press a key to cause it to return before it times out.\00"
@str.5 = [76 x i8] c"We timed out waiting for there to be data to read from the file descriptor.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca [1024 x i8]
%4 = load i32, i32* @FD_SETSIZE
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([99 x i8], [99 x i8]* @str, i64 0, i64 0))
%7 = bitcast i32* %1 to i8*
%8 = call i32 @FD_ZERO(i32* %1)
%9 = load i64, i64* @STDIN_FILENO
%10 = call i32 @FD_SET(i64 %9, i32* %1)
%11 = bitcast %struct.timeval* %2 to i8*
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 5, i32* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %13
%14 = load i64, i64* @STDIN_FILENO
%15 = add nsw i64 %14, 1
%16 = call i32 @select(i64 %15, i32* %1, i32* null, i32* null, %struct.timeval* %2)
switch i32 %16, label %19 [
i32 -1, label %17
i32 0, label %21
]
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %23
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([75 x i8], [75 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %23
21: ; preds = %0
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([76 x i8], [76 x i8]* @str.5, i64 0, i64 0))
br label %23
23: ; preds = %19, %21, %17
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%25 = load i64, i64* @STDIN_FILENO
%26 = call i32 @read(i64 %25, i8* %24, i32 1024)
%27 = load i32, i32* @EXIT_SUCCESS
%28 = call i32 @exit(i32 %27)
unreachable
}