#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 {
  int st_size;
};

/* Variables and functions */
scalar_t__ EOF;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *mmap_file(char *fn, int *len) {
  char *result;
  struct stat stat_buf;
  int fd = open(fn, O_RDONLY);
  if (fstat(fd, &stat_buf) == EOF) {
    fprintf(stderr, "mmapfile: can't stat %s\n", fn);
    fatal("mmapfile failed");
  }
  *len = stat_buf.st_size;
  result = mmap(NULL, *len, PROT_READ, MAP_PRIVATE, fd, 0);
  if (result == (char *)EOF)
    fatal("mmapfile failed");
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<mmap_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1080 <fstat@plt>
mov    0x2e40(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <mmap_file+0x66>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe4b(%rip),%rsi
call   1050 <fprintf@plt>
lea    0xe58(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
mov    0x2ddb(%rip),%rax
mov    (%rax),%ecx
mov    -0x24(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dd2(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    121e <mmap_file+0xbe>
lea    0xe00(%rip),%rdi
call   1070 <fatal@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mmap_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    0x2e43(%rip),%r12
cmp    (%r12),%rax
jne    11bf <mmap_file+0x5f>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
lea    0xe5f(%rip),%rdi
call   1070 <fatal@plt>
mov    0x8(%rsp),%esi
mov    %esi,(%r14)
mov    0x2e2b(%rip),%rax
mov    (%rax),%edx
mov    0x2dea(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    (%r12),%rax
jne    11fa <mmap_file+0x9a>
lea    0xe24(%rip),%rdi
call   1070 <fatal@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@EOF = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"mmapfile: can't stat %s\0A\00"
@.str.1 = [16 x i8] c"mmapfile failed\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mmap_file(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %3
store i32* %1, i32** %4
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = call i64 @fstat(i32 %11, %struct.stat* %6)
%13 = load i64, i64* @EOF
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %3
%18 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %15, %2
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = load i32*, i32** %4
store i32 %22, i32* %23
%24 = load i32*, i32** %4
%25 = load i32, i32* %24
%26 = load i32, i32* @PROT_READ
%27 = load i32, i32* @MAP_PRIVATE
%28 = load i32, i32* %7
%29 = call i8* @mmap(i32* null, i32 %25, i32 %26, i32 %27, i32 %28, i32 0)
store i8* %29, i8** %5
%30 = load i8*, i8** %5
%31 = load i64, i64* @EOF
%32 = inttoptr i64 %31 to i8*
%33 = icmp eq i8* %30, %32
br i1 %33, label %34, label %36
34: ; preds = %20
%35 = call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %20
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
%39 = load i8*, i8** %5
ret i8* %39
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@EOF = global i64 0
@stderr = global i32 0
@.str = [25 x i8] c"mmapfile: can't stat %s\0A\00"
@.str.1 = [16 x i8] c"mmapfile failed\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mmap_file(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = call i64 @fstat(i32 %6, %struct.stat* %3)
%8 = load i64, i64* @EOF
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %14
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %0)
%13 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %10, %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
store i32 %16, i32* %1
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @MAP_PRIVATE
%19 = tail call i8* @mmap(i32* null, i32 %16, i32 %17, i32 %18, i32 %6, i32 0)
%20 = load i64, i64* @EOF
%21 = inttoptr i64 %20 to i8*
%22 = icmp eq i8* %19, %21
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = tail call i32 @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %14
%26 = tail call i32 @close(i32 %6)
ret i8* %19
}