#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*/ memset(unsigned long *, int /*<<< orphan*/, unsigned long);
unsigned long *phys_to_virt(unsigned long);
unsigned long long virt_to_phys(unsigned long *);
void install_pte(unsigned long *cr3, int pte_level, void *virt,
                 unsigned long pte) {
  int level;
  unsigned long *pt = cr3;
  unsigned offset;
  for (level = 4; level > pte_level; --level) {
    offset = ((unsigned long)virt >> ((level - 1) * 9 + 12)) & 511;
    if (!(pt[offset] & (1ull << 0))) {
      unsigned long *new_pt = alloc_page();
      memset(new_pt, 0, 4096ul);
      pt[offset] = virt_to_phys(new_pt) | (1ull << 0) | (1ull << 1);
    }
    pt = phys_to_virt(pt[offset] & 0xffffffffff000ull);
  }
  offset = ((unsigned long)virt >> (((level - 1) * 9) + 12)) & 511;
  pt[offset] = pte;
}


x86 assembly (clang, -O0):
<install_pte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jle    120f <install_pte+0xcf>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
imul   $0x9,%ecx,%ecx
add    $0xc,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
and    $0x1ff,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    (%rax,%rcx,8),%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    11e3 <install_pte+0xa3>
mov    $0x0,%al
call   1050 <alloc_page@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x40(%rbp),%rdi
call   1060 <virt_to_phys@plt>
mov    %rax,%rdx
or     $0x1,%rdx
or     $0x2,%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
movabs $0xffffffffff000,%rdi
and    (%rax,%rcx,8),%rdi
call   1030 <phys_to_virt@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
jmp    1166 <install_pte+0x26>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
imul   $0x9,%ecx,%ecx
add    $0xc,%ecx
mov    %ecx,%ecx
shr    %cl,%rax
and    $0x1ff,%rax
mov    %eax,-0x34(%rbp)
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rax
mov    -0x34(%rbp),%ecx
mov    %rdx,(%rax,%rcx,8)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<install_pte>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rax
mov    %rdx,%r12
mov    %rdi,%r13
mov    $0x27,%ecx
cmp    $0x3,%esi
jg     11fa <install_pte+0xba>
mov    %esi,%ebx
mov    %rax,0x8(%rsp)
mov    $0x4,%r14d
mov    $0x27,%ebp
mov    %rbx,0x10(%rsp)
jmp    11a1 <install_pte+0x61>
xchg   %ax,%ax
movabs $0xffffffffff000,%rax
and    %rax,%rdi
call   1030 <phys_to_virt@plt>
mov    %rax,%r13
add    $0xffffffff,%r14d
add    $0xfffffff7,%ebp
cmp    %ebx,%r14d
jle    11ef <install_pte+0xaf>
mov    %r12,%r15
mov    %ebp,%ecx
shr    %cl,%r15
and    $0x1ff,%r15d
mov    0x0(%r13,%r15,8),%rdi
test   $0x1,%dil
jne    1180 <install_pte+0x40>
xor    %eax,%eax
call   1050 <alloc_page@plt>
mov    %rax,%rbx
mov    $0x1000,%edx
mov    %rax,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
mov    %rbx,%rdi
mov    0x10(%rsp),%rbx
call   1060 <virt_to_phys@plt>
mov    %rax,%rdi
or     $0x3,%rdi
mov    %rdi,0x0(%r13,%r15,8)
jmp    1180 <install_pte+0x40>
lea    (%rbx,%rbx,8),%ecx
add    $0x3,%ecx
mov    0x8(%rsp),%rax
shr    %cl,%r12
and    $0x1ff,%r12d
mov    %rax,0x0(%r13,%r12,8)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @install_pte(i64* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i64*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64*
%11 = alloca i32
%12 = alloca i64*
store i64* %0, i64** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i64 %3, i64* %8
%13 = load i64*, i64** %5
store i64* %13, i64** %10
store i32 4, i32* %9
br label %14
14: ; preds = %56, %4
%15 = load i32, i32* %9
%16 = load i32, i32* %6
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %59
18: ; preds = %14
%19 = load i8*, i8** %7
%20 = ptrtoint i8* %19 to i64
%21 = load i32, i32* %9
%22 = sub nsw i32 %21, 1
%23 = mul nsw i32 %22, 9
%24 = add nsw i32 %23, 12
%25 = zext i32 %24 to i64
%26 = lshr i64 %20, %25
%27 = and i64 %26, 511
%28 = trunc i64 %27 to i32
store i32 %28, i32* %11
%29 = load i64*, i64** %10
%30 = load i32, i32* %11
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i64, i64* %29, i64 %31
%33 = load i64, i64* %32
%34 = and i64 %33, 1
%35 = icmp ne i64 %34, 0
br i1 %35, label %48, label %36
36: ; preds = %18
%37 = call i64* (...) @alloc_page()
store i64* %37, i64** %12
%38 = load i64*, i64** %12
%39 = call i32 @memset(i64* %38, i32 0, i64 4096)
%40 = load i64*, i64** %12
%41 = call i64 @virt_to_phys(i64* %40)
%42 = or i64 %41, 1
%43 = or i64 %42, 2
%44 = load i64*, i64** %10
%45 = load i32, i32* %11
%46 = zext i32 %45 to i64
%47 = getelementptr inbounds i64, i64* %44, i64 %46
store i64 %43, i64* %47
br label %48
48: ; preds = %36, %18
%49 = load i64*, i64** %10
%50 = load i32, i32* %11
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i64, i64* %49, i64 %51
%53 = load i64, i64* %52
%54 = and i64 %53, 4503599627366400
%55 = call i64* @phys_to_virt(i64 %54)
store i64* %55, i64** %10
br label %56
56: ; preds = %48
%57 = load i32, i32* %9
%58 = add nsw i32 %57, -1
store i32 %58, i32* %9
br label %14
59: ; preds = %14
%60 = load i8*, i8** %7
%61 = ptrtoint i8* %60 to i64
%62 = load i32, i32* %9
%63 = sub nsw i32 %62, 1
%64 = mul nsw i32 %63, 9
%65 = add nsw i32 %64, 12
%66 = zext i32 %65 to i64
%67 = lshr i64 %61, %66
%68 = and i64 %67, 511
%69 = trunc i64 %68 to i32
store i32 %69, i32* %11
%70 = load i64, i64* %8
%71 = load i64*, i64** %10
%72 = load i32, i32* %11
%73 = zext i32 %72 to i64
%74 = getelementptr inbounds i64, i64* %71, i64 %73
store i64 %70, i64* %74
ret void
}

