#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*/ free_page(int /*<<< orphan*/);
int get_pte(int /*<<< orphan*/, void *);
int /*<<< orphan*/ phys_to_virt(int);
int read_cr3();
void vfree(void *mem) {
  unsigned long size = ((unsigned long *)mem)[-1];
  while (size) {
    free_page(phys_to_virt(get_pte(phys_to_virt(read_cr3()), mem) &
                           (0xffffffffff000ull)));
    mem += 4096ul;
    size -= 4096ul;
  }
}


x86 assembly (clang, -O0):
<vfree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x8(%rax),%rax
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11ba <vfree+0x7a>
mov    $0x0,%al
call   1030 <read_cr3@plt>
mov    %eax,%edi
call   1040 <phys_to_virt@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
call   1060 <get_pte@plt>
cltq
movabs $0xffffffffff000,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   1040 <phys_to_virt@plt>
mov    %eax,%edi
call   1050 <free_page@plt>
mov    -0x8(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1000,%rax
mov    %rax,-0x10(%rbp)
jmp    1158 <vfree+0x18>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vfree>:
push   %r14
push   %rbx
push   %rax
mov    -0x8(%rdi),%r14
test   %r14,%r14
je     118b <vfree+0x4b>
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <read_cr3@plt>
mov    %eax,%edi
call   1040 <phys_to_virt@plt>
mov    %eax,%edi
mov    %rbx,%rsi
call   1060 <get_pte@plt>
and    $0xfffff000,%eax
mov    %eax,%edi
call   1040 <phys_to_virt@plt>
mov    %eax,%edi
call   1050 <free_page@plt>
add    $0x1000,%rbx
add    $0xfffffffffffff000,%r14
jne    1150 <vfree+0x10>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define void @vfree(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = bitcast i8* %4 to i64*
%6 = getelementptr inbounds i64, i64* %5, i64 -1
%7 = load i64, i64* %6
store i64 %7, i64* %3
br label %8
8: ; preds = %11, %1
%9 = load i64, i64* %3
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %8
%12 = call i32 (...) @read_cr3()
%13 = call i32 @phys_to_virt(i32 %12)
%14 = load i8*, i8** %2
%15 = call i32 @get_pte(i32 %13, i8* %14)
%16 = sext i32 %15 to i64
%17 = and i64 %16, 4503599627366400
%18 = trunc i64 %17 to i32
%19 = call i32 @phys_to_virt(i32 %18)
%20 = call i32 @free_page(i32 %19)
%21 = load i8*, i8** %2
%22 = getelementptr i8, i8* %21, i64 4096
store i8* %22, i8** %2
%23 = load i64, i64* %3
%24 = sub i64 %23, 4096
store i64 %24, i64* %3
br label %8
25: ; preds = %8
ret void
}

LLVM IR (-O2):
define void @vfree(i8* %0) {
%2 = getelementptr inbounds i8, i8* %0, i64 -8
%3 = bitcast i8* %2 to i64*
%4 = load i64, i64* %3
%5 = icmp eq i64 %4, 0
br i1 %5, label %18, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %16, %6 ], [ %4, %1 ]
%8 = phi i8* [ %15, %6 ], [ %0, %1 ]
%9 = tail call i32 (...) @read_cr3()
%10 = tail call i32 @phys_to_virt(i32 %9)
%11 = tail call i32 @get_pte(i32 %10, i8* %8)
%12 = and i32 %11, -4096
%13 = tail call i32 @phys_to_virt(i32 %12)
%14 = tail call i32 @free_page(i32 %13)
%15 = getelementptr i8, i8* %8, i64 4096
%16 = add i64 %7, -4096
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %6
18: ; preds = %6, %1
ret void
}