#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;
};
typedef void *caddr_t;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ write(int, void *, scalar_t__);
int main(int argc, char **argv) {
  int fd;
  struct stat sb;
  void *region;
  if ((fd = open(argv[1], O_RDONLY)) < 0) {
    perror("open");
    return 1;
  }
  if (fstat(fd, &sb)) {
    perror("fstat");
    return 1;
  }
  region = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  if (region == ((caddr_t)-1)) {
    perror("mmap");
    return 1;
  }
  close(fd);
  if (write(1, region, sb.st_size) != sb.st_size) {
    perror("write");
    return 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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11b0 <main+0x50>
lea    0xe61(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x114>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11de <main+0x7e>
lea    0xe38(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x114>
mov    -0x20(%rbp),%rsi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    0x2df6(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1231 <main+0xd1>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x114>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
cmp    -0x20(%rbp),%rax
je     126d <main+0x10d>
lea    0xdb4(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x114>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
test   %eax,%eax
js     11e6 <main+0x86>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11ef <main+0x8f>
mov    (%rsp),%r15
mov    0x2e5e(%rip),%rax
mov    (%rax),%edx
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %r15,%rsi
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     11f8 <main+0x98>
mov    %rax,%r12
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x1,%edi
mov    %r12,%rsi
mov    %r15,%rdx
call   1030 <write@plt>
cmp    %r15,%rax
je     120a <main+0xaa>
lea    0xe2c(%rip),%rdi
jmp    11ff <main+0x9f>
lea    0xe13(%rip),%rdi
jmp    11ff <main+0x9f>
lea    0xe0f(%rip),%rdi
jmp    11ff <main+0x9f>
lea    0xe0c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [5 x i8] c"open\00"
@.str.1 = [6 x i8] c"fstat\00"
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
@.str.2 = [5 x i8] c"mmap\00"
@.str.3 = [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 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* %6, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %24, label %10
10: ; preds = %2
%11 = call i64 @fstat(i32 %8, %struct.stat* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %10
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i64, i64* %14
%16 = load i32, i32* @PROT_READ
%17 = load i32, i32* @MAP_SHARED
%18 = tail call i8* @mmap(i32* null, i64 %15, i32 %16, i32 %17, i32 %8, i32 0)
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %24, label %20
20: ; preds = %13
%21 = tail call i32 @close(i32 %8)
%22 = tail call i64 @write(i32 1, i8* %18, i64 %15)
%23 = icmp eq i64 %22, %15
br i1 %23, label %27, label %24
24: ; preds = %20, %13, %10, %2
%25 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), %10 ], [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %13 ], [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %20 ]
%26 = tail call i32 @perror(i8* %25)
br label %27
27: ; preds = %24, %20
%28 = phi i32 [ 0, %20 ], [ 1, %24 ]
ret i32 %28
}