#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 */

/* Variables and functions */
void *MAP_FAILED;
int MAP_PRIVATE;
int MAP_SHARED;
int O_CREAT;
int O_RDWR;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ unlink(char *);
void *alloc_filebacked_hugepage(char *filepath, int size, int private,
                                int *fd) {
  int mapflag = MAP_SHARED;
  void *addr;
  if (private)
    mapflag = MAP_PRIVATE;
  if ((*fd = open(filepath, O_CREAT | O_RDWR, 0777)) < 0) {
    perror("open");
    return NULL;
  }
  if ((addr = mmap(0, size, PROT_READ | PROT_WRITE, mapflag, *fd, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    unlink(filepath);
    return NULL;
  }
  return addr;
}


x86 assembly (clang, -O0):
<alloc_filebacked_hugepage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1178 <alloc_filebacked_hugepage+0x38>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    -0x20(%rbp),%rcx
mov    %eax,(%rcx)
cmp    $0x0,%eax
jge    11c0 <alloc_filebacked_hugepage+0x80>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1228 <alloc_filebacked_hugepage+0xe8>
mov    -0x14(%rbp),%esi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
mov    0x2ded(%rip),%rax
or     (%rax),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    (%rax),%r8d
xor    %r9d,%r9d
mov    %r9d,%edi
call   1040 <mmap@plt>
mov    %rax,-0x30(%rbp)
mov    0x2dbb(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <alloc_filebacked_hugepage+0xe0>
lea    0xe00(%rip),%rdi
call   1060 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movq   $0x0,-0x8(%rbp)
jmp    1228 <alloc_filebacked_hugepage+0xe8>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_filebacked_hugepage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e82(%rip),%rax
mov    (%rax),%r13d
mov    0x2e50(%rip),%rax
mov    (%rax),%ebx
mov    0x2e87(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    $0x1ff,%edx
call   1050 <open@plt>
mov    %eax,0x0(%rbp)
test   %eax,%eax
js     11d3 <alloc_filebacked_hugepage+0x93>
test   %r12d,%r12d
cmove  %r13d,%ebx
mov    0x2e55(%rip),%rcx
mov    0x2e1e(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
xor    %edi,%edi
mov    %r15d,%esi
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    0x2df1(%rip),%rcx
cmp    (%rcx),%rax
je     11e1 <alloc_filebacked_hugepage+0xa1>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
jmp    11f5 <alloc_filebacked_hugepage+0xb5>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
mov    %r14,%rdi
call   1030 <unlink@plt>
xor    %eax,%eax
jmp    11c4 <alloc_filebacked_hugepage+0x84>

LLVM IR (-O0):
@MAP_SHARED = global i32 0
@MAP_PRIVATE = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i8* @alloc_filebacked_hugepage(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 i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%12 = load i32, i32* @MAP_SHARED
store i32 %12, i32* %10
%13 = load i32, i32* %8
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %4
%16 = load i32, i32* @MAP_PRIVATE
store i32 %16, i32* %10
br label %17
17: ; preds = %15, %4
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_CREAT
%20 = load i32, i32* @O_RDWR
%21 = or i32 %19, %20
%22 = call i32 @open(i8* %18, i32 %21, i32 511)
%23 = load i32*, i32** %9
store i32 %22, i32* %23
%24 = icmp slt i32 %22, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %5
br label %44
27: ; preds = %17
%28 = load i32, i32* %7
%29 = load i32, i32* @PROT_READ
%30 = load i32, i32* @PROT_WRITE
%31 = or i32 %29, %30
%32 = load i32, i32* %10
%33 = load i32*, i32** %9
%34 = load i32, i32* %33
%35 = call i8* @mmap(i32 0, i32 %28, i32 %31, i32 %32, i32 %34, i32 0)
store i8* %35, i8** %11
%36 = load i8*, i8** @MAP_FAILED
%37 = icmp eq i8* %35, %36
br i1 %37, label %38, label %42
38: ; preds = %27
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%40 = load i8*, i8** %6
%41 = call i32 @unlink(i8* %40)
store i8* null, i8** %5
br label %44
42: ; preds = %27
%43 = load i8*, i8** %11
store i8* %43, i8** %5
br label %44
44: ; preds = %42, %38, %25
%45 = load i8*, i8** %5
ret i8* %45
}

LLVM IR (-O2):
@MAP_SHARED = global i32 0
@MAP_PRIVATE = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str = [5 x i8] c"open\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str.1 = [5 x i8] c"mmap\00"
define i8* @alloc_filebacked_hugepage(i8* nocapture %0, i32 %1, i32 %2, i32* nocapture writeonly %3) {
%5 = load i32, i32* @MAP_SHARED
%6 = load i32, i32* @MAP_PRIVATE
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDWR
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* %0, i32 %9, i32 511)
store i32 %10, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %26
14: ; preds = %4
%15 = icmp eq i32 %2, 0
%16 = select i1 %15, i32 %5, i32 %6
%17 = load i32, i32* @PROT_READ
%18 = load i32, i32* @PROT_WRITE
%19 = or i32 %18, %17
%20 = tail call i8* @mmap(i32 0, i32 %1, i32 %19, i32 %16, i32 %10, i32 0)
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %26
23: ; preds = %14
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @unlink(i8* %0)
br label %26
26: ; preds = %14, %23, %12
%27 = phi i8* [ null, %12 ], [ null, %23 ], [ %20, %14 ]
ret i8* %27
}