#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;
  int /*<<< orphan*/ st_mode;
};
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*/ S_ISREG(int /*<<< orphan*/);
int close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/, size_t, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int munmap(char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct stat sb;
  off_t len;
  char *p;
  int fd;
  if (argc < 2) {
    fprintf(stderr, "usage: %s <file>\n", argv[0]);
  }
  fd = open(argv[1], O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  if (fstat(fd, &sb) == -1) {
    perror("fstat");
    return 1;
  }
  if (!S_ISREG(sb.st_mode)) {
    fprintf(stderr, "%s is not a file\n", argv[1]);
  }
  p = mmap(0, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (p == MAP_FAILED) {
    perror("mmap");
    return 1;
  }
  if (close(fd) == -1) {
    perror("close");
    return 1;
  }
  for (len = 0; len < sb.st_size; len++) {
    putchar(p[len]);
  }
  if (munmap(p, sb.st_size) == -1) {
    perror("mmap");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11cc <main+0x3c>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1207 <main+0x77>
lea    0xe1c(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1234 <main+0xa4>
lea    0xdf4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    -0x18(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1262 <main+0xd2>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdc0(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rsi
mov    0x2d8b(%rip),%rax
mov    (%rax),%edx
mov    0x2d72(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d2a(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <main+0x127>
lea    0xd89(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1bd>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12e0 <main+0x150>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1bd>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1318 <main+0x188>
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <putchar@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12e8 <main+0x158>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1346 <main+0x1b6>
lea    0xcfa(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    134d <main+0x1bd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11bc <main+0x2c>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe49(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x8(%rbx),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1299 <main+0x109>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12a2 <main+0x112>
mov    0x8(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
jne    1213 <main+0x83>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xe0f(%rip),%rsi
call   1060 <fprintf@plt>
mov    (%rsp),%r12
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    0x2dc1(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %r12,%rsi
mov    %r15d,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%rbp
mov    0x2d7d(%rip),%rax
lea    0xde5(%rip),%r14
cmp    (%rax),%rbp
je     12b2 <main+0x122>
mov    %r15d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     12ab <main+0x11b>
test   %r12,%r12
je     1283 <main+0xf3>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl 0x0(%rbp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1270 <main+0xe0>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1080 <munmap@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12bf <main+0x12f>
jmp    12b2 <main+0x122>
lea    0xd72(%rip),%r14
jmp    12b2 <main+0x122>
lea    0xd6e(%rip),%r14
jmp    12b2 <main+0x122>
lea    0xd82(%rip),%r14
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [18 x i8] c"usage: %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"
@.str.3 = [18 x i8] c"%s is not a file\0A\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.4 = [5 x i8] c"mmap\00"
@.str.5 = [6 x i8] c"close\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 %18
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 ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %12, %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* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = 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 %87
28: ; preds = %18
%29 = load i32, i32* %9
%30 = call i32 @fstat(i32 %29, %struct.stat* %6)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = 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 %87
34: ; preds = %28
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @S_ISREG(i32 %36)
%38 = icmp ne i32 %37, 0
br i1 %38, label %45, label %39
39: ; preds = %34
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %39, %34
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%47 = load i64, i64* %46
%48 = load i32, i32* @PROT_READ
%49 = load i32, i32* @MAP_SHARED
%50 = load i32, i32* %9
%51 = call i8* @mmap(i32 0, i64 %47, i32 %48, i32 %49, i32 %50, i32 0)
store i8* %51, i8** %8
%52 = load i8*, i8** %8
%53 = load i8*, i8** @MAP_FAILED
%54 = icmp eq i8* %52, %53
br i1 %54, label %55, label %57
55: ; preds = %45
%56 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %87
57: ; preds = %45
%58 = load i32, i32* %9
%59 = call i32 @close(i32 %58)
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %87
63: ; preds = %57
store i64 0, i64* %7
br label %64
64: ; preds = %75, %63
%65 = load i64, i64* %7
%66 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%67 = load i64, i64* %66
%68 = icmp ult i64 %65, %67
br i1 %68, label %69, label %78
69: ; preds = %64
%70 = load i8*, i8** %8
%71 = load i64, i64* %7
%72 = getelementptr inbounds i8, i8* %70, i64 %71
%73 = load i8, i8* %72
%74 = call i32 @putchar(i8 signext %73)
br label %75
75: ; preds = %69
%76 = load i64, i64* %7
%77 = add i64 %76, 1
store i64 %77, i64* %7
br label %64
78: ; preds = %64
%79 = load i8*, i8** %8
%80 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%81 = load i64, i64* %80
%82 = call i32 @munmap(i8* %79, i64 %81)
%83 = icmp eq i32 %82, -1
br i1 %83, label %84, label %86
84: ; preds = %78
%85 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %87
86: ; preds = %78
store i32 0, i32* %3
br label %87
87: ; preds = %86, %84, %61, %55, %32, %26
%88 = load i32, i32* %3
ret i32 %88
}

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