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

/* Variables and functions */
int BPF_A;
int BPF_ABS;
int BPF_ALU;
int BPF_LD;
int BPF_MOD;
int BPF_RET;
int BPF_W;
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_cbpf(int fd, uint16_t mod) {
  struct sock_filter code[] = {
      {BPF_LD | BPF_W | BPF_ABS, 0, 0, 0},
      {BPF_ALU | BPF_MOD, 0, 0, mod},
      {BPF_RET | BPF_A, 0, 0, 0},
  };
  struct sock_fprog p = {
      .len = (sizeof(code) / sizeof((code)[0])),
      .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_cbpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%eax
mov    0x2eb2(%rip),%rcx
or     (%rcx),%eax
mov    0x2e81(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    0x2e27(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    0x2e36(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x3,-0x50(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    0x2de2(%rip),%rax
mov    (%rax),%edx
lea    -0x50(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     120b <attach_cbpf+0xeb>
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xdfa(%rip),%rdx
call   1040 <error@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_cbpf>:
sub    $0x48,%rsp
mov    0x2e85(%rip),%rax
mov    0x2ebe(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2e8b(%rip),%rax
or     (%rax),%ecx
mov    %ecx,(%rsp)
movq   $0x0,0x4(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2e46(%rip),%rax
mov    0x2e37(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
movq   $0x0,0x14(%rsp)
mov    %esi,0x1c(%rsp)
mov    0x2e5b(%rip),%rax
mov    0x2e4c(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x20(%rsp)
movq   $0x0,0x24(%rsp)
movl   $0x0,0x2c(%rsp)
movl   $0x3,0x38(%rsp)
mov    %rsp,%rax
mov    %rax,0x40(%rsp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
lea    0x38(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     11ee <attach_cbpf+0xce>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
lea    0xe1c(%rip),%rdx
mov    $0x1,%edi
call   1040 <error@plt>
add    $0x48,%rsp
ret

LLVM IR (-O0):
%struct.sock_filter = type { i32, i32, 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
@BPF_ALU = global i32 0
@BPF_MOD = global i32 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_cbpf(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [3 x %struct.sock_filter]
%6 = alloca %struct.sock_fprog
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %5, i64 0, i64 0
%8 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %7, i32 0, i32 0
%9 = load i32, i32* @BPF_LD
%10 = load i32, i32* @BPF_W
%11 = or i32 %9, %10
%12 = load i32, i32* @BPF_ABS
%13 = or i32 %11, %12
store i32 %13, i32* %8
%14 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %7, i32 0, i32 1
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %7, i32 0, i32 2
store i32 0, i32* %15
%16 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %7, i32 0, i32 3
store i32 0, i32* %16
%17 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %7, i64 1
%18 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %17, i32 0, i32 0
%19 = load i32, i32* @BPF_ALU
%20 = load i32, i32* @BPF_MOD
%21 = or i32 %19, %20
store i32 %21, i32* %18
%22 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %17, i32 0, i32 1
store i32 0, i32* %22
%23 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %17, i32 0, i32 2
store i32 0, i32* %23
%24 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %17, i32 0, i32 3
%25 = load i32, i32* %4
store i32 %25, i32* %24
%26 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %17, i64 1
%27 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 0
%28 = load i32, i32* @BPF_RET
%29 = load i32, i32* @BPF_A
%30 = or i32 %28, %29
store i32 %30, i32* %27
%31 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 1
store i32 0, i32* %31
%32 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 2
store i32 0, i32* %32
%33 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %26, i32 0, i32 3
store i32 0, i32* %33
%34 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %6, i32 0, i32 0
store i32 3, i32* %34
%35 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %6, i32 0, i32 1
%36 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %5, i64 0, i64 0
store %struct.sock_filter* %36, %struct.sock_filter** %35
%37 = load i32, i32* %3
%38 = load i32, i32* @SOL_SOCKET
%39 = load i32, i32* @SO_ATTACH_REUSEPORT_CBPF
%40 = call i64 @setsockopt(i32 %37, i32 %38, i32 %39, %struct.sock_fprog* %6, i32 16)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %2
%43 = load i32, i32* @errno
%44 = call i32 @error(i32 1, i32 %43, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %42, %2
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32, i32, 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
@BPF_ALU = global i32 0
@BPF_MOD = global i32 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_cbpf(i32 %0, i32 %1) {
%3 = alloca [3 x %struct.sock_filter]
%4 = alloca %struct.sock_fprog
%5 = bitcast [3 x %struct.sock_filter]* %3 to i8*
%6 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 0
%7 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 0, i32 0
%8 = load i32, i32* @BPF_LD
%9 = load i32, i32* @BPF_W
%10 = or i32 %9, %8
%11 = load i32, i32* @BPF_ABS
%12 = or i32 %10, %11
store i32 %12, i32* %7
%13 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 0, i32 3
store i32 0, i32* %15
%16 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 1, i32 0
%17 = load i32, i32* @BPF_ALU
%18 = load i32, i32* @BPF_MOD
%19 = or i32 %18, %17
store i32 %19, i32* %16
%20 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 1, i32 1
store i32 0, i32* %20
%21 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 1, i32 2
store i32 0, i32* %21
%22 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 1, i32 3
store i32 %1, i32* %22
%23 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 2, i32 0
%24 = load i32, i32* @BPF_RET
%25 = load i32, i32* @BPF_A
%26 = or i32 %25, %24
store i32 %26, i32* %23
%27 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 2, i32 1
store i32 0, i32* %27
%28 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 2, i32 2
store i32 0, i32* %28
%29 = getelementptr inbounds [3 x %struct.sock_filter], [3 x %struct.sock_filter]* %3, i64 0, i64 2, i32 3
store i32 0, i32* %29
%30 = bitcast %struct.sock_fprog* %4 to i8*
%31 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 0
store i32 3, i32* %31
%32 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i64 0, i32 1
store %struct.sock_filter* %6, %struct.sock_filter** %32
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_ATTACH_REUSEPORT_CBPF
%35 = call i64 @setsockopt(i32 %0, i32 %33, i32 %34, %struct.sock_fprog* %4, i32 16)
%36 = icmp eq i64 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %2
%38 = load i32, i32* @errno
%39 = call i32 @error(i32 1, i32 %38, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %37, %2
ret void
}