#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 ipv6_opt_hdr {
  int hdrlen;
  int /*<<< orphan*/ nexthdr;
};
struct bpf_flow_keys {
  int thoff;
  int /*<<< orphan*/ ip_proto;
};
struct __sk_buff {
  struct bpf_flow_keys *flow_keys;
};

/* Variables and functions */
int /*<<< orphan*/ BPF_DROP;
struct ipv6_opt_hdr *bpf_flow_dissect_get_header(struct __sk_buff *, int,
                                                 struct ipv6_opt_hdr *);
int export_flow_keys(struct bpf_flow_keys *, int /*<<< orphan*/);
int parse_ipv6_proto(struct __sk_buff *, int /*<<< orphan*/);
__attribute__((section("IPV6OP"), used)) int
bpf_func_IPV6OP(struct __sk_buff *skb) {
  struct bpf_flow_keys *keys = skb->flow_keys;
  struct ipv6_opt_hdr *ip6h, _ip6h;
  ip6h = bpf_flow_dissect_get_header(skb, sizeof(*ip6h), &_ip6h);
  if (!ip6h)
    return export_flow_keys(keys, BPF_DROP);
  keys->thoff += (1 + ip6h->hdrlen) << 3;
  keys->ip_proto = ip6h->nexthdr;
  return parse_ipv6_proto(skb, ip6h->nexthdr);
}


x86 assembly (clang, -O0):
<bpf_func_IPV6OP>:
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    1182 <bpf_func_IPV6OP+0x52>
mov    -0x18(%rbp),%rdi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
call   1040 <export_flow_keys@plt>
mov    %eax,-0x4(%rbp)
jmp    11b7 <bpf_func_IPV6OP+0x87>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
shl    $0x3,%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    0x4(%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_IPV6OP>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    (%rdi),%r14
mov    %rsp,%rdx
mov    $0x8,%esi
call   1050 <bpf_flow_dissect_get_header@plt>
test   %rax,%rax
je     116b <bpf_func_IPV6OP+0x3b>
mov    (%rax),%ecx
mov    (%r14),%edx
lea    (%rdx,%rcx,8),%ecx
add    $0x8,%ecx
mov    %ecx,(%r14)
mov    0x4(%rax),%esi
mov    %esi,0x4(%r14)
mov    %rbx,%rdi
call   1030 <parse_ipv6_proto@plt>
jmp    117c <bpf_func_IPV6OP+0x4c>
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1040 <export_flow_keys@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.__sk_buff = type { %struct.bpf_flow_keys* }
%struct.bpf_flow_keys = type { i32, i32 }
%struct.ipv6_opt_hdr = type { i32, i32 }
@BPF_DROP = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @bpf_func_IPV6OP to i8*)], section "llvm.metadata"
define i32 @bpf_func_IPV6OP(%struct.__sk_buff* %0) section "IPV6OP" {
%2 = alloca i32
%3 = alloca %struct.__sk_buff*
%4 = alloca %struct.bpf_flow_keys*
%5 = alloca %struct.ipv6_opt_hdr*
%6 = alloca %struct.ipv6_opt_hdr
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.ipv6_opt_hdr* @bpf_flow_dissect_get_header(%struct.__sk_buff* %10, i32 8, %struct.ipv6_opt_hdr* %6)
store %struct.ipv6_opt_hdr* %11, %struct.ipv6_opt_hdr** %5
%12 = load %struct.ipv6_opt_hdr*, %struct.ipv6_opt_hdr** %5
%13 = icmp ne %struct.ipv6_opt_hdr* %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 %38
18: ; preds = %1
%19 = load %struct.ipv6_opt_hdr*, %struct.ipv6_opt_hdr** %5
%20 = getelementptr inbounds %struct.ipv6_opt_hdr, %struct.ipv6_opt_hdr* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 1, %21
%23 = shl i32 %22, 3
%24 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%25 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = add nsw i32 %26, %23
store i32 %27, i32* %25
%28 = load %struct.ipv6_opt_hdr*, %struct.ipv6_opt_hdr** %5
%29 = getelementptr inbounds %struct.ipv6_opt_hdr, %struct.ipv6_opt_hdr* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = load %struct.bpf_flow_keys*, %struct.bpf_flow_keys** %4
%32 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %31, i32 0, i32 1
store i32 %30, i32* %32
%33 = load %struct.__sk_buff*, %struct.__sk_buff** %3
%34 = load %struct.ipv6_opt_hdr*, %struct.ipv6_opt_hdr** %5
%35 = getelementptr inbounds %struct.ipv6_opt_hdr, %struct.ipv6_opt_hdr* %34, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 @parse_ipv6_proto(%struct.__sk_buff* %33, i32 %36)
store i32 %37, i32* %2
br label %38
38: ; preds = %18, %14
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.__sk_buff = type { %struct.bpf_flow_keys* }
%struct.bpf_flow_keys = type { i32, i32 }
%struct.ipv6_opt_hdr = type { i32, i32 }
@BPF_DROP = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @bpf_func_IPV6OP to i8*)], section "llvm.metadata"
define i32 @bpf_func_IPV6OP(%struct.__sk_buff* %0) section "IPV6OP" {
%2 = alloca %struct.ipv6_opt_hdr
%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.ipv6_opt_hdr* %2 to i8*
%6 = call %struct.ipv6_opt_hdr* @bpf_flow_dissect_get_header(%struct.__sk_buff* %0, i32 8, %struct.ipv6_opt_hdr* %2)
%7 = icmp eq %struct.ipv6_opt_hdr* %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 %23
11: ; preds = %1
%12 = getelementptr inbounds %struct.ipv6_opt_hdr, %struct.ipv6_opt_hdr* %6, i64 0, i32 0
%13 = load i32, i32* %12
%14 = shl i32 %13, 3
%15 = add i32 %14, 8
%16 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = add nsw i32 %15, %17
store i32 %18, i32* %16
%19 = getelementptr inbounds %struct.ipv6_opt_hdr, %struct.ipv6_opt_hdr* %6, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.bpf_flow_keys, %struct.bpf_flow_keys* %4, i64 0, i32 1
store i32 %20, i32* %21
%22 = call i32 @parse_ipv6_proto(%struct.__sk_buff* %0, i32 %20)
br label %23
23: ; preds = %11, %8
%24 = phi i32 [ %22, %11 ], [ %10, %8 ]
ret i32 %24
}