#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;
  int /*<<< orphan*/ prog_type;
};
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_CALL;
int BPF_EXIT;
int /*<<< orphan*/ BPF_FUNC_get_numa_node_id;
int BPF_JMP;
int /*<<< orphan*/ BPF_PROG_LOAD;
int /*<<< orphan*/ BPF_PROG_TYPE_SOCKET_FILTER;
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_bpf(int fd) {
  static char bpf_log_buf[65536];
  static const char bpf_license[] = "";
  int bpf_fd;
  const struct bpf_insn prog[] = {
      {BPF_JMP | BPF_CALL, 0, 0, 0, BPF_FUNC_get_numa_node_id},
      {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;
  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_bpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
add    $0x14,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    0x2e23(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movl   $0x2,-0x38(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0xe14(%rip),%rax
mov    %rax,-0x38(%rbp)
lea    0x2e69(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x10000,-0x38(%rbp)
movl   $0x1,-0x38(%rbp)
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
lea    -0x38(%rbp),%rdx
mov    $0x8,%ecx
call   1060 <syscall@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1259 <attach_bpf+0x109>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdb6(%rip),%rdx
lea    0x2e0e(%rip),%rcx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
lea    -0x8(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12a3 <attach_bpf+0x153>
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xd7a(%rip),%rdx
mov    $0x0,%al
call   1070 <error@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_bpf>:
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>
lea    0x2eed(%rip),%rax
mov    %rax,0x10(%rsp)
movl   $0x1,0x10(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdx
mov    $0x8,%ecx
call   1060 <syscall@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    11ca <attach_bpf+0x7a>
mov    0x2e1a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdx
lea    0x2ea2(%rip),%rcx
mov    $0x1,%edi
xor    %eax,%eax
call   1070 <error@plt>
mov    0x2e17(%rip),%rax
mov    (%rax),%esi
mov    0x2de6(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     120f <attach_bpf+0xbf>
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
lea    0xe12(%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_bpf.bpf_log_buf = internal global [65536 x i8] zeroinitializer
@attach_bpf.bpf_license = internal constant [1 x i8] zeroinitializer
@BPF_JMP = global i32 0
@BPF_CALL = global i32 0
@BPF_FUNC_get_numa_node_id = 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_bpf(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x %struct.bpf_insn]
%5 = alloca %union.bpf_attr
store i32 %0, i32* %2
%6 = getelementptr inbounds [2 x %struct.bpf_insn], [2 x %struct.bpf_insn]* %4, i64 0, i64 0
%7 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i32 0, i32 0
%8 = load i32, i32* @BPF_JMP
%9 = load i32, i32* @BPF_CALL
%10 = or i32 %8, %9
store i32 %10, i32* %7
%11 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i32 0, i32 1
store i32 0, i32* %11
%12 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i32 0, i32 2
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i32 0, i32 3
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i32 0, i32 4
%15 = load i32, i32* @BPF_FUNC_get_numa_node_id
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %6, i64 1
%17 = bitcast %struct.bpf_insn* %16 to i8*
%18 = getelementptr inbounds %struct.bpf_insn, %struct.bpf_insn* %16, i32 0, i32 0
%19 = load i32, i32* @BPF_JMP
%20 = load i32, i32* @BPF_EXIT
%21 = or i32 %19, %20
store i32 %21, i32* %18
%22 = call i32 @memset(%union.bpf_attr* %5, i32 0, i32 8)
%23 = load i32, i32* @BPF_PROG_TYPE_SOCKET_FILTER
%24 = bitcast %union.bpf_attr* %5 to i32*
store i32 %23, i32* %24
%25 = bitcast %union.bpf_attr* %5 to i32*
store i32 2, i32* %25
%26 = ptrtoint [2 x %struct.bpf_insn]* %4 to i64
%27 = bitcast %union.bpf_attr* %5 to i64*
store i64 %26, i64* %27
%28 = bitcast %union.bpf_attr* %5 to i64*
store i64 ptrtoint ([1 x i8]* @attach_bpf.bpf_license to i64), i64* %28
%29 = bitcast %union.bpf_attr* %5 to i64*
store i64 ptrtoint ([65536 x i8]* @attach_bpf.bpf_log_buf to i64), i64* %29
%30 = bitcast %union.bpf_attr* %5 to i32*
store i32 65536, i32* %30
%31 = bitcast %union.bpf_attr* %5 to i32*
store i32 1, i32* %31
%32 = load i32, i32* @__NR_bpf
%33 = load i32, i32* @BPF_PROG_LOAD
%34 = call i32 @syscall(i32 %32, i32 %33, %union.bpf_attr* %5, i32 8)
store i32 %34, i32* %3
%35 = load i32, i32* %3
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %1
%38 = load i32, i32* @errno
%39 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %38, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([65536 x i8], [65536 x i8]* @attach_bpf.bpf_log_buf, i64 0, i64 0))
br label %40
40: ; preds = %37, %1
%41 = load i32, i32* %2
%42 = load i32, i32* @SOL_SOCKET
%43 = load i32, i32* @SO_ATTACH_REUSEPORT_EBPF
%44 = call i64 @setsockopt(i32 %41, i32 %42, i32 %43, i32* %3, i32 4)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = call i32 (i32, i32, i8*, ...) @error(i32 1, i32 %47, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %49
49: ; preds = %46, %40
%50 = load i32, i32* %3
%51 = call i32 @close(i32 %50)
ret void
}

LLVM IR (-O2):
%union.bpf_attr = type { i64 }
@attach_bpf.bpf_log_buf = internal global [65536 x i8] zeroinitializer
@BPF_JMP = global i32 0
@BPF_CALL = global i32 0
@BPF_FUNC_get_numa_node_id = 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_bpf(i32 %0) {
%2 = alloca i32
%3 = alloca %union.bpf_attr
%4 = bitcast i32* %2 to i8*
%5 = bitcast %union.bpf_attr* %3 to i8*
%6 = call i32 @memset(%union.bpf_attr* %3, i32 0, i32 8)
%7 = bitcast %union.bpf_attr* %3 to i32*
%8 = getelementptr inbounds %union.bpf_attr, %union.bpf_attr* %3, i64 0, i32 0
store i64 ptrtoint ([65536 x i8]* @attach_bpf.bpf_log_buf to i64), i64* %8
store i32 1, i32* %7
%9 = load i32, i32* @__NR_bpf
%10 = load i32, i32* @BPF_PROG_LOAD
%11 = call i32 @syscall(i32 %9, i32 %10, %union.bpf_attr* %3, i32 8)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%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_bpf.bpf_log_buf, i64 0, i64 0))
br label %16
16: ; preds = %13, %1
%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* %2, 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* %2
%26 = call i32 @close(i32 %25)
ret void
}