#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 /*<<< orphan*/ MemMapPage(char *, int /*<<< orphan*/);
int /*<<< orphan*/ MemPhysicalAlloc(int, int);
void *MemVirtualReserve(unsigned int);
void *MemVirtualAlloc(unsigned int bytes) {
  void *data = MemVirtualReserve(bytes);
  for (unsigned int off = 0; off < bytes; off += 4096) {
    MemMapPage((char *)data + off, MemPhysicalAlloc(1, 2));
  }
  return data;
}


x86 assembly (clang, -O0):
<MemVirtualAlloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <MemVirtualReserve@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1192 <MemVirtualAlloc+0x62>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    $0x1,%edi
mov    $0x2,%esi
call   1030 <MemPhysicalAlloc@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1050 <MemMapPage@plt>
mov    -0x14(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0x14(%rbp)
jmp    114e <MemVirtualAlloc+0x1e>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<MemVirtualAlloc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r14d
call   1040 <MemVirtualReserve@plt>
mov    %rax,%r15
test   %r14d,%r14d
je     1179 <MemVirtualAlloc+0x49>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %ebx,%ebp
add    %r15,%rbp
mov    $0x1,%edi
mov    $0x2,%esi
call   1030 <MemPhysicalAlloc@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1050 <MemMapPage@plt>
add    $0x1000,%ebx
cmp    %r14d,%ebx
jb     1150 <MemVirtualAlloc+0x20>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @MemVirtualAlloc(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
%5 = load i32, i32* %2
%6 = call i8* @MemVirtualReserve(i32 %5)
store i8* %6, i8** %3
store i32 0, i32* %4
br label %7
7: ; preds = %18, %1
%8 = load i32, i32* %4
%9 = load i32, i32* %2
%10 = icmp ult i32 %8, %9
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = load i8*, i8** %3
%13 = load i32, i32* %4
%14 = zext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = call i32 @MemPhysicalAlloc(i32 1, i32 2)
%17 = call i32 @MemMapPage(i8* %15, i32 %16)
br label %18
18: ; preds = %11
%19 = load i32, i32* %4
%20 = add i32 %19, 4096
store i32 %20, i32* %4
br label %7
21: ; preds = %7
%22 = load i8*, i8** %3
ret i8* %22
}

LLVM IR (-O2):
define i8* @MemVirtualAlloc(i32 %0) {
%2 = tail call i8* @MemVirtualReserve(i32 %0)
%3 = icmp eq i32 %0, 0
br i1 %3, label %4, label %5
4: ; preds = %5, %1
ret i8* %2
5: ; preds = %1, %5
%6 = phi i32 [ %11, %5 ], [ 0, %1 ]
%7 = zext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %2, i64 %7
%9 = tail call i32 @MemPhysicalAlloc(i32 1, i32 2)
%10 = tail call i32 @MemMapPage(i8* %8, i32 %9)
%11 = add i32 %6, 4096
%12 = icmp ult i32 %11, %0
br i1 %12, label %5, label %4
}