#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 open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, nr;
  char buf[16];
  fd = open(argv[1], O_RDONLY | O_NONBLOCK);
  if (fd == -1)
    perror("open");
start:
  nr = read(fd, buf, 16);
  if (nr == -1) {
    if (errno == EAGAIN)
      printf("EAGAIN\n");
    perror("read");
  }
  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    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    119e <main+0x4e>
lea    0xe67(%rip),%rdi
call   1070 <perror@plt>
jmp    11a3 <main+0x53>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11f3 <main+0xa3>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2dee(%rip),%rcx
cmp    (%rcx),%rax
jne    11e7 <main+0x97>
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rdi
mov    0x2e80(%rip),%rax
mov    0x2e81(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     119a <main+0x4a>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11a8 <main+0x58>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe5f(%rip),%rdi
call   1070 <perror@plt>
jmp    1177 <main+0x27>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
jne    11ca <main+0x7a>
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
jmp    118b <main+0x3b>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [5 x i8] c"open\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.1 = [8 x i8] c"EAGAIN\0A\00"
@.str.2 = [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* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
br label %21
21: ; preds = %20
%22 = load i32, i32* %6
%23 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 16)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %35
27: ; preds = %21
%28 = load i64, i64* @errno
%29 = load i64, i64* @EAGAIN
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %21
%36 = load i32, i32* %6
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [5 x i8] c"open\00"
@errno = global i64 0
@EAGAIN = global i64 0
@.str.2 = [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 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %2, %12
%15 = call i32 @read(i32 %10, i8* %4, i32 16)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i64, i64* @errno
%19 = load i64, i64* @EAGAIN
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %23
23: ; preds = %21, %17
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
%26 = tail call i32 @close(i32 %10)
ret i32 0
}