#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write(int /*<<< orphan*/, void *, int);
int main(int argc, char *argv[]) {
  int n, fd;
  void *map;
  struct stat statbuf;
  if (argc != 2) {
    fprintf(stderr, "usage: %s <filename>\n", argv[0]);
    exit(1);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  if (fstat(fd, &statbuf) != 0) {
    perror("fstat");
    exit(1);
  }
  map = mmap(NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (map == MAP_FAILED) {
    perror("mmap");
    exit(1);
  }
  if (close(fd) < 0) {
    perror("close");
    exit(1);
  }
  n = write(STDOUT_FILENO, map, statbuf.st_size);
  if (n != statbuf.st_size) {
    perror("write");
    exit(1);
  }
  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     11c6 <main+0x46>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ff <main+0x7f>
lea    0xe26(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0x0,%rax
je     122b <main+0xab>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%esi
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    0x2d82(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2d59(%rip),%rcx
cmp    (%rcx),%rax
jne    127e <main+0xfe>
lea    0xdb2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    12a6 <main+0x126>
lea    0xd8f(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     12e0 <main+0x160>
lea    0xd5b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%edi
jne    1228 <main+0xa8>
mov    0x8(%rsi),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     124a <main+0xca>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
test   %rax,%rax
jne    1253 <main+0xd3>
mov    0x8(%rsp),%r14d
mov    0x2e2a(%rip),%rax
mov    (%rax),%edx
mov    0x2de9(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc5(%rip),%rax
cmp    (%rax),%rbx
je     125c <main+0xdc>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
js     1265 <main+0xe5>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    %r14d,%eax
jne    126e <main+0xee>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdc5(%rip),%rdi
jmp    1275 <main+0xf5>
lea    0xdc1(%rip),%rdi
jmp    1275 <main+0xf5>
lea    0xdbe(%rip),%rdi
jmp    1275 <main+0xf5>
lea    0xdba(%rip),%rdi
jmp    1275 <main+0xf5>
lea    0xdb7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"usage: %s <filename>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
@.str.4 = [6 x i8] c"close\00"
@STDOUT_FILENO = global i32 0
@.str.5 = [6 x i8] c"write\00"
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 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 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = call i64 @fstat(i32 %15, %struct.stat* %3)
%22 = icmp eq i64 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %20
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @MAP_PRIVATE
%31 = tail call i8* @mmap(i32* null, i32 %28, i32 %29, i32 %30, i32 %15, i32 0)
%32 = load i8*, i8** @MAP_FAILED
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %26
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @exit(i32 1)
unreachable
37: ; preds = %26
%38 = tail call i64 @close(i32 %15)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %37
%44 = load i32, i32* @STDOUT_FILENO
%45 = tail call i32 @write(i32 %44, i8* %31, i32 %28)
%46 = icmp eq i32 %45, %28
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
50: ; preds = %43
ret i32 0
}