#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 {
  size_t st_size;
};
typedef size_t off_t;

/* Variables and functions */
char *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(char);
int main(int argc, char *argv[]) {
  struct stat sb;
  off_t len;
  char *p;
  int fd;
  if (argc < 2) {
    printf("%s <file>\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  if (fstat(fd, &sb) == -1) {
    perror("fstat");
    return 1;
  }
  p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  close(fd);
  for (len = 0; len < sb.st_size; len++)
    putchar(p[len]);
  munmap(p, sb.st_size);
  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    11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x150>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11fa <main+0x7a>
lea    0xe22(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x150>
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1227 <main+0xa7>
lea    0xdfa(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x150>
mov    -0x18(%rbp),%rsi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
mov    0x2dad(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d6d(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <main+0xfc>
lea    0xdab(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d0 <main+0x150>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    12bc <main+0x13c>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    128c <main+0x10c>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <munmap@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   %rbx
push   %rax
cmp    $0x1,%edi
jg     11a5 <main+0x25>
mov    (%rsi),%rsi
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    1240 <main+0xc0>
mov    0x8(%rsi),%rdi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     124b <main+0xcb>
mov    %eax,%r14d
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <fstat@plt>
cmp    $0xffffffff,%eax
je     1254 <main+0xd4>
mov    (%rsp),%r15
mov    0x2e17(%rip),%rax
mov    (%rax),%edx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r15,%rsi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2dc2(%rip),%rax
cmp    (%rax),%rbp
je     125d <main+0xdd>
mov    %r14d,%edi
call   1060 <close@plt>
test   %r15,%r15
je     1233 <main+0xb3>
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl 0x0(%rbp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1220 <main+0xa0>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <munmap@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb9(%rip),%rdi
jmp    1264 <main+0xe4>
lea    0xdb5(%rip),%rdi
jmp    1264 <main+0xe4>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    1240 <main+0xc0>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str = [11 x i8] c"%s <file>\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_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.3 = [5 x i8] c"mmap\00"
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 i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %44
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %44
17: ; preds = %9
%18 = call i32 @fstat(i32 %13, %struct.stat* %3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %44
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i64, i64* %23
%25 = load i32, i32* @PROT_READ
%26 = load i32, i32* @MAP_SHARED
%27 = tail call i8* @mmap(i32 0, i64 %24, i32 %25, i32 %26, i32 %13, i32 0)
%28 = load i8*, i8** @MAP_FAILED
%29 = icmp eq i8* %27, %28
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %44
32: ; preds = %22
%33 = tail call i32 @close(i32 %13)
%34 = icmp eq i64 %24, 0
br i1 %34, label %42, label %35
35: ; preds = %32, %35
%36 = phi i64 [ %40, %35 ], [ 0, %32 ]
%37 = getelementptr inbounds i8, i8* %27, i64 %36
%38 = load i8, i8* %37
%39 = tail call i32 @putchar(i8 signext %38)
%40 = add nuw i64 %36, 1
%41 = icmp eq i64 %40, %24
br i1 %41, label %42, label %35
42: ; preds = %35, %32
%43 = tail call i32 @munmap(i8* %27, i64 %24)
br label %44
44: ; preds = %42, %30, %20, %15, %6
%45 = phi i32 [ 1, %6 ], [ 1, %15 ], [ 1, %20 ], [ 1, %30 ], [ 0, %42 ]
ret i32 %45
}