#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*/ RLIMIT_MEMLOCK;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ pr_debug(char *);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
void rlimit__bump_memlock(void) {
  struct rlimit rlim;
  if (getrlimit(RLIMIT_MEMLOCK, &rlim) == 0) {
    rlim.rlim_cur *= 4;
    rlim.rlim_max *= 4;
    if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0) {
      rlim.rlim_cur /= 2;
      rlim.rlim_max /= 2;
      if (setrlimit(RLIMIT_MEMLOCK, &rlim) < 0)
        pr_debug("Couldn't bump rlimit(MEMLOCK), failures may take place when "
                 "creating BPF maps, etc\n");
    }
  }
}


x86 assembly (clang, -O0):
<rlimit__bump_memlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2ea9(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1050 <getrlimit@plt>
cmp    $0x0,%rax
jne    11d0 <rlimit__bump_memlock+0xa0>
mov    -0x8(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    11cb <rlimit__bump_memlock+0x9b>
mov    -0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    -0x8(%rbp),%rsi
call   1030 <setrlimit@plt>
cmp    $0x0,%rax
jge    11c6 <rlimit__bump_memlock+0x96>
lea    0xe3f(%rip),%rdi
call   1040 <pr_debug@plt>
jmp    11cb <rlimit__bump_memlock+0x9b>
jmp    11d0 <rlimit__bump_memlock+0xa0>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rlimit__bump_memlock>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rbx
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1050 <getrlimit@plt>
test   %rax,%rax
jne    11a7 <rlimit__bump_memlock+0x77>
shll   $0x2,0x8(%rsp)
shll   $0x2,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
jns    11a7 <rlimit__bump_memlock+0x77>
mov    0x8(%rsp),%eax
mov    0xc(%rsp),%ecx
mov    %eax,%edx
shr    $0x1f,%edx
add    %eax,%edx
sar    %edx
mov    %edx,0x8(%rsp)
mov    %ecx,%eax
shr    $0x1f,%eax
add    %ecx,%eax
sar    %eax
mov    %eax,0xc(%rsp)
mov    (%rbx),%edi
lea    0x8(%rsp),%rsi
call   1030 <setrlimit@plt>
test   %rax,%rax
jns    11a7 <rlimit__bump_memlock+0x77>
lea    0xe5e(%rip),%rdi
call   1040 <pr_debug@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_MEMLOCK = global i32 0
@.str = [84 x i8] c"Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc\0A\00"
define void @rlimit__bump_memlock() {
%1 = alloca %struct.rlimit
%2 = load i32, i32* @RLIMIT_MEMLOCK
%3 = call i64 @getrlimit(i32 %2, %struct.rlimit* %1)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %29
5: ; preds = %0
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%7 = load i32, i32* %6
%8 = mul nsw i32 %7, 4
store i32 %8, i32* %6
%9 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%10 = load i32, i32* %9
%11 = mul nsw i32 %10, 4
store i32 %11, i32* %9
%12 = load i32, i32* @RLIMIT_MEMLOCK
%13 = call i64 @setrlimit(i32 %12, %struct.rlimit* %1)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %28
15: ; preds = %5
%16 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 0
%17 = load i32, i32* %16
%18 = sdiv i32 %17, 2
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i32 0, i32 1
%20 = load i32, i32* %19
%21 = sdiv i32 %20, 2
store i32 %21, i32* %19
%22 = load i32, i32* @RLIMIT_MEMLOCK
%23 = call i64 @setrlimit(i32 %22, %struct.rlimit* %1)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 @pr_debug(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %15
br label %28
28: ; preds = %27, %5
br label %29
29: ; preds = %28, %0
ret void
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_MEMLOCK = global i32 0
@.str = [84 x i8] c"Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc\0A\00"
define void @rlimit__bump_memlock() {
%1 = alloca %struct.rlimit
%2 = bitcast %struct.rlimit* %1 to i8*
%3 = load i32, i32* @RLIMIT_MEMLOCK
%4 = call i64 @getrlimit(i32 %3, %struct.rlimit* %1)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %26
6: ; preds = %0
%7 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 0
%8 = load i32, i32* %7
%9 = shl nsw i32 %8, 2
store i32 %9, i32* %7
%10 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %1, i64 0, i32 1
%11 = load i32, i32* %10
%12 = shl nsw i32 %11, 2
store i32 %12, i32* %10
%13 = load i32, i32* @RLIMIT_MEMLOCK
%14 = call i64 @setrlimit(i32 %13, %struct.rlimit* %1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %6
%17 = load i32, i32* %7
%18 = sdiv i32 %17, 2
store i32 %18, i32* %7
%19 = load i32, i32* %10
%20 = sdiv i32 %19, 2
store i32 %20, i32* %10
%21 = load i32, i32* @RLIMIT_MEMLOCK
%22 = call i64 @setrlimit(i32 %21, %struct.rlimit* %1)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @pr_debug(i8* getelementptr inbounds ([84 x i8], [84 x i8]* @.str, i64 0, i64 0))
br label %26
26: ; preds = %6, %24, %16, %0
ret void
}