#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;
};

/* Variables and functions */
scalar_t__ MAP_FAILED;
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
char *mapfile(const char *fn, size_t *size) {
  struct stat st;
  char *map = NULL;
  int err;
  int fd = open(fn, O_RDONLY);
  if (fd < 0)
    return NULL;
  err = fstat(fd, &st);
  if (err < 0)
    goto out;
  *size = st.st_size;
  map = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  if (map == (char *)MAP_FAILED)
    map = NULL;
out:
  close(fd);
  return map;
}


x86 assembly (clang, -O0):
<mapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1184 <mapfile+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1214 <mapfile+0xd4>
mov    -0x30(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11a2 <mapfile+0x62>
jmp    1204 <mapfile+0xc4>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rsi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
mov    (%rax),%ecx
mov    -0x30(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dd5(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    11ff <mapfile+0xbf>
movq   $0x0,-0x28(%rbp)
jmp    1204 <mapfile+0xc4>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e8e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     11b5 <mapfile+0x75>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
test   %eax,%eax
js     11b9 <mapfile+0x79>
mov    0x8(%rsp),%rsi
mov    %rsi,(%r14)
mov    0x2e7a(%rip),%rax
mov    0x2e4b(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e38(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
mov    0x2e14(%rip),%rax
cmp    (%rax),%rbx
cmove  %r14,%rbx
jmp    11bb <mapfile+0x7b>
xor    %ebx,%ebx
jmp    11c2 <mapfile+0x82>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
define i8* @mapfile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i8* null, i8** %7
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i8* null, i8** %3
br label %44
16: ; preds = %2
%17 = load i32, i32* %9
%18 = call i32 @fstat(i32 %17, %struct.stat* %6)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %16
br label %40
22: ; preds = %16
%23 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64*, i64** %5
store i64 %24, i64* %25
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i32, i32* @PROT_READ
%29 = load i32, i32* @PROT_WRITE
%30 = or i32 %28, %29
%31 = load i32, i32* @MAP_PRIVATE
%32 = load i32, i32* %9
%33 = call i8* @mmap(i32* null, i64 %27, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = load i64, i64* @MAP_FAILED
%36 = inttoptr i64 %35 to i8*
%37 = icmp eq i8* %34, %36
br i1 %37, label %38, label %39
38: ; preds = %22
store i8* null, i8** %7
br label %39
39: ; preds = %38, %22
br label %40
40: ; preds = %39, %21
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = load i8*, i8** %7
store i8* %43, i8** %3
br label %44
44: ; preds = %40, %15
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i64 0
define i8* @mapfile(i8* nocapture %0, i64* 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 = icmp slt i32 %6, 0
br i1 %7, label %26, label %8
8: ; preds = %2
%9 = call i32 @fstat(i32 %6, %struct.stat* %3)
%10 = icmp slt i32 %9, 0
br i1 %10, label %23, label %11
11: ; preds = %8
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%13 = load i64, i64* %12
store i64 %13, i64* %1
%14 = load i32, i32* @PROT_READ
%15 = load i32, i32* @PROT_WRITE
%16 = or i32 %15, %14
%17 = load i32, i32* @MAP_PRIVATE
%18 = tail call i8* @mmap(i32* null, i64 %13, i32 %16, i32 %17, i32 %6, i32 0)
%19 = load i64, i64* @MAP_FAILED
%20 = inttoptr i64 %19 to i8*
%21 = icmp eq i8* %18, %20
%22 = select i1 %21, i8* null, i8* %18
br label %23
23: ; preds = %8, %11
%24 = phi i8* [ null, %8 ], [ %22, %11 ]
%25 = tail call i32 @close(i32 %6)
br label %26
26: ; preds = %2, %23
%27 = phi i8* [ %24, %23 ], [ null, %2 ]
ret i8* %27
}