#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 MCL_CURRENT;
int MCL_FUTURE;
int /*<<< orphan*/ _SC_MEMLOCK;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int mlockall(int);
int munlockall();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ quit(char *);
int /*<<< orphan*/ stderr;
int sysconf(int /*<<< orphan*/);
int memlock(int argc, char *argv[]) {
  int e = 0;
  errno = 0;
  if (sysconf(_SC_MEMLOCK) == -1) {
    if (errno != 0) {
      quit("(should not happen) sysconf(_SC_MEMLOCK)");
    } else {
      fprintf(stderr, "Memory locking is not supported in this environment.\n");
      e = -1;
    }
  }
  if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
    perror("mlockall(MCL_CURRENT | MCL_FUTURE)");
    e = errno;
  } else if (munlockall() == -1) {
    perror("munlockall");
    e = errno;
  }
  return e;
}


x86 assembly (clang, -O0):
<memlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e4b(%rip),%rax
movl   $0x0,(%rax)
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
cmp    $0xffffffff,%eax
jne    11dc <memlock+0x7c>
mov    0x2e27(%rip),%rax
cmpl   $0x0,(%rax)
je     11bb <memlock+0x5b>
lea    0xe4f(%rip),%rdi
call   1030 <quit@plt>
jmp    11d7 <memlock+0x77>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x14(%rbp)
jmp    11dc <memlock+0x7c>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
or     (%rax),%edi
call   1050 <mlockall@plt>
cmp    $0xffffffff,%eax
jne    1219 <memlock+0xb9>
lea    0xe5c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1246 <memlock+0xe6>
mov    $0x0,%al
call   1080 <munlockall@plt>
cmp    $0xffffffff,%eax
jne    1241 <memlock+0xe1>
lea    0xe52(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1246 <memlock+0xe6>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memlock>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%r14
movl   $0x0,(%r14)
mov    0x2e67(%rip),%rax
mov    (%rax),%edi
call   1070 <sysconf@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
jne    11b5 <memlock+0x55>
cmpl   $0x0,(%r14)
je     119b <memlock+0x3b>
lea    0xe6c(%rip),%rdi
call   1030 <quit@plt>
jmp    11b5 <memlock+0x55>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    0x2e04(%rip),%rax
mov    0x2e2d(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1050 <mlockall@plt>
cmp    $0xffffffff,%eax
je     11e7 <memlock+0x87>
xor    %eax,%eax
call   1080 <munlockall@plt>
cmp    $0xffffffff,%eax
je     11f0 <memlock+0x90>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe71(%rip),%rdi
jmp    11f7 <memlock+0x97>
lea    0xe8b(%rip),%rdi
call   1060 <perror@plt>
mov    (%r14),%ebx
jmp    11dd <memlock+0x7d>

LLVM IR (-O0):
@errno = global i32 0
@_SC_MEMLOCK = global i32 0
@.str = [41 x i8] c"(should not happen) sysconf(_SC_MEMLOCK)\00"
@stderr = global i32 0
@.str.1 = [54 x i8] c"Memory locking is not supported in this environment.\0A\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [35 x i8] c"mlockall(MCL_CURRENT | MCL_FUTURE)\00"
@.str.3 = [11 x i8] c"munlockall\00"
define i32 @memlock(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %5
store i32 0, i32* @errno
%6 = load i32, i32* @_SC_MEMLOCK
%7 = call i32 @sysconf(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = load i32, i32* @errno
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @quit(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %17
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %17
17: ; preds = %14, %12
br label %18
18: ; preds = %17, %2
%19 = load i32, i32* @MCL_CURRENT
%20 = load i32, i32* @MCL_FUTURE
%21 = or i32 %19, %20
%22 = call i32 @mlockall(i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @errno
store i32 %26, i32* %5
br label %34
27: ; preds = %18
%28 = call i32 (...) @munlockall()
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %27
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* @errno
store i32 %32, i32* %5
br label %33
33: ; preds = %30, %27
br label %34
34: ; preds = %33, %24
%35 = load i32, i32* %5
ret i32 %35
}

LLVM IR (-O2):
@errno = global i32 0
@_SC_MEMLOCK = global i32 0
@.str = [41 x i8] c"(should not happen) sysconf(_SC_MEMLOCK)\00"
@stderr = global i32 0
@.str.1 = [54 x i8] c"Memory locking is not supported in this environment.\0A\00"
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
@.str.2 = [35 x i8] c"mlockall(MCL_CURRENT | MCL_FUTURE)\00"
@.str.3 = [11 x i8] c"munlockall\00"
define i32 @memlock(i32 %0, i8** nocapture readnone %1) {
store i32 0, i32* @errno
%3 = load i32, i32* @_SC_MEMLOCK
%4 = tail call i32 @sysconf(i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @quit(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
br label %14
11: ; preds = %6
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %9, %11, %2
%15 = phi i32 [ 0, %9 ], [ -1, %11 ], [ 0, %2 ]
%16 = load i32, i32* @MCL_CURRENT
%17 = load i32, i32* @MCL_FUTURE
%18 = or i32 %17, %16
%19 = tail call i32 @mlockall(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = tail call i32 (...) @munlockall()
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %21, %14
%25 = phi i8* [ getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), %14 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), %21 ]
%26 = tail call i32 @perror(i8* %25)
%27 = load i32, i32* @errno
br label %28
28: ; preds = %24, %21
%29 = phi i32 [ %15, %21 ], [ %27, %24 ]
ret i32 %29
}