#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 insn_cnt;
  unsigned long insns;
  unsigned long license;
  unsigned long log_buf;
  int log_size;
  int log_level;
  scalar_t__ kern_version;
  int /*<<< orphan*/ prog_type;
};
typedef int /*<<< orphan*/ uint16_t;
struct bpf_insn {
  int member_0;
  int /*<<< orphan*/ member_4;
  int /*<<< orphan*/ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ prog;
typedef int /*<<< orphan*/ bpf_log_buf;
typedef int /*<<< orphan*/ bpf_fd;
typedef int /*<<< orphan*/ attr;

/* Variables and functions */
int BPF_ABS;
int BPF_ALU64;
int BPF_EXIT;
int BPF_JMP;
int BPF_K;
int BPF_LD;
int BPF_MOD;
int BPF_MOV;
int /*<<< orphan*/ BPF_PROG_LOAD;
int /*<<< orphan*/ BPF_PROG_TYPE_SOCKET_FILTER;
int /*<<< orphan*/ BPF_REG_0;
int /*<<< orphan*/ BPF_REG_1;
int /*<<< orphan*/ BPF_REG_6;
int BPF_W;
int BPF_X;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ATTACH_REUSEPORT_EBPF;
int /*<<< orphan*/ __NR_bpf;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(union bpf_attr *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, union bpf_attr *, int);
void attach_ebpf(int fd, uint16_t mod) {
  static char bpf_log_buf[65536];
  static const char bpf_license[] = "GPL";
  int bpf_fd;
  const struct bpf_insn prog[] = {
      {BPF_ALU64 | BPF_MOV | BPF_X, BPF_REG_6, BPF_REG_1, 0, 0},
      {BPF_LD | BPF_ABS | BPF_W, 0, 0, 0, 0},
      {BPF_ALU64 | BPF_MOD | BPF_K, BPF_REG_0, 0, 0, mod},
      {BPF_JMP | BPF_EXIT, 0, 0, 0, 0}};
  union bpf_attr attr;
  memset(&attr, 0, sizeof(attr));
  attr.prog_type = BPF_PROG_TYPE_SOCKET_FILTER;
  attr.insn_cnt = (sizeof(prog) / sizeof((prog)[0]));
  attr.insns = (unsigned long)&prog;
  attr.license = (unsigned long)&bpf_license;
  attr.log_buf = (unsigned long)&bpf_log_buf;
  attr.log_size = sizeof(bpf_log_buf);
  attr.log_level = 1;
  attr.kern_version = 0;
  bpf_fd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr));
  if (bpf_fd < 0)
    error(1, errno, "ebpf error. log:\n%s\n", bpf_log_buf);
  if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &bpf_fd,
                 sizeof(bpf_fd)))
    error(1, errno, "failed to set SO_ATTACH_REUSEPORT_EBPF");
  close(bpf_fd);
}


