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

/* 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;
unsigned long getpagesize();
scalar_t__ mlock2_(char *, int, int);
char *mmap(int /*<<< orphan*/ *, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
scalar_t__ munlock(char *, int);
int /*<<< orphan*/ munmap(char *, int);
scalar_t__ onfault_check(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int unlock_onfault_check(char *);
int test_mlock_onfault() {
  char *map;
  int ret = 1;
  unsigned long page_size = getpagesize();
  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;
  }
  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;
  }
  if (onfault_check(map))
    goto unmap;
  if (munlock(map, 2 * page_size)) {
    if (errno == ENOSYS) {
      printf("Cannot call new mlock family, skipping test\n");
      _exit(0);
    }
    perror("munlock()");
    goto unmap;
  }
  ret = unlock_onfault_check(map);
unmap:
  munmap(map, 2 * page_size);
out:
  return ret;
}


x86 assembly (clang, -O0):
<test_mlock_onfault>:
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   1060 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <test_mlock_onfault+0x80>
lea    0xdea(%rip),%rdi
call   10a0 <perror@plt>
jmp    1312 <test_mlock_onfault+0x172>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
mov    $0x1,%edx
call   1080 <mlock2_@plt>
cmp    $0x0,%rax
je     1280 <test_mlock_onfault+0xe0>
mov    0x2d7f(%rip),%rax
mov    (%rax),%rax
mov    0x2d8d(%rip),%rcx
cmp    (%rcx),%rax
jne    126f <test_mlock_onfault+0xcf>
lea    0xdb4(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   1040 <_exit@plt>
lea    0xdce(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ff <test_mlock_onfault+0x15f>
mov    -0x8(%rbp),%rdi
call   1050 <onfault_check@plt>
cmp    $0x0,%rax
je     1298 <test_mlock_onfault+0xf8>
jmp    12ff <test_mlock_onfault+0x15f>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
call   1030 <munlock@plt>
cmp    $0x0,%rax
je     12f3 <test_mlock_onfault+0x153>
mov    0x2d0c(%rip),%rax
mov    (%rax),%rax
mov    0x2d1a(%rip),%rcx
cmp    (%rcx),%rax
jne    12e2 <test_mlock_onfault+0x142>
lea    0xd41(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
call   1040 <_exit@plt>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
jmp    12ff <test_mlock_onfault+0x15f>
mov    -0x8(%rbp),%rdi
call   10b0 <unlock_onfault_check@plt>
mov    %eax,-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_mlock_onfault>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10c0 <getpagesize@plt>
mov    %rax,%r14
add    %r14d,%r14d
mov    0x2e40(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e0e(%rip),%rax
mov    0x2de7(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14d,%esi
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1070 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc1(%rip),%rax
cmp    (%rax),%rbx
je     1247 <test_mlock_onfault+0xa7>
mov    $0x1,%ebp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
call   1080 <mlock2_@plt>
test   %rax,%rax
jne    125a <test_mlock_onfault+0xba>
mov    %rbx,%rdi
call   1050 <onfault_check@plt>
test   %rax,%rax
jne    1235 <test_mlock_onfault+0x95>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <munlock@plt>
test   %rax,%rax
jne    1283 <test_mlock_onfault+0xe3>
mov    %rbx,%rdi
call   10b0 <unlock_onfault_check@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %r14d,%esi
call   1090 <munmap@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    1240 <test_mlock_onfault+0xa0>
mov    0x2d67(%rip),%rax
mov    (%rax),%rax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%rax
je     12a7 <test_mlock_onfault+0x107>
lea    0xda0(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    1235 <test_mlock_onfault+0x95>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    0x2d4c(%rip),%rcx
cmp    (%rcx),%rax
je     12a7 <test_mlock_onfault+0x107>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1235 <test_mlock_onfault+0x95>
lea    0xd89(%rip),%rdi
call   1060 <puts@plt>
xor    %edi,%edi
call   1040 <_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 = [10 x i8] c"munlock()\00"
define i32 @test_mlock_onfault() {
%1 = alloca i8*
%2 = alloca i32
%3 = alloca i64
store i32 1, i32* %2
%4 = call i64 (...) @getpagesize()
store i64 %4, i64* %3
%5 = load i64, i64* %3
%6 = mul i64 2, %5
%7 = trunc i64 %6 to i32
%8 = load i32, i32* @PROT_READ
%9 = load i32, i32* @PROT_WRITE
%10 = or i32 %8, %9
%11 = load i32, i32* @MAP_ANONYMOUS
%12 = load i32, i32* @MAP_PRIVATE
%13 = or i32 %11, %12
%14 = call i8* @mmap(i32* null, i32 %7, i32 %10, i32 %13, i32 0, i32 0)
store i8* %14, i8** %1
%15 = load i8*, i8** %1
%16 = load i8*, i8** @MAP_FAILED
%17 = icmp eq i8* %15, %16
br i1 %17, label %18, label %20
18: ; preds = %0
%19 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %66
20: ; preds = %0
%21 = load i8*, i8** %1
%22 = load i64, i64* %3
%23 = mul i64 2, %22
%24 = trunc i64 %23 to i32
%25 = call i64 @mlock2_(i8* %21, i32 %24, i32 1)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %36
27: ; preds = %20
%28 = load i64, i64* @errno
%29 = load i64, i64* @ENOSYS
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @_exit(i32 0)
unreachable
34: ; preds = %27
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %60
36: ; preds = %20
%37 = load i8*, i8** %1
%38 = call i64 @onfault_check(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %60
41: ; preds = %36
%42 = load i8*, i8** %1
%43 = load i64, i64* %3
%44 = mul i64 2, %43
%45 = trunc i64 %44 to i32
%46 = call i64 @munlock(i8* %42, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %57
48: ; preds = %41
%49 = load i64, i64* @errno
%50 = load i64, i64* @ENOSYS
%51 = icmp eq i64 %49, %50
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%54 = call i32 @_exit(i32 0)
unreachable
55: ; preds = %48
%56 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %60
57: ; preds = %41
%58 = load i8*, i8** %1
%59 = call i32 @unlock_onfault_check(i8* %58)
store i32 %59, i32* %2
br label %60
60: ; preds = %57, %55, %40, %34
%61 = load i8*, i8** %1
%62 = load i64, i64* %3
%63 = mul i64 2, %62
%64 = trunc i64 %63 to i32
%65 = call i32 @munmap(i8* %61, i32 %64)
br label %66
66: ; preds = %60, %18
%67 = load i32, i32* %2
ret i32 %67
}

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.3 = [10 x i8] c"munlock()\00"
@str.4 = [44 x i8] c"Cannot call new mlock family, skipping test\00"
define i32 @test_mlock_onfault() {
%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 %47
15: ; preds = %0
%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.4, 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 %44
27: ; preds = %15
%28 = tail call i64 @onfault_check(i8* %10)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %44
30: ; preds = %27
%31 = tail call i64 @munlock(i8* %10, i32 %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %42, label %33
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOSYS
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.4, i64 0, i64 0))
%39 = tail call i32 @_exit(i32 0)
unreachable
40: ; preds = %33
%41 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %44
42: ; preds = %30
%43 = tail call i32 @unlock_onfault_check(i8* %10)
br label %44
44: ; preds = %27, %42, %40, %25
%45 = phi i32 [ 1, %25 ], [ 1, %27 ], [ 1, %40 ], [ %43, %42 ]
%46 = tail call i32 @munmap(i8* %10, i32 %3)
br label %47
47: ; preds = %44, %13
%48 = phi i32 [ 1, %13 ], [ %45, %44 ]
ret i32 %48
}