#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 /*<<< orphan*/ member_1;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int MCL_CURRENT;
int MCL_FUTURE;
int /*<<< orphan*/ PR_SET_DUMPABLE;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
int /*<<< orphan*/ RLIM_INFINITY;
scalar_t__ mlockall(int);
scalar_t__ prctl(int /*<<< orphan*/, int /*<<< orphan*/, int, int, int);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit const *);
int protect_address_space(void) {
  int ret = 0;
  const struct rlimit rl = {RLIM_INFINITY, RLIM_INFINITY};
  if (setrlimit(RLIMIT_MEMLOCK, &rl))
    ret = -1;
  if (prctl(PR_SET_DUMPABLE, 0, -1, -1, -1))
    ret = -1;
  if (mlockall(MCL_CURRENT | MCL_FUTURE))
    ret = -1;
  return ret;
}


x86 assembly (clang, -O0):
<protect_address_space>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <setrlimit@plt>
cmp    $0x0,%rax
je     117a <protect_address_space+0x4a>
movl   $0xffffffff,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xffffffff,%r8d
mov    %r8d,%edx
mov    %r8d,%ecx
call   1030 <prctl@plt>
cmp    $0x0,%rax
je     11a7 <protect_address_space+0x77>
movl   $0xffffffff,-0x4(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
or     (%rax),%edi
call   1050 <mlockall@plt>
cmp    $0x0,%rax
je     11cf <protect_address_space+0x9f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<protect_address_space>:
push   %r14
push   %rbx
push   %rax
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    %eax,0x4(%rsp)
mov    0x2e95(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1040 <setrlimit@plt>
mov    %rax,%r14
mov    0x2e71(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
mov    $0xffffffff,%edx
mov    $0xffffffff,%ecx
mov    $0xffffffff,%r8d
call   1030 <prctl@plt>
mov    %rax,%rbx
mov    0x2e46(%rip),%rax
mov    0x2e67(%rip),%rcx
mov    (%rcx),%edi
or     (%rax),%edi
call   1050 <mlockall@plt>
or     %rbx,%rax
xor    %ecx,%ecx
or     %r14,%rax
setne  %cl
neg    %ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIM_INFINITY = global i32 0
@RLIMIT_MEMLOCK = global i32 0
@PR_SET_DUMPABLE = global i32 0
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
define i32 @protect_address_space() {
%1 = alloca i32
%2 = alloca %struct.rlimit
store i32 0, i32* %1
%3 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%4 = load i32, i32* @RLIM_INFINITY
store i32 %4, i32* %3
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
%6 = load i32, i32* @RLIM_INFINITY
store i32 %6, i32* %5
%7 = load i32, i32* @RLIMIT_MEMLOCK
%8 = call i64 @setrlimit(i32 %7, %struct.rlimit* %2)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %0
store i32 -1, i32* %1
br label %11
11: ; preds = %10, %0
%12 = load i32, i32* @PR_SET_DUMPABLE
%13 = call i64 @prctl(i32 %12, i32 0, i32 -1, i32 -1, i32 -1)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %11
store i32 -1, i32* %1
br label %16
16: ; preds = %15, %11
%17 = load i32, i32* @MCL_CURRENT
%18 = load i32, i32* @MCL_FUTURE
%19 = or i32 %17, %18
%20 = call i64 @mlockall(i32 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 -1, i32* %1
br label %23
23: ; preds = %22, %16
%24 = load i32, i32* %1
ret i32 %24
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIM_INFINITY = global i32 0
@RLIMIT_MEMLOCK = global i32 0
@PR_SET_DUMPABLE = global i32 0
@MCL_CURRENT = global i32 0
@MCL_FUTURE = global i32 0
define i32 @protect_address_space() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%4 = load i32, i32* @RLIM_INFINITY
store i32 %4, i32* %3
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
store i32 %4, i32* %5
%6 = load i32, i32* @RLIMIT_MEMLOCK
%7 = call i64 @setrlimit(i32 %6, %struct.rlimit* %1)
%8 = icmp ne i64 %7, 0
%9 = load i32, i32* @PR_SET_DUMPABLE
%10 = call i64 @prctl(i32 %9, i32 0, i32 -1, i32 -1, i32 -1)
%11 = icmp ne i64 %10, 0
%12 = load i32, i32* @MCL_CURRENT
%13 = load i32, i32* @MCL_FUTURE
%14 = or i32 %13, %12
%15 = call i64 @mlockall(i32 %14)
%16 = icmp ne i64 %15, 0
%17 = select i1 %16, i1 true, i1 %11
%18 = select i1 %17, i1 true, i1 %8
%19 = sext i1 %18 to i32
ret i32 %19
}