#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();
scalar_t__ is_vma_lock_on_fault(unsigned long);
int /*<<< orphan*/ printf(char *);
int unlock_onfault_check(char *map) {
  unsigned long page_size = getpagesize();
  uint64_t page1_flags;
  page1_flags = get_pageflags((unsigned long)map);
  page1_flags = get_kpageflags(page1_flags & 0x007FFFFFFFFFFFFF);
  if (page1_flags & (1UL << 18)) {
    printf("Page 1 is still marked unevictable after unlock\n");
    return 1;
  }
  if (is_vma_lock_on_fault((unsigned long)map) ||
      is_vma_lock_on_fault((unsigned long)map + page_size)) {
    printf("VMA is still lock on fault after unlock\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<unlock_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)
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
je     11ba <unlock_onfault_check+0x6a>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1203 <unlock_onfault_check+0xb3>
mov    -0x10(%rbp),%rdi
call   1050 <is_vma_lock_on_fault@plt>
cmp    $0x0,%rax
jne    11e4 <unlock_onfault_check+0x94>
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1050 <is_vma_lock_on_fault@plt>
cmp    $0x0,%rax
je     11fc <unlock_onfault_check+0xac>
lea    0xe46(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1203 <unlock_onfault_check+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unlock_onfault_check>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
xor    %eax,%eax
call   1070 <getpagesize@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <get_pageflags@plt>
mov    %eax,%edi
call   1060 <get_kpageflags@plt>
test   $0x40000,%eax
jne    11a0 <unlock_onfault_check+0x50>
mov    %rbx,%rdi
call   1050 <is_vma_lock_on_fault@plt>
lea    0xe79(%rip),%r15
test   %rax,%rax
jne    11a7 <unlock_onfault_check+0x57>
add    %rbx,%r14
mov    %r14,%rdi
call   1050 <is_vma_lock_on_fault@plt>
test   %rax,%rax
jne    11a7 <unlock_onfault_check+0x57>
xor    %eax,%eax
jmp    11b4 <unlock_onfault_check+0x64>
lea    0xe81(%rip),%r15
mov    %r15,%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [49 x i8] c"Page 1 is still marked unevictable after unlock\0A\00"
@.str.1 = [41 x i8] c"VMA is still lock on fault after unlock\0A\00"
define i32 @unlock_onfault_check(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i64 (...) @getpagesize()
store i64 %6, i64* %4
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i64
%9 = call i32 @get_pageflags(i64 %8)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = sext i32 %10 to i64
%12 = and i64 %11, 36028797018963967
%13 = trunc i64 %12 to i32
%14 = call i32 @get_kpageflags(i32 %13)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = sext i32 %15 to i64
%17 = and i64 %16, 262144
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %2
br label %36
21: ; preds = %1
%22 = load i8*, i8** %3
%23 = ptrtoint i8* %22 to i64
%24 = call i64 @is_vma_lock_on_fault(i64 %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %33, label %26
26: ; preds = %21
%27 = load i8*, i8** %3
%28 = ptrtoint i8* %27 to i64
%29 = load i64, i64* %4
%30 = add i64 %28, %29
%31 = call i64 @is_vma_lock_on_fault(i64 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %26, %21
%34 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %2
br label %36
35: ; preds = %26
store i32 0, i32* %2
br label %36
36: ; preds = %35, %33, %19
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@str = [40 x i8] c"VMA is still lock on fault after unlock\00"
@str.2 = [48 x i8] c"Page 1 is still marked unevictable after unlock\00"
define i32 @unlock_onfault_check(i8* %0) {
%2 = tail call i64 (...) @getpagesize()
%3 = ptrtoint i8* %0 to i64
%4 = tail call i32 @get_pageflags(i64 %3)
%5 = tail call i32 @get_kpageflags(i32 %4)
%6 = and i32 %5, 262144
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %1
%9 = tail call i64 @is_vma_lock_on_fault(i64 %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = add i64 %2, %3
%13 = tail call i64 @is_vma_lock_on_fault(i64 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %8, %11, %1
%16 = phi i8* [ getelementptr inbounds ([48 x i8], [48 x i8]* @str.2, i64 0, i64 0), %1 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0), %11 ], [ getelementptr inbounds ([40 x i8], [40 x i8]* @str, i64 0, i64 0), %8 ]
%17 = tail call i32 @puts(i8* dereferenceable(1) %16)
br label %18
18: ; preds = %15, %11
%19 = phi i32 [ 0, %11 ], [ 1, %15 ]
ret i32 %19
}