#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 ip {
  int ip_hl;
};
struct ethhdr {
  int /*<<< orphan*/ h_proto;
};

/* Variables and functions */
int ntohs(int /*<<< orphan*/);
const void *find_ipproto_header(const void *pkt, const struct ethhdr *ether,
                                const struct ip *ip) {
  const size_t vlan_offset = ntohs(ether->h_proto) == 0x8100 ? 4 : 0;
  return pkt + sizeof(struct ethhdr) + vlan_offset + 4 * (ip->ip_hl);
}


x86 assembly (clang, -O0):
<find_ipproto_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,%edx
xor    %eax,%eax
mov    $0x4,%ecx
cmp    $0x8100,%edx
cmove  %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
add    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    (%rcx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_ipproto_header>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rdi,%rbx
mov    (%rsi),%edi
call   1030 <ntohs@plt>
xor    %ecx,%ecx
cmp    $0x8100,%eax
sete   %cl
lea    (%rbx,%rcx,4),%rax
movslq (%r14),%rcx
lea    (%rax,%rcx,4),%rax
add    $0x4,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.ethhdr = type { i32 }
%struct.ip = type { i32 }
define i8* @find_ipproto_header(i8* %0, %struct.ethhdr* %1, %struct.ip* %2) {
%4 = alloca i8*
%5 = alloca %struct.ethhdr*
%6 = alloca %struct.ip*
%7 = alloca i64
store i8* %0, i8** %4
store %struct.ethhdr* %1, %struct.ethhdr** %5
store %struct.ip* %2, %struct.ip** %6
%8 = load %struct.ethhdr*, %struct.ethhdr** %5
%9 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %8, i32 0, i32 0
%10 = load i32, i32* %9
%11 = call i32 @ntohs(i32 %10)
%12 = icmp eq i32 %11, 33024
%13 = zext i1 %12 to i64
%14 = select i1 %12, i32 4, i32 0
%15 = sext i32 %14 to i64
store i64 %15, i64* %7
%16 = load i8*, i8** %4
%17 = getelementptr i8, i8* %16, i64 4
%18 = load i64, i64* %7
%19 = getelementptr i8, i8* %17, i64 %18
%20 = load %struct.ip*, %struct.ip** %6
%21 = getelementptr inbounds %struct.ip, %struct.ip* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = mul nsw i32 4, %22
%24 = sext i32 %23 to i64
%25 = getelementptr i8, i8* %19, i64 %24
ret i8* %25
}

LLVM IR (-O2):
%struct.ethhdr = type { i32 }
%struct.ip = type { i32 }
define i8* @find_ipproto_header(i8* readnone %0, %struct.ethhdr* nocapture %1, %struct.ip* nocapture %2) {
%4 = getelementptr inbounds %struct.ethhdr, %struct.ethhdr* %1, i64 0, i32 0
%5 = load i32, i32* %4
%6 = tail call i32 @ntohs(i32 %5)
%7 = icmp eq i32 %6, 33024
%8 = select i1 %7, i64 4, i64 0
%9 = getelementptr i8, i8* %0, i64 4
%10 = getelementptr i8, i8* %9, i64 %8
%11 = getelementptr inbounds %struct.ip, %struct.ip* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = shl nsw i32 %12, 2
%14 = sext i32 %13 to i64
%15 = getelementptr i8, i8* %10, i64 %14
ret i8* %15
}