#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;
long LONG_MAX;
int /*<<< orphan*/ _SC_PAGESIZE;
scalar_t__ errno;
int munlock(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 = munlock(page_ptr, 8);
  if (result == -1 && errno == ENOMEM) {
    printf("Test PASSED\n");
    return 0;
  } 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    1213 <main+0xd3>
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 <munlock@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    1213 <main+0xd3>
lea    0xe2d(%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    11bf <main+0x7f>
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 <munlock@plt>
cmp    $0xffffffff,%eax
jne    11b6 <main+0x76>
mov    (%rbx),%rax
mov    0x2e37(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <main+0x76>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe6a(%rip),%rdi
jmp    11c6 <main+0x86>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x2,%eax
pop    %rbx
ret

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"
@.str.2 = [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 %30
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 @munlock(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 %30
28: ; preds = %22, %11
%29 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 2, i32* %1
br label %30
30: ; preds = %28, %26, %9
%31 = load i32, i32* %1
ret i32 %31
}

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
@.str.2 = [17 x i8] c"Unexpected error\00"
@str = [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 %22
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 @munlock(i8* %11, i32 8)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %20
14: ; preds = %7
%15 = load i64, i64* @errno
%16 = load i64, i64* @ENOMEM
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %22
20: ; preds = %14, %7
%21 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %20, %18, %5
%23 = phi i32 [ 2, %5 ], [ 0, %18 ], [ 2, %20 ]
ret i32 %23
}