#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 ssize_t;

/* Variables and functions */
int MAP_ANONYMOUS;
unsigned char *MAP_FAILED;
int MAP_PRIVATE;
int O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/);
unsigned char *mmap(int /*<<< orphan*/ *, size_t const, int, int,
                    int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int read(int, unsigned char *, size_t const);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Expected 1 argument: the file to read.\n");
    return 1;
  }
  int fd = open(argv[1], O_RDONLY | 00040000);
  if (fd == -1) {
    perror("failed to open file");
    return 1;
  }
  const size_t buf_size = 4096;
  unsigned char *buf = mmap(NULL, buf_size, PROT_READ | PROT_WRITE,
                            MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (buf == MAP_FAILED) {
    perror("mmap failed");
    return 1;
  }
  while (1) {
    ssize_t amt_read = read(fd, buf, buf_size);
    if (amt_read == 0) {
      break;
    }
    if (amt_read == -1) {
      perror("failed to read file");
      return 1;
    }
    fwrite(buf, 1, amt_read, stdout);
    fflush(stdout);
  }
  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)
cmpl   $0x2,-0x8(%rbp)
je     11b1 <main+0x41>
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x170>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e20(%rip),%rax
mov    (%rax),%esi
or     $0x4000,%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f2 <main+0x82>
lea    0xe47(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x170>
movq   $0x1000,-0x20(%rbp)
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    0x2db6(%rip),%rax
or     (%rax),%edx
mov    0x2dc5(%rip),%rax
mov    (%rax),%ecx
mov    0x2d94(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d67(%rip),%rcx
cmp    (%rcx),%rax
jne    1262 <main+0xf2>
lea    0xdeb(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x170>
jmp    1267 <main+0xf7>
mov    -0x14(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    128a <main+0x11a>
jmp    12d9 <main+0x169>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    12ac <main+0x13c>
lea    0xdad(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e0 <main+0x170>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    0x2cfe(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
jmp    1267 <main+0xf7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x2,%edi
jne    1225 <main+0xb5>
mov    0x8(%rsi),%rdi
mov    0x2e58(%rip),%rax
mov    $0x4000,%esi
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1244 <main+0xd4>
mov    %eax,%r14d
mov    0x2e51(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
mov    0x2df0(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    $0x1000,%esi
xor    %edi,%edi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc8(%rip),%rax
cmp    (%rax),%rbx
je     1252 <main+0xe2>
mov    0x2dcc(%rip),%rbp
nopl   0x0(%rax)
mov    $0x1000,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1040 <read@plt>
test   %eax,%eax
je     123f <main+0xcf>
cmp    $0xffffffff,%eax
je     1260 <main+0xf0>
mov    0x0(%rbp),%ecx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   1090 <fwrite@plt>
mov    0x0(%rbp),%edi
call   1060 <fflush@plt>
jmp    11f0 <main+0x80>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
jmp    123a <main+0xca>
lea    0xde3(%rip),%rdi
call   1080 <perror@plt>
jmp    123a <main+0xca>
lea    0xde1(%rip),%rdi
call   1080 <perror@plt>
jmp    123a <main+0xca>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"Expected 1 argument: the file to read.\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@.str.3 = [20 x i8] c"failed to read file\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_RDONLY
%20 = or i32 %19, 16384
%21 = call i32 @open(i8* %18, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
26: ; preds = %15
store i64 4096, i64* %7
%27 = load i32, i32* @PROT_READ
%28 = load i32, i32* @PROT_WRITE
%29 = or i32 %27, %28
%30 = load i32, i32* @MAP_ANONYMOUS
%31 = load i32, i32* @MAP_PRIVATE
%32 = or i32 %30, %31
%33 = call i8* @mmap(i32* null, i64 4096, i32 %29, i32 %32, i32 0, i32 0)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %39
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %60
39: ; preds = %26
br label %40
40: ; preds = %39, %52
%41 = load i32, i32* %6
%42 = load i8*, i8** %8
%43 = call i32 @read(i32 %41, i8* %42, i64 4096)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %40
br label %59
47: ; preds = %40
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %60
52: ; preds = %47
%53 = load i8*, i8** %8
%54 = load i32, i32* %9
%55 = load i32, i32* @stdout
%56 = call i32 @fwrite(i8* %53, i32 1, i32 %54, i32 %55)
%57 = load i32, i32* @stdout
%58 = call i32 @fflush(i32 %57)
br label %40
59: ; preds = %46
store i32 0, i32* %3
br label %60
60: ; preds = %59, %50, %37, %24, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"Expected 1 argument: the file to read.\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"failed to open file\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [12 x i8] c"mmap failed\00"
@.str.3 = [20 x i8] c"failed to read file\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0))
br label %37
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = or i32 %10, 16384
%12 = tail call i32 @open(i8* %9, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %37
16: ; preds = %7
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = load i32, i32* @MAP_ANONYMOUS
%21 = load i32, i32* @MAP_PRIVATE
%22 = or i32 %21, %20
%23 = tail call i8* @mmap(i32* null, i64 4096, i32 %19, i32 %22, i32 0, i32 0)
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %28
26: ; preds = %16
%27 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %37
28: ; preds = %16, %32
%29 = tail call i32 @read(i32 %12, i8* %23, i64 4096)
switch i32 %29, label %32 [
i32 0, label %37
i32 -1, label %30
]
30: ; preds = %28
%31 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %37
32: ; preds = %28
%33 = load i32, i32* @stdout
%34 = tail call i32 @fwrite(i8* %23, i32 1, i32 %29, i32 %33)
%35 = load i32, i32* @stdout
%36 = tail call i32 @fflush(i32 %35)
br label %28
37: ; preds = %28, %30, %14, %26, %4
%38 = phi i32 [ 1, %4 ], [ 1, %14 ], [ 1, %26 ], [ 1, %30 ], [ %29, %28 ]
ret i32 %38
}