#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;
};
typedef int pagesize;

/* Variables and functions */
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ close(int);
scalar_t__ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, int, int /*<<< orphan*/, int /*<<< orphan*/,
           int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
void *mmapfile(char *file, size_t *size) {
  int pagesize = sysconf(_SC_PAGESIZE);
  int fd = open(file, O_RDONLY);
  void *res = NULL;
  struct stat st;
  *size = 0;
  if (fd < 0)
    return NULL;
  if (fstat(fd, &st) >= 0) {
    *size = st.st_size;
    res = mmap(NULL, (((st.st_size) + (pagesize)-1) & ~((pagesize)-1)),
               PROT_READ, MAP_SHARED, fd, 0);
    if (res == (void *)-1)
      res = NULL;
  }
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<mmapfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpl   $0x0,-0x20(%rbp)
jge    11b0 <mmapfile+0x60>
movq   $0x0,-0x8(%rbp)
jmp    1245 <mmapfile+0xf5>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jl     1235 <mmapfile+0xe5>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
movslq -0x1c(%rbp),%rcx
add    %rcx,%rax
sub    $0x1,%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
xor    $0xffffffff,%ecx
movslq %ecx,%rcx
and    %rcx,%rax
mov    %eax,%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    0x2de7(%rip),%rax
mov    (%rax),%ecx
mov    -0x20(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1230 <mmapfile+0xe0>
movq   $0x0,-0x28(%rbp)
jmp    1235 <mmapfile+0xe5>
mov    -0x20(%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):
<mmapfile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %eax,%r15d
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
movq   $0x0,(%r14)
test   %eax,%eax
js     11df <mmapfile+0x8f>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1070 <fstat@plt>
test   %rax,%rax
js     11e3 <mmapfile+0x93>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %r15d,%ecx
neg    %ecx
lea    (%r15,%rax,1),%esi
add    $0xffffffff,%esi
and    %ecx,%esi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
mov    0x2e28(%rip),%rax
mov    (%rax),%ecx
xor    %r14d,%r14d
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
cmove  %r14,%rbx
jmp    11e5 <mmapfile+0x95>
xor    %ebx,%ebx
jmp    11ec <mmapfile+0x9c>
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
@MAP_SHARED = global i32 0
define i8* @mmapfile(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca %struct.stat
store i8* %0, i8** %4
store i64* %1, i64** %5
%10 = load i32, i32* @_SC_PAGESIZE
%11 = call i32 @sysconf(i32 %10)
store i32 %11, i32* %6
%12 = load i8*, i8** %4
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
store i32 %14, i32* %7
store i8* null, i8** %8
%15 = load i64*, i64** %5
store i64 0, i64* %15
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %2
store i8* null, i8** %3
br label %51
19: ; preds = %2
%20 = load i32, i32* %7
%21 = call i64 @fstat(i32 %20, %struct.stat* %9)
%22 = icmp sge i64 %21, 0
br i1 %22, label %23, label %47
23: ; preds = %19
%24 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%25 = load i64, i64* %24
%26 = load i64*, i64** %5
store i64 %25, i64* %26
%27 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%28 = load i64, i64* %27
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = add i64 %28, %30
%32 = sub i64 %31, 1
%33 = load i32, i32* %6
%34 = sub nsw i32 %33, 1
%35 = xor i32 %34, -1
%36 = sext i32 %35 to i64
%37 = and i64 %32, %36
%38 = trunc i64 %37 to i32
%39 = load i32, i32* @PROT_READ
%40 = load i32, i32* @MAP_SHARED
%41 = load i32, i32* %7
%42 = call i8* @mmap(i32* null, i32 %38, i32 %39, i32 %40, i32 %41, i32 0)
store i8* %42, i8** %8
%43 = load i8*, i8** %8
%44 = icmp eq i8* %43, inttoptr (i64 -1 to i8*)
br i1 %44, label %45, label %46
45: ; preds = %23
store i8* null, i8** %8
br label %46
46: ; preds = %45, %23
br label %47
47: ; preds = %46, %19
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
%50 = load i8*, i8** %8
store i8* %50, i8** %3
br label %51
51: ; preds = %47, %18
%52 = load i8*, i8** %3
ret i8* %52
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@_SC_PAGESIZE = global i32 0
@O_RDONLY = global i32 0
@PROT_READ = global i32 0
@MAP_SHARED = global i32 0
define i8* @mmapfile(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 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
%8 = bitcast %struct.stat* %3 to i8*
store i64 0, i64* %1
%9 = icmp slt i32 %7, 0
br i1 %9, label %29, label %10
10: ; preds = %2
%11 = call i64 @fstat(i32 %7, %struct.stat* %3)
%12 = icmp sgt i64 %11, -1
br i1 %12, label %13, label %26
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* @MAP_SHARED
%23 = tail call i8* @mmap(i32* null, i32 %20, i32 %21, i32 %22, i32 %7, i32 0)
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
%25 = select i1 %24, i8* null, i8* %23
br label %26
26: ; preds = %13, %10
%27 = phi i8* [ %25, %13 ], [ null, %10 ]
%28 = tail call i32 @close(i32 %7)
br label %29
29: ; preds = %2, %26
%30 = phi i8* [ %27, %26 ], [ null, %2 ]
ret i8* %30
}