#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*/ allocate_frame();
void *get_next_available_virtual_frame(unsigned int);
int /*<<< orphan*/ map_page(int /*<<< orphan*/, void *, int);
void *allocate_contiguous_vframe(unsigned int size) {
  unsigned int num_of_frames = (size / 0x1000);
  if (size % 0x1000) {
    num_of_frames++;
  }
  void *vframe = get_next_available_virtual_frame(num_of_frames);
  for (int i = 0; i < num_of_frames; i++) {
    map_page(allocate_frame(), vframe + 0x1000 * i, 2);
  }
  return vframe;
}


x86 assembly (clang, -O0):
<allocate_contiguous_vframe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
shr    $0xc,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xfff,%eax
cmp    $0x0,%eax
je     115e <allocate_contiguous_vframe+0x2e>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <get_next_available_virtual_frame@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11ad <allocate_contiguous_vframe+0x7d>
mov    $0x0,%al
call   1030 <allocate_frame@plt>
mov    %eax,%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
shl    $0xc,%eax
cltq
add    %rax,%rsi
mov    $0x2,%edx
call   1040 <map_page@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1171 <allocate_contiguous_vframe+0x41>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<allocate_contiguous_vframe>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%ebx
shr    $0xc,%ebx
xor    %r15d,%r15d
test   $0xfff,%edi
setne  %r15b
lea    (%rbx,%r15,1),%edi
call   1050 <get_next_available_virtual_frame@plt>
mov    %rax,%r14
add    %r15d,%ebx
je     1183 <allocate_contiguous_vframe+0x53>
mov    %ebx,%r15d
mov    %r14,%rbx
xchg   %ax,%ax
xor    %eax,%eax
call   1030 <allocate_frame@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x2,%edx
call   1040 <map_page@plt>
add    $0x1000,%rbx
add    $0xffffffffffffffff,%r15
jne    1160 <allocate_contiguous_vframe+0x30>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @allocate_contiguous_vframe(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = udiv i32 %6, 4096
store i32 %7, i32* %3
%8 = load i32, i32* %2
%9 = urem i32 %8, 4096
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* %3
%13 = add i32 %12, 1
store i32 %13, i32* %3
br label %14
14: ; preds = %11, %1
%15 = load i32, i32* %3
%16 = call i8* @get_next_available_virtual_frame(i32 %15)
store i8* %16, i8** %4
store i32 0, i32* %5
br label %17
17: ; preds = %29, %14
%18 = load i32, i32* %5
%19 = load i32, i32* %3
%20 = icmp ult i32 %18, %19
br i1 %20, label %21, label %32
21: ; preds = %17
%22 = call i32 (...) @allocate_frame()
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = mul nsw i32 4096, %24
%26 = sext i32 %25 to i64
%27 = getelementptr i8, i8* %23, i64 %26
%28 = call i32 @map_page(i32 %22, i8* %27, i32 2)
br label %29
29: ; preds = %21
%30 = load i32, i32* %5
%31 = add nsw i32 %30, 1
store i32 %31, i32* %5
br label %17
32: ; preds = %17
%33 = load i8*, i8** %4
ret i8* %33
}

LLVM IR (-O2):
define i8* @allocate_contiguous_vframe(i32 %0) {
%2 = lshr i32 %0, 12
%3 = and i32 %0, 4095
%4 = icmp ne i32 %3, 0
%5 = zext i1 %4 to i32
%6 = add nuw nsw i32 %2, %5
%7 = tail call i8* @get_next_available_virtual_frame(i32 %6)
%8 = icmp eq i32 %6, 0
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = zext i32 %6 to i64
br label %12
11: ; preds = %12, %1
ret i8* %7
12: ; preds = %9, %12
%13 = phi i64 [ 0, %9 ], [ %18, %12 ]
%14 = tail call i32 (...) @allocate_frame()
%15 = shl nsw i64 %13, 12
%16 = getelementptr i8, i8* %7, i64 %15
%17 = tail call i32 @map_page(i32 %14, i8* %16, i32 2)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %10
br i1 %19, label %11, label %12
}