#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 */
struct pagetable {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ pagetable_getmap(struct pagetable *, unsigned int,
                                    unsigned int *);
int /*<<< orphan*/ pagetable_map(struct pagetable *, unsigned int,
                                 int /*<<< orphan*/, int);
void pagetable_alloc(struct pagetable *p, unsigned vaddr, unsigned length,
                     int flags) {
  unsigned npages = length / 4096;
  if (length % 4096) {
    npages++;
  }
  vaddr &= 0xfffff000;
  while (npages > 0) {
    unsigned paddr;
    if (!pagetable_getmap(p, vaddr, &paddr)) {
      pagetable_map(p, vaddr, 0, flags | 2);
    }
    vaddr += 4096;
    npages--;
  }
}


x86 assembly (clang, -O0):
<pagetable_alloc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x10(%rbp),%eax
shr    $0xc,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
and    $0xfff,%eax
cmp    $0x0,%eax
je     1158 <pagetable_alloc+0x38>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0xfffff000,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x18(%rbp)
jbe    11b3 <pagetable_alloc+0x93>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <pagetable_getmap@plt>
cmp    $0x0,%eax
jne    119a <pagetable_alloc+0x7a>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x14(%rbp),%ecx
or     $0x2,%ecx
xor    %edx,%edx
call   1040 <pagetable_map@plt>
mov    -0xc(%rbp),%eax
add    $0x1000,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    1163 <pagetable_alloc+0x43>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pagetable_alloc>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%eax
shr    $0xc,%eax
xor    %ebx,%ebx
test   $0xfff,%edx
setne  %bl
add    %eax,%ebx
je     118d <pagetable_alloc+0x6d>
mov    %ecx,%r14d
mov    %esi,%ebp
mov    %rdi,%r12
and    $0xfffff000,%ebp
or     $0x2,%r14d
lea    0xc(%rsp),%r15
jmp    116b <pagetable_alloc+0x4b>
nopl   0x0(%rax)
add    $0x1000,%ebp
add    $0xffffffff,%ebx
je     118d <pagetable_alloc+0x6d>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
call   1030 <pagetable_getmap@plt>
test   %eax,%eax
jne    1160 <pagetable_alloc+0x40>
mov    %r12,%rdi
mov    %ebp,%esi
xor    %edx,%edx
mov    %r14d,%ecx
call   1040 <pagetable_map@plt>
jmp    1160 <pagetable_alloc+0x40>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pagetable = type { i32 }
define void @pagetable_alloc(%struct.pagetable* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.pagetable*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store %struct.pagetable* %0, %struct.pagetable** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* %7
%12 = udiv i32 %11, 4096
store i32 %12, i32* %9
%13 = load i32, i32* %7
%14 = urem i32 %13, 4096
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* %9
%18 = add i32 %17, 1
store i32 %18, i32* %9
br label %19
19: ; preds = %16, %4
%20 = load i32, i32* %6
%21 = and i32 %20, -4096
store i32 %21, i32* %6
br label %22
22: ; preds = %36, %19
%23 = load i32, i32* %9
%24 = icmp ugt i32 %23, 0
br i1 %24, label %25, label %41
25: ; preds = %22
%26 = load %struct.pagetable*, %struct.pagetable** %5
%27 = load i32, i32* %6
%28 = call i32 @pagetable_getmap(%struct.pagetable* %26, i32 %27, i32* %10)
%29 = icmp ne i32 %28, 0
br i1 %29, label %36, label %30
30: ; preds = %25
%31 = load %struct.pagetable*, %struct.pagetable** %5
%32 = load i32, i32* %6
%33 = load i32, i32* %8
%34 = or i32 %33, 2
%35 = call i32 @pagetable_map(%struct.pagetable* %31, i32 %32, i32 0, i32 %34)
br label %36
36: ; preds = %30, %25
%37 = load i32, i32* %6
%38 = add i32 %37, 4096
store i32 %38, i32* %6
%39 = load i32, i32* %9
%40 = add i32 %39, -1
store i32 %40, i32* %9
br label %22
41: ; preds = %22
ret void
}

LLVM IR (-O2):
%struct.pagetable = type { i32 }
define void @pagetable_alloc(%struct.pagetable* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = lshr i32 %2, 12
%7 = and i32 %2, 4095
%8 = icmp ne i32 %7, 0
%9 = zext i1 %8 to i32
%10 = add nuw nsw i32 %6, %9
%11 = icmp eq i32 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %4
%13 = and i32 %1, -4096
%14 = bitcast i32* %5 to i8*
%15 = or i32 %3, 2
br label %16
16: ; preds = %12, %23
%17 = phi i32 [ %10, %12 ], [ %25, %23 ]
%18 = phi i32 [ %13, %12 ], [ %24, %23 ]
%19 = call i32 @pagetable_getmap(%struct.pagetable* %0, i32 %18, i32* %5)
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @pagetable_map(%struct.pagetable* %0, i32 %18, i32 0, i32 %15)
br label %23
23: ; preds = %21, %16
%24 = add i32 %18, 4096
%25 = add nsw i32 %17, -1
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %16
27: ; preds = %23, %4
ret void
}