#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 prog_type;
  int insn_cnt;
  size_t log_size;
  int log_level;
  int kern_version;
  void *log_buf;
  int /*<<< orphan*/ prog_name;
  void *license;
  void *insns;
};
typedef char bpf_insn;
typedef enum bpf_prog_type { ____Placeholder_bpf_prog_type } bpf_prog_type;
typedef int /*<<< orphan*/ attr;
typedef int __u32;

/* Variables and functions */
int BPF_OBJ_NAME_LEN;
int /*<<< orphan*/ BPF_PROG_LOAD;
int /*<<< orphan*/ bzero(union bpf_attr *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, char const *, int);
void *ptr_to_u64(char const *);
int strlen(char const *);
int sys_bpf(int /*<<< orphan*/, union bpf_attr *, int);
int bpf_load_program_name(enum bpf_prog_type type, const char *name,
                          const struct bpf_insn *insns, size_t insns_cnt,
                          const char *license, __u32 kern_version,
                          char *log_buf, size_t log_buf_sz) {
  int fd;
  union bpf_attr attr;
  __u32 name_len = name ? strlen(name) : 0;
  bzero(&attr, sizeof(attr));
  attr.prog_type = type;
  attr.insn_cnt = (__u32)insns_cnt;
  attr.insns = ptr_to_u64(insns);
  attr.license = ptr_to_u64(license);
  attr.log_buf = ptr_to_u64(NULL);
  attr.log_size = 0;
  attr.log_level = 0;
  attr.kern_version = kern_version;
  memcpy(attr.prog_name, name,
         ((name_len) < (BPF_OBJ_NAME_LEN - 1) ? (name_len)
                                              : (BPF_OBJ_NAME_LEN - 1)));
  fd = sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
  if (fd >= 0 || !log_buf || !log_buf_sz)
    return fd;
  attr.log_buf = ptr_to_u64(log_buf);
  attr.log_size = log_buf_sz;
  attr.log_level = 1;
  log_buf[0] = 0;
  return sys_bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
}


