#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EAGAIN;
int O_NONBLOCK;
int O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int /*<<< orphan*/, char *, int);
int main(int argc, char *argv[]) {
  int fd, retval;
  char buf[4096];
  if (argc != 2) {
    fprintf(stderr, "usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_RDONLY | O_NONBLOCK)) < 0) {
    perror(argv[1]);
    exit(1);
  }
  while ((retval = read(fd, buf, sizeof(buf))) > 0) {
    if (write(STDOUT_FILENO, buf, retval) < 0) {
      perror("writing to stdout");
      break;
    }
  }
  if (retval < 0 && errno != EAGAIN) {
    perror(argv[1]);
    exit(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a9 <main+0x49>
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11eb <main+0x8b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11f0 <main+0x90>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    1248 <main+0xe8>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    -0x1020(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1243 <main+0xe3>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
jmp    1248 <main+0xe8>
jmp    11f0 <main+0x90>
cmpl   $0x0,-0x18(%rbp)
jge    1283 <main+0x123>
mov    0x2d67(%rip),%rax
mov    (%rax),%rax
mov    0x2d4d(%rip),%rcx
cmp    (%rcx),%rax
je     1283 <main+0x123>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
xor    %eax,%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %rsi,%r14
cmp    $0x2,%edi
jne    121e <main+0xbe>
mov    0x8(%r14),%rdi
mov    0x2e54(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     120b <main+0xab>
mov    %eax,%ebp
mov    %rsp,%rbx
mov    0x2e4c(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11e3 <main+0x83>
mov    (%r15),%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <write@plt>
test   %rax,%rax
jns    11b0 <main+0x50>
lea    0xe3a(%rip),%rdi
call   1070 <perror@plt>
jmp    11fb <main+0x9b>
jns    11fb <main+0x9b>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dba(%rip),%rcx
cmp    (%rcx),%rax
jne    120b <main+0xab>
xor    %eax,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%r14),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdcf(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

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