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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
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 *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
void test_filter_without_bind(void) {
  int fd1, fd2;
  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");
  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
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11bd <test_filter_without_bind+0x5d>
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe6c(%rip),%rdx
call   1070 <error@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11fd <test_filter_without_bind+0x9d>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edi
lea    0xe46(%rip),%rdx
call   1070 <error@plt>
mov    -0x4(%rbp),%edi
mov    $0xa,%esi
call   1030 <attach_ebpf@plt>
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1040 <attach_cbpf@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_filter_without_bind>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e68(%rip),%r14
mov    (%r14),%edi
mov    0x2e56(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11b4 <test_filter_without_bind+0x54>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
lea    0xe7a(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    (%r14),%edi
mov    0x0(%rbp),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11e1 <test_filter_without_bind+0x81>
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
lea    0xe67(%rip),%rdx
mov    $0x1,%edi
call   1070 <error@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1030 <attach_ebpf@plt>
mov    %ebp,%edi
mov    $0xa,%esi
call   1040 <attach_cbpf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <close@plt>

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"
define void @test_filter_without_bind() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @stderr
%4 = 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 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %1
%8 = load i32, i32* %1
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @errno
%12 = call i32 @error(i32 1, i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %10, %0
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_DGRAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %13
%20 = load i32, i32* @errno
%21 = call i32 @error(i32 1, i32 %20, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %19, %13
%23 = load i32, i32* %1
%24 = call i32 @attach_ebpf(i32 %23, i32 10)
%25 = load i32, i32* %2
%26 = call i32 @attach_cbpf(i32 %25, i32 10)
%27 = load i32, i32* %1
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
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"
define void @test_filter_without_bind() {
%1 = load i32, i32* @stderr
%2 = tail call i32 @fprintf(i32 %1, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @errno
%9 = tail call i32 @error(i32 1, i32 %8, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %7, %0
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_DGRAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = load i32, i32* @errno
%17 = tail call i32 @error(i32 1, i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %15, %10
%19 = tail call i32 @attach_ebpf(i32 %5, i32 10)
%20 = tail call i32 @attach_cbpf(i32 %13, i32 10)
%21 = tail call i32 @close(i32 %5)
%22 = tail call i32 @close(i32 %13)
ret void
}