#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*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
char *mmap(int /*<<< orphan*/ *, unsigned int, int, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
unsigned int sysconf(int /*<<< orphan*/);
char *mapfile(const char *fn, size_t *size) {
  unsigned ps = sysconf(_SC_PAGESIZE);
  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 + ps - 1) & ~(ps - 1), 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    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11a5 <mapfile+0x55>
movq   $0x0,-0x8(%rbp)
jmp    124f <mapfile+0xff>
mov    -0x38(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1070 <fstat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11c3 <mapfile+0x73>
jmp    123f <mapfile+0xef>
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x1c(%rbp),%ecx
add    %rcx,%rax
sub    $0x1,%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
mov    %ecx,%ecx
and    %rcx,%rax
mov    %eax,%esi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    0x2dcc(%rip),%rax
or     (%rax),%edx
mov    0x2dbb(%rip),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d92(%rip),%rcx
mov    (%rcx),%rcx
cmp    %rcx,%rax
jne    123a <mapfile+0xea>
movq   $0x0,-0x30(%rbp)
jmp    123f <mapfile+0xef>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e74(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%r15d
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11e6 <mapfile+0x96>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %eax,%eax
js     11ea <mapfile+0x9a>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %r15d,%ecx
neg    %ecx
lea    (%r15,%rax,1),%esi
add    $0xffffffff,%esi
and    %ecx,%esi
mov    0x2e49(%rip),%rax
mov    0x2e12(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dff(%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    0x2ddb(%rip),%rax
cmp    (%rax),%rbx
cmove  %r14,%rbx
jmp    11ec <mapfile+0x9c>
xor    %ebx,%ebx
jmp    11f3 <mapfile+0xa3>
xor    %ebx,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@_SC_PAGESIZE = global i32 0
@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 i32
%7 = alloca %struct.stat
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
%11 = load i32, i32* @_SC_PAGESIZE
%12 = call i32 @sysconf(i32 %11)
store i32 %12, i32* %6
store i8* null, i8** %8
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_RDONLY
%15 = call i32 @open(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = load i32, i32* %10
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i8* null, i8** %3
br label %57
19: ; preds = %2
%20 = load i32, i32* %10
%21 = call i32 @fstat(i32 %20, %struct.stat* %7)
store i32 %21, i32* %9
%22 = load i32, i32* %9
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %19
br label %53
25: ; preds = %19
%26 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64*, i64** %5
store i64 %27, i64* %28
%29 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%30 = load i64, i64* %29
%31 = load i32, i32* %6
%32 = zext i32 %31 to i64
%33 = add i64 %30, %32
%34 = sub i64 %33, 1
%35 = load i32, i32* %6
%36 = sub i32 %35, 1
%37 = xor i32 %36, -1
%38 = zext i32 %37 to i64
%39 = and i64 %34, %38
%40 = trunc i64 %39 to i32
%41 = load i32, i32* @PROT_READ
%42 = load i32, i32* @PROT_WRITE
%43 = or i32 %41, %42
%44 = load i32, i32* @MAP_PRIVATE
%45 = load i32, i32* %10
%46 = call i8* @mmap(i32* null, i32 %40, i32 %43, i32 %44, i32 %45, i32 0)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = load i64, i64* @MAP_FAILED
%49 = inttoptr i64 %48 to i8*
%50 = icmp eq i8* %47, %49
br i1 %50, label %51, label %52
51: ; preds = %25
store i8* null, i8** %8
br label %52
52: ; preds = %51, %25
br label %53
53: ; preds = %52, %24
%54 = load i32, i32* %10
%55 = call i32 @close(i32 %54)
%56 = load i8*, i8** %8
store i8* %56, i8** %3
br label %57
57: ; preds = %53, %18
%58 = load i8*, i8** %3
ret i8* %58
}

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