#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 {
  scalar_t__ rlim_max;
  scalar_t__ rlim_cur;
};

/* Variables and functions */
int /*<<< orphan*/ RLIMIT_MEMLOCK;
scalar_t__ RLIM_INFINITY;
int /*<<< orphan*/ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ perror(char *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
__attribute__((constructor)) void bpf_rlimit_ctor(void) {
  struct rlimit rlim_old, rlim_new = {
                              .rlim_cur = RLIM_INFINITY,
                              .rlim_max = RLIM_INFINITY,
                          };
  getrlimit(RLIMIT_MEMLOCK, &rlim_old);
  if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) {
    perror("Unable to lift memlock rlimit");
    rlim_new.rlim_cur = rlim_old.rlim_cur + (1UL << 20);
    rlim_new.rlim_max = rlim_old.rlim_max + (1UL << 20);
    setrlimit(RLIMIT_MEMLOCK, &rlim_new);
  }
}


x86 assembly (clang, -O0):
<bpf_rlimit_ctor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <getrlimit@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    11bc <bpf_rlimit_ctor+0x8c>
lea    0xe77(%rip),%rdi
call   1040 <perror@plt>
mov    -0x8(%rbp),%rax
add    $0x100000,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x100000,%rax
mov    %rax,-0x20(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1030 <setrlimit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_rlimit_ctor>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e9c(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%rsp)
mov    %rax,0x8(%rsp)
mov    0x2e99(%rip),%rbx
mov    (%rbx),%edi
lea    0x10(%rsp),%rsi
call   1050 <getrlimit@plt>
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
js     1170 <bpf_rlimit_ctor+0x40>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe99(%rip),%rdi
call   1040 <perror@plt>
movdqa 0x10(%rsp),%xmm0
paddq  0xe76(%rip),%xmm0
movdqa %xmm0,(%rsp)
mov    (%rbx),%edi
mov    %rsp,%rsi
call   1030 <setrlimit@plt>
jmp    116a <bpf_rlimit_ctor+0x3a>

LLVM IR (-O0):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = global i64 0
@RLIMIT_MEMLOCK = global i32 0
@.str = [30 x i8] c"Unable to lift memlock rlimit\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @bpf_rlimit_ctor, i8* null }]
define void @bpf_rlimit_ctor() {
%1 = alloca %struct.rlimit
%2 = alloca %struct.rlimit
%3 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
%4 = load i64, i64* @RLIM_INFINITY
store i64 %4, i64* %3
%5 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
%6 = load i64, i64* @RLIM_INFINITY
store i64 %6, i64* %5
%7 = load i32, i32* @RLIMIT_MEMLOCK
%8 = call i32 @getrlimit(i32 %7, %struct.rlimit* %1)
%9 = load i32, i32* @RLIMIT_MEMLOCK
%10 = call i64 @setrlimit(i32 %9, %struct.rlimit* %2)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%14 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%15 = load i64, i64* %14
%16 = add i64 %15, 1048576
%17 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 1
store i64 %16, i64* %17
%18 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%19 = load i64, i64* %18
%20 = add i64 %19, 1048576
%21 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %2, i32 0, i32 0
store i64 %20, i64* %21
%22 = load i32, i32* @RLIMIT_MEMLOCK
%23 = call i64 @setrlimit(i32 %22, %struct.rlimit* %2)
br label %24
24: ; preds = %12, %0
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i64, i64 }
@RLIM_INFINITY = global i64 0
@RLIMIT_MEMLOCK = global i32 0
@.str = [30 x i8] c"Unable to lift memlock rlimit\00"
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @bpf_rlimit_ctor, i8* null }]
define void @bpf_rlimit_ctor() {
%1 = alloca <2 x i64>
%2 = bitcast <2 x i64>* %1 to %struct.rlimit*
%3 = alloca <2 x i64>
%4 = bitcast <2 x i64>* %3 to %struct.rlimit*
%5 = bitcast <2 x i64>* %1 to i8*
%6 = bitcast <2 x i64>* %3 to i8*
%7 = getelementptr inbounds <2 x i64>, <2 x i64>* %3, i64 0, i64 0
%8 = load i64, i64* @RLIM_INFINITY
store i64 %8, i64* %7
%9 = getelementptr inbounds <2 x i64>, <2 x i64>* %3, i64 0, i64 1
store i64 %8, i64* %9
%10 = load i32, i32* @RLIMIT_MEMLOCK
%11 = call i32 @getrlimit(i32 %10, %struct.rlimit* %2)
%12 = load i32, i32* @RLIMIT_MEMLOCK
%13 = call i64 @setrlimit(i32 %12, %struct.rlimit* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %0
%16 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%17 = load <2 x i64>, <2 x i64>* %1
%18 = add <2 x i64> %17, <i64 1048576, i64 1048576>
store <2 x i64> %18, <2 x i64>* %3
%19 = load i32, i32* @RLIMIT_MEMLOCK
%20 = call i64 @setrlimit(i32 %19, %struct.rlimit* %4)
br label %21
21: ; preds = %15, %0
ret void
}