#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 */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getpagesize();
char *malloc(int);
int mlock(char *, int);
int /*<<< orphan*/ munlock(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int psize = getpagesize();
  char *bytes = NULL;
  char *p;
  int ret;
  if ((bytes = malloc(psize)) == NULL) {
    fprintf(stderr, "malloc() failed!\n");
    exit(1);
  }
  p = bytes + (psize - ((unsigned int)bytes % psize));
  ret = mlock(p, 1);
  if (ret != 0) {
    perror("mlock() failed!");
    exit(1);
  }
  munlock(bytes, psize);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1080 <getpagesize@plt>
mov    %eax,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11cd <main+0x5d>
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%ecx
mov    -0x20(%rbp),%rax
xor    %edx,%edx
divl   -0x14(%rbp)
mov    -0x38(%rbp),%rax
sub    %edx,%ecx
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1040 <mlock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     1221 <main+0xb1>
lea    0xe00(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <munlock@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1080 <getpagesize@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11b7 <main+0x47>
mov    %rax,%rbx
xor    %edx,%edx
div    %ebp
mov    %ebp,%edi
sub    %edx,%edi
add    %rbx,%rdi
mov    $0x1,%esi
call   1040 <mlock@plt>
test   %eax,%eax
jne    11d6 <main+0x66>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1030 <munlock@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe35(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"malloc() failed!\0A\00"
@.str.1 = [16 x i8] c"mlock() failed!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (...) @getpagesize()
store i32 %10, i32* %6
store i8* null, i8** %7
%11 = load i32, i32* %6
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %7
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8*, i8** %7
%20 = load i32, i32* %6
%21 = load i8*, i8** %7
%22 = ptrtoint i8* %21 to i32
%23 = load i32, i32* %6
%24 = urem i32 %22, %23
%25 = sub i32 %20, %24
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %19, i64 %26
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = call i32 @mlock(i8* %28, i32 1)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %18
%36 = load i8*, i8** %7
%37 = load i32, i32* %6
%38 = call i32 @munlock(i8* %36, i32 %37)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"malloc() failed!\0A\00"
@.str.1 = [16 x i8] c"mlock() failed!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 (...) @getpagesize()
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = ptrtoint i8* %4 to i64
%12 = trunc i64 %11 to i32
%13 = urem i32 %12, %3
%14 = sub i32 %3, %13
%15 = zext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %4, i64 %15
%17 = tail call i32 @mlock(i8* %16, i32 1)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %10
%20 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %10
%23 = tail call i32 @munlock(i8* %4, i32 %3)
ret i32 0
}