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

/* Variables and functions */
int /*<<< orphan*/ BPF_PROG_LOAD;
int /*<<< orphan*/ bzero(union bpf_attr *, int);
void *ptr_to_u64(char *);
int sys_bpf(int /*<<< orphan*/, union bpf_attr *, int);
int bpf_load_program(enum bpf_prog_type type, struct bpf_insn *insns,
                     size_t insns_cnt, char *license, __u32 kern_version,
                     char *log_buf, size_t log_buf_sz) {
  int fd;
  union bpf_attr attr;
  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;
  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>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
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    %r9,-0x30(%rbp)
lea    -0x40(%rbp),%rdi
mov    $0x8,%esi
call   1050 <bzero@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ptr_to_u64@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <ptr_to_u64@plt>
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <ptr_to_u64@plt>
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2e34(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11e7 <bpf_load_program+0xb7>
cmpq   $0x0,-0x30(%rbp)
je     11e7 <bpf_load_program+0xb7>
cmpq   $0x0,0x10(%rbp)
jne    11f2 <bpf_load_program+0xc2>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122f <bpf_load_program+0xff>
mov    -0x30(%rbp),%rdi
call   1040 <ptr_to_u64@plt>
mov    %rax,-0x40(%rbp)
mov    0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_load_program>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%rbx
mov    %rsi,%rdi
mov    0x30(%rsp),%r12
call   1040 <ptr_to_u64@plt>
mov    %rbx,%rdi
call   1040 <ptr_to_u64@plt>
xor    %edi,%edi
call   1040 <ptr_to_u64@plt>
mov    %r15,(%rsp)
mov    0x2e90(%rip),%rbx
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
test   %r12,%r12
je     119b <bpf_load_program+0x7b>
test   %r14,%r14
je     119b <bpf_load_program+0x7b>
test   %eax,%eax
jns    119b <bpf_load_program+0x7b>
mov    %r14,%rdi
call   1040 <ptr_to_u64@plt>
mov    %r12,(%rsp)
movl   $0x1,(%rsp)
movb   $0x0,(%r14)
mov    (%rbx),%edi
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <sys_bpf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.bpf_insn = type opaque
%union.bpf_attr = type { i64 }
@BPF_PROG_LOAD = global i32 0
define i32 @bpf_load_program(i32 %0, %struct.bpf_insn* %1, i64 %2, i8* %3, i8* %4, i8* %5, i64 %6) {
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.bpf_insn*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
%17 = alloca %union.bpf_attr
store i32 %0, i32* %9
store %struct.bpf_insn* %1, %struct.bpf_insn** %10
store i64 %2, i64* %11
store i8* %3, i8** %12
store i8* %4, i8** %13
store i8* %5, i8** %14
store i64 %6, i64* %15
%18 = call i32 @bzero(%union.bpf_attr* %17, i32 8)
%19 = load i32, i32* %9
%20 = bitcast %union.bpf_attr* %17 to i32*
store i32 %19, i32* %20
%21 = load i64, i64* %11
%22 = inttoptr i64 %21 to i8*
%23 = bitcast %union.bpf_attr* %17 to i8**
store i8* %22, i8** %23
%24 = load %struct.bpf_insn*, %struct.bpf_insn** %10
%25 = bitcast %struct.bpf_insn* %24 to i8*
%26 = call i8* @ptr_to_u64(i8* %25)
%27 = bitcast %union.bpf_attr* %17 to i8**
store i8* %26, i8** %27
%28 = load i8*, i8** %12
%29 = call i8* @ptr_to_u64(i8* %28)
%30 = bitcast %union.bpf_attr* %17 to i8**
store i8* %29, i8** %30
%31 = call i8* @ptr_to_u64(i8* null)
%32 = bitcast %union.bpf_attr* %17 to i8**
store i8* %31, i8** %32
%33 = bitcast %union.bpf_attr* %17 to i64*
store i64 0, i64* %33
%34 = bitcast %union.bpf_attr* %17 to i32*
store i32 0, i32* %34
%35 = load i8*, i8** %13
%36 = bitcast %union.bpf_attr* %17 to i8**
store i8* %35, i8** %36
%37 = load i32, i32* @BPF_PROG_LOAD
%38 = call i32 @sys_bpf(i32 %37, %union.bpf_attr* %17, i32 8)
store i32 %38, i32* %16
%39 = load i32, i32* %16
%40 = icmp sge i32 %39, 0
br i1 %40, label %47, label %41
41: ; preds = %7
%42 = load i8*, i8** %14
%43 = icmp ne i8* %42, null
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i64, i64* %15
%46 = icmp ne i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44, %41, %7
%48 = load i32, i32* %16
store i32 %48, i32* %8
br label %60
49: ; preds = %44
%50 = load i8*, i8** %14
%51 = call i8* @ptr_to_u64(i8* %50)
%52 = bitcast %union.bpf_attr* %17 to i8**
store i8* %51, i8** %52
%53 = load i64, i64* %15
%54 = bitcast %union.bpf_attr* %17 to i64*
store i64 %53, i64* %54
%55 = bitcast %union.bpf_attr* %17 to i32*
store i32 1, i32* %55
%56 = load i8*, i8** %14
%57 = getelementptr inbounds i8, i8* %56, i64 0
store i8 0, i8* %57
%58 = load i32, i32* @BPF_PROG_LOAD
%59 = call i32 @sys_bpf(i32 %58, %union.bpf_attr* %17, i32 8)
store i32 %59, i32* %8
br label %60
60: ; preds = %49, %47
%61 = load i32, i32* %8
ret i32 %61
}

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