#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*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEPORT;
int /*<<< orphan*/ attach_cbpf(int, int);
int /*<<< orphan*/ attach_ebpf(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void test_filter_without_bind(void) {
  int fd1, fd2, opt = 1;
  fprintf(stderr, "Testing filter add without bind...\n");
  fd1 = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd1 < 0)
    error(1, errno, "failed to create socket 1");
  fd2 = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd2 < 0)
    error(1, errno, "failed to create socket 2");
  if (setsockopt(fd1, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
    error(1, errno, "failed to set SO_REUSEPORT on socket 1");
  if (setsockopt(fd2, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)))
    error(1, errno, "failed to set SO_REUSEPORT on socket 2");
  attach_ebpf(fd1, 10);
  attach_cbpf(fd2, 10);
  close(fd1);
  close(fd2);
}


x86 assembly (clang, -O0):
<test_filter_without_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0xc(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11d4 <test_filter_without_bind+0x64>
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe55(%rip),%rdx
call   1080 <error@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1214 <test_filter_without_bind+0xa4>
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe2f(%rip),%rdx
call   1080 <error@plt>
mov    -0x4(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     125c <test_filter_without_bind+0xec>
mov    0x2d6f(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe01(%rip),%rdx
call   1080 <error@plt>
mov    -0x8(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    0x2d69(%rip),%rax
mov    (%rax),%edx
lea    -0xc(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12a4 <test_filter_without_bind+0x134>
mov    0x2d27(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xde0(%rip),%rdx
call   1080 <error@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1040 <attach_ebpf@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1050 <attach_cbpf@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_filter_without_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movl   $0x1,0x4(%rsp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e45(%rip),%r14
mov    (%r14),%edi
mov    0x2e2b(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11cf <test_filter_without_bind+0x5f>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
lea    0xe5f(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    (%r14),%edi
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11fc <test_filter_without_bind+0x8c>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xe4c(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    0x2de5(%rip),%r14
mov    (%r14),%esi
mov    0x2dcb(%rip),%r15
mov    (%r15),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1241 <test_filter_without_bind+0xd1>
mov    0x2d8a(%rip),%rax
mov    (%rax),%esi
lea    0xe21(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    (%r14),%esi
mov    (%r15),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1278 <test_filter_without_bind+0x108>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
lea    0xe11(%rip),%rdx
mov    $0x1,%edi
call   1080 <error@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1040 <attach_ebpf@plt>
mov    %ebp,%edi
mov    $0xa,%esi
call   1050 <attach_cbpf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"Testing filter add without bind...\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@.str.1 = [26 x i8] c"failed to create socket 1\00"
@.str.2 = [26 x i8] c"failed to create socket 2\00"
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@.str.3 = [39 x i8] c"failed to set SO_REUSEPORT on socket 1\00"
@.str.4 = [39 x i8] c"failed to set SO_REUSEPORT on socket 2\00"
define void @test_filter_without_bind() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 1, i32* %3
%4 = load i32, i32* @stderr
%5 = call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = call i32 @error(i32 1, i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %11, %0
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_DGRAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = call i32 @error(i32 1, i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %20, %14
%24 = load i32, i32* %1
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_REUSEPORT
%27 = call i64 @setsockopt(i32 %24, i32 %25, i32 %26, i32* %3, i32 4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* @errno
%31 = call i32 @error(i32 1, i32 %30, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %29, %23
%33 = load i32, i32* %2
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEPORT
%36 = call i64 @setsockopt(i32 %33, i32 %34, i32 %35, i32* %3, i32 4)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @errno
%40 = call i32 @error(i32 1, i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %38, %32
%42 = load i32, i32* %1
%43 = call i32 @attach_ebpf(i32 %42, i32 10)
%44 = load i32, i32* %2
%45 = call i32 @attach_cbpf(i32 %44, i32 10)
%46 = load i32, i32* %1
%47 = call i32 @close(i32 %46)
%48 = load i32, i32* %2
%49 = call i32 @close(i32 %48)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"Testing filter add without bind...\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@errno = global i32 0
@.str.1 = [26 x i8] c"failed to create socket 1\00"
@.str.2 = [26 x i8] c"failed to create socket 2\00"
@SOL_SOCKET = global i32 0
@SO_REUSEPORT = global i32 0
@.str.3 = [39 x i8] c"failed to set SO_REUSEPORT on socket 1\00"
@.str.4 = [39 x i8] c"failed to set SO_REUSEPORT on socket 2\00"
define void @test_filter_without_bind() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
store i32 1, i32* %1
%3 = load i32, i32* @stderr
%4 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_DGRAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i32 @error(i32 1, i32 %10, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %9, %0
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = tail call i32 @error(i32 1, i32 %18, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %17, %12
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEPORT
%23 = call i64 @setsockopt(i32 %7, i32 %21, i32 %22, i32* %1, i32 4)
%24 = icmp eq i64 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20
%26 = load i32, i32* @errno
%27 = call i32 @error(i32 1, i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %25, %20
%29 = load i32, i32* @SOL_SOCKET
%30 = load i32, i32* @SO_REUSEPORT
%31 = call i64 @setsockopt(i32 %15, i32 %29, i32 %30, i32* %1, i32 4)
%32 = icmp eq i64 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* @errno
%35 = call i32 @error(i32 1, i32 %34, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %33, %28
%37 = call i32 @attach_ebpf(i32 %7, i32 10)
%38 = call i32 @attach_cbpf(i32 %15, i32 10)
%39 = call i32 @close(i32 %7)
%40 = call i32 @close(i32 %15)
ret void
}