#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;
  int member_1;
  int member_2;
  int member_3;
  int code;
};
typedef int /*<<< orphan*/ bpf_prog;
typedef int /*<<< orphan*/ bpf_filter;

/* Variables and functions */
int PACKET_FANOUT_DATA;
int SOL_PACKET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int, int, struct sock_fprog *, int);
__attribute__((__unused__)) void sock_setfilter(int fd, int lvl, int optnum) {
  struct sock_filter bpf_filter[] = {
      {0x80, 0, 0, 0x00000000}, {0x35, 0, 4, 100}, {0x30, 0, 0, 0x00000050},
      {0x15, 1, 0, 'a'},        {0x15, 0, 1, 'b'}, {0x06, 0, 0, 0x00000060},
      {0x06, 0, 0, 0x00000000},
  };
  struct sock_fprog bpf_prog;
  if (lvl == SOL_PACKET && optnum == PACKET_FANOUT_DATA)
    bpf_filter[5].code = 0x16;
  bpf_prog.filter = bpf_filter;
  bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
  if (setsockopt(fd, lvl, optnum, &bpf_prog, sizeof(bpf_prog))) {
    perror("setsockopt SO_ATTACH_FILTER");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<sock_setfilter>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0xa0(%rbp),%rdi
lea    0xe9e(%rip),%rsi
mov    $0x8c,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%eax
mov    0x2e7a(%rip),%rcx
cmp    (%rcx),%eax
jne    1197 <sock_setfilter+0x57>
mov    -0xc(%rbp),%eax
mov    0x2e50(%rip),%rcx
cmp    (%rcx),%eax
jne    1197 <sock_setfilter+0x57>
movl   $0x16,-0x2c(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
movl   $0x7,-0xb0(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    -0xb0(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     11ea <sock_setfilter+0xaa>
lea    0xeb1(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_setfilter>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xea5(%rip),%rsi
lea    0x10(%rsp),%r15
mov    $0x8c,%edx
mov    %r15,%rdi
call   1040 <memcpy@plt>
mov    0x2e7c(%rip),%rax
cmp    %ebp,(%rax)
jne    118e <sock_setfilter+0x4e>
mov    0x2e59(%rip),%rax
cmp    %ebx,(%rax)
jne    118e <sock_setfilter+0x4e>
movl   $0x16,0x84(%rsp)
mov    %r15,0x8(%rsp)
movl   $0x7,(%rsp)
mov    %rsp,%rcx
mov    %r14d,%edi
mov    %ebp,%esi
mov    %ebx,%edx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11c2 <sock_setfilter+0x82>
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xec3(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sock_filter = type { i32, i32, i32, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@__const.sock_setfilter.bpf_filter = [7 x %struct.sock_filter] [%struct.sock_filter { i32 128, i32 0, i32 0, i32 0, i32 0 }, %struct.sock_filter { i32 53, i32 0, i32 4, i32 100, i32 0 }, %struct.sock_filter { i32 48, i32 0, i32 0, i32 80, i32 0 }, %struct.sock_filter { i32 21, i32 1, i32 0, i32 97, i32 0 }, %struct.sock_filter { i32 21, i32 0, i32 1, i32 98, i32 0 }, %struct.sock_filter { i32 6, i32 0, i32 0, i32 96, i32 0 }, %struct.sock_filter { i32 6, i32 0, i32 0, i32 0, i32 0 }]
@SOL_PACKET = global i32 0
@PACKET_FANOUT_DATA = global i32 0
@.str = [28 x i8] c"setsockopt SO_ATTACH_FILTER\00"
define void @sock_setfilter(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [7 x %struct.sock_filter]
%8 = alloca %struct.sock_fprog
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%9 = bitcast [7 x %struct.sock_filter]* %7 to i8*
%10 = load i32, i32* %5
%11 = load i32, i32* @SOL_PACKET
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %20
13: ; preds = %3
%14 = load i32, i32* %6
%15 = load i32, i32* @PACKET_FANOUT_DATA
%16 = icmp eq i32 %14, %15
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = getelementptr inbounds [7 x %struct.sock_filter], [7 x %struct.sock_filter]* %7, i64 0, i64 5
%19 = getelementptr inbounds %struct.sock_filter, %struct.sock_filter* %18, i32 0, i32 4
store i32 22, i32* %19
br label %20
20: ; preds = %17, %13, %3
%21 = getelementptr inbounds [7 x %struct.sock_filter], [7 x %struct.sock_filter]* %7, i64 0, i64 0
%22 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %8, i32 0, i32 1
store %struct.sock_filter* %21, %struct.sock_filter** %22
%23 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %8, i32 0, i32 0
store i32 7, i32* %23
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = call i64 @setsockopt(i32 %24, i32 %25, i32 %26, %struct.sock_fprog* %8, i32 16)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %20
ret void
}

LLVM IR (-O2):
%struct.sock_filter = type { i32, i32, i32, i32, i32 }
%struct.sock_fprog = type { i32, %struct.sock_filter* }
@__const.sock_setfilter.bpf_filter = [7 x %struct.sock_filter] [%struct.sock_filter { i32 128, i32 0, i32 0, i32 0, i32 0 }, %struct.sock_filter { i32 53, i32 0, i32 4, i32 100, i32 0 }, %struct.sock_filter { i32 48, i32 0, i32 0, i32 80, i32 0 }, %struct.sock_filter { i32 21, i32 1, i32 0, i32 97, i32 0 }, %struct.sock_filter { i32 21, i32 0, i32 1, i32 98, i32 0 }, %struct.sock_filter { i32 6, i32 0, i32 0, i32 96, i32 0 }, %struct.sock_filter { i32 6, i32 0, i32 0, i32 0, i32 0 }]
@SOL_PACKET = global i32 0
@PACKET_FANOUT_DATA = global i32 0
@.str = [28 x i8] c"setsockopt SO_ATTACH_FILTER\00"
define void @sock_setfilter(i32 %0, i32 %1, i32 %2) {
%4 = alloca [7 x %struct.sock_filter]
%5 = alloca %struct.sock_fprog
%6 = bitcast [7 x %struct.sock_filter]* %4 to i8*
%7 = bitcast %struct.sock_fprog* %5 to i8*
%8 = load i32, i32* @SOL_PACKET
%9 = icmp eq i32 %8, %1
%10 = load i32, i32* @PACKET_FANOUT_DATA
%11 = icmp eq i32 %10, %2
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %15
13: ; preds = %3
%14 = getelementptr inbounds [7 x %struct.sock_filter], [7 x %struct.sock_filter]* %4, i64 0, i64 5, i32 4
store i32 22, i32* %14
br label %15
15: ; preds = %13, %3
%16 = getelementptr inbounds [7 x %struct.sock_filter], [7 x %struct.sock_filter]* %4, i64 0, i64 0
%17 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %5, i64 0, i32 1
store %struct.sock_filter* %16, %struct.sock_filter** %17
%18 = getelementptr inbounds %struct.sock_fprog, %struct.sock_fprog* %5, i64 0, i32 0
store i32 7, i32* %18
%19 = call i64 @setsockopt(i32 %0, i32 %1, i32 %2, %struct.sock_fprog* %5, i32 16)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
ret void
}