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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ htons(size_t);
int encap_udp(uint8_t *buf, size_t pos, size_t len, in_port_t dport,
              in_port_t sport) {
  struct udphdr *udphdr = (void *)&buf[pos - sizeof *udphdr];
  assert(pos >= sizeof *udphdr);
  udphdr->uh_sport = sport;
  udphdr->uh_dport = dport;
  udphdr->uh_ulen = htons(len - pos + sizeof *udphdr);
  udphdr->uh_sum = 0;
  return pos - sizeof *udphdr;
}


x86 assembly (clang, -O0):
<encap_udp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    $0x20,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x20,-0x10(%rbp)
setae  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x18(%rax)
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,0x10(%rax)
mov    -0x18(%rbp),%rdi
sub    -0x10(%rbp),%rdi
add    $0x20,%rdi
call   1030 <htons@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x30(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
sub    $0x20,%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encap_udp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r15
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
lea    -0x20(%rsi),%rbp
xor    %edi,%edi
cmp    $0x20,%rsi
setae  %dil
call   1040 <assert@plt>
mov    %r15,-0x68(%r13,%rbx,4)
mov    %r14,-0x70(%r13,%rbx,4)
sub    %rbx,%r12
add    $0x20,%r12
mov    %r12,%rdi
call   1030 <htons@plt>
mov    %eax,-0x78(%r13,%rbx,4)
movq   $0x0,-0x80(%r13,%rbx,4)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udphdr = type { i64, i32, i8*, i8* }
define i32 @encap_udp(i32* %0, i64 %1, i64 %2, i8* %3, i8* %4) {
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca %struct.udphdr*
store i32* %0, i32** %6
store i64 %1, i64* %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i8* %4, i8** %10
%12 = load i32*, i32** %6
%13 = load i64, i64* %7
%14 = sub i64 %13, 32
%15 = getelementptr inbounds i32, i32* %12, i64 %14
%16 = bitcast i32* %15 to i8*
%17 = bitcast i8* %16 to %struct.udphdr*
store %struct.udphdr* %17, %struct.udphdr** %11
%18 = load i64, i64* %7
%19 = icmp uge i64 %18, 32
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i8*, i8** %10
%23 = load %struct.udphdr*, %struct.udphdr** %11
%24 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %23, i32 0, i32 3
store i8* %22, i8** %24
%25 = load i8*, i8** %9
%26 = load %struct.udphdr*, %struct.udphdr** %11
%27 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %26, i32 0, i32 2
store i8* %25, i8** %27
%28 = load i64, i64* %8
%29 = load i64, i64* %7
%30 = sub i64 %28, %29
%31 = add i64 %30, 32
%32 = call i32 @htons(i64 %31)
%33 = load %struct.udphdr*, %struct.udphdr** %11
%34 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %33, i32 0, i32 1
store i32 %32, i32* %34
%35 = load %struct.udphdr*, %struct.udphdr** %11
%36 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %35, i32 0, i32 0
store i64 0, i64* %36
%37 = load i64, i64* %7
%38 = sub i64 %37, 32
%39 = trunc i64 %38 to i32
ret i32 %39
}

LLVM IR (-O2):
define i32 @encap_udp(i32* nocapture writeonly %0, i64 %1, i64 %2, i8* %3, i8* %4) {
%6 = add i64 %1, -32
%7 = getelementptr inbounds i32, i32* %0, i64 %6
%8 = icmp ugt i64 %1, 31
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = getelementptr inbounds i32, i32* %7, i64 6
%12 = bitcast i32* %11 to i8**
store i8* %4, i8** %12
%13 = getelementptr inbounds i32, i32* %7, i64 4
%14 = bitcast i32* %13 to i8**
store i8* %3, i8** %14
%15 = sub i64 32, %1
%16 = add i64 %15, %2
%17 = tail call i32 @htons(i64 %16)
%18 = getelementptr inbounds i32, i32* %7, i64 2
store i32 %17, i32* %18
%19 = bitcast i32* %7 to i64*
store i64 0, i64* %19
%20 = trunc i64 %6 to i32
ret i32 %20
}