#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 sk_buff {
  int dummy;
};

/* Variables and functions */
scalar_t__ ETH_P_IP;
scalar_t__ ETH_P_IPV6;
scalar_t__ skb_ether_protocol(struct sk_buff *);
unsigned char *skb_network_header(struct sk_buff *);
unsigned char *skb_transport_header(struct sk_buff *skb) {
  unsigned char *h = skb_network_header(skb);
  if (skb_ether_protocol(skb) == ETH_P_IP)
    return h + 4 * ((*h) & 0xF);
  if (skb_ether_protocol(skb) == ETH_P_IPV6)
    return h + 20;
  return NULL;
}


x86 assembly (clang, -O0):
<skb_transport_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <skb_network_header@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <skb_ether_protocol@plt>
mov    0x2e97(%rip),%rcx
cmp    (%rcx),%rax
jne    1172 <skb_transport_header+0x52>
mov    -0x18(%rbp),%rax
mov    -0x18(%rbp),%rcx
movzbl (%rcx),%ecx
and    $0xf,%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <skb_transport_header+0x84>
mov    -0x10(%rbp),%rdi
call   1030 <skb_ether_protocol@plt>
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
jne    119c <skb_transport_header+0x7c>
mov    -0x18(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x8(%rbp)
jmp    11a4 <skb_transport_header+0x84>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<skb_transport_header>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1040 <skb_network_header@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <skb_ether_protocol@plt>
mov    0x2ea2(%rip),%rcx
cmp    (%rcx),%rax
jne    1150 <skb_transport_header+0x30>
movzbl (%r14),%eax
and    $0xf,%eax
lea    (%r14,%rax,4),%rax
jmp    116f <skb_transport_header+0x4f>
mov    %rbx,%rdi
call   1030 <skb_ether_protocol@plt>
mov    %rax,%rcx
mov    0x2e76(%rip),%rdx
add    $0x14,%r14
xor    %eax,%eax
cmp    (%rdx),%rcx
cmove  %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.sk_buff = type { i32 }
@ETH_P_IP = global i64 0
@ETH_P_IPV6 = global i64 0
define i8* @skb_transport_header(%struct.sk_buff* %0) {
%2 = alloca i8*
%3 = alloca %struct.sk_buff*
%4 = alloca i8*
store %struct.sk_buff* %0, %struct.sk_buff** %3
%5 = load %struct.sk_buff*, %struct.sk_buff** %3
%6 = call i8* @skb_network_header(%struct.sk_buff* %5)
store i8* %6, i8** %4
%7 = load %struct.sk_buff*, %struct.sk_buff** %3
%8 = call i64 @skb_ether_protocol(%struct.sk_buff* %7)
%9 = load i64, i64* @ETH_P_IP
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %20
11: ; preds = %1
%12 = load i8*, i8** %4
%13 = load i8*, i8** %4
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = and i32 %15, 15
%17 = mul nsw i32 4, %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
store i8* %19, i8** %2
br label %29
20: ; preds = %1
%21 = load %struct.sk_buff*, %struct.sk_buff** %3
%22 = call i64 @skb_ether_protocol(%struct.sk_buff* %21)
%23 = load i64, i64* @ETH_P_IPV6
%24 = icmp eq i64 %22, %23
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 20
store i8* %27, i8** %2
br label %29
28: ; preds = %20
store i8* null, i8** %2
br label %29
29: ; preds = %28, %25, %11
%30 = load i8*, i8** %2
ret i8* %30
}

LLVM IR (-O2):
%struct.sk_buff = type { i32 }
@ETH_P_IP = global i64 0
@ETH_P_IPV6 = global i64 0
define i8* @skb_transport_header(%struct.sk_buff* %0) {
%2 = tail call i8* @skb_network_header(%struct.sk_buff* %0)
%3 = tail call i64 @skb_ether_protocol(%struct.sk_buff* %0)
%4 = load i64, i64* @ETH_P_IP
%5 = icmp eq i64 %3, %4
br i1 %5, label %6, label %12
6: ; preds = %1
%7 = load i8, i8* %2
%8 = shl i8 %7, 2
%9 = and i8 %8, 60
%10 = zext i8 %9 to i64
%11 = getelementptr inbounds i8, i8* %2, i64 %10
br label %18
12: ; preds = %1
%13 = tail call i64 @skb_ether_protocol(%struct.sk_buff* %0)
%14 = load i64, i64* @ETH_P_IPV6
%15 = icmp eq i64 %13, %14
%16 = getelementptr inbounds i8, i8* %2, i64 20
%17 = select i1 %15, i8* %16, i8* null
br label %18
18: ; preds = %12, %6
%19 = phi i8* [ %11, %6 ], [ %17, %12 ]
ret i8* %19
}