#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 MAP_ANONYMOUS;
void *MAP_FAILED;
int MAP_PRIVATE;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
void *mmap(int /*<<< orphan*/, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
void *alloc_anonymous_hugepage(int size, int private) {
  void *addr;
  int mapflag = MAP_ANONYMOUS | 0x40000;
  if (private)
    mapflag |= MAP_PRIVATE;
  else
    mapflag |= MAP_SHARED;
  if ((addr = mmap(0, size, PROT_READ | PROT_WRITE, mapflag, -1, 0)) ==
      MAP_FAILED) {
    perror("mmap");
    return NULL;
  }
  return addr;
}


x86 assembly (clang, -O0):
<alloc_anonymous_hugepage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
or     $0x40000,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     115d <alloc_anonymous_hugepage+0x3d>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    116c <alloc_anonymous_hugepage+0x4c>
mov    0x2e84(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
mov    0x2e82(%rip),%rax
mov    (%rax),%edx
mov    0x2e49(%rip),%rax
or     (%rax),%edx
mov    -0x1c(%rbp),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11c2 <alloc_anonymous_hugepage+0xa2>
lea    0xe50(%rip),%rdi
call   1040 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11ca <alloc_anonymous_hugepage+0xaa>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<alloc_anonymous_hugepage>:
push   %rax
mov    %edi,%eax
mov    0x2eae(%rip),%rcx
mov    (%rcx),%ecx
test   %esi,%esi
jne    1139 <alloc_anonymous_hugepage+0x19>
mov    0x2eb1(%rip),%rdx
jmp    1140 <alloc_anonymous_hugepage+0x20>
mov    0x2e80(%rip),%rdx
or     (%rdx),%ecx
or     $0x40000,%ecx
mov    0x2ea9(%rip),%rsi
mov    0x2e72(%rip),%rdx
mov    (%rdx),%edx
or     (%rsi),%edx
xor    %edi,%edi
mov    %eax,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     117a <alloc_anonymous_hugepage+0x5a>
pop    %rcx
ret
lea    0xe7f(%rip),%rdi
call   1040 <perror@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @alloc_anonymous_hugepage(i32 %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* @MAP_ANONYMOUS
%9 = or i32 %8, 262144
store i32 %9, i32* %7
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @MAP_PRIVATE
%14 = load i32, i32* %7
%15 = or i32 %14, %13
store i32 %15, i32* %7
br label %20
16: ; preds = %2
%17 = load i32, i32* @MAP_SHARED
%18 = load i32, i32* %7
%19 = or i32 %18, %17
store i32 %19, i32* %7
br label %20
20: ; preds = %16, %12
%21 = load i32, i32* %4
%22 = load i32, i32* @PROT_READ
%23 = load i32, i32* @PROT_WRITE
%24 = or i32 %22, %23
%25 = load i32, i32* %7
%26 = call i8* @mmap(i32 0, i32 %21, i32 %24, i32 %25, i32 -1, i32 0)
store i8* %26, i8** %6
%27 = load i8*, i8** @MAP_FAILED
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %31
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %3
br label %33
31: ; preds = %20
%32 = load i8*, i8** %6
store i8* %32, i8** %3
br label %33
33: ; preds = %31, %29
%34 = load i8*, i8** %3
ret i8* %34
}

LLVM IR (-O2):
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_SHARED = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_FAILED = global i8* null
@.str = [5 x i8] c"mmap\00"
define i8* @alloc_anonymous_hugepage(i32 %0, i32 %1) {
%3 = load i32, i32* @MAP_ANONYMOUS
%4 = icmp eq i32 %1, 0
%5 = load i32, i32* @MAP_SHARED
%6 = load i32, i32* @MAP_PRIVATE
%7 = select i1 %4, i32 %5, i32 %6
%8 = or i32 %3, %7
%9 = or i32 %8, 262144
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %11, %10
%13 = tail call i8* @mmap(i32 0, i32 %0, i32 %12, i32 %9, i32 -1, i32 0)
%14 = load i8*, i8** @MAP_FAILED
%15 = icmp eq i8* %13, %14
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %2, %16
%19 = phi i8* [ null, %16 ], [ %13, %2 ]
ret i8* %19
}