#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 */
scalar_t__ FD_ISSET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
scalar_t__ STDIN_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(scalar_t__, char *, int);
int select(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int main(void) {
  struct timeval tv;
  fd_set readfds;
  int ret;
  FD_ZERO(&readfds);
  FD_SET(STDIN_FILENO, &readfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  ret = select(STDIN_FILENO + 1, &readfds, NULL, NULL, &tv);
  if (ret == -1) {
    perror("select");
    return 1;
  } else if (!ret) {
    printf("%d seconds elapsed.\n", 5);
    return 0;
  }
  if (FD_ISSET(STDIN_FILENO, &readfds)) {
    char buf[1024 + 1];
    int len;
    len = read(STDIN_FILENO, buf, 1024);
    if (len == -1) {
      perror("read");
      return 1;
    }
    if (len) {
      buf[len] = '\0';
      printf("read: %s\n", buf);
    }
    return 0;
  }
  fprintf(stderr, "This should not happen!\n");
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x1c(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1204 <main+0x84>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ee <main+0x16e>
cmpl   $0x0,-0x20(%rbp)
jne    122d <main+0xad>
lea    0xdf2(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ee <main+0x16e>
jmp    1232 <main+0xb2>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12d2 <main+0x152>
mov    0x2d82(%rip),%rax
mov    (%rax),%rdi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0xffffffff,-0x434(%rbp)
jne    1295 <main+0x115>
lea    0xd98(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ee <main+0x16e>
cmpl   $0x0,-0x434(%rbp)
je     12c6 <main+0x146>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ee <main+0x16e>
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
lea    0xd49(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x430,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    0x2e38(%rip),%r15
mov    (%r15),%rdi
mov    %rbx,%rsi
call   10a0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%r15),%rdi
add    $0x1,%rdi
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1080 <select@plt>
test   %eax,%eax
je     1235 <main+0xb5>
cmp    $0xffffffff,%eax
je     1275 <main+0xf5>
mov    (%r15),%rdi
lea    0xc(%rsp),%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     124a <main+0xca>
mov    (%r15),%rdi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,%r14d
test   %eax,%eax
je     1265 <main+0xe5>
cmp    $0xffffffff,%r14d
je     1283 <main+0x103>
movslq %r14d,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xdfd(%rip),%rdi
xor    %r14d,%r14d
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1265 <main+0xe5>
lea    0xdcb(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1265 <main+0xe5>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd84(%rip),%rdi
call   1090 <perror@plt>
jmp    125f <main+0xdf>
lea    0xd92(%rip),%rdi
call   1090 <perror@plt>
jmp    125f <main+0xdf>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [21 x i8] c"%d seconds elapsed.\0A\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [10 x i8] c"read: %s\0A\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"This should not happen!\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1025 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 @FD_ZERO(i32* %3)
%8 = load i64, i64* @STDIN_FILENO
%9 = call i32 @FD_SET(i64 %8, i32* %3)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 0
store i32 5, i32* %10
%11 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i32 0, i32 1
store i64 0, i64* %11
%12 = load i64, i64* @STDIN_FILENO
%13 = add nsw i64 %12, 1
%14 = call i32 @select(i64 %13, i32* %3, i32* null, i32* null, %struct.timeval* %2)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %50
19: ; preds = %0
%20 = load i32, i32* %4
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %19
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i32 0, i32* %1
br label %50
24: ; preds = %19
br label %25
25: ; preds = %24
%26 = load i64, i64* @STDIN_FILENO
%27 = call i64 @FD_ISSET(i64 %26, i32* %3)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = load i64, i64* @STDIN_FILENO
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%32 = call i32 @read(i64 %30, i8* %31, i32 1024)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %50
37: ; preds = %29
%38 = load i32, i32* %6
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %37
%41 = load i32, i32* %6
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 %42
store i8 0, i8* %43
%44 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %40, %37
store i32 0, i32* %1
br label %50
47: ; preds = %25
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %50
50: ; preds = %47, %46, %35, %22, %17
%51 = load i32, i32* %1
ret i32 %51
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i64 0
@.str = [7 x i8] c"select\00"
@.str.1 = [21 x i8] c"%d seconds elapsed.\0A\00"
@.str.2 = [5 x i8] c"read\00"
@.str.3 = [10 x i8] c"read: %s\0A\00"
@stderr = global i32 0
@.str.4 = [25 x i8] c"This should not happen!\0A\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca i32
%3 = alloca [1025 x i8]
%4 = bitcast %struct.timeval* %1 to i8*
%5 = bitcast i32* %2 to i8*
%6 = call i32 @FD_ZERO(i32* %2)
%7 = load i64, i64* @STDIN_FILENO
%8 = call i32 @FD_SET(i64 %7, i32* %2)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
store i32 5, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
store i64 0, i64* %10
%11 = load i64, i64* @STDIN_FILENO
%12 = add nsw i64 %11, 1
%13 = call i32 @select(i64 %12, i32* %2, i32* null, i32* null, %struct.timeval* %1)
switch i32 %13, label %18 [
i32 -1, label %14
i32 0, label %16
]
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %37
16: ; preds = %0
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 5)
br label %37
18: ; preds = %0
%19 = load i64, i64* @STDIN_FILENO
%20 = call i64 @FD_ISSET(i64 %19, i32* %2)
%21 = icmp eq i64 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %18
%23 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%24 = load i64, i64* @STDIN_FILENO
%25 = call i32 @read(i64 %24, i8* %23, i32 1024)
switch i32 %25, label %28 [
i32 -1, label %26
i32 0, label %32
]
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %32
28: ; preds = %22
%29 = sext i32 %25 to i64
%30 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %23)
br label %32
32: ; preds = %28, %22, %26
%33 = phi i32 [ 1, %26 ], [ %25, %22 ], [ 0, %28 ]
br label %37
34: ; preds = %18
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %34, %32, %16, %14
%38 = phi i32 [ 1, %14 ], [ %33, %32 ], [ 1, %34 ], [ 0, %16 ]
ret i32 %38
}