x86 assembly (clang, -O0):
<bpf_load_program_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1193 <bpf_load_program_name+0x43>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x40(%rbp)
jmp    119d <bpf_load_program_name+0x4d>
xor    %eax,%eax
mov    %eax,-0x40(%rbp)
jmp    119d <bpf_load_program_name+0x4d>
mov    -0x40(%rbp),%eax
mov    %eax,-0x3c(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <ptr_to_u64@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <ptr_to_u64@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1050 <ptr_to_u64@plt>
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x3c(%rbp),%eax
mov    0x2dce(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    122a <bpf_load_program_name+0xda>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x50(%rbp)
jmp    1239 <bpf_load_program_name+0xe9>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x4c(%rbp),%edi
mov    -0x50(%rbp),%edx
call   1060 <memcpy@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1282 <bpf_load_program_name+0x132>
cmpq   $0x0,0x10(%rbp)
je     1282 <bpf_load_program_name+0x132>
cmpq   $0x0,0x18(%rbp)
jne    128d <bpf_load_program_name+0x13d>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ca <bpf_load_program_name+0x17a>
mov    0x10(%rbp),%rdi
call   1050 <ptr_to_u64@plt>
mov    %rax,-0x38(%rbp)
mov    0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x38(%rbp)
mov    0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_load_program_name>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r14d
mov    %r8,%r15
mov    %rdx,%r12
mov    %rsi,%rbx
mov    0x48(%rsp),%r13
test   %rsi,%rsi
je     116d <bpf_load_program_name+0x2d>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
jmp    116f <bpf_load_program_name+0x2f>
xor    %ebp,%ebp
mov    %r12,%rdi
call   1050 <ptr_to_u64@plt>
mov    %r15,%rdi
call   1050 <ptr_to_u64@plt>
xor    %edi,%edi
call   1050 <ptr_to_u64@plt>
movq   $0x0,(%rsp)
mov    %r14d,(%rsp)
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
add    $0xffffffff,%edx
cmp    %edx,%ebp
cmovl  %ebp,%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    0x2e33(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
test   %r13,%r13
je     11fe <bpf_load_program_name+0xbe>
mov    0x40(%rsp),%rbx
test   %rbx,%rbx
je     11fe <bpf_load_program_name+0xbe>
test   %eax,%eax
jns    11fe <bpf_load_program_name+0xbe>
mov    %rbx,%rdi
call   1050 <ptr_to_u64@plt>
mov    %r13,(%rsp)
movl   $0x1,(%rsp)
movb   $0x0,(%rbx)
mov    0x0(%rbp),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.bpf_insn = type opaque
%union.bpf_attr = type { i64 }
@BPF_OBJ_NAME_LEN = global i32 0
@BPF_PROG_LOAD = global i32 0
define i32 @bpf_load_program_name(i32 %0, i8* %1, %struct.bpf_insn* %2, i64 %3, i8* %4, i32 %5, i8* %6, i64 %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca %struct.bpf_insn*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i64
%18 = alloca i32
%19 = alloca %union.bpf_attr
%20 = alloca i32
store i32 %0, i32* %10
store i8* %1, i8** %11
store %struct.bpf_insn* %2, %struct.bpf_insn** %12
store i64 %3, i64* %13
store i8* %4, i8** %14
store i32 %5, i32* %15
store i8* %6, i8** %16
store i64 %7, i64* %17
%21 = load i8*, i8** %11
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %26
23: ; preds = %8
%24 = load i8*, i8** %11
%25 = call i32 @strlen(i8* %24)
br label %27
26: ; preds = %8
br label %27
27: ; preds = %26, %23
%28 = phi i32 [ %25, %23 ], [ 0, %26 ]
store i32 %28, i32* %20
%29 = call i32 @bzero(%union.bpf_attr* %19, i32 8)
%30 = load i32, i32* %10
%31 = bitcast %union.bpf_attr* %19 to i32*
store i32 %30, i32* %31
%32 = load i64, i64* %13
%33 = trunc i64 %32 to i32
%34 = bitcast %union.bpf_attr* %19 to i32*
store i32 %33, i32* %34
%35 = load %struct.bpf_insn*, %struct.bpf_insn** %12
%36 = bitcast %struct.bpf_insn* %35 to i8*
%37 = call i8* @ptr_to_u64(i8* %36)
%38 = bitcast %union.bpf_attr* %19 to i8**
store i8* %37, i8** %38
%39 = load i8*, i8** %14
%40 = call i8* @ptr_to_u64(i8* %39)
%41 = bitcast %union.bpf_attr* %19 to i8**
store i8* %40, i8** %41
%42 = call i8* @ptr_to_u64(i8* null)
%43 = bitcast %union.bpf_attr* %19 to i8**
store i8* %42, i8** %43
%44 = bitcast %union.bpf_attr* %19 to i64*
store i64 0, i64* %44
%45 = bitcast %union.bpf_attr* %19 to i32*
store i32 0, i32* %45
%46 = load i32, i32* %15
%47 = bitcast %union.bpf_attr* %19 to i32*
store i32 %46, i32* %47
%48 = bitcast %union.bpf_attr* %19 to i32*
%49 = load i32, i32* %48
%50 = load i8*, i8** %11
%51 = load i32, i32* %20
%52 = load i32, i32* @BPF_OBJ_NAME_LEN
%53 = sub nsw i32 %52, 1
%54 = icmp slt i32 %51, %53
br i1 %54, label %55, label %57
55: ; preds = %27
%56 = load i32, i32* %20
br label %60
57: ; preds = %27
%58 = load i32, i32* @BPF_OBJ_NAME_LEN
%59 = sub nsw i32 %58, 1
br label %60
60: ; preds = %57, %55
%61 = phi i32 [ %56, %55 ], [ %59, %57 ]
%62 = call i32 @memcpy(i32 %49, i8* %50, i32 %61)
%63 = load i32, i32* @BPF_PROG_LOAD
%64 = call i32 @sys_bpf(i32 %63, %union.bpf_attr* %19, i32 8)
store i32 %64, i32* %18
%65 = load i32, i32* %18
%66 = icmp sge i32 %65, 0
br i1 %66, label %73, label %67
67: ; preds = %60
%68 = load i8*, i8** %16
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %73
70: ; preds = %67
%71 = load i64, i64* %17
%72 = icmp ne i64 %71, 0
br i1 %72, label %75, label %73
73: ; preds = %70, %67, %60
%74 = load i32, i32* %18
store i32 %74, i32* %9
br label %86
75: ; preds = %70
%76 = load i8*, i8** %16
%77 = call i8* @ptr_to_u64(i8* %76)
%78 = bitcast %union.bpf_attr* %19 to i8**
store i8* %77, i8** %78
%79 = load i64, i64* %17
%80 = bitcast %union.bpf_attr* %19 to i64*
store i64 %79, i64* %80
%81 = bitcast %union.bpf_attr* %19 to i32*
store i32 1, i32* %81
%82 = load i8*, i8** %16
%83 = getelementptr inbounds i8, i8* %82, i64 0
store i8 0, i8* %83
%84 = load i32, i32* @BPF_PROG_LOAD
%85 = call i32 @sys_bpf(i32 %84, %union.bpf_attr* %19, i32 8)
store i32 %85, i32* %9
br label %86
86: ; preds = %75, %73
%87 = load i32, i32* %9
ret i32 %87
}

LLVM IR (-O2):
%struct.bpf_insn = type opaque
%union.bpf_attr = type { i64 }
@BPF_OBJ_NAME_LEN = global i32 0
@BPF_PROG_LOAD = global i32 0
define i32 @bpf_load_program_name(i32 %0, i8* %1, %struct.bpf_insn* %2, i64 %3, i8* %4, i32 %5, i8* %6, i64 %7) {
%9 = alloca %union.bpf_attr
%10 = bitcast %union.bpf_attr* %9 to i8*
%11 = icmp eq i8* %1, null
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %1)
br label %14
14: ; preds = %8, %12
%15 = phi i32 [ %13, %12 ], [ 0, %8 ]
%16 = bitcast %union.bpf_attr* %9 to i32*
%17 = bitcast %struct.bpf_insn* %2 to i8*
%18 = tail call i8* @ptr_to_u64(i8* %17)
%19 = tail call i8* @ptr_to_u64(i8* %4)
%20 = tail call i8* @ptr_to_u64(i8* null)
%21 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %9, i64 0, i32 0
store i64 0, i64* %21
store i32 %5, i32* %16
%22 = load i32, i32* @BPF_OBJ_NAME_LEN
%23 = add nsw i32 %22, -1
%24 = icmp slt i32 %15, %23
%25 = select i1 %24, i32 %15, i32 %23
%26 = tail call i32 @memcpy(i32 %5, i8* %1, i32 %25)
%27 = load i32, i32* @BPF_PROG_LOAD
%28 = call i32 @sys_bpf(i32 %27, %union.bpf_attr* %9, i32 8)
%29 = icmp slt i32 %28, 0
%30 = icmp ne i8* %6, null
%31 = and i1 %30, %29
%32 = icmp ne i64 %7, 0
%33 = and i1 %32, %31
br i1 %33, label %34, label %38
34: ; preds = %14
%35 = call i8* @ptr_to_u64(i8* %6)
store i64 %7, i64* %21
store i32 1, i32* %16
store i8 0, i8* %6
%36 = load i32, i32* @BPF_PROG_LOAD
%37 = call i32 @sys_bpf(i32 %36, %union.bpf_attr* %9, i32 8)
br label %38
38: ; preds = %14, %34
%39 = phi i32 [ %37, %34 ], [ %28, %14 ]
ret i32 %39
}