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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, uintptr_t, uintptr_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int pread(int, uintptr_t *, int, uintptr_t);
int /*<<< orphan*/ stderr;
uint64_t virtual_to_physical(uintptr_t *ptr) {
  uintptr_t virt_page;
  static int pagemap_fd;
  virt_page = ((uintptr_t)ptr) / 4096;
  if (pagemap_fd == 0) {
    if ((pagemap_fd = open("/proc/self/pagemap", O_RDONLY)) <= 0) {
      perror("open pagemap");
      return 0;
    }
  }
  uintptr_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    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
shr    $0xc,%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,0x2ee5(%rip)
jne    11a7 <virtual_to_physical+0x67>
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1040 <open@plt>
mov    %eax,0x2ec4(%rip)
cmp    $0x0,%eax
jg     11a2 <virtual_to_physical+0x62>
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1245 <virtual_to_physical+0x105>
jmp    11a7 <virtual_to_physical+0x67>
mov    0x2e97(%rip),%edi
mov    -0x18(%rbp),%rcx
shl    $0x3,%rcx
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1060 <pread@plt>
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    $0x8,%rax
je     11ed <virtual_to_physical+0xad>
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    1245 <virtual_to_physical+0x105>
movabs $0x8000000000000000,%rax
and    -0x20(%rbp),%rax
cmp    $0x0,%rax
jne    122f <virtual_to_physical+0xef>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe09(%rip),%rsi
call   1030 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1245 <virtual_to_physical+0x105>
movabs $0x7fffffffffffff,%rax
and    -0x20(%rbp),%rax
shl    $0xc,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virtual_to_physical>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2ef6(%rip),%edi
test   %edi,%edi
jne    1173 <virtual_to_physical+0x33>
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1040 <open@plt>
mov    %eax,%edi
mov    %eax,0x2ed5(%rip)
test   %eax,%eax
jle    11cf <virtual_to_physical+0x8f>
shr    $0xc,%rbx
lea    0x0(,%rbx,8),%rcx
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
call   1060 <pread@plt>
cmp    $0x8,%eax
jne    11c6 <virtual_to_physical+0x86>
mov    0x8(%rsp),%rax
test   %rax,%rax
js     11bc <virtual_to_physical+0x7c>
mov    0x2e34(%rip),%rcx
mov    (%rcx),%edi
lea    0xe79(%rip),%rsi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1030 <fprintf@plt>
xor    %eax,%eax
jmp    11c0 <virtual_to_physical+0x80>
shl    $0xc,%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe53(%rip),%rdi
jmp    11d6 <virtual_to_physical+0x96>
lea    0xe3d(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
jmp    11c0 <virtual_to_physical+0x80>

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 i64 @virtual_to_physical(i64* %0) {
%2 = alloca i64
%3 = alloca i64*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i32
store i64* %0, i64** %3
%7 = load i64*, i64** %3
%8 = ptrtoint i64* %7 to i64
%9 = udiv i64 %8, 4096
store i64 %9, i64* %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 i64 0, i64* %2
br label %42
18: ; preds = %12
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* @virtual_to_physical.pagemap_fd
%21 = load i64, i64* %4
%22 = mul i64 %21, 8
%23 = call i32 @pread(i32 %20, i64* %5, i32 8, i64 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = icmp ne i64 %25, 8
br i1 %26, label %27, label %29
27: ; preds = %19
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i64 0, i64* %2
br label %42
29: ; preds = %19
%30 = load i64, i64* %5
%31 = and i64 %30, -9223372036854775808
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = load i64, i64* %4
%36 = load i64, i64* %5
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %35, i64 %36)
store i64 0, i64* %2
br label %42
38: ; preds = %29
%39 = load i64, i64* %5
%40 = and i64 %39, 36028797018963967
%41 = mul i64 %40, 4096
store i64 %41, i64* %2
br label %42
42: ; preds = %38, %33, %27, %16
%43 = load i64, i64* %2
ret i64 %43
}

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 i64 @virtual_to_physical(i64* %0) {
%2 = alloca i64
%3 = ptrtoint i64* %0 to i64
%4 = lshr i64 %3, 12
%5 = load i32, i32* @virtual_to_physical.pagemap_fd
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %13
7: ; preds = %1
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* @virtual_to_physical.pagemap_fd
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %31
13: ; preds = %7, %1
%14 = phi i32 [ %9, %7 ], [ %5, %1 ]
%15 = bitcast i64* %2 to i8*
%16 = shl nuw nsw i64 %4, 3
%17 = call i32 @pread(i32 %14, i64* %2, i32 8, i64 %16)
%18 = icmp eq i32 %17, 8
br i1 %18, label %21, label %19
19: ; preds = %13
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %29
21: ; preds = %13
%22 = load i64, i64* %2
%23 = icmp sgt i64 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i64 %4, i64 %22)
br label %29
27: ; preds = %21
%28 = shl i64 %22, 12
br label %29
29: ; preds = %27, %24, %19
%30 = phi i64 [ 0, %19 ], [ 0, %24 ], [ %28, %27 ]
br label %31
31: ; preds = %29, %11
%32 = phi i64 [ 0, %11 ], [ %30, %29 ]
ret i64 %32
}