#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 scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
long getpagesize();
scalar_t__ lseek(long, unsigned long, int /*<<< orphan*/);
long open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(long, int *, int);
unsigned long mem_addr_vir2phy(unsigned long vir) {
  long fd;
  long page_size = getpagesize();
  unsigned long vir_page_idx = vir / page_size;
  unsigned long pfn_item_offset = vir_page_idx * sizeof(uint64_t);
  uint64_t pfn_item;
  unsigned long phyaddr;
  fd = open("/proc/self/pagemap", O_RDONLY);
  if (fd < 0) {
    printf("open %s failed", "/proc/self/pagemap");
    return -1;
  }
  if ((off_t)-1 == lseek(fd, pfn_item_offset, SEEK_SET)) {
    printf("lseek %s failed", "/proc/self/pagemap");
    return -1;
  }
  if (sizeof(uint64_t) != read(fd, &pfn_item, sizeof(uint64_t))) {
    printf("read %s failed", "/proc/self/pagemap");
    return -1;
  }
  if (0 == (pfn_item & (((uint64_t)1) << 63))) {
    printf("page is not present");
    return -1;
  }
  phyaddr =
      (pfn_item & ((((uint64_t)1) << 55) - 1)) * page_size + vir % page_size;
  return phyaddr;
}


x86 assembly (clang, -O0):
<mem_addr_vir2phy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
shl    $0x2,%rax
mov    %rax,-0x30(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe6d(%rip),%rdi
call   1060 <open@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11c9 <mem_addr_vir2phy+0x79>
lea    0xe65(%rip),%rdi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ae <mem_addr_vir2phy+0x15e>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    %rax,%rcx
mov    $0xffffffffffffffff,%rax
cmp    %rcx,%rax
jne    1214 <mem_addr_vir2phy+0xc4>
lea    0xe29(%rip),%rdi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ae <mem_addr_vir2phy+0x15e>
mov    -0x18(%rbp),%rdi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
movslq %eax,%rcx
mov    $0x4,%eax
cmp    %rcx,%rax
je     1259 <mem_addr_vir2phy+0x109>
lea    0xdf4(%rip),%rdi
lea    0xdbb(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ae <mem_addr_vir2phy+0x15e>
and    -0x34(%rbp),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
jne    1281 <mem_addr_vir2phy+0x131>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12ae <mem_addr_vir2phy+0x15e>
and    -0x34(%rbp),%eax
cltq
imul   -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
mov    -0x48(%rbp),%rax
add    %rdx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mem_addr_vir2phy>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %rax,%r15
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1060 <open@plt>
test   %rax,%rax
js     1199 <mem_addr_vir2phy+0x49>
mov    %rax,%rbx
mov    %r15,%rax
or     %r14,%rax
shr    $0x20,%rax
je     11a2 <mem_addr_vir2phy+0x52>
mov    %r14,%rax
xor    %edx,%edx
div    %r15
jmp    11aa <mem_addr_vir2phy+0x5a>
lea    0xe73(%rip),%rdi
jmp    11ef <mem_addr_vir2phy+0x9f>
mov    %r14d,%eax
xor    %edx,%edx
div    %r15d
shl    $0x2,%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     11e8 <mem_addr_vir2phy+0x98>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
je     11fd <mem_addr_vir2phy+0xad>
lea    0xe4c(%rip),%rdi
jmp    11ef <mem_addr_vir2phy+0x9f>
lea    0xe33(%rip),%rdi
lea    0xe0a(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffffffffffff,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"open %s failed\00"
@SEEK_SET = global i32 0
@.str.2 = [16 x i8] c"lseek %s failed\00"
@.str.3 = [15 x i8] c"read %s failed\00"
@.str.4 = [20 x i8] c"page is not present\00"
define i64 @mem_addr_vir2phy(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
store i64 %0, i64* %3
%10 = call i64 (...) @getpagesize()
store i64 %10, i64* %5
%11 = load i64, i64* %3
%12 = load i64, i64* %5
%13 = udiv i64 %11, %12
store i64 %13, i64* %6
%14 = load i64, i64* %6
%15 = mul i64 %14, 4
store i64 %15, i64* %7
%16 = load i32, i32* @O_RDONLY
%17 = call i64 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %16)
store i64 %17, i64* %4
%18 = load i64, i64* %4
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %1
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %54
22: ; preds = %1
%23 = load i64, i64* %4
%24 = load i64, i64* %7
%25 = load i32, i32* @SEEK_SET
%26 = call i64 @lseek(i64 %23, i64 %24, i32 %25)
%27 = icmp eq i64 -1, %26
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %54
30: ; preds = %22
%31 = load i64, i64* %4
%32 = call i32 @read(i64 %31, i32* %8, i32 4)
%33 = sext i32 %32 to i64
%34 = icmp ne i64 4, %33
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i64 -1, i64* %2
br label %54
37: ; preds = %30
%38 = load i32, i32* %8
%39 = and i32 %38, poison
%40 = icmp eq i32 0, %39
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
store i64 -1, i64* %2
br label %54
43: ; preds = %37
%44 = load i32, i32* %8
%45 = and i32 %44, poison
%46 = sext i32 %45 to i64
%47 = load i64, i64* %5
%48 = mul nsw i64 %46, %47
%49 = load i64, i64* %3
%50 = load i64, i64* %5
%51 = urem i64 %49, %50
%52 = add i64 %48, %51
store i64 %52, i64* %9
%53 = load i64, i64* %9
store i64 %53, i64* %2
br label %54
54: ; preds = %43, %41, %35, %28, %20
%55 = load i64, i64* %2
ret i64 %55
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"open %s failed\00"
@SEEK_SET = global i32 0
@.str.2 = [16 x i8] c"lseek %s failed\00"
@.str.3 = [15 x i8] c"read %s failed\00"
define i64 @mem_addr_vir2phy(i64 %0) {
%2 = alloca i32
%3 = tail call i64 (...) @getpagesize()
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i64 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %17, label %8
8: ; preds = %1
%9 = udiv i64 %0, %3
%10 = shl i64 %9, 2
%11 = load i32, i32* @SEEK_SET
%12 = tail call i64 @lseek(i64 %6, i64 %10, i32 %11)
%13 = icmp eq i64 %12, -1
br i1 %13, label %17, label %14
14: ; preds = %8
%15 = call i32 @read(i64 %6, i32* %2, i32 4)
%16 = icmp eq i32 %15, 4
br i1 %16, label %20, label %17
17: ; preds = %14, %8, %1
%18 = phi i8* [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %1 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), %8 ], [ getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), %14 ]
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %18, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
ret i64 -1
}