#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 stat {
  int st_size;
};

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  struct stat st;
  char *map;
  int fd;
  if (argc < 2) {
    fprintf(stderr, "Error: no filename given\n");
    return 1;
  } else if (argc > 2) {
    fprintf(stderr, "Error: too many arguments given\n");
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open()");
    return 1;
  }
  fstat(fd, &st);
  map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (map == NULL) {
    perror("mmap()");
    close(fd);
    return 1;
  }
  for (int i = 0; i < st.st_size; i++) {
    putc(map[i], stdout);
  }
  fflush(stdout);
  munmap(map, st.st_size);
  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)
cmpl   $0x2,-0x8(%rbp)
jge    11d1 <main+0x41>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x172>
cmpl   $0x2,-0x8(%rbp)
jle    11fc <main+0x6c>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x172>
jmp    1201 <main+0x71>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dd0(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    123c <main+0xac>
lea    0xe10(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x172>
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <fstat@plt>
mov    -0x18(%rbp),%esi
mov    0x2da6(%rip),%rax
mov    (%rax),%edx
mov    0x2d65(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    129c <main+0x10c>
lea    0xdbf(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1302 <main+0x172>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12d9 <main+0x149>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    0x2d07(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edi
call   1050 <putc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    12a3 <main+0x113>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <munmap@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x1,%edi
jg     11b6 <main+0x26>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
jmp    127c <main+0xec>
cmp    $0x2,%edi
jne    126c <main+0xdc>
mov    0x8(%rsi),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0x103>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
mov    0x8(%rsp),%r15d
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15d,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
test   %rax,%rax
je     12a1 <main+0x111>
mov    %rax,%rbp
test   %r15d,%r15d
jle    1247 <main+0xb7>
xor    %ebx,%ebx
mov    0x2d9e(%rip),%r12
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%rbx,1),%edi
mov    (%r12),%esi
call   1050 <putc@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1230 <main+0xa0>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %rbp,%rdi
mov    %r15d,%esi
call   1080 <munmap@plt>
mov    %r14d,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    1286 <main+0xf6>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xd9e(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda1(%rip),%rdi
call   10a0 <perror@plt>
jmp    1281 <main+0xf1>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
call   1040 <close@plt>
jmp    1281 <main+0xf1>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Error: no filename given\0A\00"
@.str.1 = [33 x i8] c"Error: too many arguments given\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.3 = [7 x i8] c"mmap()\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
%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 slt 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 ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %73
15: ; preds = %2
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %73
21: ; preds = %15
br label %22
22: ; preds = %21
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = call i32 @open(i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %73
32: ; preds = %22
%33 = load i32, i32* %8
%34 = call i32 @fstat(i32 %33, %struct.stat* %6)
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @MAP_PRIVATE
%39 = load i32, i32* %8
%40 = call i8* @mmap(i32* null, i32 %36, i32 %37, i32 %38, i32 %39, i32 0)
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %47
43: ; preds = %32
%44 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32, i32* %8
%46 = call i32 @close(i32 %45)
store i32 1, i32* %3
br label %73
47: ; preds = %32
store i32 0, i32* %9
br label %48
48: ; preds = %61, %47
%49 = load i32, i32* %9
%50 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%51 = load i32, i32* %50
%52 = icmp slt i32 %49, %51
br i1 %52, label %53, label %64
53: ; preds = %48
%54 = load i8*, i8** %7
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %54, i64 %56
%58 = load i8, i8* %57
%59 = load i32, i32* @stdout
%60 = call i32 @putc(i8 signext %58, i32 %59)
br label %61
61: ; preds = %53
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %48
64: ; preds = %48
%65 = load i32, i32* @stdout
%66 = call i32 @fflush(i32 %65)
%67 = load i8*, i8** %7
%68 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%69 = load i32, i32* %68
%70 = call i32 @munmap(i8* %67, i32 %69)
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
store i32 0, i32* %3
br label %73
73: ; preds = %64, %43, %30, %18, %12
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [26 x i8] c"Error: no filename given\0A\00"
@.str.1 = [33 x i8] c"Error: too many arguments given\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [7 x i8] c"open()\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@.str.3 = [7 x i8] c"mmap()\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %50
9: ; preds = %2
%10 = icmp eq i32 %0, 2
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %50
14: ; preds = %9
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_RDONLY
%18 = tail call i32 @open(i8* %16, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %50
22: ; preds = %14
%23 = call i32 @fstat(i32 %18, %struct.stat* %3)
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_PRIVATE
%28 = tail call i8* @mmap(i32* null, i32 %25, i32 %26, i32 %27, i32 %18, i32 0)
%29 = icmp eq i8* %28, null
br i1 %29, label %34, label %30
30: ; preds = %22
%31 = icmp sgt i32 %25, 0
br i1 %31, label %32, label %37
32: ; preds = %30
%33 = zext i32 %25 to i64
br label %42
34: ; preds = %22
%35 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @close(i32 %18)
br label %50
37: ; preds = %42, %30
%38 = load i32, i32* @stdout
%39 = tail call i32 @fflush(i32 %38)
%40 = tail call i32 @munmap(i8* %28, i32 %25)
%41 = tail call i32 @close(i32 %18)
br label %50
42: ; preds = %32, %42
%43 = phi i64 [ 0, %32 ], [ %48, %42 ]
%44 = getelementptr inbounds i8, i8* %28, i64 %43
%45 = load i8, i8* %44
%46 = load i32, i32* @stdout
%47 = tail call i32 @putc(i8 signext %45, i32 %46)
%48 = add nuw nsw i64 %43, 1
%49 = icmp eq i64 %48, %33
br i1 %49, label %37, label %42
50: ; preds = %37, %34, %20, %11, %6
%51 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 1, %20 ], [ 1, %34 ], [ 0, %37 ]
ret i32 %51
}