#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int prog_fd;
};
union bpf_attr {
  int key_size;
  int value_size;
  int max_entries;
  long insn_cnt;
  TYPE_1__ test;
  void *license;
  void *insns;
  int /*<<< orphan*/ prog_type;
  int /*<<< orphan*/ map_type;
};
typedef void *uint64_t;
typedef int /*<<< orphan*/ attr;

/* Variables and functions */
int /*<<< orphan*/ BPF_MAP_CREATE;
int /*<<< orphan*/ BPF_MAP_TYPE_HASH;
int /*<<< orphan*/ BPF_PROG_LOAD;
int /*<<< orphan*/ BPF_PROG_TEST_RUN;
int /*<<< orphan*/ BPF_PROG_TYPE_SOCKET_FILTER;
int /*<<< orphan*/ SYS_bpf;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ cover_start();
int /*<<< orphan*/ cover_stop();
int /*<<< orphan*/ memset(union bpf_attr *, int /*<<< orphan*/, int);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, union bpf_attr *, int);
void bpf(const char *data, long size) {
  union bpf_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.map_type = BPF_MAP_TYPE_HASH;
  attr.key_size = 8;
  attr.value_size = 8;
  attr.max_entries = 2;
  int mfd = syscall(SYS_bpf, BPF_MAP_CREATE, &attr, sizeof(attr));
  memset(&attr, 0, sizeof(attr));
  attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  attr.insns = (uint64_t)data;
  attr.insn_cnt = size / 8;
  attr.license = (uint64_t) "GPL";
  cover_start();
  int pfd = syscall(SYS_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  if (pfd != -1) {
    memset(&attr, 0, sizeof(attr));
    attr.test.prog_fd = pfd;
    syscall(SYS_bpf, BPF_PROG_TEST_RUN, &attr, sizeof(attr));
  }
  cover_stop();
  close(pfd);
  close(mfd);
}


x86 assembly (clang, -O0):
<bpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
movl   $0x8,-0x18(%rbp)
movl   $0x8,-0x18(%rbp)
movl   $0x2,-0x18(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e37(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x8,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
lea    0xe0f(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1060 <cover_start@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     125f <bpf+0x10f>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    0x2d71(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
mov    $0x0,%al
call   1070 <cover_stop@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
movl   $0x2,(%rsp)
mov    0x2e61(%rip),%r15
mov    (%r15),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
lea    0xe57(%rip),%rax
mov    %rax,(%rsp)
xor    %eax,%eax
call   1060 <cover_start@plt>
mov    (%r15),%edi
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1202 <bpf+0xb2>
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %ebx,(%rsp)
mov    (%r15),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdx
mov    $0x8,%ecx
call   1050 <syscall@plt>
xor    %eax,%eax
call   1070 <cover_stop@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %r14d,%edi
call   1040 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%union.bpf_attr = type { i64 }
%struct.TYPE_2__ = type { i32 }
@BPF_MAP_TYPE_HASH = global i32 0
@SYS_bpf = global i32 0
@BPF_MAP_CREATE = global i32 0
@BPF_PROG_TYPE_SOCKET_FILTER = global i32 0
@.str = [4 x i8] c"GPL\00"
@BPF_PROG_LOAD = global i32 0
@BPF_PROG_TEST_RUN = global i32 0
define void @bpf(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %union.bpf_attr
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = call i32 @memset(%union.bpf_attr* %5, i32 0, i32 8)
%9 = load i32, i32* @BPF_MAP_TYPE_HASH
%10 = bitcast %union.bpf_attr* %5 to i32*
store i32 %9, i32* %10
%11 = bitcast %union.bpf_attr* %5 to i32*
store i32 8, i32* %11
%12 = bitcast %union.bpf_attr* %5 to i32*
store i32 8, i32* %12
%13 = bitcast %union.bpf_attr* %5 to i32*
store i32 2, i32* %13
%14 = load i32, i32* @SYS_bpf
%15 = load i32, i32* @BPF_MAP_CREATE
%16 = call i32 @syscall(i32 %14, i32 %15, %union.bpf_attr* %5, i32 8)
store i32 %16, i32* %6
%17 = call i32 @memset(%union.bpf_attr* %5, i32 0, i32 8)
%18 = load i32, i32* @BPF_PROG_TYPE_SOCKET_FILTER
%19 = bitcast %union.bpf_attr* %5 to i32*
store i32 %18, i32* %19
%20 = load i8*, i8** %3
%21 = bitcast %union.bpf_attr* %5 to i8**
store i8* %20, i8** %21
%22 = load i64, i64* %4
%23 = sdiv i64 %22, 8
%24 = bitcast %union.bpf_attr* %5 to i64*
store i64 %23, i64* %24
%25 = bitcast %union.bpf_attr* %5 to i8**
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %25
%26 = call i32 (...) @cover_start()
%27 = load i32, i32* @SYS_bpf
%28 = load i32, i32* @BPF_PROG_LOAD
%29 = call i32 @syscall(i32 %27, i32 %28, %union.bpf_attr* %5, i32 8)
store i32 %29, i32* %7
%30 = load i32, i32* %7
%31 = icmp ne i32 %30, -1
br i1 %31, label %32, label %40
32: ; preds = %2
%33 = call i32 @memset(%union.bpf_attr* %5, i32 0, i32 8)
%34 = load i32, i32* %7
%35 = bitcast %union.bpf_attr* %5 to %struct.TYPE_2__*
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* @SYS_bpf
%38 = load i32, i32* @BPF_PROG_TEST_RUN
%39 = call i32 @syscall(i32 %37, i32 %38, %union.bpf_attr* %5, i32 8)
br label %40
40: ; preds = %32, %2
%41 = call i32 (...) @cover_stop()
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
ret void
}

LLVM IR (-O2):
%union.bpf_attr = type { i64 }
@BPF_MAP_TYPE_HASH = global i32 0
@SYS_bpf = global i32 0
@BPF_MAP_CREATE = global i32 0
@BPF_PROG_TYPE_SOCKET_FILTER = global i32 0
@.str = [4 x i8] c"GPL\00"
@BPF_PROG_LOAD = global i32 0
@BPF_PROG_TEST_RUN = global i32 0
define void @bpf(i8* %0, i64 %1) {
%3 = alloca %union.bpf_attr
%4 = bitcast %union.bpf_attr* %3 to i8*
%5 = call i32 @memset(%union.bpf_attr* %3, i32 0, i32 8)
%6 = bitcast %union.bpf_attr* %3 to i32*
store i32 2, i32* %6
%7 = load i32, i32* @SYS_bpf
%8 = load i32, i32* @BPF_MAP_CREATE
%9 = call i32 @syscall(i32 %7, i32 %8, %union.bpf_attr* %3, i32 8)
%10 = call i32 @memset(%union.bpf_attr* %3, i32 0, i32 8)
%11 = bitcast %union.bpf_attr* %3 to i8**
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = call i32 (...) @cover_start()
%13 = load i32, i32* @SYS_bpf
%14 = load i32, i32* @BPF_PROG_LOAD
%15 = call i32 @syscall(i32 %13, i32 %14, %union.bpf_attr* %3, i32 8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %22, label %17
17: ; preds = %2
%18 = call i32 @memset(%union.bpf_attr* %3, i32 0, i32 8)
store i32 %15, i32* %6
%19 = load i32, i32* @SYS_bpf
%20 = load i32, i32* @BPF_PROG_TEST_RUN
%21 = call i32 @syscall(i32 %19, i32 %20, %union.bpf_attr* %3, i32 8)
br label %22
22: ; preds = %17, %2
%23 = call i32 (...) @cover_stop()
%24 = call i32 @close(i32 %15)
%25 = call i32 @close(i32 %9)
ret void
}