#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 uint32_t;
typedef int /*<<< orphan*/ fd;

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_ATTACH_BPF;
int /*<<< orphan*/ assert(int);
int bpf_obj_get(char const *);
int bpf_obj_pin(int, char const *);
int bpf_prog_create(char const *);
int /*<<< orphan*/ errno;
int open_raw_sock(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
char *strerror(int /*<<< orphan*/);
int bpf_do_prog(const char *file, uint32_t flags, const char *object) {
  int fd, sock, ret;
  if (flags & (1 << 0)) {
    fd = bpf_prog_create(object);
    printf("bpf: prog fd:%d (%s)\n", fd, strerror(errno));
    assert(fd > 0);
    ret = bpf_obj_pin(fd, file);
    printf("bpf: pin ret:(%d,%s)\n", ret, strerror(errno));
    assert(ret == 0);
  } else {
    fd = bpf_obj_get(file);
    printf("bpf: get fd:%d (%s)\n", fd, strerror(errno));
    assert(fd > 0);
  }
  sock = open_raw_sock("lo");
  assert(sock > 0);
  ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &fd, sizeof(fd));
  printf("bpf: sock:%d <- fd:%d attached ret:(%d,%s)\n", sock, fd, ret,
         strerror(errno));
  assert(ret == 0);
  return 0;
}


x86 assembly (clang, -O0):
<bpf_do_prog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     1234 <bpf_do_prog+0xb4>
mov    -0x18(%rbp),%rdi
call   1080 <bpf_prog_create@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x2c(%rbp),%esi
mov    %rax,%rdx
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1040 <bpf_obj_pin@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%esi
mov    %rax,%rdx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
jmp    1279 <bpf_do_prog+0xf9>
mov    -0x8(%rbp),%rdi
call   1050 <bpf_obj_get@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
lea    0xdc1(%rip),%rdi
call   1030 <open_raw_sock@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
mov    -0x20(%rbp),%edi
mov    0x2d45(%rip),%rax
mov    (%rax),%esi
mov    0x2d24(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    %rax,%r8
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1090 <assert@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_do_prog>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
test   $0x1,%sil
jne    11c7 <bpf_do_prog+0x47>
mov    %rbx,%rdi
call   1050 <bpf_obj_get@plt>
mov    %eax,%ebx
mov    %eax,0xc(%rsp)
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xe78(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
setg   %al
jmp    1230 <bpf_do_prog+0xb0>
mov    %rdx,%rdi
call   1080 <bpf_prog_create@plt>
mov    %eax,%ebp
mov    %eax,0xc(%rsp)
mov    0x2dfc(%rip),%r14
mov    (%r14),%edi
call   10a0 <strerror@plt>
lea    0xe15(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1090 <assert@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <bpf_obj_pin@plt>
mov    %eax,%ebx
mov    (%r14),%edi
call   10a0 <strerror@plt>
lea    0xdf7(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <printf@plt>
test   %ebx,%ebx
sete   %al
movzbl %al,%edi
call   1090 <assert@plt>
lea    0xe02(%rip),%rdi
call   1030 <open_raw_sock@plt>
mov    %eax,%ebx
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1090 <assert@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1060 <setsockopt@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%r14d
mov    0x2d53(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdb1(%rip),%rdi
mov    %ebx,%esi
mov    %r14d,%edx
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
test   %ebp,%ebp
sete   %dil
call   1090 <assert@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"bpf: prog fd:%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"bpf: pin ret:(%d,%s)\0A\00"
@.str.2 = [21 x i8] c"bpf: get fd:%d (%s)\0A\00"
@.str.3 = [3 x i8] c"lo\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_BPF = global i32 0
@.str.4 = [44 x i8] c"bpf: sock:%d <- fd:%d attached ret:(%d,%s)\0A\00"
define i32 @bpf_do_prog(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i32, i32* %5
%11 = and i32 %10, 1
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %35
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = call i32 @bpf_prog_create(i8* %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %18)
%20 = load i32, i32* %7
%21 = icmp sgt i32 %20, 0
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %7
%25 = load i8*, i8** %4
%26 = call i32 @bpf_obj_pin(i32 %24, i8* %25)
store i32 %26, i32* %9
%27 = load i32, i32* %9
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %27, i8* %29)
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 0
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
br label %46
35: ; preds = %3
%36 = load i8*, i8** %4
%37 = call i32 @bpf_obj_get(i8* %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %38, i8* %40)
%42 = load i32, i32* %7
%43 = icmp sgt i32 %42, 0
%44 = zext i1 %43 to i32
%45 = call i32 @assert(i32 %44)
br label %46
46: ; preds = %35, %13
%47 = call i32 @open_raw_sock(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp sgt i32 %48, 0
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32, i32* %8
%53 = load i32, i32* @SOL_SOCKET
%54 = load i32, i32* @SO_ATTACH_BPF
%55 = call i32 @setsockopt(i32 %52, i32 %53, i32 %54, i32* %7, i32 4)
store i32 %55, i32* %9
%56 = load i32, i32* %8
%57 = load i32, i32* %7
%58 = load i32, i32* %9
%59 = load i32, i32* @errno
%60 = call i8* @strerror(i32 %59)
%61 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 %57, i32 %58, i8* %60)
%62 = load i32, i32* %9
%63 = icmp eq i32 %62, 0
%64 = zext i1 %63 to i32
%65 = call i32 @assert(i32 %64)
ret i32 0
}

LLVM IR (-O2):
@.str = [22 x i8] c"bpf: prog fd:%d (%s)\0A\00"
@errno = global i32 0
@.str.1 = [22 x i8] c"bpf: pin ret:(%d,%s)\0A\00"
@.str.2 = [21 x i8] c"bpf: get fd:%d (%s)\0A\00"
@.str.3 = [3 x i8] c"lo\00"
@SOL_SOCKET = global i32 0
@SO_ATTACH_BPF = global i32 0
@.str.4 = [44 x i8] c"bpf: sock:%d <- fd:%d attached ret:(%d,%s)\0A\00"
define i32 @bpf_do_prog(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = and i32 %1, 1
%7 = icmp eq i32 %6, 0
br i1 %7, label %21, label %8
8: ; preds = %3
%9 = tail call i32 @bpf_prog_create(i8* %2)
store i32 %9, i32* %4
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %11)
%13 = icmp sgt i32 %9, 0
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @bpf_obj_pin(i32 %9, i8* %0)
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %16, i8* %18)
%20 = icmp eq i32 %16, 0
br label %27
21: ; preds = %3
%22 = tail call i32 @bpf_obj_get(i8* %0)
store i32 %22, i32* %4
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 %22, i8* %24)
%26 = icmp sgt i32 %22, 0
br label %27
27: ; preds = %21, %8
%28 = phi i1 [ %26, %21 ], [ %20, %8 ]
%29 = zext i1 %28 to i32
%30 = tail call i32 @assert(i32 %29)
%31 = tail call i32 @open_raw_sock(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp sgt i32 %31, 0
%33 = zext i1 %32 to i32
%34 = tail call i32 @assert(i32 %33)
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_ATTACH_BPF
%37 = call i32 @setsockopt(i32 %31, i32 %35, i32 %36, i32* %4, i32 4)
%38 = load i32, i32* %4
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0), i32 %31, i32 %38, i32 %37, i8* %40)
%42 = icmp eq i32 %37, 0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
ret i32 0
}