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

/* Variables and functions */
int /*<<< orphan*/ MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ fstat(int, struct stat *);
void *mmap(int /*<<< orphan*/ *, unsigned int, int /*<<< orphan*/,
           int /*<<< orphan*/, int, int /*<<< orphan*/);
int open(char const *, int);
void *mapFile(const char *name, int flags, int *fileFD,
              unsigned int *fileSize) {
  int fd = open(name, flags);
  if (fileFD) {
    *fileFD = fd;
  }
  if (fd < 0) {
    return NULL;
  }
  struct stat fileStats;
  fstat(fd, &fileStats);
  if (fileSize) {
    *fileSize = fileStats.st_size;
  }
  return mmap(NULL, fileStats.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
}


x86 assembly (clang, -O0):
<mapFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     116a <mapFile+0x3a>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x2c(%rbp)
jge    1181 <mapFile+0x51>
movq   $0x0,-0x8(%rbp)
jmp    11ca <mapFile+0x9a>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1050 <fstat@plt>
cmpq   $0x0,-0x28(%rbp)
je     11a1 <mapFile+0x71>
mov    -0x30(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%esi
mov    0x2e4d(%rip),%rax
mov    (%rax),%edx
mov    0x2e24(%rip),%rax
mov    (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1030 <mmap@plt>
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    %rcx,%r14
mov    %rdx,%rbx
call   1040 <open@plt>
mov    %eax,%ebp
test   %rbx,%rbx
je     114c <mapFile+0x1c>
mov    %ebp,(%rbx)
test   %ebp,%ebp
js     118f <mapFile+0x5f>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1050 <fstat@plt>
mov    0x8(%rsp),%esi
test   %r14,%r14
je     1168 <mapFile+0x38>
mov    %esi,(%r14)
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
mov    0x2e60(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    %ebp,%r8d
xor    %r9d,%r9d
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <mmap@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stat = type { i32 }
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @mapFile(i8* nocapture %0, i32 %1, i32* writeonly %2, i32* writeonly %3) {
%5 = alloca %struct.stat
%6 = tail call i32 @open(i8* %0, i32 %1)
%7 = icmp eq i32* %2, null
br i1 %7, label %9, label %8
8: ; preds = %4
store i32 %6, i32* %2
br label %9
9: ; preds = %8, %4
%10 = icmp slt i32 %6, 0
br i1 %10, label %22, label %11
11: ; preds = %9
%12 = bitcast %struct.stat* %5 to i8*
%13 = call i32 @fstat(i32 %6, %struct.stat* %5)
%14 = icmp eq i32* %3, null
%15 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%16 = load i32, i32* %15
br i1 %14, label %18, label %17
17: ; preds = %11
store i32 %16, i32* %3
br label %18
18: ; preds = %11, %17
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_PRIVATE
%21 = tail call i8* @mmap(i32* null, i32 %16, i32 %19, i32 %20, i32 %6, i32 0)
br label %22
22: ; preds = %9, %18
%23 = phi i8* [ %21, %18 ], [ null, %9 ]
ret i8* %23
}