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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ exit(int);
unsigned long getpagesize();
int lseek(int, unsigned long, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, unsigned long *, int);
void verify_page_fault(void *addr, unsigned long memory) {
  unsigned long psize, offset, npages, pfn, iterator;
  int fd;
  void *tmp;
  psize = getpagesize();
  npages = memory / psize;
  fd = open("/proc/self/pagemap", O_RDONLY);
  if (fd == -1) {
    perror("open() failed");
    exit(-1);
  }
  for (iterator = 0; iterator < npages; iterator++) {
    tmp = addr + iterator * psize;
    offset = ((unsigned long)tmp / psize) * 8;
    if (lseek(fd, offset, SEEK_SET) == -1) {
      perror("lseek() failed");
      exit(-1);
    }
    if (read(fd, &pfn, sizeof(pfn)) == -1) {
      perror("read() failed");
      exit(-1);
    }
    if (!((pfn >> 63) & (1UL))) {
      printf("Pfn bit is not set !! So Some pages are not faulted\n");
      exit(-1);
    }
  }
}


x86 assembly (clang, -O0):
<verify_page_fault>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
mov    %rax,-0x28(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
lea    0xe55(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    11d3 <verify_page_fault+0x63>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
movq   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12b7 <verify_page_fault+0x147>
mov    -0x8(%rbp),%rax
mov    -0x38(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x18(%rbp)
shl    $0x3,%rax
mov    %rax,-0x20(%rbp)
mov    -0x3c(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1245 <verify_page_fault+0xd5>
lea    0xdeb(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x3c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
jne    1275 <verify_page_fault+0x105>
lea    0xdca(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rax
shr    $0x3f,%rax
and    $0x1,%rax
cmp    $0x0,%rax
jne    12a1 <verify_page_fault+0x131>
lea    0xdac(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
jmp    12a6 <verify_page_fault+0x136>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11db <verify_page_fault+0x6b>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_page_fault>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %rax,%r14
mov    %r15,%rax
or     %r14,%rax
shr    $0x20,%rax
je     11a4 <verify_page_fault+0x34>
mov    %r15,%rax
xor    %edx,%edx
div    %r14
mov    %rax,%rbp
jmp    11ae <verify_page_fault+0x3e>
mov    %r15d,%eax
xor    %edx,%edx
div    %r14d
mov    %eax,%ebp
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     12a0 <verify_page_fault+0x130>
cmp    %r15,%r14
ja     125c <verify_page_fault+0xec>
mov    %eax,%r12d
cmp    $0x2,%rbp
mov    $0x1,%r13d
cmovae %rbp,%r13
xor    %ebp,%ebp
mov    %rsp,%r15
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rax
or     %r14,%rax
shr    $0x20,%rax
je     1210 <verify_page_fault+0xa0>
mov    %rbx,%rax
xor    %edx,%edx
div    %r14
jmp    1217 <verify_page_fault+0xa7>
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%eax
xor    %edx,%edx
div    %r14d
shl    $0x3,%rax
mov    0x2db6(%rip),%rcx
mov    (%rcx),%edx
mov    %r12d,%edi
mov    %rax,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     126b <verify_page_fault+0xfb>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x8,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1274 <verify_page_fault+0x104>
cmpq   $0x0,(%rsp)
jns    128a <verify_page_fault+0x11a>
add    $0x1,%rbp
add    %r14,%rbx
cmp    %r13,%rbp
jne    11f0 <verify_page_fault+0x80>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdaf(%rip),%rdi
jmp    127b <verify_page_fault+0x10b>
lea    0xdb5(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xdad(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xd6c(%rip),%rdi
jmp    127b <verify_page_fault+0x10b>

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@SEEK_SET = global i32 0
@.str.2 = [15 x i8] c"lseek() failed\00"
@.str.3 = [14 x i8] c"read() failed\00"
@.str.4 = [53 x i8] c"Pfn bit is not set !! So Some pages are not faulted\0A\00"
define void @verify_page_fault(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%12 = call i64 (...) @getpagesize()
store i64 %12, i64* %5
%13 = load i64, i64* %4
%14 = load i64, i64* %5
%15 = udiv i64 %13, %14
store i64 %15, i64* %7
%16 = load i32, i32* @O_RDONLY
%17 = call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %16)
store i32 %17, i32* %10
%18 = load i32, i32* %10
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %2
store i64 0, i64* %9
br label %24
24: ; preds = %63, %23
%25 = load i64, i64* %9
%26 = load i64, i64* %7
%27 = icmp ult i64 %25, %26
br i1 %27, label %28, label %66
28: ; preds = %24
%29 = load i8*, i8** %3
%30 = load i64, i64* %9
%31 = load i64, i64* %5
%32 = mul i64 %30, %31
%33 = getelementptr i8, i8* %29, i64 %32
store i8* %33, i8** %11
%34 = load i8*, i8** %11
%35 = ptrtoint i8* %34 to i64
%36 = load i64, i64* %5
%37 = udiv i64 %35, %36
%38 = mul i64 %37, 8
store i64 %38, i64* %6
%39 = load i32, i32* %10
%40 = load i64, i64* %6
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %39, i64 %40, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %28
%45 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 @exit(i32 -1)
unreachable
47: ; preds = %28
%48 = load i32, i32* %10
%49 = call i32 @read(i32 %48, i64* %8, i32 8)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %54
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %47
%55 = load i64, i64* %8
%56 = lshr i64 %55, 63
%57 = and i64 %56, 1
%58 = icmp ne i64 %57, 0
br i1 %58, label %62, label %59
59: ; preds = %54
%60 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0))
%61 = call i32 @exit(i32 -1)
unreachable
62: ; preds = %54
br label %63
63: ; preds = %62
%64 = load i64, i64* %9
%65 = add i64 %64, 1
store i64 %65, i64* %9
br label %24
66: ; preds = %24
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/self/pagemap\00"
@O_RDONLY = global i32 0
@.str.1 = [14 x i8] c"open() failed\00"
@SEEK_SET = global i32 0
@.str.2 = [15 x i8] c"lseek() failed\00"
@.str.3 = [14 x i8] c"read() failed\00"
@str = [52 x i8] c"Pfn bit is not set !! So Some pages are not faulted\00"
define void @verify_page_fault(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = tail call i64 (...) @getpagesize()
%6 = udiv i64 %1, %5
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %14, label %10
10: ; preds = %2
%11 = icmp ugt i64 %5, %1
br i1 %11, label %45, label %12
12: ; preds = %10
%13 = call i64 @llvm.umax.i64(i64 %6, i64 1)
br label %19
14: ; preds = %2
%15 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %38
%18 = icmp eq i64 %41, %13
br i1 %18, label %45, label %19
19: ; preds = %12, %17
%20 = phi i64 [ %41, %17 ], [ 0, %12 ]
%21 = mul i64 %20, %5
%22 = getelementptr i8, i8* %0, i64 %21
%23 = ptrtoint i8* %22 to i64
%24 = udiv i64 %23, %5
%25 = shl i64 %24, 3
%26 = load i32, i32* @SEEK_SET
%27 = tail call i32 @lseek(i32 %8, i64 %25, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 -1)
unreachable
32: ; preds = %19
%33 = call i32 @read(i32 %8, i64* %3, i32 8)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%37 = tail call i32 @exit(i32 -1)
unreachable
38: ; preds = %32
%39 = load i64, i64* %3
%40 = icmp sgt i64 %39, -1
%41 = add nuw i64 %20, 1
br i1 %40, label %42, label %17
42: ; preds = %38
%43 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0))
%44 = tail call i32 @exit(i32 -1)
unreachable
45: ; preds = %17, %10
ret void
}