#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]);
    return 1;
  }
  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]);
    return 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]);
  putchar('\n');
  if (munmap(p, sb.st_size) == -1) {
    perror("munmap");
    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    11d8 <main+0x48>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1213 <main+0x83>
lea    0xe10(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x34(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1240 <main+0xb0>
lea    0xde8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x18(%rbp),%edi
call   1070 <S_ISREG@plt>
cmp    $0x0,%eax
jne    127a <main+0xea>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdb4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x20(%rbp),%rsi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
mov    0x2d5a(%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    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
jne    12cf <main+0x13f>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
jne    12f8 <main+0x168>
lea    0xd4d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1330 <main+0x1a0>
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    1300 <main+0x170>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <munmap@plt>
cmp    $0xffffffff,%eax
jne    1368 <main+0x1d8>
lea    0xce3(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    136f <main+0x1df>
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   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     11bf <main+0x2f>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12cc <main+0x13c>
mov    0x8(%rbx),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a5 <main+0x115>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
cmp    $0xffffffff,%eax
je     12ae <main+0x11e>
mov    0x10(%rsp),%edi
call   1070 <S_ISREG@plt>
test   %eax,%eax
je     128a <main+0xfa>
mov    0x8(%rsp),%r15
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    0x2dd0(%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    0x2d8c(%rip),%rax
cmp    (%rax),%rbp
je     12b7 <main+0x127>
mov    %r14d,%edi
call   1050 <close@plt>
cmp    $0xffffffff,%eax
je     12c0 <main+0x130>
test   %r15,%r15
je     1263 <main+0xd3>
xor    %ebx,%ebx
nopl   (%rax)
movsbl 0x0(%rbp,%rbx,1),%edi
call   1030 <putchar@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1250 <main+0xc0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <munmap@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    12d1 <main+0x141>
lea    0xdb2(%rip),%rdi
jmp    12c7 <main+0x137>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd7f(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12cc <main+0x13c>
lea    0xd66(%rip),%rdi
jmp    12c7 <main+0x137>
lea    0xd62(%rip),%rdi
jmp    12c7 <main+0x137>
lea    0xd71(%rip),%rdi
jmp    12c7 <main+0x137>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
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"
@.str.6 = [7 x i8] c"munmap\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)
store i32 1, i32* %3
br label %88
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* %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 %88
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 %88
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)
store i32 1, i32* %3
br label %88
45: ; preds = %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 %88
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 %88
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 = call i32 @putchar(i8 signext 10)
%80 = load i8*, i8** %8
%81 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%82 = load i64, i64* %81
%83 = call i32 @munmap(i8* %80, i64 %82)
%84 = icmp eq i32 %83, -1
br i1 %84, label %85, label %87
85: ; preds = %78
%86 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %88
87: ; preds = %78
store i32 0, i32* %3
br label %88
88: ; preds = %87, %85, %61, %55, %39, %32, %26, %12
%89 = load i32, i32* %3
ret i32 %89
}

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"
@.str.6 = [7 x i8] c"munmap\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 %62
10: ; preds = %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 %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %62
18: ; preds = %10
%19 = call i32 @fstat(i32 %14, %struct.stat* %3)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %62
23: ; preds = %18
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%25 = load i32, i32* %24
%26 = tail call i32 @S_ISREG(i32 %25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %23
%29 = load i32, i32* @stderr
%30 = load i8*, i8** %11
%31 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %30)
br label %62
32: ; preds = %23
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @MAP_SHARED
%37 = tail call i8* @mmap(i32 0, i64 %34, i32 %35, i32 %36, i32 %14, i32 0)
%38 = load i8*, i8** @MAP_FAILED
%39 = icmp eq i8* %37, %38
br i1 %39, label %40, label %42
40: ; preds = %32
%41 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %62
42: ; preds = %32
%43 = tail call i32 @close(i32 %14)
%44 = icmp eq i32 %43, -1
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = icmp eq i64 %34, 0
br i1 %46, label %56, label %49
47: ; preds = %42
%48 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %62
49: ; preds = %45, %49
%50 = phi i64 [ %54, %49 ], [ 0, %45 ]
%51 = getelementptr inbounds i8, i8* %37, i64 %50
%52 = load i8, i8* %51
%53 = tail call i32 @putchar(i8 signext %52)
%54 = add nuw i64 %50, 1
%55 = icmp eq i64 %54, %34
br i1 %55, label %56, label %49
56: ; preds = %49, %45
%57 = tail call i32 @putchar(i8 signext 10)
%58 = tail call i32 @munmap(i8* %37, i64 %34)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %62
62: ; preds = %56, %60, %47, %40, %28, %21, %16, %6
%63 = phi i32 [ 1, %6 ], [ 1, %16 ], [ 1, %21 ], [ 1, %40 ], [ 1, %47 ], [ 1, %60 ], [ 1, %28 ], [ 0, %56 ]
ret i32 %63
}