LLVM IR (-O2):
define void @install_pte(i64* nocapture %0, i32 %1, i8* %2, i64 %3) {
%5 = icmp slt i32 %1, 4
%6 = ptrtoint i8* %2 to i64
br i1 %5, label %7, label %34
7: ; preds = %4, %24
%8 = phi i32 [ %28, %24 ], [ 4, %4 ]
%9 = phi i64* [ %27, %24 ], [ %0, %4 ]
%10 = mul i32 %8, 9
%11 = add i32 %10, 3
%12 = zext i32 %11 to i64
%13 = lshr i64 %6, %12
%14 = and i64 %13, 511
%15 = getelementptr inbounds i64, i64* %9, i64 %14
%16 = load i64, i64* %15
%17 = and i64 %16, 1
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %7
%20 = tail call i64* (...) @alloc_page()
%21 = tail call i32 @memset(i64* %20, i32 0, i64 4096)
%22 = tail call i64 @virt_to_phys(i64* %20)
%23 = or i64 %22, 3
store i64 %23, i64* %15
br label %24
24: ; preds = %19, %7
%25 = phi i64 [ %23, %19 ], [ %16, %7 ]
%26 = and i64 %25, 4503599627366400
%27 = tail call i64* @phys_to_virt(i64 %26)
%28 = add nsw i32 %8, -1
%29 = icmp sgt i32 %28, %1
br i1 %29, label %7, label %30
30: ; preds = %24
%31 = mul i32 %1, 9
%32 = add i32 %31, 3
%33 = zext i32 %32 to i64
br label %34
34: ; preds = %4, %30
%35 = phi i64* [ %27, %30 ], [ %0, %4 ]
%36 = phi i64 [ %33, %30 ], [ 39, %4 ]
%37 = lshr i64 %6, %36
%38 = and i64 %37, 511
%39 = getelementptr inbounds i64, i64* %35, i64 %38
store i64 %3, i64* %39
ret void
}