#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 */
struct sock_fprog {
  int len;
  struct sock_filter *filter;
};
struct sock_filter {
  int member_0;
  scalar_t__ member_3;
  int /*<<< orphan*/ member_2;
  int /*<<< orphan*/ member_1;
};
typedef int /*<<< orphan*/ p;

/* Variables and functions */
int BPF_A;
int BPF_ABS;
int BPF_LD;
int BPF_RET;
int BPF_W;
scalar_t__ SKF_AD_CPU;
scalar_t__ SKF_AD_OFF;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ATTACH_REUSEPORT_CBPF;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct sock_fprog *, int);
void attach_bpf(int fd) {
  struct sock_filter code[] = {
      {BPF_LD | BPF_W | BPF_ABS, 0, 0, SKF_AD_OFF + SKF_AD_CPU},
      {BPF_RET | BPF_A, 0, 0, 0},
  };
  struct sock_fprog p = {
      .len = 2,
      .filter = code,
  };
  if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p))) {
    error(1, errno, "failed to set SO_ATTACH_REUSEPORT_CBPF");
  }
}


x86 assembly (clang, -O0):
<attach_bpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%eax
mov    0x2e95(%rip),%rcx
or     (%rcx),%eax
mov    0x2e64(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e48(%rip),%rax
mov    (%rax),%rax
mov    0x2e46(%rip),%rcx
add    (%rcx),%rax
mov    %eax,-0x2c(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x28(%rbp)
movl   $0x2,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
lea    -0x50(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11ec <attach_bpf+0xbc>
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe19(%rip),%rdx
call   1050 <error@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_bpf>:
sub    $0x48,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
movaps %xmm0,0x20(%rsp)
mov    0x2e68(%rip),%rax
mov    0x2eb1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
movaps %xmm0,0x30(%rsp)
mov    0x2e79(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2e5c(%rip),%rax
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%ecx
add    (%rax),%ecx
mov    %ecx,0x24(%rsp)
mov    0x2e66(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x28(%rsp)
movl   $0x2,(%rsp)
lea    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    0x2e06(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11d1 <attach_bpf+0xb1>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
add    $0x48,%rsp
ret

LLVM IR (-O0):
%struct.sock_filter = type { i32, i64, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@BPF_LD = global i32 0
@BPF_W = global i32 0
@BPF_ABS = global i32 0
@SKF_AD_OFF = global i64 0
@SKF_AD_CPU = global i64 0
@BPF_RET = global i32 0
@BPF_A = global i32 0
@SOL_SOCKET = global i32 0
@SO_ATTACH_REUSEPORT_CBPF = global i32 0
@errno = global i32 0
@.str = [39 x i8] c"failed to set SO_ATTACH_REUSEPORT_CBPF\00"
define void @attach_bpf(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x %struct.sock_filter]
%4 = alloca %struct.sock_fprog
store i32 %0, i32* %2
%5 = bitcast [2 x %struct.sock_filter]* %3 to i8*
%6 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %3, i64 0, i64 0
%7 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %6, i32 0, i32 0
%8 = load i32, i32* @BPF_LD
%9 = load i32, i32* @BPF_W
%10 = or i32 %8, %9
%11 = load i32, i32* @BPF_ABS
%12 = or i32 %10, %11
store i32 %12, i32* %7
%13 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %6, i32 0, i32 3
%14 = load i64, i64* @SKF_AD_OFF
%15 = load i64, i64* @SKF_AD_CPU
%16 = add nsw i64 %14, %15
%17 = trunc i64 %16 to i32
store i32 %17, i32* %13
%18 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %6, i64 1
%19 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %18, i32 0, i32 0
%20 = load i32, i32* @BPF_RET
%21 = load i32, i32* @BPF_A
%22 = or i32 %20, %21
store i32 %22, i32* %19
%23 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i32 0, i32 0
store i32 2, i32* %23
%24 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i32 0, i32 1
%25 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %3, i64 0, i64 0
store %struct.sock_filter* %25, %struct.sock_filter** %24
%26 = load i32, i32* %2
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_ATTACH_REUSEPORT_CBPF
%29 = call i64 @setsockopt(i32 %26, i32 %27, i32 %28, %struct.sock_fprog* %4, i32 16)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %1
%32 = load i32, i32* @errno
%33 = call i32 @error(i32 1, i32 %32, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %34
34: ; preds = %31, %1
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32, i64, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@BPF_LD = global i32 0
@BPF_W = global i32 0
@BPF_ABS = global i32 0
@SKF_AD_OFF = global i64 0
@SKF_AD_CPU = global i64 0
@BPF_RET = global i32 0
@BPF_A = global i32 0
@SOL_SOCKET = global i32 0
@SO_ATTACH_REUSEPORT_CBPF = global i32 0
@errno = global i32 0
@.str = [39 x i8] c"failed to set SO_ATTACH_REUSEPORT_CBPF\00"
define void @attach_bpf(i32 %0) {
%2 = alloca [2 x %struct.sock_filter]
%3 = alloca %struct.sock_fprog
%4 = bitcast [2 x %struct.sock_filter]* %2 to i8*
%5 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 0
%6 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 0, i32 0
%7 = load i32, i32* @BPF_LD
%8 = load i32, i32* @BPF_W
%9 = or i32 %8, %7
%10 = load i32, i32* @BPF_ABS
%11 = or i32 %9, %10
store i32 %11, i32* %6
%12 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 0, i32 3
%13 = load i64, i64* @SKF_AD_OFF
%14 = load i64, i64* @SKF_AD_CPU
%15 = add nsw i64 %14, %13
%16 = trunc i64 %15 to i32
store i32 %16, i32* %12
%17 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 1, i32 0
%18 = load i32, i32* @BPF_RET
%19 = load i32, i32* @BPF_A
%20 = or i32 %19, %18
store i32 %20, i32* %17
%21 = bitcast %struct.sock_fprog* %3 to i8*
%22 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %3, i64 0, i32 0
store i32 2, i32* %22
%23 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %3, i64 0, i32 1
store %struct.sock_filter* %5, %struct.sock_filter** %23
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_ATTACH_REUSEPORT_CBPF
%26 = call i64 @setsockopt(i32 %0, i32 %24, i32 %25, %struct.sock_fprog* %3, i32 16)
%27 = icmp eq i64 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %1
%29 = load i32, i32* @errno
%30 = call i32 @error(i32 1, i32 %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %31
31: ; preds = %28, %1
ret void
}