#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;
struct udphdr {
  int /*<<< orphan*/ uh_sport;
  int /*<<< orphan*/ uh_dport;
  int /*<<< orphan*/ uh_ulen;
};
typedef int /*<<< orphan*/ in_port_t;

/* Variables and functions */
size_t ntohs(int /*<<< orphan*/);
int decap_udp(uint8_t *buf, size_t pos, size_t len, size_t *bytes,
              in_port_t *dport, in_port_t *sport) {
  struct udphdr *udphdr = (void *)&buf[pos];
  size_t ulen;
  if (pos + sizeof *udphdr > len) {
    return -1;
  }
  ulen = ntohs(udphdr->uh_ulen);
  if (pos + ulen > len || ulen < sizeof *udphdr) {
    return -1;
  }
  *bytes = ulen - sizeof *udphdr;
  *dport = udphdr->uh_dport;
  *sport = udphdr->uh_sport;
  return pos + sizeof *udphdr;
}


x86 assembly (clang, -O0):
<decap_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
add    $0xc,%rax
cmp    -0x20(%rbp),%rax
jbe    1161 <decap_udp+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <decap_udp+0xbd>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edi
call   1030 <ntohs@plt>
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rax
add    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
ja     118e <decap_udp+0x7e>
cmpq   $0xc,-0x48(%rbp)
jae    119a <decap_udp+0x8a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <decap_udp+0xbd>
mov    -0x48(%rbp),%rcx
sub    $0xc,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rax
mov    (%rax),%ecx
mov    -0x38(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0xc,%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decap_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xc(%rsi),%r15
mov    $0xffffffff,%ebp
cmp    %rdx,%r15
ja     117c <decap_udp+0x6c>
mov    %rcx,%r13
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %r8,0x8(%rsp)
mov    %r9,0x10(%rsp)
mov    0x8(%rdi,%rsi,4),%edi
call   1030 <ntohs@plt>
cmp    $0xc,%rax
jb     117c <decap_udp+0x6c>
lea    (%rax,%rbx,1),%rcx
cmp    %r14,%rcx
ja     117c <decap_udp+0x6c>
add    $0xfffffffffffffff4,%rax
mov    %rax,0x0(%r13)
mov    0x4(%r12,%rbx,4),%eax
mov    0x8(%rsp),%rcx
mov    %eax,(%rcx)
mov    (%r12,%rbx,4),%eax
mov    0x10(%rsp),%rcx
mov    %eax,(%rcx)
mov    %r15d,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udphdr = type { i32, i32, i32 }
define i32 @decap_udp(i32* %0, i64 %1, i64 %2, i64* %3, i32* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64*
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca %struct.udphdr*
%15 = alloca i64
store i32* %0, i32** %8
store i64 %1, i64* %9
store i64 %2, i64* %10
store i64* %3, i64** %11
store i32* %4, i32** %12
store i32* %5, i32** %13
%16 = load i32*, i32** %8
%17 = load i64, i64* %9
%18 = getelementptr inbounds i32, i32* %16, i64 %17
%19 = bitcast i32* %18 to i8*
%20 = bitcast i8* %19 to %struct.udphdr*
store %struct.udphdr* %20, %struct.udphdr** %14
%21 = load i64, i64* %9
%22 = add i64 %21, 12
%23 = load i64, i64* %10
%24 = icmp ugt i64 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %6
store i32 -1, i32* %7
br label %55
26: ; preds = %6
%27 = load %struct.udphdr*, %struct.udphdr** %14
%28 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %27, i32 0, i32 2
%29 = load i32, i32* %28
%30 = call i64 @ntohs(i32 %29)
store i64 %30, i64* %15
%31 = load i64, i64* %9
%32 = load i64, i64* %15
%33 = add i64 %31, %32
%34 = load i64, i64* %10
%35 = icmp ugt i64 %33, %34
br i1 %35, label %39, label %36
36: ; preds = %26
%37 = load i64, i64* %15
%38 = icmp ult i64 %37, 12
br i1 %38, label %39, label %40
39: ; preds = %36, %26
store i32 -1, i32* %7
br label %55
40: ; preds = %36
%41 = load i64, i64* %15
%42 = sub i64 %41, 12
%43 = load i64*, i64** %11
store i64 %42, i64* %43
%44 = load %struct.udphdr*, %struct.udphdr** %14
%45 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %44, i32 0, i32 1
%46 = load i32, i32* %45
%47 = load i32*, i32** %12
store i32 %46, i32* %47
%48 = load %struct.udphdr*, %struct.udphdr** %14
%49 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32*, i32** %13
store i32 %50, i32* %51
%52 = load i64, i64* %9
%53 = add i64 %52, 12
%54 = trunc i64 %53 to i32
store i32 %54, i32* %7
br label %55
55: ; preds = %40, %39, %25
%56 = load i32, i32* %7
ret i32 %56
}

LLVM IR (-O2):
define i32 @decap_udp(i32* nocapture %0, i64 %1, i64 %2, i64* nocapture writeonly %3, i32* nocapture writeonly %4, i32* nocapture writeonly %5) {
%7 = getelementptr inbounds i32, i32* %0, i64 %1
%8 = add i64 %1, 12
%9 = icmp ugt i64 %8, %2
br i1 %9, label %24, label %10
10: ; preds = %6
%11 = getelementptr inbounds i32, i32* %7, i64 2
%12 = load i32, i32* %11
%13 = tail call i64 @ntohs(i32 %12)
%14 = add i64 %13, %1
%15 = icmp ugt i64 %14, %2
%16 = icmp ult i64 %13, 12
%17 = or i1 %16, %15
br i1 %17, label %24, label %18
18: ; preds = %10
%19 = add i64 %13, -12
store i64 %19, i64* %3
%20 = getelementptr inbounds i32, i32* %7, i64 1
%21 = load i32, i32* %20
store i32 %21, i32* %4
%22 = load i32, i32* %7
store i32 %22, i32* %5
%23 = trunc i64 %8 to i32
br label %24
24: ; preds = %10, %6, %18
%25 = phi i32 [ %23, %18 ], [ -1, %6 ], [ -1, %10 ]
ret i32 %25
}