#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 /*<<< orphan*/ uint8_t;
typedef int uint32_t;
struct ip6_hdr {
  int dummy;
};
struct ip {
  int dummy;
};
struct ether_header {
  int /*<<< orphan*/ ether_type;
};

/* Variables and functions */
#define ETHERTYPE_ARP 131
#define ETHERTYPE_IP 130
#define ETHERTYPE_IPV6 129
#define ETHERTYPE_VLAN 128
int decode_ip_n_hash(struct ip *, int /*<<< orphan*/, int /*<<< orphan*/);
int decode_ipv6_n_hash(struct ip6_hdr *, int /*<<< orphan*/,
                       int /*<<< orphan*/);
int decode_others_n_hash(struct ether_header *, int /*<<< orphan*/);
int decode_vlan_n_hash(struct ether_header *, int /*<<< orphan*/,
                       int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
uint32_t pkt_hdr_hash(const unsigned char *buffer, uint8_t hash_split,
                      uint8_t seed) {
  int rc = 0;
  struct ether_header *ethh = (struct ether_header *)buffer;
  switch (ntohs(ethh->ether_type)) {
  case ETHERTYPE_IP:
    rc = decode_ip_n_hash((struct ip *)(ethh + 1), hash_split, seed);
    break;
  case ETHERTYPE_IPV6:
    rc = decode_ipv6_n_hash((struct ip6_hdr *)(ethh + 1), hash_split, seed);
    break;
  case ETHERTYPE_VLAN:
    rc = decode_vlan_n_hash(ethh, hash_split, seed);
    break;
  case ETHERTYPE_ARP:
  default:
    rc = decode_others_n_hash(ethh, seed);
    break;
  }
  return rc;
}


x86 assembly (clang, -O0):
<pkt_hdr_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     11f4 <pkt_hdr_hash+0xa4>
mov    -0x28(%rbp),%rax
lea    0xe67(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1070 <decode_ip_n_hash@plt>
mov    %eax,-0x14(%rbp)
jmp    1203 <pkt_hdr_hash+0xb3>
mov    -0x20(%rbp),%rdi
add    $0x4,%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1030 <decode_ipv6_n_hash@plt>
mov    %eax,-0x14(%rbp)
jmp    1203 <pkt_hdr_hash+0xb3>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1060 <decode_vlan_n_hash@plt>
mov    %eax,-0x14(%rbp)
jmp    1203 <pkt_hdr_hash+0xb3>
jmp    11f4 <pkt_hdr_hash+0xa4>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1050 <decode_others_n_hash@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkt_hdr_hash>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebx
mov    %esi,%r14d
mov    %rdi,%rbp
mov    (%rdi),%edi
call   1040 <ntohs@plt>
cmp    $0x80,%eax
je     118d <pkt_hdr_hash+0x3d>
cmp    $0x81,%eax
je     119e <pkt_hdr_hash+0x4e>
cmp    $0x82,%eax
jne    11b3 <pkt_hdr_hash+0x63>
add    $0x4,%rbp
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <decode_ip_n_hash@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1060 <decode_vlan_n_hash@plt>
add    $0x4,%rbp
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %ebx,%edx
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <decode_ipv6_n_hash@plt>
mov    %rbp,%rdi
mov    %ebx,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <decode_others_n_hash@plt>

LLVM IR (-O0):
%struct.ether_header = type { i32 }
%struct.ip = type { i32 }
%struct.ip6_hdr = type { i32 }
define i32 @pkt_hdr_hash(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ether_header*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = load i8*, i8** %4
%10 = bitcast i8* %9 to %struct.ether_header*
store %struct.ether_header* %10, %struct.ether_header** %8
%11 = load %struct.ether_header*, %struct.ether_header** %8
%12 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %11, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i32 @ntohs(i32 %13)
switch i32 %14, label %35 [
i32 130, label %15
i32 129, label %22
i32 128, label %29
i32 131, label %34
]
15: ; preds = %3
%16 = load %struct.ether_header*, %struct.ether_header** %8
%17 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %16, i64 1
%18 = bitcast %struct.ether_header* %17 to %struct.ip*
%19 = load i32, i32* %5
%20 = load i32, i32* %6
%21 = call i32 @decode_ip_n_hash(%struct.ip* %18, i32 %19, i32 %20)
store i32 %21, i32* %7
br label %39
22: ; preds = %3
%23 = load %struct.ether_header*, %struct.ether_header** %8
%24 = getelementptr inbounds %struct.ether_header, %struct.ether_header* %23, i64 1
%25 = bitcast %struct.ether_header* %24 to %struct.ip6_hdr*
%26 = load i32, i32* %5
%27 = load i32, i32* %6
%28 = call i32 @decode_ipv6_n_hash(%struct.ip6_hdr* %25, i32 %26, i32 %27)
store i32 %28, i32* %7
br label %39
29: ; preds = %3
%30 = load %struct.ether_header*, %struct.ether_header** %8
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = call i32 @decode_vlan_n_hash(%struct.ether_header* %30, i32 %31, i32 %32)
store i32 %33, i32* %7
br label %39
34: ; preds = %3
br label %35
35: ; preds = %3, %34
%36 = load %struct.ether_header*, %struct.ether_header** %8
%37 = load i32, i32* %6
%38 = call i32 @decode_others_n_hash(%struct.ether_header* %36, i32 %37)
store i32 %38, i32* %7
br label %39
39: ; preds = %35, %29, %22, %15
%40 = load i32, i32* %7
ret i32 %40
}

LLVM IR (-O2):
%struct.ether_header = type { i32 }
%struct.ip = type { i32 }
%struct.ip6_hdr = type { i32 }
define i32 @pkt_hdr_hash(i8* %0, i32 %1, i32 %2) {
%4 = bitcast i8* %0 to %struct.ether_header*
%5 = bitcast i8* %0 to i32*
%6 = load i32, i32* %5
%7 = tail call i32 @ntohs(i32 %6)
switch i32 %7, label %18 [
i32 130, label %8
i32 129, label %12
i32 128, label %16
]
8: ; preds = %3
%9 = getelementptr inbounds i8, i8* %0, i64 4
%10 = bitcast i8* %9 to %struct.ip*
%11 = tail call i32 @decode_ip_n_hash(%struct.ip* %10, i32 %1, i32 %2)
br label %20
12: ; preds = %3
%13 = getelementptr inbounds i8, i8* %0, i64 4
%14 = bitcast i8* %13 to %struct.ip6_hdr*
%15 = tail call i32 @decode_ipv6_n_hash(%struct.ip6_hdr* %14, i32 %1, i32 %2)
br label %20
16: ; preds = %3
%17 = tail call i32 @decode_vlan_n_hash(%struct.ether_header* %4, i32 %1, i32 %2)
br label %20
18: ; preds = %3
%19 = tail call i32 @decode_others_n_hash(%struct.ether_header* %4, i32 %2)
br label %20
20: ; preds = %18, %16, %12, %8
%21 = phi i32 [ %19, %18 ], [ %17, %16 ], [ %15, %12 ], [ %11, %8 ]
ret i32 %21
}