#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 */
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);
uintptr_t virtual_to_physical(void *);
void *allocate_huge_page(int size) {
  int shmid = -1;
  uintptr_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,-0x28(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%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,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    0x2dbd(%rip),%rcx
cmp    (%rcx),%rax
jne    1201 <allocate_huge_page+0x91>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x28(%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    -0x28(%rbp),%rdi
call   1060 <virtual_to_physical@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1239 <allocate_huge_page+0xc9>
jmp    12c8 <allocate_huge_page+0x158>
movabs $0x500000000000,%rax
or     -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x2d48(%rip),%rcx
cmp    (%rcx),%rax
jne    1276 <allocate_huge_page+0x106>
jmp    12c8 <allocate_huge_page+0x158>
mov    -0x30(%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    -0x30(%rbp),%rdi
mov    -0xc(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x28(%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    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1327 <allocate_huge_page+0x1b7>
mov    -0x28(%rbp),%rax
mov    0x2cdd(%rip),%rcx
cmp    (%rcx),%rax
je     12e5 <allocate_huge_page+0x175>
mov    -0x28(%rbp),%rdi
call   1040 <shmdt@plt>
mov    -0x30(%rbp),%rax
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%rax
je     1302 <allocate_huge_page+0x192>
mov    -0x30(%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   %rax,%rax
je     11e2 <allocate_huge_page+0x72>
movabs $0x500000000000,%rcx
or     %rcx,%rax
mov    %r15d,%edi
mov    %rax,%rsi
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 i64
%6 = alloca i64
%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 %66
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 %66
33: ; preds = %27
%34 = load i8*, i8** %7
%35 = call i64 @virtual_to_physical(i8* %34)
store i64 %35, i64* %5
%36 = load i64, i64* %5
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
br label %66
39: ; preds = %33
%40 = load i64, i64* %5
%41 = or i64 %40, 87960930222080
store i64 %41, i64* %6
%42 = load i32, i32* %4
%43 = load i64, i64* %6
%44 = inttoptr i64 %43 to i8*
%45 = call i8* @shmat(i32 %42, i8* %44, i32 0)
store i8* %45, i8** %8
%46 = load i8*, i8** %8
%47 = load i8*, i8** @MAP_FAILED
%48 = icmp eq i8* %46, %47
br i1 %48, label %49, label %50
49: ; preds = %39
br label %66
50: ; preds = %39
%51 = load i8*, i8** %8
%52 = load i32, i32* %3
%53 = call i64 @mlock(i8* %51, i32 %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %50
br label %66
56: ; preds = %50
%57 = load i8*, i8** %8
%58 = load i32, i32* %3
%59 = call i32 @memset(i8* %57, i32 0, i32 %58)
%60 = load i8*, i8** %7
%61 = call i32 @shmdt(i8* %60)
%62 = load i32, i32* %4
%63 = load i32, i32* @IPC_RMID
%64 = call i32 @shmctl(i32 %62, i32 %63, i32 0)
%65 = load i8*, i8** %8
store i8* %65, i8** %2
br label %88
66: ; preds = %55, %49, %38, %32, %26
%67 = load i8*, i8** %7
%68 = load i8*, i8** @MAP_FAILED
%69 = icmp ne i8* %67, %68
br i1 %69, label %70, label %73
70: ; preds = %66
%71 = load i8*, i8** %7
%72 = call i32 @shmdt(i8* %71)
br label %73
73: ; preds = %70, %66
%74 = load i8*, i8** %8
%75 = load i8*, i8** @MAP_FAILED
%76 = icmp ne i8* %74, %75
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i8*, i8** %8
%79 = call i32 @shmdt(i8* %78)
br label %80
80: ; preds = %77, %73
%81 = load i32, i32* %4
%82 = icmp ne i32 %81, -1
br i1 %82, label %83, label %87
83: ; preds = %80
%84 = load i32, i32* %4
%85 = load i32, i32* @IPC_RMID
%86 = call i32 @shmctl(i32 %84, i32 %85, i32 0)
br label %87
87: ; preds = %83, %80
store i8* null, i8** %2
br label %88
88: ; preds = %87, %56
%89 = load i8*, i8** %2
ret i8* %89
}

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 i64 @virtual_to_physical(i8* %12)
%20 = icmp eq i64 %19, 0
br i1 %20, label %33, label %21
21: ; preds = %18
%22 = or i64 %19, 87960930222080
%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
}