#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 */
typedef int uint64_t;
typedef int /*<<< orphan*/ data;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, int *, int, int);
int /*<<< orphan*/ stderr;
uint64_t virtual_to_physical(void *ptr) {
  uint64_t virt_page;
  static int pagemap_fd;
  virt_page = ((uint64_t)ptr) / 4096;
  if (pagemap_fd == 0) {
    if ((pagemap_fd = open("/proc/self/pagemap", O_RDONLY)) <= 0) {
      perror("open pagemap");
      return 0;
    }
  }
  uint64_t data;
  int len;
  len = pread(pagemap_fd, &data, sizeof(data), virt_page * sizeof(uint64_t));
  if (len != sizeof(data)) {
    perror("pread");
    return 0;
  }
  if ((data & (1ULL << 63)) == 0) {
    fprintf(stderr, "page %lx not present: %lx", virt_page, data);
    return 0;
  }
  return (data & ((1ULL << 55) - 1)) * 4096;
}


x86 assembly (clang, -O0):
<virtual_to_physical>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,0x2ee2(%rip)
jne    11a9 <virtual_to_physical+0x69>
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ec1(%rip)
cmp    $0x0,%eax
jg     11a4 <virtual_to_physical+0x64>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <virtual_to_physical+0x10a>
jmp    11a9 <virtual_to_physical+0x69>
mov    0x2e95(%rip),%edi
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%ecx
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <pread@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x4,%rax
je     11f0 <virtual_to_physical+0xb0>
lea    0xe41(%rip),%rdi
call   1050 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <virtual_to_physical+0x10a>
movslq -0x18(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1232 <virtual_to_physical+0xf2>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe05(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    124a <virtual_to_physical+0x10a>
movslq -0x18(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
shl    $0xc,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virtual_to_physical>:
push   %rbx
sub    $0x10,%rsp
lea    0xfff(%rdi),%ebx
test   %edi,%edi
cmovns %edi,%ebx
mov    0x2eee(%rip),%edi
test   %edi,%edi
jne    117b <virtual_to_physical+0x3b>
mov    0x2e87(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ecd(%rip)
test   %eax,%eax
jle    11d0 <virtual_to_physical+0x90>
sar    $0xc,%ebx
lea    0x0(,%rbx,4),%ecx
lea    0xc(%rsp),%rsi
mov    $0x4,%edx
call   1060 <pread@plt>
cmp    $0x4,%eax
jne    11c7 <virtual_to_physical+0x87>
mov    0xc(%rsp),%eax
test   %eax,%eax
js     11be <virtual_to_physical+0x7e>
mov    0x2e30(%rip),%rcx
mov    (%rcx),%edi
lea    0xe75(%rip),%rsi
mov    %ebx,%edx
mov    %eax,%ecx
call   1030 <fprintf@plt>
xor    %eax,%eax
jmp    11c1 <virtual_to_physical+0x81>
shl    $0xc,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe52(%rip),%rdi
jmp    11d7 <virtual_to_physical+0x97>
lea    0xe3c(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    11c1 <virtual_to_physical+0x81>

LLVM IR (-O0):
@virtual_to_physical.pagemap_fd = internal global i32 0
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open pagemap\00"
@.str.2 = [6 x i8] c"pread\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"page %lx not present: %lx\00"
define i32 @virtual_to_physical(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i32
%9 = sdiv i32 %8, 4096
store i32 %9, i32* %4
%10 = load i32, i32* @virtual_to_physical.pagemap_fd
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %19
12: ; preds = %1
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* @virtual_to_physical.pagemap_fd
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %2
br label %47
18: ; preds = %12
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* @virtual_to_physical.pagemap_fd
%21 = load i32, i32* %4
%22 = sext i32 %21 to i64
%23 = mul i64 %22, 4
%24 = trunc i64 %23 to i32
%25 = call i32 @pread(i32 %20, i32* %5, i32 4, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = icmp ne i64 %27, 4
br i1 %28, label %29, label %31
29: ; preds = %19
%30 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %47
31: ; preds = %19
%32 = load i32, i32* %5
%33 = sext i32 %32 to i64
%34 = and i64 %33, -9223372036854775808
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i32, i32* @stderr
%38 = load i32, i32* %4
%39 = load i32, i32* %5
%40 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %38, i32 %39)
store i32 0, i32* %2
br label %47
41: ; preds = %31
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = and i64 %43, 36028797018963967
%45 = mul i64 %44, 4096
%46 = trunc i64 %45 to i32
store i32 %46, i32* %2
br label %47
47: ; preds = %41, %36, %29, %16
%48 = load i32, i32* %2
ret i32 %48
}

LLVM IR (-O2):
@virtual_to_physical.pagemap_fd = internal unnamed_addr global i32 0
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [13 x i8] c"open pagemap\00"
@.str.2 = [6 x i8] c"pread\00"
@stderr = global i32 0
@.str.3 = [26 x i8] c"page %lx not present: %lx\00"
define i32 @virtual_to_physical(i8* %0) {
%2 = alloca i32
%3 = ptrtoint i8* %0 to i64
%4 = trunc i64 %3 to i32
%5 = sdiv i32 %4, 4096
%6 = load i32, i32* @virtual_to_physical.pagemap_fd
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* @virtual_to_physical.pagemap_fd
%11 = icmp slt i32 %10, 1
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %32
14: ; preds = %8, %1
%15 = phi i32 [ %10, %8 ], [ %6, %1 ]
%16 = bitcast i32* %2 to i8*
%17 = shl nsw i32 %5, 2
%18 = call i32 @pread(i32 %15, i32* %2, i32 4, i32 %17)
%19 = icmp eq i32 %18, 4
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %30
22: ; preds = %14
%23 = load i32, i32* %2
%24 = icmp sgt i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %5, i32 %23)
br label %30
28: ; preds = %22
%29 = shl i32 %23, 12
br label %30
30: ; preds = %28, %25, %20
%31 = phi i32 [ 0, %20 ], [ 0, %25 ], [ %29, %28 ]
br label %32
32: ; preds = %30, %12
%33 = phi i32 [ 0, %12 ], [ %31, %30 ]
ret i32 %33
}