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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(unsigned long long *, int, int, int /*<<< orphan*/ *);
int lseek(int /*<<< orphan*/, int, int /*<<< orphan*/);
int sysconf(int /*<<< orphan*/);
intptr_t Mytop(uintptr_t vaddr) {
  FILE *pagemap;
  intptr_t paddr = 0;
  int offset = (vaddr / sysconf(_SC_PAGESIZE)) * sizeof(uint64_t);
  uint64_t e;
  if ((pagemap = fopen("/proc/self/pagemap", "r"))) {
    if (lseek(fileno(pagemap), offset, SEEK_SET) == offset) {
      if (fread(&e, sizeof(uint64_t), 1, pagemap)) {
        if (e & (1ULL << 63)) {
          paddr = e & ((1ULL << 54) - 1);
          paddr = paddr * sysconf(_SC_PAGESIZE);
          paddr = paddr | (vaddr & (sysconf(_SC_PAGESIZE) - 1));
        }
      }
    }
    fclose(pagemap);
  }
  return paddr;
}


x86 assembly (clang, -O0):
<Mytop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
xor    %edx,%edx
div    %rcx
shl    $0x3,%rax
mov    %eax,-0x1c(%rbp)
lea    0xe5a(%rip),%rdi
lea    0xe66(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12a2 <Mytop+0x142>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    -0x1c(%rbp),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    -0x1c(%rbp),%eax
jne    1299 <Mytop+0x139>
mov    -0x10(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     1294 <Mytop+0x134>
movabs $0x8000000000000000,%rax
and    -0x28(%rbp),%rax
cmp    $0x0,%rax
je     128f <Mytop+0x12f>
movabs $0x3fffffffffffff,%rax
and    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
imul   %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    -0x40(%rbp),%rcx
mov    %eax,%edx
mov    -0x38(%rbp),%rax
sub    $0x1,%edx
movslq %edx,%rdx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1294 <Mytop+0x134>
jmp    1299 <Mytop+0x139>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mytop>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2e71(%rip),%r12
mov    (%r12),%edi
call   1080 <sysconf@plt>
mov    %eax,%ecx
movslq %eax,%rsi
mov    %r14,%rax
or     %rsi,%rax
shr    $0x20,%rax
je     119e <Mytop+0x3e>
mov    %r14,%rax
xor    %edx,%edx
div    %rsi
mov    %rax,%rbx
jmp    11a7 <Mytop+0x47>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
mov    %eax,%ebx
lea    0xe52(%rip),%rdi
lea    0xe5e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1215 <Mytop+0xb5>
mov    %rax,%r15
shl    $0x3,%ebx
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    0x2e04(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    %ebx,%esi
call   1050 <lseek@plt>
cmp    %ebx,%eax
jne    1209 <Mytop+0xa9>
lea    0x8(%rsp),%rdi
mov    $0x8,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     1209 <Mytop+0xa9>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
js     1228 <Mytop+0xc8>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1217 <Mytop+0xb7>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
movabs $0x3fffffffffffff,%rax
and    %rax,%rbx
mov    (%r12),%edi
call   1080 <sysconf@plt>
movslq %eax,%r13
imul   %rbx,%r13
mov    (%r12),%edi
call   1080 <sysconf@plt>
add    $0xffffffff,%eax
movslq %eax,%rbx
and    %r14,%rbx
or     %r13,%rbx
jmp    120b <Mytop+0xab>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@.str = [19 x i8] c"/proc/self/pagemap\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i64 @Mytop(i64 %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store i64 %0, i64* %2
store i64 0, i64* %4
%7 = load i64, i64* %2
%8 = load i32, i32* @_SC_PAGESIZE
%9 = call i32 @sysconf(i32 %8)
%10 = sext i32 %9 to i64
%11 = udiv i64 %7, %10
%12 = mul i64 %11, 8
%13 = trunc i64 %12 to i32
store i32 %13, i32* %5
%14 = call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %53
16: ; preds = %1
%17 = load i32*, i32** %3
%18 = call i32 @fileno(i32* %17)
%19 = load i32, i32* %5
%20 = load i32, i32* @SEEK_SET
%21 = call i32 @lseek(i32 %18, i32 %19, i32 %20)
%22 = load i32, i32* %5
%23 = icmp eq i32 %21, %22
br i1 %23, label %24, label %50
24: ; preds = %16
%25 = load i32*, i32** %3
%26 = call i64 @fread(i64* %6, i32 8, i32 1, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %49
28: ; preds = %24
%29 = load i64, i64* %6
%30 = and i64 %29, -9223372036854775808
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %48
32: ; preds = %28
%33 = load i64, i64* %6
%34 = and i64 %33, 18014398509481983
store i64 %34, i64* %4
%35 = load i64, i64* %4
%36 = load i32, i32* @_SC_PAGESIZE
%37 = call i32 @sysconf(i32 %36)
%38 = sext i32 %37 to i64
%39 = mul nsw i64 %35, %38
store i64 %39, i64* %4
%40 = load i64, i64* %4
%41 = load i64, i64* %2
%42 = load i32, i32* @_SC_PAGESIZE
%43 = call i32 @sysconf(i32 %42)
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = and i64 %41, %45
%47 = or i64 %40, %46
store i64 %47, i64* %4
br label %48
48: ; preds = %32, %28
br label %49
49: ; preds = %48, %24
br label %50
50: ; preds = %49, %16
%51 = load i32*, i32** %3
%52 = call i32 @fclose(i32* %51)
br label %53
53: ; preds = %50, %1
%54 = load i64, i64* %4
ret i64 %54
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@.str = [19 x i8] c"/proc/self/pagemap\00"
@.str.1 = [2 x i8] c"r\00"
@SEEK_SET = global i32 0
define i64 @Mytop(i64 %0) {
%2 = alloca i64
%3 = load i32, i32* @_SC_PAGESIZE
%4 = tail call i32 @sysconf(i32 %3)
%5 = sext i32 %4 to i64
%6 = udiv i64 %0, %5
%7 = trunc i64 %6 to i32
%8 = shl i32 %7, 3
%9 = bitcast i64* %2 to i8*
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %38, label %12
12: ; preds = %1
%13 = tail call i32 @fileno(i32* %10)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %13, i32 %8, i32 %14)
%16 = icmp eq i32 %15, %8
br i1 %16, label %17, label %35
17: ; preds = %12
%18 = call i64 @fread(i64* %2, i32 8, i32 1, i32* %10)
%19 = icmp eq i64 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %17
%21 = load i64, i64* %2
%22 = icmp sgt i64 %21, -1
br i1 %22, label %35, label %23
23: ; preds = %20
%24 = and i64 %21, 18014398509481983
%25 = load i32, i32* @_SC_PAGESIZE
%26 = tail call i32 @sysconf(i32 %25)
%27 = sext i32 %26 to i64
%28 = mul nsw i64 %24, %27
%29 = load i32, i32* @_SC_PAGESIZE
%30 = tail call i32 @sysconf(i32 %29)
%31 = add nsw i32 %30, -1
%32 = sext i32 %31 to i64
%33 = and i64 %32, %0
%34 = or i64 %33, %28
br label %35
35: ; preds = %17, %23, %20, %12
%36 = phi i64 [ %34, %23 ], [ 0, %20 ], [ 0, %17 ], [ 0, %12 ]
%37 = tail call i32 @fclose(i32* %10)
br label %38
38: ; preds = %35, %1
%39 = phi i64 [ %36, %35 ], [ 0, %1 ]
ret i64 %39
}