#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 */

/* Variables and functions */
scalar_t__ EAGAIN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, nr;
  char buf[16];
  fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (argc != 2) {
    printf("usage: %s filename\n", argv[0]);
    exit(1);
  }
  if (fd == -1)
    perror("open");
start:
  nr = read(fd, buf, 16);
  if (nr == -1) {
    if (errno == EAGAIN)
      printf("EAGAIN\n");
    perror("read");
  }
  if (nr > 0) {
    write(1, buf, nr);
    goto start;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
cmpl   $0xffffffff,-0x14(%rbp)
jne    11e7 <main+0x77>
lea    0xe32(%rip),%rdi
call   1080 <perror@plt>
jmp    11ec <main+0x7c>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    123e <main+0xce>
mov    0x2dbf(%rip),%rax
mov    (%rax),%rax
mov    0x2da5(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <main+0xc2>
lea    0xdee(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde8(%rip),%rdi
call   1080 <perror@plt>
cmpl   $0x0,-0x18(%rbp)
jle    125e <main+0xee>
lea    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    11ec <main+0x7c>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rdi
mov    0x2e48(%rip),%rax
mov    0x2e49(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0x2,%ebx
jne    1258 <main+0xd8>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
je     1247 <main+0xc7>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     1207 <main+0x87>
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
test   %eax,%eax
jle    1235 <main+0xb5>
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
jne    11e0 <main+0x60>
mov    0x2dc2(%rip),%rax
mov    (%rax),%rax
mov    0x2da8(%rip),%rcx
cmp    (%rcx),%rax
jne    1229 <main+0xa9>
lea    0xdfa(%rip),%rdi
call   1030 <puts@plt>
lea    0xde9(%rip),%rdi
call   1090 <perror@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
jmp    11bc <main+0x3c>
mov    (%r14),%rsi
lea    0xd9e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [20 x i8] c"usage: %s filename\0A\00"
@.str.1 = [5 x i8] c"open\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [8 x i8] c"EAGAIN\0A\00"
@.str.3 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = load i32, i32* @O_NONBLOCK
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %11, i32 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %24
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
br label %30
30: ; preds = %47, %29
%31 = load i32, i32* %6
%32 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%33 = call i32 @read(i32 %31, i8* %32, i32 16)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %44
36: ; preds = %30
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp eq i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %30
%45 = load i32, i32* %7
%46 = icmp sgt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%49 = load i32, i32* %7
%50 = call i32 @write(i32 1, i8* %48, i32 %49)
br label %30
51: ; preds = %44
%52 = load i32, i32* %6
%53 = call i32 @close(i32 %52)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [20 x i8] c"usage: %s filename\0A\00"
@.str.1 = [5 x i8] c"open\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.3 = [5 x i8] c"read\00"
@str = [7 x i8] c"EAGAIN\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = load i32, i32* @O_NONBLOCK
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %6, i32 %9)
%11 = icmp eq i32 %0, 2
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i8*, i8** %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = icmp eq i32 %10, -1
br i1 %17, label %18, label %20
18: ; preds = %16
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %16
%21 = call i32 @read(i32 %10, i8* %4, i32 16)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %31
23: ; preds = %34, %20
%24 = load i64, i64* @errno
%25 = load i64, i64* @EAGAIN
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %23, %27
%30 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %38
31: ; preds = %20, %34
%32 = phi i32 [ %36, %34 ], [ %21, %20 ]
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %31
%35 = call i32 @write(i32 1, i8* %4, i32 %32)
%36 = call i32 @read(i32 %10, i8* %4, i32 16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %23, label %31
38: ; preds = %31, %29
%39 = tail call i32 @close(i32 %10)
ret i32 0
}