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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, unsigned long, int /*<<< orphan*/);
unsigned long getpagesize();
int /*<<< orphan*/ perror(char *);
uint64_t get_pageflags(unsigned long addr) {
  FILE *file;
  uint64_t pfn;
  unsigned long offset;
  file = fopen("/proc/self/pagemap", "r");
  if (!file) {
    perror("fopen pagemap");
    _exit(1);
  }
  offset = addr / getpagesize() * sizeof(pfn);
  if (fseek(file, offset, SEEK_SET)) {
    perror("fseek pagemap");
    _exit(1);
  }
  if (fread(&pfn, sizeof(pfn), 1, file) != 1) {
    perror("fread pagemap");
    _exit(1);
  }
  fclose(file);
  return pfn;
}


x86 assembly (clang, -O0):
<get_pageflags>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe7d(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b4 <get_pageflags+0x44>
lea    0xe70(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1090 <getpagesize@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
xor    %edx,%edx
div    %rcx
shl    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
call   1060 <fseek@plt>
cmp    $0x0,%rax
je     120d <get_pageflags+0x9d>
lea    0xe25(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rcx
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
cmp    $0x1,%eax
je     1243 <get_pageflags+0xd3>
lea    0xdfd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_pageflags>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xe82(%rip),%rdi
lea    0xe8e(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1203 <get_pageflags+0x93>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <getpagesize@plt>
mov    %rax,%rcx
or     %r14,%rax
shr    $0x20,%rax
je     11af <get_pageflags+0x3f>
mov    %r14,%rax
xor    %edx,%edx
div    %rcx
jmp    11b6 <get_pageflags+0x46>
mov    %r14d,%eax
xor    %edx,%edx
div    %ecx
shl    $0x2,%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%edx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <fseek@plt>
test   %rax,%rax
jne    120c <get_pageflags+0x9c>
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1215 <get_pageflags+0xa5>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe0b(%rip),%rdi
jmp    121c <get_pageflags+0xac>
lea    0xe10(%rip),%rdi
jmp    121c <get_pageflags+0xac>
lea    0xe15(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"fopen pagemap\00"
@SEEK_SET = global i32 0
@.str.3 = [14 x i8] c"fseek pagemap\00"
@.str.4 = [14 x i8] c"fread pagemap\00"
define i32 @get_pageflags(i64 %0) {
%2 = alloca i64
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i64
store i64 %0, i64* %2
%6 = 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* %6, i32** %3
%7 = load i32*, i32** %3
%8 = icmp ne i32* %7, null
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%11 = call i32 @_exit(i32 1)
unreachable
12: ; preds = %1
%13 = load i64, i64* %2
%14 = call i64 (...) @getpagesize()
%15 = udiv i64 %13, %14
%16 = mul i64 %15, 4
store i64 %16, i64* %5
%17 = load i32*, i32** %3
%18 = load i64, i64* %5
%19 = load i32, i32* @SEEK_SET
%20 = call i64 @fseek(i32* %17, i64 %18, i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @_exit(i32 1)
unreachable
25: ; preds = %12
%26 = load i32*, i32** %3
%27 = call i32 @fread(i32* %4, i32 4, i32 1, i32* %26)
%28 = icmp ne i32 %27, 1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @_exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32*, i32** %3
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %4
ret i32 %35
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"fopen pagemap\00"
@SEEK_SET = global i32 0
@.str.3 = [14 x i8] c"fseek pagemap\00"
@.str.4 = [14 x i8] c"fread pagemap\00"
define i32 @get_pageflags(i64 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%8 = tail call i32 @_exit(i32 1)
unreachable
9: ; preds = %1
%10 = tail call i64 (...) @getpagesize()
%11 = udiv i64 %0, %10
%12 = shl i64 %11, 2
%13 = load i32, i32* @SEEK_SET
%14 = tail call i64 @fseek(i32* %4, i64 %12, i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%18 = tail call i32 @_exit(i32 1)
unreachable
19: ; preds = %9
%20 = call i32 @fread(i32* %2, i32 4, i32 1, i32* %4)
%21 = icmp eq i32 %20, 1
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%24 = tail call i32 @_exit(i32 1)
unreachable
25: ; preds = %19
%26 = tail call i32 @fclose(i32* %4)
%27 = load i32, i32* %2
ret i32 %27
}