x86 assembly (clang, -O0):
<attach_ebpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x60(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    0x2dfe(%rip),%rcx
or     (%rcx),%eax
mov    0x2e25(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x60(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x5c(%rbp)
mov    0x2dcd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x50(%rbp)
add    $0x14,%rdi
mov    %rdi,-0x70(%rbp)
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    -0x70(%rbp),%rdi
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    0x2e02(%rip),%rcx
or     (%rcx),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x4c(%rbp)
add    $0x14,%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%eax
mov    0x2d78(%rip),%rcx
or     (%rcx),%eax
mov    0x2ddf(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x28(%rbp)
add    $0x14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    0x2d82(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
lea    -0x68(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2cf6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x68(%rbp)
movl   $0x4,-0x68(%rbp)
lea    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    0xd8b(%rip),%rax
mov    %rax,-0x68(%rbp)
lea    0x2de0(%rip),%rax
mov    %rax,-0x68(%rbp)
movl   $0x10000,-0x68(%rbp)
movl   $0x1,-0x68(%rbp)
movq   $0x0,-0x68(%rbp)
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
lea    -0x68(%rbp),%rdx
mov    $0x8,%ecx
call   1060 <syscall@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12ea <attach_ebpf+0x19a>
mov    0x2caa(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd28(%rip),%rdx
lea    0x2d7d(%rip),%rcx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
mov    0x2cec(%rip),%rax
mov    (%rax),%esi
mov    0x2c5b(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     1334 <attach_ebpf+0x1e4>
mov    0x2c59(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xcec(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_ebpf>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
movq   $0x0,0x10(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
mov    $0x8,%ecx
call   1060 <syscall@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    11bf <attach_ebpf+0x6f>
mov    0x2e25(%rip),%rax
mov    (%rax),%esi
lea    0xe54(%rip),%rdx
lea    0x2ead(%rip),%rcx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <error@plt>
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1204 <attach_ebpf+0xb4>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <error@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.bpf_insn = type { i32, i32, i32, i32, i32 }
%union.bpf_attr = type { i64 }
@attach_ebpf.bpf_log_buf = internal global [65536 x i8] zeroinitializer
@attach_ebpf.bpf_license = internal constant [4 x i8] c"GPL\00"
@BPF_ALU64 = global i32 0
@BPF_MOV = global i32 0
@BPF_X = global i32 0
@BPF_REG_6 = global i32 0
@BPF_REG_1 = global i32 0
@BPF_LD = global i32 0
@BPF_ABS = global i32 0
@BPF_W = global i32 0
@BPF_MOD = global i32 0
@BPF_K = global i32 0
@BPF_REG_0 = global i32 0
@BPF_JMP = global i32 0
@BPF_EXIT = global i32 0
@BPF_PROG_TYPE_SOCKET_FILTER = global i32 0
@__NR_bpf = global i32 0
@BPF_PROG_LOAD = global i32 0
@errno = global i32 0
@.str = [21 x i8] c"ebpf error. log:\0A%s\0A\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_REUSEPORT_EBPF = global i32 0
@.str.1 = [39 x i8] c"failed to set SO_ATTACH_REUSEPORT_EBPF\00"
define void @attach_ebpf(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [4 x %struct.bpf_insn]
%7 = alloca %union.bpf_attr
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = getelementptr inbounds [4 x %struct.bpf_insn], [4 x %struct.bpf_insn]* %6, i64 0, i64 0
%9 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i32 0, i32 0
%10 = load i32, i32* @BPF_ALU64
%11 = load i32, i32* @BPF_MOV
%12 = or i32 %10, %11
%13 = load i32, i32* @BPF_X
%14 = or i32 %12, %13
store i32 %14, i32* %9
%15 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i32 0, i32 1
%16 = load i32, i32* @BPF_REG_6
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i32 0, i32 2
%18 = load i32, i32* @BPF_REG_1
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i32 0, i32 3
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i32 0, i32 4
store i32 0, i32* %20
%21 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %8, i64 1
%22 = bitcast %struct.bpf_insn* %21 to i8*
%23 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %21, i32 0, i32 0
%24 = load i32, i32* @BPF_LD
%25 = load i32, i32* @BPF_ABS
%26 = or i32 %24, %25
%27 = load i32, i32* @BPF_W
%28 = or i32 %26, %27
store i32 %28, i32* %23
%29 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %21, i64 1
%30 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i32 0, i32 0
%31 = load i32, i32* @BPF_ALU64
%32 = load i32, i32* @BPF_MOD
%33 = or i32 %31, %32
%34 = load i32, i32* @BPF_K
%35 = or i32 %33, %34
store i32 %35, i32* %30
%36 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i32 0, i32 1
%37 = load i32, i32* @BPF_REG_0
store i32 %37, i32* %36
%38 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i32 0, i32 2
store i32 0, i32* %38
%39 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i32 0, i32 3
store i32 0, i32* %39
%40 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i32 0, i32 4
%41 = load i32, i32* %4
store i32 %41, i32* %40
%42 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %29, i64 1
%43 = bitcast %struct.bpf_insn* %42 to i8*
%44 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %42, i32 0, i32 0
%45 = load i32, i32* @BPF_JMP
%46 = load i32, i32* @BPF_EXIT
%47 = or i32 %45, %46
store i32 %47, i32* %44
%48 = call i32 @memset(%union.bpf_attr* %7, i32 0, i32 8)
%49 = load i32, i32* @BPF_PROG_TYPE_SOCKET_FILTER
%50 = bitcast %union.bpf_attr* %7 to i32*
store i32 %49, i32* %50
%51 = bitcast %union.bpf_attr* %7 to i32*
store i32 4, i32* %51
%52 = ptrtoint [4 x %struct.bpf_insn]* %6 to i64
%53 = bitcast %union.bpf_attr* %7 to i64*
store i64 %52, i64* %53
%54 = bitcast %union.bpf_attr* %7 to i64*
store i64 ptrtoint ([4 x i8]* @attach_ebpf.bpf_license to i64), i64* %54
%55 = bitcast %union.bpf_attr* %7 to i64*
store i64 ptrtoint ([65536 x i8]* @attach_ebpf.bpf_log_buf to i64), i64* %55
%56 = bitcast %union.bpf_attr* %7 to i32*
store i32 65536, i32* %56
%57 = bitcast %union.bpf_attr* %7 to i32*
store i32 1, i32* %57
%58 = bitcast %union.bpf_attr* %7 to i64*
store i64 0, i64* %58
%59 = load i32, i32* @__NR_bpf
%60 = load i32, i32* @BPF_PROG_LOAD
%61 = call i32 @syscall(i32 %59, i32 %60, %union.bpf_attr* %7, i32 8)
store i32 %61, i32* %5
%62 = load i32, i32* %5
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %2
%65 = load i32, i32* @errno
%66 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %65, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([65536 x i8], [65536 x i8]* @attach_ebpf.bpf_log_buf, i64 0, i64 0))
br label %67
67: ; preds = %64, %2
%68 = load i32, i32* %3
%69 = load i32, i32* @SOL_SOCKET
%70 = load i32, i32* @SO_ATTACH_REUSEPORT_EBPF
%71 = call i64 @setsockopt(i32 %68, i32 %69, i32 %70, i32* %5, i32 4)
%72 = icmp ne i64 %71, 0
br i1 %72, label %73, label %76
73: ; preds = %67
%74 = load i32, i32* @errno
%75 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %74, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %76
76: ; preds = %73, %67
%77 = load i32, i32* %5
%78 = call i32 @close(i32 %77)
ret void
}

LLVM IR (-O2):
%union.bpf_attr = type { i64 }
@attach_ebpf.bpf_log_buf = internal global [65536 x i8] zeroinitializer
@BPF_ALU64 = global i32 0
@BPF_MOV = global i32 0
@BPF_X = global i32 0
@BPF_REG_6 = global i32 0
@BPF_REG_1 = global i32 0
@BPF_LD = global i32 0
@BPF_ABS = global i32 0
@BPF_W = global i32 0
@BPF_MOD = global i32 0
@BPF_K = global i32 0
@BPF_REG_0 = global i32 0
@BPF_JMP = global i32 0
@BPF_EXIT = global i32 0
@BPF_PROG_TYPE_SOCKET_FILTER = global i32 0
@__NR_bpf = global i32 0
@BPF_PROG_LOAD = global i32 0
@errno = global i32 0
@.str = [21 x i8] c"ebpf error. log:\0A%s\0A\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_REUSEPORT_EBPF = global i32 0
@.str.1 = [39 x i8] c"failed to set SO_ATTACH_REUSEPORT_EBPF\00"
define void @attach_ebpf(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca %union.bpf_attr
%5 = bitcast i32* %3 to i8*
%6 = bitcast %union.bpf_attr* %4 to i8*
%7 = call i32 @memset(%union.bpf_attr* %4, i32 0, i32 8)
%8 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %4, i64 0, i32 0
store i64 0, i64* %8
%9 = load i32, i32* @__NR_bpf
%10 = load i32, i32* @BPF_PROG_LOAD
%11 = call i32 @syscall(i32 %9, i32 %10, %union.bpf_attr* %4, i32 8)
store i32 %11, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %14, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([65536 x i8], [65536 x i8]* @attach_ebpf.bpf_log_buf, i64 0, i64 0))
br label %16
16: ; preds = %13, %2
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_ATTACH_REUSEPORT_EBPF
%19 = call i64 @setsockopt(i32 %0, i32 %17, i32 %18, i32* %3, i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %22, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
%25 = load i32, i32* %3
%26 = call i32 @close(i32 %25)
ret void
}