#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 ipv6hdr {
  int /*<<< orphan*/ nexthdr;
  int /*<<< orphan*/ saddr;
};
struct bpf_flow_keys {
  int thoff;
  int flags;
  int /*<<< orphan*/ flow_label;
  int /*<<< orphan*/ ip_proto;
  int /*<<< orphan*/ ipv6_src;
  int /*<<< orphan*/ addr_proto;
};
struct __sk_buff {
  struct bpf_flow_keys *flow_keys;
};

/* Variables and functions */
int /*<<< orphan*/ BPF_DROP;
int BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL;
int /*<<< orphan*/ BPF_OK;
int /*<<< orphan*/ ETH_P_IPV6;
struct ipv6hdr *bpf_flow_dissect_get_header(struct __sk_buff *, int,
                                            struct ipv6hdr *);
int export_flow_keys(struct bpf_flow_keys *, int /*<<< orphan*/);
int /*<<< orphan*/ ip6_flowlabel(struct ipv6hdr *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int parse_ipv6_proto(struct __sk_buff *, int /*<<< orphan*/);
__attribute__((section("IPV6"), used)) int
bpf_func_IPV6(struct __sk_buff *skb) {
  struct bpf_flow_keys *keys = skb->flow_keys;
  struct ipv6hdr *ip6h, _ip6h;
  ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
  if (!ip6h)
    return export_flow_keys(keys, BPF_DROP);
  keys->addr_proto = ETH_P_IPV6;
  memcpy(&keys->ipv6_src, &ip6h->saddr, 2 * sizeof(ip6h->saddr));
  keys->thoff += sizeof(struct ipv6hdr);
  keys->ip_proto = ip6h->nexthdr;
  keys->flow_label = ip6_flowlabel(ip6h);
  if (keys->flags & BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
    return export_flow_keys(keys, BPF_OK);
  return parse_ipv6_proto(skb, ip6h->nexthdr);
}


x86 assembly (clang, -O0):
<bpf_func_IPV6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
lea    -0x28(%rbp),%rdx
call   1050 <bpf_flow_dissect_get_header@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11a2 <bpf_func_IPV6+0x52>
mov    -0x18(%rbp),%rdi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
call   1040 <export_flow_keys@plt>
mov    %eax,-0x4(%rbp)
jmp    123d <bpf_func_IPV6+0xed>
mov    0x2e27(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x14(%rax)
mov    -0x18(%rbp),%rdi
add    $0x10,%rdi
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x18(%rbp),%rax
movslq (%rax),%rcx
add    $0x8,%rcx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x20(%rbp),%rdi
call   1070 <ip6_flowlabel@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    0x2dea(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     122b <bpf_func_IPV6+0xdb>
mov    -0x18(%rbp),%rdi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
call   1040 <export_flow_keys@plt>
mov    %eax,-0x4(%rbp)
jmp    123d <bpf_func_IPV6+0xed>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
call   1030 <parse_ipv6_proto@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bpf_func_IPV6>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    (%rdi),%rbx
lea    0x8(%rsp),%rdx
mov    $0x8,%esi
call   1050 <bpf_flow_dissect_get_header@plt>
test   %rax,%rax
je     11c2 <bpf_func_IPV6+0x72>
mov    %rax,%r15
mov    0x2e53(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rbx)
lea    0x10(%rbx),%rdi
mov    %r15,%rsi
add    $0x4,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
addl   $0x8,(%rbx)
mov    (%r15),%eax
mov    %eax,0xc(%rbx)
mov    %r15,%rdi
call   1070 <ip6_flowlabel@plt>
mov    %eax,0x8(%rbx)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
test   %eax,0x4(%rbx)
je     11d5 <bpf_func_IPV6+0x85>
mov    0x2e08(%rip),%rax
jmp    11c9 <bpf_func_IPV6+0x79>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <export_flow_keys@plt>
jmp    11e0 <bpf_func_IPV6+0x90>
mov    (%r15),%esi
mov    %r14,%rdi
call   1030 <parse_ipv6_proto@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.__sk_buff = type { %struct.bpf_flow_keys* }
%struct.bpf_flow_keys = type { i32, i32, i32, i32, i32, i32 }
%struct.ipv6hdr = type { i32, i32 }
@BPF_DROP = global i32 0
@ETH_P_IPV6 = global i32 0
@BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL = global i32 0
@BPF_OK = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @bpf_func_IPV6 to i8*)], section "llvm.metadata"
define i32 @bpf_func_IPV6(%struct.__sk_buff* %0) section "IPV6" {
%2 = alloca i32
%3 = alloca %struct.__sk_buff*
%4 = alloca %struct.bpf_flow_keys*
%5 = alloca %struct.ipv6hdr*
%6 = alloca %struct.ipv6hdr
store %struct.__sk_buff* %0, %struct.__sk_buff** %3
%7 = load %struct.__sk_buff*, %struct.__sk_buff** %3
%8 = getelementptr inbounds %struct.__sk_buff, %struct.__sk_buff* %7, i32 0, i32 0
%9 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %8
store %struct.bpf_flow_keys* %9, %struct.bpf_flow_keys** %4
%10 = load %struct.__sk_buff*, %struct.__sk_buff** %3
%11 = call %struct.ipv6hdr* @bpf_flow_dissect_get_header(%struct.__sk_buff* %10, i32 8, %struct.ipv6hdr* %6)
store %struct.ipv6hdr* %11, %struct.ipv6hdr** %5
%12 = load %struct.ipv6hdr*, %struct.ipv6hdr** %5
%13 = icmp ne %struct.ipv6hdr* %12, null
br i1 %13, label %18, label %14
14: ; preds = %1
%15 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%16 = load i32, i32* @BPF_DROP
%17 = call i32 @export_flow_keys(%struct.bpf_flow_keys* %15, i32 %16)
store i32 %17, i32* %2
br label %58
18: ; preds = %1
%19 = load i32, i32* @ETH_P_IPV6
%20 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%21 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %20, i32 0, i32 5
store i32 %19, i32* %21
%22 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%23 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %22, i32 0, i32 4
%24 = load %struct.ipv6hdr*, %struct.ipv6hdr** %5
%25 = getelementptr inbounds %struct.ipv6hdr, %struct.ipv6hdr* %24, i32 0, i32 1
%26 = call i32 @memcpy(i32* %23, i32* %25, i32 8)
%27 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%28 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = sext i32 %29 to i64
%31 = add i64 %30, 8
%32 = trunc i64 %31 to i32
store i32 %32, i32* %28
%33 = load %struct.ipv6hdr*, %struct.ipv6hdr** %5
%34 = getelementptr inbounds %struct.ipv6hdr, %struct.ipv6hdr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%37 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %36, i32 0, i32 3
store i32 %35, i32* %37
%38 = load %struct.ipv6hdr*, %struct.ipv6hdr** %5
%39 = call i32 @ip6_flowlabel(%struct.ipv6hdr* %38)
%40 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%41 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %40, i32 0, i32 2
store i32 %39, i32* %41
%42 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%43 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load i32, i32* @BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
%46 = and i32 %44, %45
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %18
%49 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%50 = load i32, i32* @BPF_OK
%51 = call i32 @export_flow_keys(%struct.bpf_flow_keys* %49, i32 %50)
store i32 %51, i32* %2
br label %58
52: ; preds = %18
%53 = load %struct.__sk_buff*, %struct.__sk_buff** %3
%54 = load %struct.ipv6hdr*, %struct.ipv6hdr** %5
%55 = getelementptr inbounds %struct.ipv6hdr, %struct.ipv6hdr* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @parse_ipv6_proto(%struct.__sk_buff* %53, i32 %56)
store i32 %57, i32* %2
br label %58
58: ; preds = %52, %48, %14
%59 = load i32, i32* %2
ret i32 %59
}

LLVM IR (-O2):
%struct.__sk_buff = type { %struct.bpf_flow_keys* }
%struct.bpf_flow_keys = type { i32, i32, i32, i32, i32, i32 }
%struct.ipv6hdr = type { i32, i32 }
@BPF_DROP = global i32 0
@ETH_P_IPV6 = global i32 0
@BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL = global i32 0
@BPF_OK = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @bpf_func_IPV6 to i8*)], section "llvm.metadata"
define i32 @bpf_func_IPV6(%struct.__sk_buff* %0) section "IPV6" {
%2 = alloca %struct.ipv6hdr
%3 = getelementptr inbounds %struct.__sk_buff, %struct.__sk_buff* %0, i64 0, i32 0
%4 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %3
%5 = bitcast %struct.ipv6hdr* %2 to i8*
%6 = call %struct.ipv6hdr* @bpf_flow_dissect_get_header(%struct.__sk_buff* %0, i32 8, %struct.ipv6hdr* %2)
%7 = icmp eq %struct.ipv6hdr* %6, null
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @BPF_DROP
%10 = call i32 @export_flow_keys(%struct.bpf_flow_keys* %4, i32 %9)
br label %36
11: ; preds = %1
%12 = load i32, i32* @ETH_P_IPV6
%13 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 5
store i32 %12, i32* %13
%14 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 4
%15 = getelementptr inbounds %struct.ipv6hdr, %struct.ipv6hdr* %6, i64 0, i32 1
%16 = call i32 @memcpy(i32* %14, i32* %15, i32 8)
%17 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 0
%18 = load i32, i32* %17
%19 = add i32 %18, 8
store i32 %19, i32* %17
%20 = getelementptr inbounds %struct.ipv6hdr, %struct.ipv6hdr* %6, i64 0, i32 0
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 3
store i32 %21, i32* %22
%23 = call i32 @ip6_flowlabel(%struct.ipv6hdr* %6)
%24 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 1
%26 = load i32, i32* %25
%27 = load i32, i32* @BPF_FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL
%28 = and i32 %27, %26
%29 = icmp eq i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %11
%31 = load i32, i32* @BPF_OK
%32 = call i32 @export_flow_keys(%struct.bpf_flow_keys* %4, i32 %31)
br label %36
33: ; preds = %11
%34 = load i32, i32* %20
%35 = call i32 @parse_ipv6_proto(%struct.__sk_buff* %0, i32 %34)
br label %36
36: ; preds = %33, %30, %8
%37 = phi i32 [ %32, %30 ], [ %35, %33 ], [ %10, %8 ]
ret i32 %37
}