#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 */
unsigned long *alloc_page();
int /*<<< orphan*/ install_large_page(unsigned long *, unsigned long, void *);
int /*<<< orphan*/ install_page(unsigned long *, unsigned long, void *);
int /*<<< orphan*/ load_cr3(int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned long *, int /*<<< orphan*/, unsigned long);
int /*<<< orphan*/ print(char *);
int /*<<< orphan*/ virt_to_phys(unsigned long *);
void setup_mmu(unsigned long len) {
  unsigned long *cr3 = alloc_page();
  unsigned long phys = 0;
  if (len < (1ul << 32))
    len = 1ul << 32;
  memset(cr3, 0, 4096ul);
  while (phys + (512 * 4096ul) <= len) {
    install_large_page(cr3, phys, (void *)phys);
    phys += (512 * 4096ul);
  }
  while (phys + 4096ul <= len) {
    install_page(cr3, phys, (void *)phys);
    phys += 4096ul;
  }
  load_cr3(virt_to_phys(cr3));
  print("paging enabled\n");
}


x86 assembly (clang, -O0):
<setup_mmu>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1070 <alloc_page@plt>
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movabs $0x100000000,%rax
cmp    %rax,-0x8(%rbp)
jae    11b1 <setup_mmu+0x41>
movabs $0x100000000,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
add    $0x200000,%rax
cmp    -0x8(%rbp),%rax
ja     11f9 <setup_mmu+0x89>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <install_large_page@plt>
mov    -0x18(%rbp),%rax
add    $0x200000,%rax
mov    %rax,-0x18(%rbp)
jmp    11c1 <setup_mmu+0x51>
jmp    11fe <setup_mmu+0x8e>
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
cmp    -0x8(%rbp),%rax
ja     1236 <setup_mmu+0xc6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <install_page@plt>
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x18(%rbp)
jmp    11fe <setup_mmu+0x8e>
mov    -0x10(%rbp),%rdi
call   1080 <virt_to_phys@plt>
mov    %eax,%edi
call   1090 <load_cr3@plt>
lea    0xdb3(%rip),%rdi
call   1060 <print@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_mmu>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r15
xor    %r12d,%r12d
xor    %eax,%eax
call   1070 <alloc_page@plt>
mov    %rax,%r14
movabs $0x100000000,%r13
cmp    %r13,%r15
cmova  %r15,%r13
mov    $0x1000,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
nopl   0x0(%rax)
mov    %r12,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1030 <install_large_page@plt>
add    $0x200000,%r12
add    $0x400000,%rbx
cmp    %r13,%rbx
jbe    11b0 <setup_mmu+0x40>
lea    0x1000(%r12),%rax
cmp    %r13,%rax
ja     1215 <setup_mmu+0xa5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r12,%rdx
call   1040 <install_page@plt>
lea    0x1000(%r12),%rax
add    $0x2000,%r12
cmp    %r13,%r12
mov    %rax,%r12
jbe    11f0 <setup_mmu+0x80>
mov    %r14,%rdi
call   1080 <virt_to_phys@plt>
mov    %eax,%edi
call   1090 <load_cr3@plt>
lea    0xdd5(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1060 <print@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"paging enabled\0A\00"
define void @setup_mmu(i64 %0) {
%2 = alloca i64
%3 = alloca i64*
%4 = alloca i64
store i64 %0, i64* %2
%5 = call i64* (...) @alloc_page()
store i64* %5, i64** %3
store i64 0, i64* %4
%6 = load i64, i64* %2
%7 = icmp ult i64 %6, 4294967296
br i1 %7, label %8, label %9
8: ; preds = %1
store i64 4294967296, i64* %2
br label %9
9: ; preds = %8, %1
%10 = load i64*, i64** %3
%11 = call i32 @memset(i64* %10, i32 0, i64 4096)
br label %12
12: ; preds = %17, %9
%13 = load i64, i64* %4
%14 = add i64 %13, 2097152
%15 = load i64, i64* %2
%16 = icmp ule i64 %14, %15
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = load i64*, i64** %3
%19 = load i64, i64* %4
%20 = load i64, i64* %4
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @install_large_page(i64* %18, i64 %19, i8* %21)
%23 = load i64, i64* %4
%24 = add i64 %23, 2097152
store i64 %24, i64* %4
br label %12
25: ; preds = %12
br label %26
26: ; preds = %31, %25
%27 = load i64, i64* %4
%28 = add i64 %27, 4096
%29 = load i64, i64* %2
%30 = icmp ule i64 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %26
%32 = load i64*, i64** %3
%33 = load i64, i64* %4
%34 = load i64, i64* %4
%35 = inttoptr i64 %34 to i8*
%36 = call i32 @install_page(i64* %32, i64 %33, i8* %35)
%37 = load i64, i64* %4
%38 = add i64 %37, 4096
store i64 %38, i64* %4
br label %26
39: ; preds = %26
%40 = load i64*, i64** %3
%41 = call i32 @virt_to_phys(i64* %40)
%42 = call i32 @load_cr3(i32 %41)
%43 = call i32 @print(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"paging enabled\0A\00"
define void @setup_mmu(i64 %0) {
%2 = tail call i64* (...) @alloc_page()
%3 = icmp ugt i64 %0, 4294967296
%4 = select i1 %3, i64 %0, i64 4294967296
%5 = tail call i32 @memset(i64* %2, i32 0, i64 4096)
br label %9
6: ; preds = %9
%7 = or i64 %10, 4096
%8 = icmp ugt i64 %7, %4
br i1 %8, label %23, label %16
9: ; preds = %1, %9
%10 = phi i64 [ 2097152, %1 ], [ %14, %9 ]
%11 = phi i64 [ 0, %1 ], [ %10, %9 ]
%12 = inttoptr i64 %11 to i8*
%13 = tail call i32 @install_large_page(i64* %2, i64 %11, i8* %12)
%14 = add i64 %10, 2097152
%15 = icmp ugt i64 %14, %4
br i1 %15, label %6, label %9
16: ; preds = %6, %16
%17 = phi i64 [ %21, %16 ], [ %7, %6 ]
%18 = phi i64 [ %17, %16 ], [ %10, %6 ]
%19 = inttoptr i64 %18 to i8*
%20 = tail call i32 @install_page(i64* %2, i64 %18, i8* %19)
%21 = add i64 %17, 4096
%22 = icmp ugt i64 %21, %4
br i1 %22, label %23, label %16
23: ; preds = %16, %6
%24 = tail call i32 @virt_to_phys(i64* %2)
%25 = tail call i32 @load_cr3(i32 %24)
%26 = tail call i32 @print(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
ret void
}