#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*/ ptbits;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
uint64_t virt_to_phys(void *vmem) {
  int pagemap_fd;
  uint64_t pfn, ptbits, physaddr, seek_loc;
  pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
  if (pagemap_fd < 0) {
    printf("pagemap open error\n");
    exit(1);
  }
  pfn = ((uint64_t)(vmem)) / (4 << 10);
  seek_loc = pfn * sizeof(uint64_t);
  if (lseek(pagemap_fd, seek_loc, SEEK_SET) != seek_loc) {
    printf("lseek to 0x%lx error\n", seek_loc);
    exit(1);
  }
  if (read(pagemap_fd, &ptbits, sizeof(ptbits)) != sizeof(ptbits)) {
    printf("read ptbits\n");
    exit(1);
  }
  physaddr = (ptbits & 0x7fffffffffffffULL) * (4 << 10);
  close(pagemap_fd);
  return physaddr;
}


x86 assembly (clang, -O0):
<virt_to_phys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xe84(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a6 <virt_to_phys+0x46>
lea    0xe7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rax
mov    $0x1000,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
movslq -0x10(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
mov    0x2e0b(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    -0x1c(%rbp),%eax
je     11f8 <virt_to_phys+0x98>
mov    -0x1c(%rbp),%esi
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1060 <read@plt>
cltq
cmp    $0x4,%rax
je     122d <virt_to_phys+0xcd>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movslq -0x14(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
shl    $0xc,%rax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<virt_to_phys>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0xe75(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11f0 <virt_to_phys+0x80>
mov    %eax,%r14d
lea    0xfff(%rbx),%ebp
test   %ebx,%ebx
cmovns %ebx,%ebp
sar    $0xc,%ebp
shl    $0x2,%ebp
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %ebp,%esi
call   1050 <lseek@plt>
cmp    %ebp,%eax
jne    11f9 <virt_to_phys+0x89>
lea    0xc(%rsp),%rsi
mov    %r14d,%edi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x4,%eax
jne    1213 <virt_to_phys+0xa3>
mov    0xc(%rsp),%ebx
shl    $0xc,%ebx
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe3e(%rip),%rdi
jmp    121a <virt_to_phys+0xaa>
lea    0xe13(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe0f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [20 x i8] c"pagemap open error\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [22 x i8] c"lseek to 0x%lx error\0A\00"
@.str.3 = [13 x i8] c"read ptbits\0A\00"
define i32 @virt_to_phys(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %3
%10 = load i32, i32* %3
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load i8*, i8** %2
%17 = ptrtoint i8* %16 to i32
%18 = sdiv i32 %17, 4096
store i32 %18, i32* %4
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 4
%22 = trunc i64 %21 to i32
store i32 %22, i32* %7
%23 = load i32, i32* %3
%24 = load i32, i32* %7
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %23, i32 %24, i32 %25)
%27 = load i32, i32* %7
%28 = icmp ne i32 %26, %27
br i1 %28, label %29, label %33
29: ; preds = %15
%30 = load i32, i32* %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %15
%34 = load i32, i32* %3
%35 = call i32 @read(i32 %34, i32* %5, i32 4)
%36 = sext i32 %35 to i64
%37 = icmp ne i64 %36, 4
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %33
%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* %6
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@SEEK_SET = global i32 0
@.str.2 = [22 x i8] c"lseek to 0x%lx error\0A\00"
@str = [12 x i8] c"read ptbits\00"
@str.4 = [19 x i8] c"pagemap open error\00"
define i32 @virt_to_phys(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.4, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = ptrtoint i8* %0 to i64
%12 = trunc i64 %11 to i32
%13 = sdiv i32 %12, 4096
%14 = shl nsw i32 %13, 2
%15 = load i32, i32* @SEEK_SET
%16 = tail call i32 @lseek(i32 %5, i32 %14, i32 %15)
%17 = icmp eq i32 %16, %14
br i1 %17, label %21, label %18
18: ; preds = %10
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = call i32 @read(i32 %5, i32* %2, i32 4)
%23 = icmp eq i32 %22, 4
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = load i32, i32* %2
%29 = shl i32 %28, 12
%30 = tail call i32 @close(i32 %5)
ret i32 %29
}