#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 */
scalar_t__ ENOSYS;
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_PRIVATE;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
scalar_t__ errno;
int get_kpageflags(int);
int get_pageflags(unsigned long);
unsigned long getpagesize();
int /*<<< orphan*/ is_vma_lock_on_fault(unsigned long);
scalar_t__ mlock2_(char *, int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int test_lock_onfault_of_present() {
  char *map;
  int ret = 1;
  unsigned long page_size = getpagesize();
  uint64_t page1_flags, page2_flags;
  map = mmap(NULL, 2 * page_size, PROT_READ | PROT_WRITE,
             MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  if (map == MAP_FAILED) {
    perror("test_mlock_locked mmap");
    goto out;
  }
  *map = 'a';
  if (mlock2_(map, 2 * page_size, 1)) {
    if (errno == ENOSYS) {
      printf("Cannot call new mlock family, skipping test\n");
      _exit(0);
    }
    perror("mlock2(MLOCK_ONFAULT)");
    goto unmap;
  }
  page1_flags = get_pageflags((unsigned long)map);
  page2_flags = get_pageflags((unsigned long)map + page_size);
  page1_flags = get_kpageflags(page1_flags & 0x007FFFFFFFFFFFFFULL);
  page2_flags = get_kpageflags(page2_flags & 0x007FFFFFFFFFFFFFULL);
  if ((page1_flags & (1UL << 18)) == 0) {
    printf("Failed to make present page unevictable\n");
    goto unmap;
  }
  if (!is_vma_lock_on_fault((unsigned long)map) ||
      !is_vma_lock_on_fault((unsigned long)map + page_size)) {
    printf("VMA with present pages is not marked lock on fault\n");
    goto unmap;
  }
  ret = 0;
unmap:
  munmap(map, 2 * page_size);
out:
  return ret;
}


x86 assembly (clang, -O0):
<test_lock_onfault_of_present>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0xc(%rbp)
mov    $0x0,%al
call   10c0 <getpagesize@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
mov    0x2e2d(%rip),%rax
mov    (%rax),%edx
mov    0x2dec(%rip),%rax
or     (%rax),%edx
mov    0x2dfb(%rip),%rax
mov    (%rax),%ecx
mov    0x2dd2(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
mov    %r9d,%r8d
call   1040 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <test_lock_onfault_of_present+0x80>
lea    0xdea(%rip),%rdi
call   10a0 <perror@plt>
jmp    1351 <test_lock_onfault_of_present+0x1b1>
mov    -0x8(%rbp),%rax
movb   $0x61,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
mov    $0x1,%edx
call   1070 <mlock2_@plt>
cmp    $0x0,%rax
je     1287 <test_lock_onfault_of_present+0xe7>
mov    0x2d78(%rip),%rax
mov    (%rax),%rax
mov    0x2d86(%rip),%rcx
cmp    (%rcx),%rax
jne    1276 <test_lock_onfault_of_present+0xd6>
lea    0xdad(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   1030 <_exit@plt>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
jmp    133e <test_lock_onfault_of_present+0x19e>
mov    -0x8(%rbp),%rdi
call   1060 <get_pageflags@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1060 <get_pageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   10b0 <get_kpageflags@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x20(%rbp),%rax
movabs $0x7fffffffffffff,%rcx
and    %rcx,%rax
mov    %eax,%edi
call   10b0 <get_kpageflags@plt>
mov    %eax,-0x20(%rbp)
movslq -0x1c(%rbp),%rax
and    $0x40000,%rax
cmp    $0x0,%rax
jne    12fe <test_lock_onfault_of_present+0x15e>
lea    0xd66(%rip),%rdi
call   1050 <printf@plt>
jmp    133e <test_lock_onfault_of_present+0x19e>
mov    -0x8(%rbp),%rdi
call   1080 <is_vma_lock_on_fault@plt>
cmp    $0x0,%eax
je     1326 <test_lock_onfault_of_present+0x186>
mov    -0x8(%rbp),%rdi
add    -0x18(%rbp),%rdi
call   1080 <is_vma_lock_on_fault@plt>
cmp    $0x0,%eax
jne    1337 <test_lock_onfault_of_present+0x197>
lea    0xd56(%rip),%rdi
call   1050 <printf@plt>
jmp    133e <test_lock_onfault_of_present+0x19e>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
call   1090 <munmap@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lock_onfault_of_present>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <getpagesize@plt>
mov    %rax,%r15
lea    (%rax,%rax,1),%r14d
mov    0x2e38(%rip),%rax
mov    0x2df9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e06(%rip),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2db9(%rip),%rax
cmp    (%rax),%rbx
je     129f <test_lock_onfault_of_present+0xff>
movb   $0x61,(%rbx)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
call   1070 <mlock2_@plt>
test   %rax,%rax
jne    12b2 <test_lock_onfault_of_present+0x112>
mov    %rbx,%rdi
call   1060 <get_pageflags@plt>
mov    %eax,%r12d
add    %rbx,%r15
mov    %r15,%rdi
call   1060 <get_pageflags@plt>
mov    %eax,%r13d
mov    %r12d,%edi
call   10b0 <get_kpageflags@plt>
mov    %eax,%ebp
mov    %r13d,%edi
call   10b0 <get_kpageflags@plt>
test   $0x40000,%ebp
jne    1258 <test_lock_onfault_of_present+0xb8>
lea    0xe0a(%rip),%rdi
jmp    1279 <test_lock_onfault_of_present+0xd9>
mov    %rbx,%rdi
call   1080 <is_vma_lock_on_fault@plt>
test   %eax,%eax
je     1272 <test_lock_onfault_of_present+0xd2>
mov    %r15,%rdi
call   1080 <is_vma_lock_on_fault@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    1283 <test_lock_onfault_of_present+0xe3>
lea    0xdb4(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <munmap@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    128e <test_lock_onfault_of_present+0xee>
mov    0x2d0f(%rip),%rax
mov    (%rax),%rax
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%rax
je     12d6 <test_lock_onfault_of_present+0x136>
lea    0xd48(%rip),%rdi
call   10a0 <perror@plt>
jmp    127e <test_lock_onfault_of_present+0xde>
lea    0xdab(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [23 x i8] c"test_mlock_locked mmap\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.1 = [45 x i8] c"Cannot call new mlock family, skipping test\0A\00"
@.str.2 = [22 x i8] c"mlock2(MLOCK_ONFAULT)\00"
@.str.3 = [41 x i8] c"Failed to make present page unevictable\0A\00"
@.str.4 = [52 x i8] c"VMA with present pages is not marked lock on fault\0A\00"
define i32 @test_lock_onfault_of_present() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
store i32 1, i32* %2
%6 = call i64 (...) @getpagesize()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = mul i64 2, %7
%9 = trunc i64 %8 to i32
%10 = load i32, i32* @PROT_READ
%11 = load i32, i32* @PROT_WRITE
%12 = or i32 %10, %11
%13 = load i32, i32* @MAP_ANONYMOUS
%14 = load i32, i32* @MAP_PRIVATE
%15 = or i32 %13, %14
%16 = call i8* @mmap(i32* null, i32 %9, i32 %12, i32 %15, i32 0, i32 0)
store i8* %16, i8** %1
%17 = load i8*, i8** %1
%18 = load i8*, i8** @MAP_FAILED
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %85
22: ; preds = %0
%23 = load i8*, i8** %1
store i8 97, i8* %23
%24 = load i8*, i8** %1
%25 = load i64, i64* %3
%26 = mul i64 2, %25
%27 = trunc i64 %26 to i32
%28 = call i64 @mlock2_(i8* %24, i32 %27, i32 1)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %39
30: ; preds = %22
%31 = load i64, i64* @errno
%32 = load i64, i64* @ENOSYS
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %37
34: ; preds = %30
%35 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%36 = call i32 @_exit(i32 0)
unreachable
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %79
39: ; preds = %22
%40 = load i8*, i8** %1
%41 = ptrtoint i8* %40 to i64
%42 = call i32 @get_pageflags(i64 %41)
store i32 %42, i32* %4
%43 = load i8*, i8** %1
%44 = ptrtoint i8* %43 to i64
%45 = load i64, i64* %3
%46 = add i64 %44, %45
%47 = call i32 @get_pageflags(i64 %46)
store i32 %47, i32* %5
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = and i64 %49, 36028797018963967
%51 = trunc i64 %50 to i32
%52 = call i32 @get_kpageflags(i32 %51)
store i32 %52, i32* %4
%53 = load i32, i32* %5
%54 = sext i32 %53 to i64
%55 = and i64 %54, 36028797018963967
%56 = trunc i64 %55 to i32
%57 = call i32 @get_kpageflags(i32 %56)
store i32 %57, i32* %5
%58 = load i32, i32* %4
%59 = sext i32 %58 to i64
%60 = and i64 %59, 262144
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %39
%63 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %79
64: ; preds = %39
%65 = load i8*, i8** %1
%66 = ptrtoint i8* %65 to i64
%67 = call i32 @is_vma_lock_on_fault(i64 %66)
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %76
69: ; preds = %64
%70 = load i8*, i8** %1
%71 = ptrtoint i8* %70 to i64
%72 = load i64, i64* %3
%73 = add i64 %71, %72
%74 = call i32 @is_vma_lock_on_fault(i64 %73)
%75 = icmp ne i32 %74, 0
br i1 %75, label %78, label %76
76: ; preds = %69, %64
%77 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.4, i64 0, i64 0))
br label %79
78: ; preds = %69
store i32 0, i32* %2
br label %79
79: ; preds = %78, %76, %62, %37
%80 = load i8*, i8** %1
%81 = load i64, i64* %3
%82 = mul i64 2, %81
%83 = trunc i64 %82 to i32
%84 = call i32 @munmap(i8* %80, i32 %83)
br label %85
85: ; preds = %79, %20
%86 = load i32, i32* %2
ret i32 %86
}

LLVM IR (-O2):
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_FAILED = global i8* null
@.str = [23 x i8] c"test_mlock_locked mmap\00"
@errno = global i64 0
@ENOSYS = global i64 0
@.str.2 = [22 x i8] c"mlock2(MLOCK_ONFAULT)\00"
@str = [51 x i8] c"VMA with present pages is not marked lock on fault\00"
@str.5 = [40 x i8] c"Failed to make present page unevictable\00"
@str.6 = [44 x i8] c"Cannot call new mlock family, skipping test\00"
define i32 @test_lock_onfault_of_present() {
%1 = tail call i64 (...) @getpagesize()
%2 = trunc i64 %1 to i32
%3 = shl i32 %2, 1
%4 = load i32, i32* @PROT_READ
%5 = load i32, i32* @PROT_WRITE
%6 = or i32 %5, %4
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = load i32, i32* @MAP_PRIVATE
%9 = or i32 %8, %7
%10 = tail call i8* @mmap(i32* null, i32 %3, i32 %6, i32 %9, i32 0, i32 0)
%11 = load i8*, i8** @MAP_FAILED
%12 = icmp eq i8* %10, %11
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %49
15: ; preds = %0
store i8 97, i8* %10
%16 = tail call i64 @mlock2_(i8* %10, i32 %3, i32 1)
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = load i64, i64* @errno
%20 = load i64, i64* @ENOSYS
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.6, i64 0, i64 0))
%24 = tail call i32 @_exit(i32 0)
unreachable
25: ; preds = %18
%26 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %46
27: ; preds = %15
%28 = ptrtoint i8* %10 to i64
%29 = tail call i32 @get_pageflags(i64 %28)
%30 = add i64 %1, %28
%31 = tail call i32 @get_pageflags(i64 %30)
%32 = tail call i32 @get_kpageflags(i32 %29)
%33 = tail call i32 @get_kpageflags(i32 %31)
%34 = and i32 %32, 262144
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %27
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.5, i64 0, i64 0))
br label %46
38: ; preds = %27
%39 = tail call i32 @is_vma_lock_on_fault(i64 %28)
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %38
%42 = tail call i32 @is_vma_lock_on_fault(i64 %30)
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41, %38
%45 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str, i64 0, i64 0))
br label %46
46: ; preds = %41, %44, %36, %25
%47 = phi i32 [ 1, %25 ], [ 1, %36 ], [ 1, %44 ], [ 0, %41 ]
%48 = tail call i32 @munmap(i8* %10, i32 %3)
br label %49
49: ; preds = %46, %13
%50 = phi i32 [ 1, %13 ], [ %47, %46 ]
ret i32 %50
}