#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 */
union bpf_attr {
  int map_type;
  int key_size;
  int value_size;
  int max_entries;
  int map_flags;
};
struct rlimit {
  int /*<<< orphan*/ rlim_max;
  int /*<<< orphan*/ rlim_cur;
};
typedef enum bpf_map_type { ____Placeholder_bpf_map_type } bpf_map_type;
typedef int /*<<< orphan*/ attr;

/* Variables and functions */
int /*<<< orphan*/ BPF_MAP_CREATE;
scalar_t__ EPERM;
int /*<<< orphan*/ RLIMIT_MEMLOCK;
int /*<<< orphan*/ RLIM_INFINITY;
scalar_t__ errno;
scalar_t__ getrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ memset(union bpf_attr *, int /*<<< orphan*/, int);
scalar_t__ setrlimit(int /*<<< orphan*/, struct rlimit *);
int syscall(int, int /*<<< orphan*/, union bpf_attr *, int);
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
                   int max_entries, int map_flags) {
  union bpf_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.map_type = map_type;
  attr.key_size = key_size;
  attr.value_size = value_size;
  attr.max_entries = max_entries;
  attr.map_flags = map_flags;
  int ret = syscall(321, BPF_MAP_CREATE, &attr, sizeof(attr));
  if (ret < 0 && errno == EPERM) {
    struct rlimit rl = {};
    if (getrlimit(RLIMIT_MEMLOCK, &rl) == 0) {
      rl.rlim_max = RLIM_INFINITY;
      rl.rlim_cur = rl.rlim_max;
      if (setrlimit(RLIMIT_MEMLOCK, &rl) == 0)
        ret = syscall(321, BPF_MAP_CREATE, &attr, sizeof(attr));
    }
  }
  return ret;
}


