#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 */
struct rlimit {
  int rlim_cur;
  int rlim_max;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
int /*<<< orphan*/ _SC_PAGESIZE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(void *);
void *malloc(long);
int mlock(void *, long);
int /*<<< orphan*/ pexit(char *);
int /*<<< orphan*/ printf(char *, ...);
int setrlimit(int /*<<< orphan*/, struct rlimit *);
char *strerror(int /*<<< orphan*/);
long sysconf(int /*<<< orphan*/);
int main() {
  int i, j;
  long pagesize;
  void *mems[(5)];
  struct rlimit rl;
  pagesize = sysconf(_SC_PAGESIZE);
  if (pagesize == -1)
    pexit("sysconf");
  rl.rlim_cur = (5) * pagesize;
  rl.rlim_max = (5) * pagesize;
  printf("Setting RLIMIT_MEMLOCK to %d pages\n", (5));
  if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
    pexit("setrlimit");
  for (i = 0;; i++) {
    printf("-> Page %d: ", i + 1);
    mems[i] = malloc(pagesize);
    if (mems[i] == NULL)
      pexit("malloc");
    if (mlock(mems[i], pagesize) == -1) {
      printf("%s\n", strerror(errno));
      break;
    } else {
      printf("OK\n");
    }
  }
  for (j = 0; j <= i; j++)
    free(mems[j]);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
call   1080 <sysconf@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0xffffffffffffffff,-0x18(%rbp)
jne    11c8 <main+0x38>
lea    0xe3d(%rip),%rdi
call   10a0 <pexit@plt>
imul   $0x5,-0x18(%rbp),%rax
mov    %eax,-0x48(%rbp)
imul   $0x5,-0x18(%rbp),%rax
mov    %eax,-0x44(%rbp)
lea    0xe29(%rip),%rdi
mov    $0x5,%esi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    -0x48(%rbp),%rsi
call   1070 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1212 <main+0x82>
lea    0xe1f(%rip),%rdi
call   10a0 <pexit@plt>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%esi
add    $0x1,%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
movslq -0x8(%rbp),%rax
mov    %rcx,-0x40(%rbp,%rax,8)
movslq -0x8(%rbp),%rax
cmpq   $0x0,-0x40(%rbp,%rax,8)
jne    125e <main+0xce>
lea    0xdea(%rip),%rdi
call   10a0 <pexit@plt>
movslq -0x8(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <mlock@plt>
cmp    $0xffffffff,%eax
jne    129d <main+0x10d>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12be <main+0x12e>
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b0 <main+0x120>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1219 <main+0x89>
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     12ed <main+0x15d>
movslq -0xc(%rbp),%rax
mov    -0x40(%rbp,%rax,8),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    12c5 <main+0x135>
mov    0x2cd4(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e2b(%rip),%rax
mov    (%rax),%edi
call   1090 <sysconf@plt>
mov    %rax,%r13
cmp    $0xffffffffffffffff,%rax
jne    11d1 <main+0x31>
lea    0xe34(%rip),%rdi
call   10b0 <pexit@plt>
lea    0x0(%r13,%r13,4),%eax
mov    %eax,0x8(%rsp)
mov    %eax,0xc(%rsp)
lea    0xe23(%rip),%rdi
mov    $0x5,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1080 <setrlimit@plt>
cmp    $0xffffffff,%eax
jne    1215 <main+0x75>
lea    0xe1c(%rip),%rdi
call   10b0 <pexit@plt>
lea    0xe1a(%rip),%r14
lea    0xe20(%rip),%r15
lea    0xe20(%rip),%r12
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    0x1(%rbp),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,0x10(%rsp,%rbp,8)
test   %rax,%rax
jne    125a <main+0xba>
mov    %r15,%rdi
call   10b0 <pexit@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <mlock@plt>
cmp    $0xffffffff,%eax
je     1278 <main+0xd8>
mov    %r12,%rdi
call   1050 <puts@plt>
add    $0x1,%rbp
jmp    1230 <main+0x90>
mov    0x2d51(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rdi
call   1050 <puts@plt>
add    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x10(%rsp,%rbx,8),%rdi
call   1030 <free@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    12a0 <main+0x100>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@.str.1 = [36 x i8] c"Setting RLIMIT_MEMLOCK to %d pages\0A\00"
@RLIMIT_MEMLOCK = global i32 0
@.str.2 = [10 x i8] c"setrlimit\00"
@.str.3 = [13 x i8] c"-> Page %d: \00"
@.str.4 = [7 x i8] c"malloc\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@errno = global i32 0
@.str.6 = [4 x i8] c"OK\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i64
%5 = alloca [5 x i8*]
%6 = alloca %struct.rlimit
store i32 0, i32* %1
%7 = load i32, i32* @_SC_PAGESIZE
%8 = call i64 @sysconf(i32 %7)
store i64 %8, i64* %4
%9 = load i64, i64* %4
%10 = icmp eq i64 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %0
%14 = load i64, i64* %4
%15 = mul nsw i64 5, %14
%16 = trunc i64 %15 to i32
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i64, i64* %4
%19 = mul nsw i64 5, %18
%20 = trunc i64 %19 to i32
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %6, i32 0, i32 1
store i32 %20, i32* %21
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 5)
%23 = load i32, i32* @RLIMIT_MEMLOCK
%24 = call i32 @setrlimit(i32 %23, %struct.rlimit* %6)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %13
%27 = call i32 @pexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %13
store i32 0, i32* %2
br label %29
29: ; preds = %60, %28
%30 = load i32, i32* %2
%31 = add nsw i32 %30, 1
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %31)
%33 = load i64, i64* %4
%34 = call i8* @malloc(i64 %33)
%35 = load i32, i32* %2
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %36
store i8* %34, i8** %37
%38 = load i32, i32* %2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %39
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %45
43: ; preds = %29
%44 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %43, %29
%46 = load i32, i32* %2
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %47
%49 = load i8*, i8** %48
%50 = load i64, i64* %4
%51 = call i32 @mlock(i8* %49, i64 %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %57
53: ; preds = %45
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %55)
br label %63
57: ; preds = %45
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0))
br label %59
59: ; preds = %57
br label %60
60: ; preds = %59
%61 = load i32, i32* %2
%62 = add nsw i32 %61, 1
store i32 %62, i32* %2
br label %29
63: ; preds = %53
store i32 0, i32* %3
br label %64
64: ; preds = %74, %63
%65 = load i32, i32* %3
%66 = load i32, i32* %2
%67 = icmp sle i32 %65, %66
br i1 %67, label %68, label %77
68: ; preds = %64
%69 = load i32, i32* %3
%70 = sext i32 %69 to i64
%71 = getelementptr inbounds [5 x i8*], [5 x i8*]* %5, i64 0, i64 %70
%72 = load i8*, i8** %71
%73 = call i32 @free(i8* %72)
br label %74
74: ; preds = %68
%75 = load i32, i32* %3
%76 = add nsw i32 %75, 1
store i32 %76, i32* %3
br label %64
77: ; preds = %64
%78 = load i32, i32* @EXIT_SUCCESS
%79 = call i32 @exit(i32 %78)
unreachable
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@_SC_PAGESIZE = global i32 0
@.str = [8 x i8] c"sysconf\00"
@.str.1 = [36 x i8] c"Setting RLIMIT_MEMLOCK to %d pages\0A\00"
@RLIMIT_MEMLOCK = global i32 0
@.str.2 = [10 x i8] c"setrlimit\00"
@.str.3 = [13 x i8] c"-> Page %d: \00"
@.str.4 = [7 x i8] c"malloc\00"
@errno = global i32 0
@EXIT_SUCCESS = global i32 0
@str = [3 x i8] c"OK\00"
define i32 @main() {
%1 = alloca [5 x i8*]
%2 = alloca %struct.rlimit
%3 = bitcast [5 x i8*]* %1 to i8*
%4 = bitcast %struct.rlimit* %2 to i8*
%5 = load i32, i32* @_SC_PAGESIZE
%6 = tail call i64 @sysconf(i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = tail call i32 @pexit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %0
%11 = trunc i64 %6 to i32
%12 = mul i32 %11, 5
%13 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i64 0, i32 1
store i32 %12, i32* %14
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 5)
%16 = load i32, i32* @RLIMIT_MEMLOCK
%17 = call i32 @setrlimit(i32 %16, %struct.rlimit* %2)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %21
19: ; preds = %10
%20 = call i32 @pexit(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %10
br label %22
22: ; preds = %21, %41
%23 = phi i64 [ %24, %41 ], [ 0, %21 ]
%24 = add nuw i64 %23, 1
%25 = trunc i64 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i8* @malloc(i64 %6)
%28 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 %23
store i8* %27, i8** %28
%29 = icmp eq i8* %27, null
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @pexit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
%33 = call i32 @mlock(i8* %27, i64 %6)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @puts(i8* dereferenceable(1) %37)
%39 = add i64 %23, 1
%40 = and i64 %39, 4294967295
br label %43
41: ; preds = %32
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %22
43: ; preds = %35, %43
%44 = phi i64 [ 0, %35 ], [ %48, %43 ]
%45 = getelementptr inbounds [5 x i8*], [5 x i8*]* %1, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = call i32 @free(i8* %46)
%48 = add nuw nsw i64 %44, 1
%49 = icmp eq i64 %48, %40
br i1 %49, label %50, label %43
50: ; preds = %43
%51 = load i32, i32* @EXIT_SUCCESS
%52 = call i32 @exit(i32 %51)
unreachable
}