#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*/ printf(char *, 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)
    return 1;
  else if (!ret)
    return 0;
  if (FD_ISSET(STDIN_FILENO, &readfds)) {
    char buf[1024 + 1];
    int len;
    len = read(STDIN_FILENO, buf, 1024);
    if (len == -1)
      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    0x2e46(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1090 <FD_SET@plt>
movl   $0x5,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e24(%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    11e8 <main+0x78>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x141>
cmpl   $0x0,-0x20(%rbp)
jne    11fe <main+0x8e>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <main+0x141>
jmp    1203 <main+0x93>
mov    0x2dce(%rip),%rax
mov    (%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1295 <main+0x125>
mov    0x2db1(%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    125a <main+0xea>
movl   $0x1,-0x4(%rbp)
jmp    12b1 <main+0x141>
cmpl   $0x0,-0x434(%rbp)
je     1289 <main+0x119>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xd7c(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b1 <main+0x141>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd65(%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   %r14
push   %rbx
sub    $0x428,%rsp
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <FD_ZERO@plt>
mov    0x2e4a(%rip),%r14
mov    (%r14),%rdi
mov    %rbx,%rsi
call   1090 <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   1080 <select@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     1237 <main+0xc7>
test   %eax,%eax
jne    11d4 <main+0x64>
xor    %ebx,%ebx
jmp    1237 <main+0xc7>
mov    (%r14),%rdi
lea    0xc(%rsp),%rsi
call   1030 <FD_ISSET@plt>
test   %rax,%rax
je     121b <main+0xab>
mov    (%r14),%rdi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1232 <main+0xc2>
test   %eax,%eax
je     11d0 <main+0x60>
cltq
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xdf1(%rip),%rdi
lea    0x20(%rsp),%rsi
call   1040 <printf@plt>
jmp    11d0 <main+0x60>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1060 <fprintf@plt>
jmp    1237 <main+0xc7>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i64 0
@.str = [10 x i8] c"read: %s\0A\00"
@stderr = global i32 0
@.str.1 = [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 %18
17: ; preds = %0
store i32 1, i32* %1
br label %47
18: ; preds = %0
%19 = load i32, i32* %4
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %18
store i32 0, i32* %1
br label %47
22: ; preds = %18
br label %23
23: ; preds = %22
%24 = load i64, i64* @STDIN_FILENO
%25 = call i64 @FD_ISSET(i64 %24, i32* %3)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %44
27: ; preds = %23
%28 = load i64, i64* @STDIN_FILENO
%29 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%30 = call i32 @read(i64 %28, i8* %29, i32 1024)
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %27
store i32 1, i32* %1
br label %47
34: ; preds = %27
%35 = load i32, i32* %6
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %34
%38 = load i32, i32* %6
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 %39
store i8 0, i8* %40
%41 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%42 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %37, %34
store i32 0, i32* %1
br label %47
44: ; preds = %23
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %47
47: ; preds = %44, %43, %33, %21, %17
%48 = load i32, i32* %1
ret i32 %48
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@STDIN_FILENO = global i64 0
@.str = [10 x i8] c"read: %s\0A\00"
@stderr = global i32 0
@.str.1 = [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 %15 [
i32 -1, label %33
i32 0, label %14
]
14: ; preds = %0
br label %33
15: ; preds = %0
%16 = load i64, i64* @STDIN_FILENO
%17 = call i64 @FD_ISSET(i64 %16, i32* %2)
%18 = icmp eq i64 %17, 0
br i1 %18, label %30, label %19
19: ; preds = %15
%20 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%21 = load i64, i64* @STDIN_FILENO
%22 = call i32 @read(i64 %21, i8* %20, i32 1024)
switch i32 %22, label %23 [
i32 -1, label %28
i32 0, label %27
]
23: ; preds = %19
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %27
27: ; preds = %19, %23
br label %28
28: ; preds = %19, %27
%29 = phi i32 [ 0, %27 ], [ 1, %19 ]
br label %33
30: ; preds = %15
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %0, %30, %28, %14
%34 = phi i32 [ %29, %28 ], [ 1, %30 ], [ 0, %14 ], [ 1, %0 ]
ret i32 %34
}