x86 assembly (clang, -O0):
<bpf_create_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8d,-0x14(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    $0x141,%edi
lea    -0x18(%rbp),%rdx
mov    $0x4,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124c <bpf_create_map+0x10c>
mov    0x2e1a(%rip),%rax
mov    (%rax),%rax
mov    0x2df8(%rip),%rcx
cmp    (%rcx),%rax
jne    124c <bpf_create_map+0x10c>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1060 <getrlimit@plt>
cmp    $0x0,%rax
jne    1247 <bpf_create_map+0x107>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <setrlimit@plt>
cmp    $0x0,%rax
jne    1242 <bpf_create_map+0x102>
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
mov    $0x141,%edi
lea    -0x18(%rbp),%rdx
mov    $0x4,%ecx
call   1040 <syscall@plt>
mov    %eax,-0x1c(%rbp)
jmp    1247 <bpf_create_map+0x107>
jmp    124c <bpf_create_map+0x10c>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_create_map>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1030 <memset@plt>
mov    %ebx,(%rsp)
mov    0x2e80(%rip),%r15
mov    (%r15),%esi
mov    $0x141,%edi
mov    %r14,%rdx
mov    $0x4,%ecx
call   1040 <syscall@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11f5 <bpf_create_map+0xb5>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    0x2e24(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <bpf_create_map+0xb5>
movq   $0x0,0x8(%rsp)
mov    0x2e37(%rip),%r14
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1060 <getrlimit@plt>
test   %rax,%rax
jne    11f5 <bpf_create_map+0xb5>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %eax,0xc(%rsp)
mov    (%r14),%edi
lea    0x8(%rsp),%rsi
call   1050 <setrlimit@plt>
test   %rax,%rax
jne    11f5 <bpf_create_map+0xb5>
mov    (%r15),%esi
mov    %rsp,%rdx
mov    $0x141,%edi
mov    $0x4,%ecx
call   1040 <syscall@plt>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%union.bpf_attr = type { i32 }
%struct.rlimit = type { i32, i32 }
@BPF_MAP_CREATE = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@RLIMIT_MEMLOCK = global i32 0
@RLIM_INFINITY = global i32 0
define i32 @bpf_create_map(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %union.bpf_attr
%12 = alloca i32
%13 = alloca %struct.rlimit
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = call i32 @memset(%union.bpf_attr* %11, i32 0, i32 4)
%15 = load i32, i32* %6
%16 = bitcast %union.bpf_attr* %11 to i32*
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = bitcast %union.bpf_attr* %11 to i32*
store i32 %17, i32* %18
%19 = load i32, i32* %8
%20 = bitcast %union.bpf_attr* %11 to i32*
store i32 %19, i32* %20
%21 = load i32, i32* %9
%22 = bitcast %union.bpf_attr* %11 to i32*
store i32 %21, i32* %22
%23 = load i32, i32* %10
%24 = bitcast %union.bpf_attr* %11 to i32*
store i32 %23, i32* %24
%25 = load i32, i32* @BPF_MAP_CREATE
%26 = call i32 @syscall(i32 321, i32 %25, %union.bpf_attr* %11, i32 4)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %52
29: ; preds = %5
%30 = load i64, i64* @errno
%31 = load i64, i64* @EPERM
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = bitcast %struct.rlimit* %13 to i8*
%35 = load i32, i32* @RLIMIT_MEMLOCK
%36 = call i64 @getrlimit(i32 %35, %struct.rlimit* %13)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %51
38: ; preds = %33
%39 = load i32, i32* @RLIM_INFINITY
%40 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %13, i32 0, i32 0
store i32 %39, i32* %40
%41 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %13, i32 0, i32 0
%42 = load i32, i32* %41
%43 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %13, i32 0, i32 1
store i32 %42, i32* %43
%44 = load i32, i32* @RLIMIT_MEMLOCK
%45 = call i64 @setrlimit(i32 %44, %struct.rlimit* %13)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %38
%48 = load i32, i32* @BPF_MAP_CREATE
%49 = call i32 @syscall(i32 321, i32 %48, %union.bpf_attr* %11, i32 4)
store i32 %49, i32* %12
br label %50
50: ; preds = %47, %38
br label %51
51: ; preds = %50, %33
br label %52
52: ; preds = %51, %29, %5
%53 = load i32, i32* %12
ret i32 %53
}

LLVM IR (-O2):
%union.bpf_attr = type { i32 }
%struct.rlimit = type { i32, i32 }
@BPF_MAP_CREATE = global i32 0
@errno = global i64 0
@EPERM = global i64 0
@RLIMIT_MEMLOCK = global i32 0
@RLIM_INFINITY = global i32 0
define i32 @bpf_create_map(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %union.bpf_attr
%7 = alloca i64
%8 = bitcast i64* %7 to %struct.rlimit*
%9 = bitcast %union.bpf_attr* %6 to i8*
%10 = call i32 @memset(%union.bpf_attr* %6, i32 0, i32 4)
%11 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %6, i64 0, i32 0
store i32 %4, i32* %11
%12 = load i32, i32* @BPF_MAP_CREATE
%13 = call i32 @syscall(i32 321, i32 %12, %union.bpf_attr* %6, i32 4)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %36
15: ; preds = %5
%16 = load i64, i64* @errno
%17 = load i64, i64* @EPERM
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %36
19: ; preds = %15
%20 = bitcast i64* %7 to i8*
store i64 0, i64* %7
%21 = load i32, i32* @RLIMIT_MEMLOCK
%22 = call i64 @getrlimit(i32 %21, %struct.rlimit* %8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i32, i32* @RLIM_INFINITY
%26 = bitcast i64* %7 to i32*
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i64 0, i32 1
store i32 %25, i32* %27
%28 = load i32, i32* @RLIMIT_MEMLOCK
%29 = call i64 @setrlimit(i32 %28, %struct.rlimit* %8)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = load i32, i32* @BPF_MAP_CREATE
%33 = call i32 @syscall(i32 321, i32 %32, %union.bpf_attr* %6, i32 4)
br label %34
34: ; preds = %24, %31, %19
%35 = phi i32 [ %33, %31 ], [ %13, %24 ], [ %13, %19 ]
br label %36
36: ; preds = %34, %15, %5
%37 = phi i32 [ %35, %34 ], [ %13, %15 ], [ %13, %5 ]
ret i32 %37
}