#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 dummy;
};
typedef int /*<<< orphan*/ bpf_prog;
typedef int /*<<< orphan*/ bpf_filter;

/* Variables and functions */
scalar_t__ BPF_A;
scalar_t__ BPF_ABS;
scalar_t__ BPF_B;
scalar_t__ BPF_LD;
scalar_t__ BPF_RET;
struct sock_filter BPF_STMT(scalar_t__, int);
int /*<<< orphan*/ PACKET_FANOUT_DATA;
int /*<<< orphan*/ SOL_PACKET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                      struct sock_fprog *, int);
void sock_fanout_set_cbpf(int fd) {
  struct sock_filter bpf_filter[] = {
      BPF_STMT(BPF_LD + BPF_B + BPF_ABS, 80),
      BPF_STMT(BPF_RET + BPF_A, 0),
  };
  struct sock_fprog bpf_prog;
  bpf_prog.filter = bpf_filter;
  bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  if (setsockopt(fd, SOL_PACKET, PACKET_FANOUT_DATA, &bpf_prog,
                 sizeof(bpf_prog))) {
    perror("fanout data cbpf");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<sock_fanout_set_cbpf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%rdi
mov    0x2e5c(%rip),%rax
add    (%rax),%rdi
mov    0x2e6a(%rip),%rax
add    (%rax),%rdi
mov    $0x50,%esi
call   1040 <BPF_STMT@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%rdi
mov    0x2e51(%rip),%rax
add    (%rax),%rdi
xor    %esi,%esi
call   1040 <BPF_STMT@plt>
mov    %eax,-0xc(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x2,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11e7 <sock_fanout_set_cbpf+0xa7>
lea    0xe28(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_fanout_set_cbpf>:
push   %rbx
sub    $0x20,%rsp
mov    0x2e74(%rip),%rcx
mov    0x2e65(%rip),%rax
mov    (%rax),%rax
add    (%rcx),%rax
mov    %edi,%ebx
mov    0x2e6e(%rip),%rcx
add    (%rcx),%rax
mov    %rax,%rdi
mov    $0x50,%esi
call   1040 <BPF_STMT@plt>
mov    %eax,0x8(%rsp)
mov    0x2e63(%rip),%rax
mov    0x2e54(%rip),%rcx
mov    (%rcx),%rdi
add    (%rax),%rdi
xor    %esi,%esi
call   1040 <BPF_STMT@plt>
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11d6 <sock_fanout_set_cbpf+0x96>
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe23(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@BPF_LD = global i64 0
@BPF_B = global i64 0
@BPF_ABS = global i64 0
@BPF_RET = global i64 0
@BPF_A = global i64 0
@SOL_PACKET = global i32 0
@PACKET_FANOUT_DATA = global i32 0
@.str = [17 x i8] c"fanout data cbpf\00"
define void @sock_fanout_set_cbpf(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x %struct.sock_filter]
%4 = alloca %struct.sock_fprog
store i32 %0, i32* %2
%5 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %3, i64 0, i64 0
%6 = load i64, i64* @BPF_LD
%7 = load i64, i64* @BPF_B
%8 = add nsw i64 %6, %7
%9 = load i64, i64* @BPF_ABS
%10 = add nsw i64 %8, %9
%11 = call i32 @BPF_STMT(i64 %10, i32 80)
%12 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %5, i32 0, i32 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %5, i64 1
%14 = load i64, i64* @BPF_RET
%15 = load i64, i64* @BPF_A
%16 = add nsw i64 %14, %15
%17 = call i32 @BPF_STMT(i64 %16, i32 0)
%18 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %13, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %3, i64 0, i64 0
%20 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i32 0, i32 1
store %struct.sock_filter* %19, %struct.sock_filter** %20
%21 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %4, i32 0, i32 0
store i32 2, i32* %21
%22 = load i32, i32* %2
%23 = load i32, i32* @SOL_PACKET
%24 = load i32, i32* @PACKET_FANOUT_DATA
%25 = call i64 @setsockopt(i32 %22, i32 %23, i32 %24, %struct.sock_fprog* %4, i32 16)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %1
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %1
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@BPF_LD = global i64 0
@BPF_B = global i64 0
@BPF_ABS = global i64 0
@BPF_RET = global i64 0
@BPF_A = global i64 0
@SOL_PACKET = global i32 0
@PACKET_FANOUT_DATA = global i32 0
@.str = [17 x i8] c"fanout data cbpf\00"
define void @sock_fanout_set_cbpf(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 = load i64, i64* @BPF_LD
%7 = load i64, i64* @BPF_B
%8 = add nsw i64 %7, %6
%9 = load i64, i64* @BPF_ABS
%10 = add nsw i64 %8, %9
%11 = tail call i32 @BPF_STMT(i64 %10, i32 80)
%12 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 0, i32 0
store i32 %11, i32* %12
%13 = load i64, i64* @BPF_RET
%14 = load i64, i64* @BPF_A
%15 = add nsw i64 %14, %13
%16 = tail call i32 @BPF_STMT(i64 %15, i32 0)
%17 = getelementptr inbounds [2 x %struct.sock_filter], [2 x %struct.sock_filter]* %2, i64 0, i64 1, i32 0
store i32 %16, i32* %17
%18 = bitcast %struct.sock_fprog* %3 to i8*
%19 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %3, i64 0, i32 1
store %struct.sock_filter* %5, %struct.sock_filter** %19
%20 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %3, i64 0, i32 0
store i32 2, i32* %20
%21 = load i32, i32* @SOL_PACKET
%22 = load i32, i32* @PACKET_FANOUT_DATA
%23 = call i64 @setsockopt(i32 %0, i32 %21, i32 %22, %struct.sock_fprog* %3, i32 16)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %1
%26 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %1
ret void
}