#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;
int MAP_PRIVATE;
int /*<<< orphan*/ PROT_READ;
int /*<<< orphan*/ _SC_PAGE_SIZE;
int /*<<< orphan*/ assert(int);
void *mmap(int /*<<< orphan*/, long, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int munmap(void *, long);
long sysconf(int /*<<< orphan*/);
__attribute__((unused)) void *get_unmapped_page(void) {
  void *ptr;
  int r;
  long pagesz = sysconf(_SC_PAGE_SIZE);
  assert(pagesz == 4096 || pagesz == 16384 || pagesz == 65536);
  ptr = mmap(0, pagesz, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
  assert(ptr != (void *)-1);
  r = munmap(ptr, pagesz);
  assert(r == 0);
  return ptr;
}


x86 assembly (clang, -O0):
<get_unmapped_page>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e81(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,-0x18(%rbp)
mov    $0x1,%al
cmpq   $0x1000,-0x18(%rbp)
mov    %al,-0x19(%rbp)
je     118e <get_unmapped_page+0x4e>
mov    $0x1,%al
cmpq   $0x4000,-0x18(%rbp)
mov    %al,-0x19(%rbp)
je     118e <get_unmapped_page+0x4e>
cmpq   $0x10000,-0x18(%rbp)
sete   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x18(%rbp),%rsi
mov    0x2e52(%rip),%rax
mov    (%rax),%edx
mov    0x2e31(%rip),%rax
mov    (%rax),%ecx
mov    0x2e10(%rip),%rax
or     (%rax),%ecx
xor    %r9d,%r9d
mov    $0xffffffff,%r8d
mov    %r9d,%edi
call   1030 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <munmap@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_unmapped_page>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,%r14
mov    $0x1,%edi
cmp    $0x1000,%rax
je     1178 <get_unmapped_page+0x38>
cmp    $0x4000,%r14
je     1178 <get_unmapped_page+0x38>
xor    %edi,%edi
cmp    $0x10000,%r14
sete   %dil
call   1040 <assert@plt>
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
mov    0x2e53(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbx
xor    %edi,%edi
cmp    $0xffffffffffffffff,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <munmap@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1040 <assert@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @get_unmapped_page() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
%4 = load i32, i32* @_SC_PAGE_SIZE
%5 = call i64 @sysconf(i32 %4)
store i64 %5, i64* %3
%6 = load i64, i64* %3
%7 = icmp eq i64 %6, 4096
br i1 %7, label %14, label %8
8: ; preds = %0
%9 = load i64, i64* %3
%10 = icmp eq i64 %9, 16384
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i64, i64* %3
%13 = icmp eq i64 %12, 65536
br label %14
14: ; preds = %11, %8, %0
%15 = phi i1 [ true, %8 ], [ true, %0 ], [ %13, %11 ]
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i64, i64* %3
%19 = load i32, i32* @PROT_READ
%20 = load i32, i32* @MAP_ANONYMOUS
%21 = load i32, i32* @MAP_PRIVATE
%22 = or i32 %20, %21
%23 = call i8* @mmap(i32 0, i64 %18, i32 %19, i32 %22, i32 -1, i32 0)
store i8* %23, i8** %1
%24 = load i8*, i8** %1
%25 = icmp ne i8* %24, inttoptr (i64 -1 to i8*)
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i8*, i8** %1
%29 = load i64, i64* %3
%30 = call i32 @munmap(i8* %28, i64 %29)
store i32 %30, i32* %2
%31 = load i32, i32* %2
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i8*, i8** %1
ret i8* %35
}

LLVM IR (-O2):
@_SC_PAGE_SIZE = global i32 0
@PROT_READ = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
define i8* @get_unmapped_page() {
%1 = load i32, i32* @_SC_PAGE_SIZE
%2 = tail call i64 @sysconf(i32 %1)
switch i64 %2, label %3 [
i64 16384, label %6
i64 4096, label %6
]
3: ; preds = %0
%4 = icmp eq i64 %2, 65536
%5 = zext i1 %4 to i32
br label %6
6: ; preds = %0, %0, %3
%7 = phi i32 [ 1, %0 ], [ %5, %3 ], [ 1, %0 ]
%8 = tail call i32 @assert(i32 %7)
%9 = load i32, i32* @PROT_READ
%10 = load i32, i32* @MAP_ANONYMOUS
%11 = load i32, i32* @MAP_PRIVATE
%12 = or i32 %11, %10
%13 = tail call i8* @mmap(i32 0, i64 %2, i32 %9, i32 %12, i32 -1, i32 0)
%14 = icmp ne i8* %13, inttoptr (i64 -1 to i8*)
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i32 @munmap(i8* %13, i64 %2)
%18 = icmp eq i32 %17, 0
%19 = zext i1 %18 to i32
%20 = tail call i32 @assert(i32 %19)
ret i8* %13
}