#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*/ uint16_t;
struct udphdr {
  int /*<<< orphan*/ dest;
  int /*<<< orphan*/ source;
};
struct ip {
  scalar_t__ ip_p;
};
struct ethhdr {
  int dummy;
};

/* Variables and functions */
scalar_t__ IPPROTO_UDP;
scalar_t__ find_ipproto_header(void const *, struct ethhdr const *,
                               struct ip const *);
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
const struct udphdr *find_udp_header(const void *pkt,
                                     const struct ethhdr *ether,
                                     const struct ip *ip, uint16_t *src,
                                     uint16_t *dest) {
  if (!(ip && ip->ip_p == IPPROTO_UDP)) {
    return NULL;
  }
  const struct udphdr *udp =
      (const struct udphdr *)find_ipproto_header(pkt, ether, ip);
  *src = ntohs(udp->source);
  *dest = ntohs(udp->dest);
  return udp;
}


x86 assembly (clang, -O0):
<find_udp_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     115e <find_udp_header+0x3e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2e83(%rip),%rcx
cmp    (%rcx),%rax
je     116b <find_udp_header+0x4b>
movq   $0x0,-0x8(%rbp)
jmp    11af <find_udp_header+0x8f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <find_ipproto_header@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_udp_header>:
push   %r15
push   %r14
push   %rbx
test   %rdx,%rdx
je     115e <find_udp_header+0x3e>
mov    %rcx,%r15
mov    (%rdx),%rax
mov    0x2ea1(%rip),%rcx
cmp    (%rcx),%rax
jne    115e <find_udp_header+0x3e>
mov    %r8,%r14
call   1030 <find_ipproto_header@plt>
mov    %rax,%rbx
mov    0x4(%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r15)
mov    (%rbx),%edi
call   1040 <ntohs@plt>
mov    %eax,(%r14)
jmp    1160 <find_udp_header+0x40>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.udphdr = type { i32, i32 }
%struct.ethhdr = type { i32 }
%struct.ip = type { i64 }
@IPPROTO_UDP = global i64 0
define %struct.udphdr* @find_udp_header(i8* %0, %struct.ethhdr* %1, %struct.ip* %2, i32* %3, i32* %4) {
%6 = alloca %struct.udphdr*
%7 = alloca i8*
%8 = alloca %struct.ethhdr*
%9 = alloca %struct.ip*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca %struct.udphdr*
store i8* %0, i8** %7
store %struct.ethhdr* %1, %struct.ethhdr** %8
store %struct.ip* %2, %struct.ip** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
%13 = load %struct.ip*, %struct.ip** %9
%14 = icmp ne %struct.ip* %13, null
br i1 %14, label %15, label %21
15: ; preds = %5
%16 = load %struct.ip*, %struct.ip** %9
%17 = getelementptr inbounds %struct.ip, %struct.ip* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @IPPROTO_UDP
%20 = icmp eq i64 %18, %19
br i1 %20, label %22, label %21
21: ; preds = %15, %5
store %struct.udphdr* null, %struct.udphdr** %6
br label %39
22: ; preds = %15
%23 = load i8*, i8** %7
%24 = load %struct.ethhdr*, %struct.ethhdr** %8
%25 = load %struct.ip*, %struct.ip** %9
%26 = call i64 @find_ipproto_header(i8* %23, %struct.ethhdr* %24, %struct.ip* %25)
%27 = inttoptr i64 %26 to %struct.udphdr*
store %struct.udphdr* %27, %struct.udphdr** %12
%28 = load %struct.udphdr*, %struct.udphdr** %12
%29 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %28, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i32 @ntohs(i32 %30)
%32 = load i32*, i32** %10
store i32 %31, i32* %32
%33 = load %struct.udphdr*, %struct.udphdr** %12
%34 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
%37 = load i32*, i32** %11
store i32 %36, i32* %37
%38 = load %struct.udphdr*, %struct.udphdr** %12
store %struct.udphdr* %38, %struct.udphdr** %6
br label %39
39: ; preds = %22, %21
%40 = load %struct.udphdr*, %struct.udphdr** %6
ret %struct.udphdr* %40
}

LLVM IR (-O2):
%struct.udphdr = type { i32, i32 }
%struct.ethhdr = type { i32 }
%struct.ip = type { i64 }
@IPPROTO_UDP = global i64 0
define %struct.udphdr* @find_udp_header(i8* %0, %struct.ethhdr* %1, %struct.ip* %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4) {
%6 = icmp eq %struct.ip* %2, null
br i1 %6, label %21, label %7
7: ; preds = %5
%8 = getelementptr inbounds %struct.ip, %struct.ip* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = load i64, i64* @IPPROTO_UDP
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %21
12: ; preds = %7
%13 = tail call i64 @find_ipproto_header(i8* %0, %struct.ethhdr* %1, %struct.ip* %2)
%14 = inttoptr i64 %13 to %struct.udphdr*
%15 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %14, i64 0, i32 1
%16 = load i32, i32* %15
%17 = tail call i32 @ntohs(i32 %16)
store i32 %17, i32* %3
%18 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %14, i64 0, i32 0
%19 = load i32, i32* %18
%20 = tail call i32 @ntohs(i32 %19)
store i32 %20, i32* %4
br label %21
21: ; preds = %5, %7, %12
%22 = phi %struct.udphdr* [ %14, %12 ], [ null, %7 ], [ null, %5 ]
ret %struct.udphdr* %22
}