#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(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int O_EXCL;
int O_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct timeval tv;
  fd_set readfds;
  int ret, fd, fd_max;
  fd = open(argv[1], O_RDONLY | O_EXCL, 0644);
  if (fd == -1)
    fd_max = 0;
  FD_ZERO(&readfds);
  FD_SET(STDIN_FILENO, &readfds);
  FD_SET(fd, &readfds);
  tv.tv_sec = 5;
  tv.tv_usec = 0;
  ret = select(fd_max + 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;
  }
  if (FD_ISSET(fd, &readfds)) {
    char buf[128];
    int len;
    printf("\"%s\" is readable now!\n", argv[1]);
    len = read(fd, buf, 128);
    if (!len) {
      printf("file closed already!\n");
      return 1;
    }
    printf("buf: %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    $0x4e0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e0f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11e1 <main+0x51>
movl   $0x0,-0x30(%rbp)
lea    -0x24(%rbp),%rdi
call   1070 <FD_ZERO@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   10b0 <FD_SET@plt>
mov    -0x2c(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   10b0 <FD_SET@plt>
movl   $0x5,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x30(%rbp),%edi
add    $0x1,%edi
lea    -0x24(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x20(%rbp),%r8
mov    %rcx,%rdx
call   1080 <select@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1256 <main+0xc6>
lea    0xdbb(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cc <main+0x23c>
cmpl   $0x0,-0x28(%rbp)
jne    127f <main+0xef>
lea    0xda0(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cc <main+0x23c>
jmp    1284 <main+0xf4>
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     1322 <main+0x192>
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0xffffffff,-0x444(%rbp)
jne    12e5 <main+0x155>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cc <main+0x23c>
cmpl   $0x0,-0x444(%rbp)
je     1316 <main+0x186>
movslq -0x444(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xd12(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cc <main+0x23c>
mov    -0x2c(%rbp),%edi
lea    -0x24(%rbp),%rsi
call   1030 <FD_ISSET@plt>
cmp    $0x0,%rax
je     13b0 <main+0x220>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
lea    -0x4d0(%rbp),%rsi
mov    $0x80,%edx
call   1050 <read@plt>
mov    %eax,-0x4d4(%rbp)
cmpl   $0x0,-0x4d4(%rbp)
jne    138f <main+0x1ff>
lea    0xcc6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13cc <main+0x23c>
lea    -0x4d0(%rbp),%rsi
lea    0xcbb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    13cc <main+0x23c>
mov    0x2c21(%rip),%rax
mov    (%rax),%edi
lea    0xca1(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x430,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    0x2e2b(%rip),%rax
mov    0x2e0c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1a4,%edx
call   10a0 <open@plt>
mov    %eax,%r15d
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <FD_ZERO@plt>
mov    0x2ddf(%rip),%r12
mov    (%r12),%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
call   10c0 <FD_SET@plt>
movl   $0x5,0x10(%rsp)
movq   $0x0,0x18(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%r8
mov    $0x1,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <select@plt>
test   %eax,%eax
je     1280 <main+0xe0>
cmp    $0xffffffff,%eax
je     130e <main+0x16e>
mov    (%r12),%edi
lea    0xc(%rsp),%rsi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     1298 <main+0xf8>
mov    (%r12),%edi
lea    0x20(%rsp),%rsi
mov    $0x400,%edx
call   1060 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
je     1328 <main+0x188>
cmp    $0xffffffff,%ebx
je     1317 <main+0x177>
movslq %ebx,%rax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xda3(%rip),%rdi
jmp    12d9 <main+0x139>
lea    0xd80(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1328 <main+0x188>
lea    0xc(%rsp),%rsi
mov    %r15d,%edi
call   1040 <FD_ISSET@plt>
test   %rax,%rax
je     12e9 <main+0x149>
mov    0x8(%r14),%rsi
lea    0xd76(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rsi
mov    %r15d,%edi
mov    $0x80,%edx
call   1060 <read@plt>
test   %eax,%eax
je     1300 <main+0x160>
lea    0xd69(%rip),%rdi
xor    %ebx,%ebx
lea    0x20(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1328 <main+0x188>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1323 <main+0x183>
lea    0xd5d(%rip),%rdi
call   1030 <puts@plt>
jmp    1323 <main+0x183>
lea    0xceb(%rip),%rdi
jmp    131e <main+0x17e>
lea    0xcfe(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@STDIN_FILENO = global i32 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"
@.str.4 = [23 x i8] c"\22%s\22 is readable now!\0A\00"
@.str.5 = [22 x i8] c"file closed already!\0A\00"
@.str.6 = [9 x i8] c"buf: %s\0A\00"
@stderr = global i32 0
@.str.7 = [25 x i8] c"This should not happen!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timeval
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1025 x i8]
%12 = alloca i32
%13 = alloca [128 x i8]
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = load i32, i32* @O_EXCL
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %17, i32 %20, i32 420)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 0, i32* %10
br label %25
25: ; preds = %24, %2
%26 = call i32 @FD_ZERO(i32* %7)
%27 = load i32, i32* @STDIN_FILENO
%28 = call i32 @FD_SET(i32 %27, i32* %7)
%29 = load i32, i32* %9
%30 = call i32 @FD_SET(i32 %29, i32* %7)
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 0
store i32 5, i32* %31
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %6, i32 0, i32 1
store i64 0, i64* %32
%33 = load i32, i32* %10
%34 = add nsw i32 %33, 1
%35 = call i32 @select(i32 %34, i32* %7, i32* null, i32* null, %struct.timeval* %6)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %90
40: ; preds = %25
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = 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* %3
br label %90
45: ; preds = %40
br label %46
46: ; preds = %45
%47 = load i32, i32* @STDIN_FILENO
%48 = call i64 @FD_ISSET(i32 %47, i32* %7)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %68
50: ; preds = %46
%51 = load i32, i32* @STDIN_FILENO
%52 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 1024)
store i32 %53, i32* %12
%54 = load i32, i32* %12
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %90
58: ; preds = %50
%59 = load i32, i32* %12
%60 = icmp ne i32 %59, 0
br i1 %60, label %61, label %67
61: ; preds = %58
%62 = load i32, i32* %12
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 %63
store i8 0, i8* %64
%65 = getelementptr inbounds [1025 x i8], [1025 x i8]* %11, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %65)
br label %67
67: ; preds = %61, %58
store i32 0, i32* %3
br label %90
68: ; preds = %46
%69 = load i32, i32* %9
%70 = call i64 @FD_ISSET(i32 %69, i32* %7)
%71 = icmp ne i64 %70, 0
br i1 %71, label %72, label %87
72: ; preds = %68
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %75)
%77 = load i32, i32* %9
%78 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%79 = call i32 @read(i32 %77, i8* %78, i32 128)
store i32 %79, i32* %14
%80 = load i32, i32* %14
%81 = icmp ne i32 %80, 0
br i1 %81, label %84, label %82
82: ; preds = %72
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %90
84: ; preds = %72
%85 = getelementptr inbounds [128 x i8], [128 x i8]* %13, i64 0, i64 0
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %85)
store i32 0, i32* %3
br label %90
87: ; preds = %68
%88 = load i32, i32* @stderr
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %90
90: ; preds = %87, %84, %82, %67, %56, %43, %38
%91 = load i32, i32* %3
ret i32 %91
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@O_RDONLY = global i32 0
@O_EXCL = global i32 0
@STDIN_FILENO = global i32 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"
@.str.4 = [23 x i8] c"\22%s\22 is readable now!\0A\00"
@.str.6 = [9 x i8] c"buf: %s\0A\00"
@stderr = global i32 0
@.str.7 = [25 x i8] c"This should not happen!\0A\00"
@str = [21 x i8] c"file closed already!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca [1025 x i8]
%6 = alloca [128 x i8]
%7 = bitcast %struct.timeval* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = load i32, i32* @O_EXCL
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* %10, i32 %13, i32 420)
%15 = call i32 @FD_ZERO(i32* %4)
%16 = load i32, i32* @STDIN_FILENO
%17 = call i32 @FD_SET(i32 %16, i32* %4)
%18 = call i32 @FD_SET(i32 %14, i32* %4)
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 5, i32* %19
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 0, i64* %20
%21 = call i32 @select(i32 1, i32* %4, i32* null, i32* null, %struct.timeval* %3)
switch i32 %21, label %26 [
i32 -1, label %22
i32 0, label %24
]
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %60
24: ; preds = %2
%25 = 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 %60
26: ; preds = %2
%27 = load i32, i32* @STDIN_FILENO
%28 = call i64 @FD_ISSET(i32 %27, i32* %4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %26
%31 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 0
%32 = load i32, i32* @STDIN_FILENO
%33 = call i32 @read(i32 %32, i8* %31, i32 1024)
switch i32 %33, label %36 [
i32 -1, label %34
i32 0, label %40
]
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %40
36: ; preds = %30
%37 = sext i32 %33 to i64
%38 = getelementptr inbounds [1025 x i8], [1025 x i8]* %5, i64 0, i64 %37
store i8 0, i8* %38
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %40
40: ; preds = %36, %30, %34
%41 = phi i32 [ 1, %34 ], [ %33, %30 ], [ 0, %36 ]
br label %60
42: ; preds = %26
%43 = call i64 @FD_ISSET(i32 %14, i32* %4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %57, label %45
45: ; preds = %42
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%47 = load i8*, i8** %9
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* %47)
%49 = call i32 @read(i32 %14, i8* %46, i32 128)
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %55
53: ; preds = %45
%54 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0), i8* %46)
br label %55
55: ; preds = %53, %51
%56 = phi i32 [ 0, %53 ], [ 1, %51 ]
br label %60
57: ; preds = %42
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.7, i64 0, i64 0))
br label %60
60: ; preds = %57, %55, %40, %24, %22
%61 = phi i32 [ 1, %22 ], [ %41, %40 ], [ %56, %55 ], [ 1, %57 ], [ 0, %24 ]
ret i32 %61
}