#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 bpf_tunnel_key {
  int tunnel_id;
  int remote_ipv4;
};
struct __sk_buff {
  int dummy;
};
typedef int /*<<< orphan*/ key;
typedef int /*<<< orphan*/ fmt;

/* Variables and functions */
int TC_ACT_OK;
int TC_ACT_SHOT;
int bpf_skb_get_tunnel_key(struct __sk_buff *, struct bpf_tunnel_key *, int,
                           int /*<<< orphan*/);
int /*<<< orphan*/ bpf_trace_printk(char *, int, int, int);
__attribute__((section("gre_get_tunnel"), used)) int
_gre_get_tunnel(struct __sk_buff *skb) {
  int ret;
  struct bpf_tunnel_key key;
  char fmt[] = "key %d remote ip 0x%x\n";
  ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0);
  if (ret < 0) {
    do {
      char fmt[] = "ERROR line:%d ret:%d\n";
      bpf_trace_printk(fmt, sizeof(fmt), 77, ret);
    } while (0);
    return TC_ACT_SHOT;
  }
  bpf_trace_printk(fmt, sizeof(fmt), key.tunnel_id, key.remote_ipv4);
  return TC_ACT_OK;
}


x86 assembly (clang, -O0):
<_gre_get_tunnel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0xecd(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xeca(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xec8(%rip),%eax
mov    %eax,-0x30(%rbp)
mov    0xec2(%rip),%ax
mov    %ax,-0x2c(%rbp)
mov    0xeba(%rip),%al
mov    %al,-0x2a(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <bpf_skb_get_tunnel_key@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d6 <_gre_get_tunnel+0xb6>
jmp    1185 <_gre_get_tunnel+0x65>
mov    0xe94(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0xe91(%rip),%rax
mov    %rax,-0x58(%rbp)
mov    0xe8f(%rip),%eax
mov    %eax,-0x50(%rbp)
mov    0xe89(%rip),%ax
mov    %ax,-0x4c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x16,%esi
mov    $0x4d,%edx
call   1040 <bpf_trace_printk@plt>
mov    0x2e1c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11f6 <_gre_get_tunnel+0xd6>
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    $0x17,%esi
call   1040 <bpf_trace_printk@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_gre_get_tunnel>:
sub    $0x48,%rsp
movaps 0xed5(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0xa782578302070,%rax
mov    %rax,0x3f(%rsp)
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1030 <bpf_skb_get_tunnel_key@plt>
test   %eax,%eax
js     1174 <_gre_get_tunnel+0x54>
mov    0x8(%rsp),%edx
mov    0xc(%rsp),%ecx
lea    0x30(%rsp),%rdi
mov    $0x17,%esi
call   1040 <bpf_trace_printk@plt>
mov    0x2e6e(%rip),%rax
jmp    11ac <_gre_get_tunnel+0x8c>
movaps 0xea5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0xa64253a746572,%rcx
mov    %rcx,0x1e(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x16,%esi
mov    $0x4d,%edx
mov    %eax,%ecx
call   1040 <bpf_trace_printk@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
add    $0x48,%rsp
ret

LLVM IR (-O0):
%struct.__sk_buff = type { i32 }
%struct.bpf_tunnel_key = type { i32, i32 }
@__const._gre_get_tunnel.fmt = [23 x i8] c"key %d remote ip 0x%x\0A\00"
@__const._gre_get_tunnel.fmt.1 = [22 x i8] c"ERROR line:%d ret:%d\0A\00"
@TC_ACT_SHOT = global i32 0
@TC_ACT_OK = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @_gre_get_tunnel to i8*)], section "llvm.metadata"
define i32 @_gre_get_tunnel(%struct.__sk_buff* %0) section "gre_get_tunnel" {
%2 = alloca i32
%3 = alloca %struct.__sk_buff*
%4 = alloca i32
%5 = alloca %struct.bpf_tunnel_key
%6 = alloca [23 x i8]
%7 = alloca [22 x i8]
store %struct.__sk_buff* %0, %struct.__sk_buff** %3
%8 = bitcast [23 x i8]* %6 to i8*
%9 = load %struct.__sk_buff*, %struct.__sk_buff** %3
%10 = call i32 @bpf_skb_get_tunnel_key(%struct.__sk_buff* %9, %struct.bpf_tunnel_key* %5, i32 8, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %1
br label %14
14: ; preds = %13
%15 = bitcast [22 x i8]* %7 to i8*
%16 = getelementptr inbounds [22 x i8], [22 x i8]* %7, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @bpf_trace_printk(i8* %16, i32 22, i32 77, i32 %17)
br label %19
19: ; preds = %14
%20 = load i32, i32* @TC_ACT_SHOT
store i32 %20, i32* %2
br label %29
21: ; preds = %1
%22 = getelementptr inbounds [23 x i8], [23 x i8]* %6, i64 0, i64 0
%23 = getelementptr inbounds %struct.bpf_tunnel_key, %struct.bpf_tunnel_key* %5, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.bpf_tunnel_key, %struct.bpf_tunnel_key* %5, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i32 @bpf_trace_printk(i8* %22, i32 23, i32 %24, i32 %26)
%28 = load i32, i32* @TC_ACT_OK
store i32 %28, i32* %2
br label %29
29: ; preds = %21, %19
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
%struct.__sk_buff = type { i32 }
%struct.bpf_tunnel_key = type { i32, i32 }
@__const._gre_get_tunnel.fmt = [23 x i8] c"key %d remote ip 0x%x\0A\00"
@__const._gre_get_tunnel.fmt.1 = [22 x i8] c"ERROR line:%d ret:%d\0A\00"
@TC_ACT_SHOT = global i32 0
@TC_ACT_OK = global i32 0
@llvm.compiler.used = appending global [1 x i8*] [i8* bitcast (i32 (%struct.__sk_buff*)* @_gre_get_tunnel to i8*)], section "llvm.metadata"
define i32 @_gre_get_tunnel(%struct.__sk_buff* %0) section "gre_get_tunnel" {
%2 = alloca %struct.bpf_tunnel_key
%3 = alloca [23 x i8]
%4 = alloca [22 x i8]
%5 = bitcast %struct.bpf_tunnel_key* %2 to i8*
%6 = getelementptr inbounds [23 x i8], [23 x i8]* %3, i64 0, i64 0
%7 = call i32 @bpf_skb_get_tunnel_key(%struct.__sk_buff* %0, %struct.bpf_tunnel_key* %2, i32 8, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = getelementptr inbounds [22 x i8], [22 x i8]* %4, i64 0, i64 0
%11 = call i32 @bpf_trace_printk(i8* %10, i32 22, i32 77, i32 %7)
br label %18
12: ; preds = %1
%13 = getelementptr inbounds %struct.bpf_tunnel_key, %struct.bpf_tunnel_key* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.bpf_tunnel_key, %struct.bpf_tunnel_key* %2, i64 0, i32 1
%16 = load i32, i32* %15
%17 = call i32 @bpf_trace_printk(i8* %6, i32 23, i32 %14, i32 %16)
br label %18
18: ; preds = %12, %9
%19 = phi i32* [ @TC_ACT_SHOT, %9 ], [ @TC_ACT_OK, %12 ]
%20 = load i32, i32* %19
ret i32 %20
}