#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 */
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
void *mmap(int /*<<< orphan*/, scalar_t__, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int, void *, scalar_t__);
int main(int argc, char **argv) {
  int fd, r;
  off_t sz;
  void *p;
  if (argc < 2) {
    fprintf(stderr, "%s [file] - cats a file using mmap\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDONLY);
  if (fd < 0) {
    fprintf(stderr, "open() failed\n");
    return 1;
  }
  sz = lseek(fd, 0, SEEK_END);
  if (sz == (off_t)-1) {
    fprintf(stderr, "lseek() failed\n");
    return 1;
  }
  p = mmap(0, sz, PROT_READ, 0, fd, 0);
  if (p == (void *)-1) {
    fprintf(stderr, "mmap() failed\n");
    return 1;
  }
  r = write(1, p, sz);
  if (r < 0) {
    fprintf(stderr, "write() failed\n");
    return 1;
  }
  close(fd);
  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    11aa <main+0x4a>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe69(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x17d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1f(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f0 <main+0x90>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x17d>
mov    -0x14(%rbp),%edi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    1235 <main+0xd5>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x17d>
mov    -0x20(%rbp),%rsi
mov    0x2db8(%rip),%rax
mov    (%rax),%edx
mov    -0x14(%rbp),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
mov    %r9d,%ecx
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    128c <main+0x12c>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x17d>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12ce <main+0x16e>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
lea    0xd97(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12dd <main+0x17d>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     1188 <main+0x28>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe84(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    123b <main+0xdb>
mov    0x8(%rsi),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     1200 <main+0xa0>
mov    %eax,%ebx
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1212 <main+0xb2>
mov    %rax,%r14
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
mov    %r14,%rsi
xor    %ecx,%ecx
mov    %ebx,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
cmp    $0xffffffffffffffff,%rax
je     1224 <main+0xc4>
mov    $0x1,%ebp
mov    $0x1,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     1247 <main+0xe7>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %ebp,%ebp
jmp    1240 <main+0xe0>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
jmp    1234 <main+0xd4>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
jmp    1234 <main+0xd4>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1240 <main+0xe0>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"%s [file] - cats a file using mmap\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"open() failed\0A\00"
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"lseek() failed\0A\00"
@PROT_READ = global i32 0
@.str.3 = [15 x i8] c"mmap() failed\0A\00"
@.str.4 = [16 x i8] c"write() failed\0A\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 i64
%9 = alloca i8*
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 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 1, i32* %3
br label %60
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* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %60
29: ; preds = %18
%30 = load i32, i32* %6
%31 = load i32, i32* @SEEK_END
%32 = call i64 @lseek(i32 %30, i32 0, i32 %31)
store i64 %32, i64* %8
%33 = load i64, i64* %8
%34 = icmp eq i64 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %60
38: ; preds = %29
%39 = load i64, i64* %8
%40 = load i32, i32* @PROT_READ
%41 = load i32, i32* %6
%42 = call i8* @mmap(i32 0, i64 %39, i32 %40, i32 0, i32 %41, i32 0)
store i8* %42, i8** %9
%43 = load i8*, i8** %9
%44 = icmp eq i8* %43, inttoptr (i64 -1 to i8*)
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %60
48: ; preds = %38
%49 = load i8*, i8** %9
%50 = load i64, i64* %8
%51 = call i32 @write(i32 1, i8* %49, i64 %50)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* @stderr
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %60
57: ; preds = %48
%58 = load i32, i32* %6
%59 = call i32 @close(i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %57, %54, %45, %35, %26, %12
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"%s [file] - cats a file using mmap\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"open() failed\0A\00"
@SEEK_END = global i32 0
@.str.2 = [16 x i8] c"lseek() failed\0A\00"
@PROT_READ = global i32 0
@.str.3 = [15 x i8] c"mmap() failed\0A\00"
@.str.4 = [16 x i8] c"write() failed\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %39
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %8
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %39
17: ; preds = %8
%18 = load i32, i32* @SEEK_END
%19 = tail call i64 @lseek(i32 %12, i32 0, i32 %18)
%20 = icmp eq i64 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %39
24: ; preds = %17
%25 = load i32, i32* @PROT_READ
%26 = tail call i8* @mmap(i32 0, i64 %19, i32 %25, i32 0, i32 %12, i32 0)
%27 = icmp eq i8* %26, inttoptr (i64 -1 to i8*)
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %39
31: ; preds = %24
%32 = tail call i32 @write(i32 1, i8* %26, i64 %19)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = tail call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %39
37: ; preds = %31
%38 = tail call i32 @close(i32 %12)
br label %39
39: ; preds = %37, %34, %28, %21, %14, %4
%40 = phi i32 [ 1, %4 ], [ 1, %14 ], [ 1, %21 ], [ 1, %28 ], [ 1, %34 ], [ 0, %37 ]
ret i32 %40
}