#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 */
typedef int uint64_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_PRIVATE;
int /*<<< orphan*/ IPC_RMID;
void *MAP_FAILED;
int SHM_HUGETLB;
int SHM_R;
int SHM_W;
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
scalar_t__ mlock(void *, int);
void *shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(void *);
int shmget(int /*<<< orphan*/, int, int);
int virtual_to_physical(void *);
void *allocate_huge_page(int size) {
  int shmid = -1;
  uint64_t physical_address, virtual_address;
  void *tmpptr = MAP_FAILED;
  void *realptr = MAP_FAILED;
  shmid = shmget(IPC_PRIVATE, size, SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W);
  tmpptr = shmat(shmid, NULL, 0);
  if (tmpptr == MAP_FAILED) {
    goto fail;
  }
  if (mlock(tmpptr, size) != 0) {
    goto fail;
  }
  physical_address = virtual_to_physical(tmpptr);
  if (physical_address == 0) {
    goto fail;
  }
  virtual_address = physical_address | 0x500000000000ULL;
  realptr = shmat(shmid, (void *)virtual_address, 0);
  if (realptr == MAP_FAILED) {
    goto fail;
  }
  if (mlock(realptr, size) != 0) {
    goto fail;
  }
  memset(realptr, 0, size);
  shmdt(tmpptr);
  shmctl(shmid, IPC_RMID, 0);
  return realptr;
fail:
  if (tmpptr != MAP_FAILED) {
    shmdt(tmpptr);
  }
  if (realptr != MAP_FAILED) {
    shmdt(realptr);
  }
  if (shmid != -1) {
    shmctl(shmid, IPC_RMID, 0);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<allocate_huge_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%esi
mov    0x2e2f(%rip),%rax
mov    (%rax),%edx
mov    0x2e06(%rip),%rax
or     (%rax),%edx
mov    0x2e25(%rip),%rax
or     (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
call   1090 <shmget@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <allocate_huge_page+0x91>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mlock@plt>
cmp    $0x0,%rax
je     121c <allocate_huge_page+0xac>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x20(%rbp),%rdi
call   1060 <virtual_to_physical@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1237 <allocate_huge_page+0xc7>
jmp    12c8 <allocate_huge_page+0x158>
movslq -0x14(%rbp),%rax
movabs $0x500000000000,%rcx
or     %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%edi
movslq -0x18(%rbp),%rsi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%rax
jne    1276 <allocate_huge_page+0x106>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <mlock@plt>
cmp    $0x0,%rax
je     1291 <allocate_huge_page+0x121>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x10(%rbp),%edi
mov    0x2d16(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1327 <allocate_huge_page+0x1b7>
mov    -0x20(%rbp),%rax
mov    0x2cdd(%rip),%rcx
cmp    (%rcx),%rax
je     12e5 <allocate_huge_page+0x175>
mov    -0x20(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x28(%rbp),%rax
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%rax
je     1302 <allocate_huge_page+0x192>
mov    -0x28(%rbp),%rdi
call   1040 <shmdt@plt>
cmpl   $0xffffffff,-0x10(%rbp)
je     131f <allocate_huge_page+0x1af>
mov    -0x10(%rbp),%edi
mov    0x2cb2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <shmctl@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_huge_page>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    0x2e2e(%rip),%r12
mov    (%r12),%rbx
mov    0x2e53(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e39(%rip),%rax
mov    0x2e42(%rip),%rcx
or     (%rcx),%edx
mov    (%rax),%edi
mov    0x2e07(%rip),%rax
or     (%rax),%edx
mov    %r14d,%esi
call   1090 <shmget@plt>
mov    %eax,%r15d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbp
cmp    (%r12),%rax
je     11e2 <allocate_huge_page+0x72>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1030 <mlock@plt>
test   %rax,%rax
je     1228 <allocate_huge_page+0xb8>
cmp    (%r12),%rbp
je     11f4 <allocate_huge_page+0x84>
mov    %rbp,%rdi
call   1040 <shmdt@plt>
mov    (%r12),%rbp
cmp    %rbp,%rbx
je     1201 <allocate_huge_page+0x91>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%r15d
je     121c <allocate_huge_page+0xac>
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbp,%rdi
call   1060 <virtual_to_physical@plt>
test   %eax,%eax
je     11e2 <allocate_huge_page+0x72>
movslq %eax,%rsi
mov    %r15d,%edi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbx
cmp    (%r12),%rax
je     11e2 <allocate_huge_page+0x72>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <mlock@plt>
test   %rax,%rax
jne    11e2 <allocate_huge_page+0x72>
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1050 <memset@plt>
mov    %rbp,%rdi
call   1040 <shmdt@plt>
jmp    1209 <allocate_huge_page+0x99>

LLVM IR (-O0):
@MAP_FAILED = global i8* null
@IPC_PRIVATE = global i32 0
@SHM_HUGETLB = global i32 0
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@IPC_RMID = global i32 0
define i8* @allocate_huge_page(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i32 %0, i32* %3
store i32 -1, i32* %4
%9 = load i8*, i8** @MAP_FAILED
store i8* %9, i8** %7
%10 = load i8*, i8** @MAP_FAILED
store i8* %10, i8** %8
%11 = load i32, i32* @IPC_PRIVATE
%12 = load i32, i32* %3
%13 = load i32, i32* @SHM_HUGETLB
%14 = load i32, i32* @IPC_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @SHM_R
%17 = or i32 %15, %16
%18 = load i32, i32* @SHM_W
%19 = or i32 %17, %18
%20 = call i32 @shmget(i32 %11, i32 %12, i32 %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = call i8* @shmat(i32 %21, i8* null, i32 0)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = load i8*, i8** @MAP_FAILED
%25 = icmp eq i8* %23, %24
br i1 %25, label %26, label %27
26: ; preds = %1
br label %69
27: ; preds = %1
%28 = load i8*, i8** %7
%29 = load i32, i32* %3
%30 = call i64 @mlock(i8* %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %27
br label %69
33: ; preds = %27
%34 = load i8*, i8** %7
%35 = call i32 @virtual_to_physical(i8* %34)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %69
39: ; preds = %33
%40 = load i32, i32* %5
%41 = sext i32 %40 to i64
%42 = or i64 %41, 87960930222080
%43 = trunc i64 %42 to i32
store i32 %43, i32* %6
%44 = load i32, i32* %4
%45 = load i32, i32* %6
%46 = sext i32 %45 to i64
%47 = inttoptr i64 %46 to i8*
%48 = call i8* @shmat(i32 %44, i8* %47, i32 0)
store i8* %48, i8** %8
%49 = load i8*, i8** %8
%50 = load i8*, i8** @MAP_FAILED
%51 = icmp eq i8* %49, %50
br i1 %51, label %52, label %53
52: ; preds = %39
br label %69
53: ; preds = %39
%54 = load i8*, i8** %8
%55 = load i32, i32* %3
%56 = call i64 @mlock(i8* %54, i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
br label %69
59: ; preds = %53
%60 = load i8*, i8** %8
%61 = load i32, i32* %3
%62 = call i32 @memset(i8* %60, i32 0, i32 %61)
%63 = load i8*, i8** %7
%64 = call i32 @shmdt(i8* %63)
%65 = load i32, i32* %4
%66 = load i32, i32* @IPC_RMID
%67 = call i32 @shmctl(i32 %65, i32 %66, i32 0)
%68 = load i8*, i8** %8
store i8* %68, i8** %2
br label %91
69: ; preds = %58, %52, %38, %32, %26
%70 = load i8*, i8** %7
%71 = load i8*, i8** @MAP_FAILED
%72 = icmp ne i8* %70, %71
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i8*, i8** %7
%75 = call i32 @shmdt(i8* %74)
br label %76
76: ; preds = %73, %69
%77 = load i8*, i8** %8
%78 = load i8*, i8** @MAP_FAILED
%79 = icmp ne i8* %77, %78
br i1 %79, label %80, label %83
80: ; preds = %76
%81 = load i8*, i8** %8
%82 = call i32 @shmdt(i8* %81)
br label %83
83: ; preds = %80, %76
%84 = load i32, i32* %4
%85 = icmp ne i32 %84, -1
br i1 %85, label %86, label %90
86: ; preds = %83
%87 = load i32, i32* %4
%88 = load i32, i32* @IPC_RMID
%89 = call i32 @shmctl(i32 %87, i32 %88, i32 0)
br label %90
90: ; preds = %86, %83
store i8* null, i8** %2
br label %91
91: ; preds = %90, %59
%92 = load i8*, i8** %2
ret i8* %92
}

LLVM IR (-O2):
@MAP_FAILED = global i8* null
@IPC_PRIVATE = global i32 0
@SHM_HUGETLB = global i32 0
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@IPC_RMID = global i32 0
define i8* @allocate_huge_page(i32 %0) {
%2 = load i8*, i8** @MAP_FAILED
%3 = load i32, i32* @IPC_PRIVATE
%4 = load i32, i32* @SHM_HUGETLB
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @SHM_R
%8 = or i32 %6, %7
%9 = load i32, i32* @SHM_W
%10 = or i32 %8, %9
%11 = tail call i32 @shmget(i32 %3, i32 %0, i32 %10)
%12 = tail call i8* @shmat(i32 %11, i8* null, i32 0)
%13 = load i8*, i8** @MAP_FAILED
%14 = icmp eq i8* %12, %13
br i1 %14, label %33, label %15
15: ; preds = %1
%16 = tail call i64 @mlock(i8* %12, i32 %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %33
18: ; preds = %15
%19 = tail call i32 @virtual_to_physical(i8* %12)
%20 = icmp eq i32 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %18
%22 = sext i32 %19 to i64
%23 = inttoptr i64 %22 to i8*
%24 = tail call i8* @shmat(i32 %11, i8* %23, i32 0)
%25 = load i8*, i8** @MAP_FAILED
%26 = icmp eq i8* %24, %25
br i1 %26, label %33, label %27
27: ; preds = %21
%28 = tail call i64 @mlock(i8* %24, i32 %0)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = tail call i32 @memset(i8* %24, i32 0, i32 %0)
%32 = tail call i32 @shmdt(i8* %12)
br label %47
33: ; preds = %27, %21, %18, %15, %1
%34 = phi i8* [ %2, %1 ], [ %2, %15 ], [ %2, %18 ], [ %24, %21 ], [ %24, %27 ]
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %12, %35
br i1 %36, label %40, label %37
37: ; preds = %33
%38 = tail call i32 @shmdt(i8* %12)
%39 = load i8*, i8** @MAP_FAILED
br label %40
40: ; preds = %37, %33
%41 = phi i8* [ %39, %37 ], [ %12, %33 ]
%42 = icmp eq i8* %34, %41
br i1 %42, label %45, label %43
43: ; preds = %40
%44 = tail call i32 @shmdt(i8* %34)
br label %45
45: ; preds = %43, %40
%46 = icmp eq i32 %11, -1
br i1 %46, label %51, label %47
47: ; preds = %45, %30
%48 = phi i8* [ %24, %30 ], [ null, %45 ]
%49 = load i32, i32* @IPC_RMID
%50 = tail call i32 @shmctl(i32 %11, i32 %49, i32 0)
br label %51
51: ; preds = %47, %45
%52 = phi i8* [ null, %45 ], [ %48, %47 ]
ret i8* %52
}