#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__ ENOMEM;
scalar_t__ EPERM;
long LONG_MAX;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ errno;
int mlock(void *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
long sysconf(int /*<<< orphan*/);
int main(void) {
  int result;
  long page_size;
  void *page_ptr;
  page_size = sysconf(_SC_PAGESIZE);
  if (errno) {
    perror("An error occurs when calling sysconf()");
    return 2;
  }
  page_ptr = (void *)(LONG_MAX - (LONG_MAX % page_size));
  result = mlock(page_ptr, 8);
  if (result == -1 && errno == ENOMEM) {
    printf("Test PASSED\n");
    return 0;
  } else if (errno == EPERM) {
    printf("You don't have permission to lock your address space.\nTry to "
           "rerun this test as root.\n");
    return 2;
  } else {
    perror("Unexpected error");
    return 2;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e92(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    %rax,-0x10(%rbp)
mov    0x2e60(%rip),%rax
cmpq   $0x0,(%rax)
je     118a <main+0x4a>
lea    0xe87(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1245 <main+0x105>
mov    0x2e3f(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%rax
cqto
idivq  -0x10(%rbp)
mov    -0x20(%rbp),%rax
sub    %rdx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x8,%esi
call   1030 <mlock@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    1200 <main+0xc0>
mov    0x2df3(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <main+0xc0>
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1245 <main+0x105>
mov    0x2dc1(%rip),%rax
mov    (%rax),%rax
mov    0x2da7(%rip),%rcx
cmp    (%rcx),%rax
jne    1232 <main+0xf2>
lea    0xe13(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1245 <main+0x105>
lea    0xe52(%rip),%rdi
call   1050 <perror@plt>
movl   $0x2,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%edi
call   1060 <sysconf@plt>
mov    0x2e72(%rip),%rbx
cmpq   $0x0,(%rbx)
jne    11d5 <main+0x95>
mov    %rax,%rcx
mov    0x2e6a(%rip),%rax
mov    (%rax),%rdi
mov    %rcx,%rax
or     %rdi,%rax
shr    $0x20,%rax
je     117f <main+0x3f>
mov    %rdi,%rax
cqto
idiv   %rcx
jmp    1185 <main+0x45>
mov    %edi,%eax
xor    %edx,%edx
div    %ecx
sub    %rdx,%rdi
mov    $0x8,%esi
call   1030 <mlock@plt>
mov    (%rbx),%rcx
cmp    $0xffffffff,%eax
jne    11b6 <main+0x76>
mov    0x2e37(%rip),%rax
cmp    (%rax),%rcx
jne    11b6 <main+0x76>
lea    0xee1(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2dfb(%rip),%rax
cmp    (%rax),%rcx
jne    11de <main+0x9e>
lea    0xe6f(%rip),%rdi
call   1040 <puts@plt>
mov    $0x2,%eax
pop    %rbx
ret
lea    0xe24(%rip),%rdi
jmp    11e5 <main+0xa5>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
jmp    11ce <main+0x8e>

LLVM IR (-O0):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@.str = [39 x i8] c"An error occurs when calling sysconf()\00"
@LONG_MAX = global i64 0
@ENOMEM = global i64 0
@.str.1 = [13 x i8] c"Test PASSED\0A\00"
@EPERM = global i64 0
@.str.2 = [87 x i8] c"You don't have permission to lock your address space.\0ATry to rerun this test as root.\0A\00"
@.str.3 = [17 x i8] c"Unexpected error\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @_SC_PAGESIZE
%6 = call i64 @sysconf(i32 %5)
store i64 %6, i64* %3
%7 = load i64, i64* @errno
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %1
br label %36
11: ; preds = %0
%12 = load i64, i64* @LONG_MAX
%13 = load i64, i64* @LONG_MAX
%14 = load i64, i64* %3
%15 = srem i64 %13, %14
%16 = sub nsw i64 %12, %15
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @mlock(i8* %18, i32 8)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %11
%23 = load i64, i64* @errno
%24 = load i64, i64* @ENOMEM
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %36
28: ; preds = %22, %11
%29 = load i64, i64* @errno
%30 = load i64, i64* @EPERM
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @printf(i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %36
34: ; preds = %28
%35 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %1
br label %36
36: ; preds = %34, %32, %26, %9
%37 = load i32, i32* %1
ret i32 %37
}

LLVM IR (-O2):
@_SC_PAGESIZE = global i32 0
@errno = global i64 0
@.str = [39 x i8] c"An error occurs when calling sysconf()\00"
@LONG_MAX = global i64 0
@ENOMEM = global i64 0
@EPERM = global i64 0
@.str.3 = [17 x i8] c"Unexpected error\00"
@str = [86 x i8] c"You don't have permission to lock your address space.\0ATry to rerun this test as root.\00"
@str.4 = [12 x i8] c"Test PASSED\00"
define i32 @main() {
%1 = load i32, i32* @_SC_PAGESIZE
%2 = tail call i64 @sysconf(i32 %1)
%3 = load i64, i64* @errno
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %27
7: ; preds = %0
%8 = load i64, i64* @LONG_MAX
%9 = srem i64 %8, %2
%10 = sub nsw i64 %8, %9
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @mlock(i8* %11, i32 8)
%13 = icmp eq i32 %12, -1
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOMEM
%16 = icmp eq i64 %14, %15
%17 = select i1 %13, i1 %16, i1 false
br i1 %17, label %18, label %20
18: ; preds = %7
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.4, i64 0, i64 0))
br label %27
20: ; preds = %7
%21 = load i64, i64* @EPERM
%22 = icmp eq i64 %14, %21
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @str, i64 0, i64 0))
br label %27
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %23, %18, %5
%28 = phi i32 [ 2, %5 ], [ 0, %18 ], [ 2, %23 ], [ 2, %25 ]
ret i32 %28
}