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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
char *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int munmap(char *, scalar_t__);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ write(int /*<<< orphan*/, char *, scalar_t__);
int main(int argc, char **argv) {
  char *addr;
  int fd;
  struct stat st;
  if (argc != 2) {
    printf("%s <filename>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror("open() ");
    exit(EXIT_FAILURE);
  }
  if (stat(argv[1], &st) < 0) {
    perror("stat() ");
    exit(EXIT_FAILURE);
  }
  addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (addr == MAP_FAILED) {
    perror("mmap() ");
    exit(EXIT_FAILURE);
  }
  if (write(STDOUT_FILENO, addr, st.st_size) != st.st_size) {
    perror("write() ");
    exit(EXIT_FAILURE);
  }
  if (munmap(addr, st.st_size) == -1) {
    perror("munmap() ");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


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     11c1 <main+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11fd <main+0x7d>
lea    0xe25(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1232 <main+0xb2>
lea    0xdf8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
mov    0x2d72(%rip),%rax
mov    (%rax),%ecx
mov    -0x1c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2d49(%rip),%rcx
cmp    (%rcx),%rax
jne    128a <main+0x10a>
lea    0xda8(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1030 <write@plt>
cmp    -0x28(%rbp),%rax
je     12c4 <main+0x144>
lea    0xd76(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    12f4 <main+0x174>
lea    0xd4f(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ad <main+0x2d>
mov    0x8(%rbx),%rdi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
jns    11c1 <main+0x41>
lea    0xe67(%rip),%rdi
jmp    1253 <main+0xd3>
mov    (%rbx),%rsi
lea    0xe49(%rip),%rdi
call   1050 <printf@plt>
jmp    1258 <main+0xd8>
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
jns    11dd <main+0x5d>
lea    0xe3c(%rip),%rdi
jmp    1253 <main+0xd3>
mov    (%rsp),%rsi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    0x2dc7(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2da6(%rip),%rax
cmp    (%rax),%rbx
jne    1218 <main+0x98>
lea    0xe09(%rip),%rdi
jmp    1253 <main+0xd3>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    (%rsp),%rdx
mov    %rbx,%rsi
call   1030 <write@plt>
cmp    (%rsp),%rax
jne    124c <main+0xcc>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1070 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1266 <main+0xe6>
lea    0xde6(%rip),%rdi
jmp    1253 <main+0xd3>
lea    0xdd4(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [15 x i8] c"%s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"open() \00"
@.str.2 = [8 x i8] c"stat() \00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [8 x i8] c"mmap() \00"
@STDOUT_FILENO = global i32 0
@.str.4 = [9 x i8] c"write() \00"
@.str.5 = [10 x i8] c"munmap() \00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
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 i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
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 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %18
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i64 @stat(i8* %32, %struct.stat* %8)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %29
%36 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %29
%40 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%41 = load i64, i64* %40
%42 = load i32, i32* @PROT_READ
%43 = load i32, i32* @MAP_PRIVATE
%44 = load i32, i32* %7
%45 = call i8* @mmap(i32* null, i64 %41, i32 %42, i32 %43, i32 %44, i32 0)
store i8* %45, i8** %6
%46 = load i8*, i8** %6
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %53
49: ; preds = %39
%50 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%51 = load i32, i32* @EXIT_FAILURE
%52 = call i32 @exit(i32 %51)
unreachable
53: ; preds = %39
%54 = load i32, i32* @STDOUT_FILENO
%55 = load i8*, i8** %6
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i64, i64* %56
%58 = call i64 @write(i32 %54, i8* %55, i64 %57)
%59 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%60 = load i64, i64* %59
%61 = icmp ne i64 %58, %60
br i1 %61, label %62, label %66
62: ; preds = %53
%63 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %53
%67 = load i8*, i8** %6
%68 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%69 = load i64, i64* %68
%70 = call i32 @munmap(i8* %67, i64 %69)
%71 = icmp eq i32 %70, -1
br i1 %71, label %72, label %76
72: ; preds = %66
%73 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%74 = load i32, i32* @EXIT_FAILURE
%75 = call i32 @exit(i32 %74)
unreachable
76: ; preds = %66
%77 = load i32, i32* @EXIT_SUCCESS
%78 = call i32 @exit(i32 %77)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [15 x i8] c"%s <filename>\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [8 x i8] c"open() \00"
@.str.2 = [8 x i8] c"stat() \00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [8 x i8] c"mmap() \00"
@STDOUT_FILENO = global i32 0
@.str.4 = [9 x i8] c"write() \00"
@.str.5 = [10 x i8] c"munmap() \00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDONLY
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %11
%22 = load i8*, i8** %12
%23 = call i64 @stat(i8* %22, %struct.stat* %3)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %21
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i64, i64* %30
%32 = load i32, i32* @PROT_READ
%33 = load i32, i32* @MAP_PRIVATE
%34 = call i8* @mmap(i32* null, i64 %31, i32 %32, i32 %33, i32 %15, i32 0)
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %41
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %29
%42 = load i32, i32* @STDOUT_FILENO
%43 = load i64, i64* %30
%44 = call i64 @write(i32 %42, i8* %34, i64 %43)
%45 = load i64, i64* %30
%46 = icmp eq i64 %44, %45
br i1 %46, label %51, label %47
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %41
%52 = call i32 @munmap(i8* %34, i64 %44)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %58
54: ; preds = %51
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
%56 = load i32, i32* @EXIT_FAILURE
%57 = call i32 @exit(i32 %56)
unreachable
58: ; preds = %51
%59 = load i32, i32* @EXIT_SUCCESS
%60 = call i32 @exit(i32 %59)
unreachable
}