#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__ lock_check(char *);
scalar_t__ mlock2_(char *, int, int /*<<< orphan*/);
char *mmap(int /*<<< orphan*/ *, int, int, int, int /*<<< orphan*/,
           int /*<<< orphan*/);
scalar_t__ munlock(char *, int);
int /*<<< orphan*/ munmap(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int unlock_lock_check(char *);
int test_mlock_lock() {
  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, 0)) {
    if (errno == ENOSYS) {
      printf("Cannot call new mlock family, skipping test\n");
      _exit(0);
    }
    perror("mlock2(0)");
    goto unmap;
  }
  if (lock_check(map))
    goto unmap;
  if (munlock(map, 2 * page_size)) {
    perror("munlock()");
    goto unmap;
  }
  ret = unlock_lock_check(map);
unmap:
  munmap(map, 2 * page_size);
out:
  return ret;
}


x86 assembly (clang, -O0):
<test_mlock_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x1,-0xc(%rbp)
mov    $0x0,%al
call   10b0 <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   1050 <mmap@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2daa(%rip),%rcx
cmp    (%rcx),%rax
jne    1220 <test_mlock_lock+0x80>
lea    0xdea(%rip),%rdi
call   10a0 <perror@plt>
jmp    12e2 <test_mlock_lock+0x142>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <mlock2_@plt>
cmp    $0x0,%rax
je     127d <test_mlock_lock+0xdd>
mov    0x2d82(%rip),%rax
mov    (%rax),%rax
mov    0x2d90(%rip),%rcx
cmp    (%rcx),%rax
jne    126c <test_mlock_lock+0xcc>
lea    0xdb7(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   1040 <_exit@plt>
lea    0xdd1(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cf <test_mlock_lock+0x12f>
mov    -0x8(%rbp),%rdi
call   10c0 <lock_check@plt>
cmp    $0x0,%rax
je     1295 <test_mlock_lock+0xf5>
jmp    12cf <test_mlock_lock+0x12f>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
shl    $0x1,%rax
mov    %eax,%esi
call   1030 <munlock@plt>
cmp    $0x0,%rax
je     12c3 <test_mlock_lock+0x123>
lea    0xd95(%rip),%rdi
call   10a0 <perror@plt>
jmp    12cf <test_mlock_lock+0x12f>
mov    -0x8(%rbp),%rdi
call   1080 <unlock_lock_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_lock>:
push   %rbp
push   %r14
push   %rbx
xor    %eax,%eax
call   10b0 <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   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2dc1(%rip),%rax
cmp    (%rax),%rbx
je     1244 <test_mlock_lock+0xa4>
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %edx,%edx
call   1070 <mlock2_@plt>
test   %rax,%rax
jne    1257 <test_mlock_lock+0xb7>
mov    %rbx,%rdi
call   10c0 <lock_check@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    1232 <test_mlock_lock+0x92>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1030 <munlock@plt>
test   %rax,%rax
jne    1280 <test_mlock_lock+0xe0>
mov    %rbx,%rdi
call   1080 <unlock_lock_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    0xdb5(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    123d <test_mlock_lock+0x9d>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rax
mov    0x2d78(%rip),%rcx
cmp    (%rcx),%rax
je     128e <test_mlock_lock+0xee>
lea    0xda3(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%ebp
jmp    1232 <test_mlock_lock+0x92>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
jmp    1232 <test_mlock_lock+0x92>
lea    0xd96(%rip),%rdi
call   1050 <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 = [10 x i8] c"mlock2(0)\00"
@.str.3 = [10 x i8] c"munlock()\00"
define i32 @test_mlock_lock() {
%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 %59
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 0)
%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 ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %53
36: ; preds = %20
%37 = load i8*, i8** %1
%38 = call i64 @lock_check(i8* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
br label %53
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 %50
48: ; preds = %41
%49 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %53
50: ; preds = %41
%51 = load i8*, i8** %1
%52 = call i32 @unlock_lock_check(i8* %51)
store i32 %52, i32* %2
br label %53
53: ; preds = %50, %48, %40, %34
%54 = load i8*, i8** %1
%55 = load i64, i64* %3
%56 = mul i64 2, %55
%57 = trunc i64 %56 to i32
%58 = call i32 @munmap(i8* %54, i32 %57)
br label %59
59: ; preds = %53, %18
%60 = load i32, i32* %2
ret i32 %60
}

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 = [10 x i8] c"mlock2(0)\00"
@.str.3 = [10 x i8] c"munlock()\00"
@str = [44 x i8] c"Cannot call new mlock family, skipping test\00"
define i32 @test_mlock_lock() {
%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 %40
15: ; preds = %0
%16 = tail call i64 @mlock2_(i8* %10, i32 %3, i32 0)
%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, i64 0, i64 0))
%24 = tail call i32 @_exit(i32 0)
unreachable
25: ; preds = %18
%26 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %37
27: ; preds = %15
%28 = tail call i64 @lock_check(i8* %10)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = tail call i64 @munlock(i8* %10, i32 %3)
%32 = icmp eq i64 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %37
35: ; preds = %30
%36 = tail call i32 @unlock_lock_check(i8* %10)
br label %37
37: ; preds = %27, %35, %33, %25
%38 = phi i32 [ 1, %25 ], [ 1, %27 ], [ 1, %33 ], [ %36, %35 ]
%39 = tail call i32 @munmap(i8* %10, i32 %3)
br label %40
40: ; preds = %37, %13
%41 = phi i32 [ 1, %13 ], [ %38, %37 ]
ret i32 %41
}