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

/* Variables and functions */
int get_kpageflags(int);
int get_pageflags(unsigned long);
unsigned long getpagesize();
int /*<<< orphan*/ is_vma_lock_on_fault(unsigned long);
int /*<<< orphan*/ printf(char *);
int onfault_check(char *map) {
  unsigned long page_size = getpagesize();
  uint64_t page1_flags, page2_flags;
  page1_flags = get_pageflags((unsigned long)map);
  page2_flags = get_pageflags((unsigned long)map + page_size);
  if ((page1_flags & 0x8000000000000000ULL) ||
      (page2_flags & 0x8000000000000000ULL)) {
    printf("Pages were made present by MLOCK_ONFAULT\n");
    return 1;
  }
  *map = 'a';
  page1_flags = get_pageflags((unsigned long)map);
  page2_flags = get_pageflags((unsigned long)map + page_size);
  if ((page1_flags & 0x8000000000000000ULL) == 0) {
    printf("Page 1 is not present after fault\n");
    return 1;
  } else if (page2_flags & 0x8000000000000000ULL) {
    printf("Page 2 was made present\n");
    return 1;
  }
  page1_flags = get_kpageflags(page1_flags & 0x007FFFFFFFFFFFFFULL);
  if ((page1_flags & (1UL << 18)) == 0) {
    printf("Failed to make faulted page unevictable\n");
    return 1;
  }
  if (!is_vma_lock_on_fault((unsigned long)map)) {
    printf("VMA is not marked for lock on fault\n");
    return 1;
  }
  if (!is_vma_lock_on_fault((unsigned long)map + page_size)) {
    printf("VMA is not marked for lock on fault\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<onfault_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <get_pageflags@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1040 <get_pageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    11b9 <onfault_check+0x69>
movslq -0x20(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     11d1 <onfault_check+0x81>
lea    0xe40(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
mov    -0x10(%rbp),%rax
movb   $0x61,(%rax)
mov    -0x10(%rbp),%rdi
call   1040 <get_pageflags@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1040 <get_pageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
jne    1227 <onfault_check+0xd7>
lea    0xe14(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
movslq -0x20(%rbp),%rax
movabs $0x8000000000000000,%rcx
and    %rcx,%rax
cmp    $0x0,%rax
je     125a <onfault_check+0x10a>
lea    0xe04(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
jmp    125f <onfault_check+0x10f>
movslq -0x1c(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   1060 <get_kpageflags@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
and    $0x40000,%rax
cmp    $0x0,%rax
jne    12a6 <onfault_check+0x156>
lea    0xdd1(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
mov    -0x10(%rbp),%rdi
call   1050 <is_vma_lock_on_fault@plt>
cmp    $0x0,%eax
jne    12d0 <onfault_check+0x180>
lea    0xdd0(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1050 <is_vma_lock_on_fault@plt>
cmp    $0x0,%eax
jne    12fe <onfault_check+0x1ae>
lea    0xda2(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1305 <onfault_check+0x1b5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<onfault_check>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <get_pageflags@plt>
mov    %eax,%ebp
add    %r14,%rbx
mov    %rbx,%rdi
call   1040 <get_pageflags@plt>
lea    0xf06(%rip),%r15
test   %ebp,%ebp
js     11a9 <onfault_check+0x59>
test   %eax,%eax
js     11a9 <onfault_check+0x59>
movb   $0x61,(%r14)
mov    %r14,%rdi
call   1040 <get_pageflags@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1040 <get_pageflags@plt>
test   %ebp,%ebp
js     11c1 <onfault_check+0x71>
lea    0xebb(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
test   %eax,%eax
js     11dc <onfault_check+0x8c>
mov    %ebp,%edi
call   1060 <get_kpageflags@plt>
test   $0x40000,%eax
jne    11e5 <onfault_check+0x95>
lea    0xe4a(%rip),%r15
jmp    11a9 <onfault_check+0x59>
lea    0xe69(%rip),%r15
jmp    11a9 <onfault_check+0x59>
mov    %r14,%rdi
call   1050 <is_vma_lock_on_fault@plt>
lea    0xe0c(%rip),%r15
test   %eax,%eax
je     11a9 <onfault_check+0x59>
mov    %rbx,%rdi
call   1050 <is_vma_lock_on_fault@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    11b6 <onfault_check+0x66>
jmp    11a9 <onfault_check+0x59>

LLVM IR (-O0):
@.str = [42 x i8] c"Pages were made present by MLOCK_ONFAULT\0A\00"
@.str.1 = [35 x i8] c"Page 1 is not present after fault\0A\00"
@.str.2 = [25 x i8] c"Page 2 was made present\0A\00"
@.str.3 = [41 x i8] c"Failed to make faulted page unevictable\0A\00"
@.str.4 = [37 x i8] c"VMA is not marked for lock on fault\0A\00"
define i32 @onfault_check(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = call i64 (...) @getpagesize()
store i64 %7, i64* %4
%8 = load i8*, i8** %3
%9 = ptrtoint i8* %8 to i64
%10 = call i32 @get_pageflags(i64 %9)
store i32 %10, i32* %5
%11 = load i8*, i8** %3
%12 = ptrtoint i8* %11 to i64
%13 = load i64, i64* %4
%14 = add i64 %12, %13
%15 = call i32 @get_pageflags(i64 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %5
%17 = sext i32 %16 to i64
%18 = and i64 %17, -9223372036854775808
%19 = icmp ne i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %1
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = and i64 %22, -9223372036854775808
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20, %1
%26 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %80
27: ; preds = %20
%28 = load i8*, i8** %3
store i8 97, i8* %28
%29 = load i8*, i8** %3
%30 = ptrtoint i8* %29 to i64
%31 = call i32 @get_pageflags(i64 %30)
store i32 %31, i32* %5
%32 = load i8*, i8** %3
%33 = ptrtoint i8* %32 to i64
%34 = load i64, i64* %4
%35 = add i64 %33, %34
%36 = call i32 @get_pageflags(i64 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = and i64 %38, -9223372036854775808
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %27
%42 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %80
43: ; preds = %27
%44 = load i32, i32* %6
%45 = sext i32 %44 to i64
%46 = and i64 %45, -9223372036854775808
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %2
br label %80
50: ; preds = %43
br label %51
51: ; preds = %50
%52 = load i32, i32* %5
%53 = sext i32 %52 to i64
%54 = and i64 %53, 36028797018963967
%55 = trunc i64 %54 to i32
%56 = call i32 @get_kpageflags(i32 %55)
store i32 %56, i32* %5
%57 = load i32, i32* %5
%58 = sext i32 %57 to i64
%59 = and i64 %58, 262144
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %51
%62 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %2
br label %80
63: ; preds = %51
%64 = load i8*, i8** %3
%65 = ptrtoint i8* %64 to i64
%66 = call i32 @is_vma_lock_on_fault(i64 %65)
%67 = icmp ne i32 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %63
%69 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %80
70: ; preds = %63
%71 = load i8*, i8** %3
%72 = ptrtoint i8* %71 to i64
%73 = load i64, i64* %4
%74 = add i64 %72, %73
%75 = call i32 @is_vma_lock_on_fault(i64 %74)
%76 = icmp ne i32 %75, 0
br i1 %76, label %79, label %77
77: ; preds = %70
%78 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %2
br label %80
79: ; preds = %70
store i32 0, i32* %2
br label %80
80: ; preds = %79, %77, %68, %61, %48, %41, %25
%81 = load i32, i32* %2
ret i32 %81
}

LLVM IR (-O2):
@str.5 = [36 x i8] c"VMA is not marked for lock on fault\00"
@str.6 = [40 x i8] c"Failed to make faulted page unevictable\00"
@str.7 = [24 x i8] c"Page 2 was made present\00"
@str.8 = [34 x i8] c"Page 1 is not present after fault\00"
@str.9 = [41 x i8] c"Pages were made present by MLOCK_ONFAULT\00"
define i32 @onfault_check(i8* %0) {
%2 = tail call i64 (...) @getpagesize()
%3 = ptrtoint i8* %0 to i64
%4 = tail call i32 @get_pageflags(i64 %3)
%5 = add i64 %2, %3
%6 = tail call i32 @get_pageflags(i64 %5)
%7 = icmp sgt i32 %4, -1
%8 = icmp sgt i32 %6, -1
%9 = select i1 %7, i1 %8, i1 false
br i1 %9, label %10, label %26
10: ; preds = %1
store i8 97, i8* %0
%11 = tail call i32 @get_pageflags(i64 %3)
%12 = tail call i32 @get_pageflags(i64 %5)
%13 = icmp sgt i32 %11, -1
br i1 %13, label %26, label %14
14: ; preds = %10
%15 = icmp sgt i32 %12, -1
br i1 %15, label %16, label %26
16: ; preds = %14
%17 = tail call i32 @get_kpageflags(i32 %11)
%18 = and i32 %17, 262144
%19 = icmp eq i32 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %16
%21 = tail call i32 @is_vma_lock_on_fault(i64 %3)
%22 = icmp eq i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = tail call i32 @is_vma_lock_on_fault(i64 %5)
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23, %20, %16, %14, %10, %1
%27 = phi i8* [ getelementptr inbounds ([41 x i8], [41 x i8]* @str.9, i64 0, i64 0), %1 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @str.8, i64 0, i64 0), %10 ], [ getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0), %14 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str.6, i64 0, i64 0), %16 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0), %20 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @str.5, i64 0, i64 0), %23 ]
%28 = tail call i32 @puts(i8* dereferenceable(1) %27)
br label %29
29: ; preds = %26, %23
%30 = phi i32 [ 0, %23 ], [ 1, %26 ]
ret i32 %30
}