#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   1060 <FD_ZERO@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e2c(%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   1070 <select@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    11f4 <main+0x84>
lea    0xe1d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c2 <main+0x152>
cmpl   $0x0,-0x20(%rbp)
jne    121d <main+0xad>
lea    0xe02(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c2 <main+0x152>
jmp    1222 <main+0xb2>
mov    0x2db7(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     12bb <main+0x14b>
mov    0x2d9a(%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    1285 <main+0x115>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c2 <main+0x152>
cmpl   $0x0,-0x434(%rbp)
je     12b6 <main+0x146>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12bb <main+0x14b>
movl   $0x0,-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   1060 <FD_ZERO@plt>
mov    0x2e50(%rip),%r15
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1090 <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   1070 <select@plt>
test   %eax,%eax
je     121c <main+0xac>
cmp    $0xffffffff,%eax
je     123f <main+0xcf>
mov    (%r15),%rdi
lea    0xc(%rsp),%rsi
call   1030 <FD_ISSET@plt>
xor    %r14d,%r14d
test   %rax,%rax
je     122f <main+0xbf>
mov    (%r15),%rdi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     122f <main+0xbf>
cmp    $0xffffffff,%eax
je     1248 <main+0xd8>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xe13(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    122f <main+0xbf>
lea    0xde4(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14d,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdba(%rip),%rdi
jmp    124f <main+0xdf>
lea    0xdcd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    122f <main+0xbf>

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
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 %48
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 %48
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 %48
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
br label %47
47: ; preds = %46, %25
store i32 0, i32* %1
br label %48
48: ; preds = %47, %35, %22, %17
%49 = load i32, i32* %1
ret i32 %49
}

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
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 %33
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 %33
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 %33, 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 %26 [
i32 -1, label %31
i32 0, label %30
]
26: ; preds = %22
%27 = sext i32 %25 to i64
%28 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %27
store i8 0, i8* %28
%29 = 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 %30
30: ; preds = %22, %26
br label %33
31: ; preds = %22
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %18, %30, %31, %16, %14
%34 = phi i32 [ 1, %14 ], [ 1, %31 ], [ 0, %16 ], [ 0, %30 ], [ 0, %18 ]
ret i32 %34
}