#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 {
  scalar_t__ len;
};

/* Variables and functions */
int /*<<< orphan*/ IPPROTO_UDP;
int /*<<< orphan*/ get_ip_csum(void const *, unsigned long, unsigned long);
unsigned long htons(int /*<<< orphan*/);
int ntohs(scalar_t__);
uint16_t get_udp_csum(const struct udphdr *udph, int alen) {
  unsigned long pseudo_sum, csum_len;
  const void *csum_start = udph;
  pseudo_sum = htons(IPPROTO_UDP);
  pseudo_sum += udph->len;
  csum_start -= alen * 2;
  csum_len = ntohs(udph->len) + alen * 2;
  return get_ip_csum(csum_start, csum_len >> 1, pseudo_sum);
}


x86 assembly (clang, -O0):
<get_udp_csum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rdx
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <ntohs@plt>
mov    -0xc(%rbp),%ecx
shl    $0x1,%ecx
add    %ecx,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
shr    $0x1,%rsi
mov    -0x18(%rbp),%rdx
call   1050 <get_ip_csum@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_udp_csum>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    (%rbx),%rdi
lea    (%rax,%rdi,1),%r15
lea    (%r14,%r14,1),%eax
cltq
sub    %rax,%rbx
call   1040 <ntohs@plt>
lea    (%rax,%r14,2),%eax
movslq %eax,%rsi
shr    %rsi
mov    %rbx,%rdi
mov    %r15,%rdx
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <get_ip_csum@plt>

LLVM IR (-O0):
%struct.udphdr = type { i64 }
@IPPROTO_UDP = global i32 0
define i32 @get_udp_csum(%struct.udphdr* %0, i32 %1) {
%3 = alloca %struct.udphdr*
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i8*
store %struct.udphdr* %0, %struct.udphdr** %3
store i32 %1, i32* %4
%8 = load %struct.udphdr*, %struct.udphdr** %3
%9 = bitcast %struct.udphdr* %8 to i8*
store i8* %9, i8** %7
%10 = load i32, i32* @IPPROTO_UDP
%11 = call i64 @htons(i32 %10)
store i64 %11, i64* %5
%12 = load %struct.udphdr*, %struct.udphdr** %3
%13 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* %5
%16 = add i64 %15, %14
store i64 %16, i64* %5
%17 = load i32, i32* %4
%18 = mul nsw i32 %17, 2
%19 = load i8*, i8** %7
%20 = sext i32 %18 to i64
%21 = sub i64 0, %20
%22 = getelementptr i8, i8* %19, i64 %21
store i8* %22, i8** %7
%23 = load %struct.udphdr*, %struct.udphdr** %3
%24 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %23, i32 0, i32 0
%25 = load i64, i64* %24
%26 = call i32 @ntohs(i64 %25)
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, 2
%29 = add nsw i32 %26, %28
%30 = sext i32 %29 to i64
store i64 %30, i64* %6
%31 = load i8*, i8** %7
%32 = load i64, i64* %6
%33 = lshr i64 %32, 1
%34 = load i64, i64* %5
%35 = call i32 @get_ip_csum(i8* %31, i64 %33, i64 %34)
ret i32 %35
}

LLVM IR (-O2):
%struct.udphdr = type { i64 }
@IPPROTO_UDP = global i32 0
define i32 @get_udp_csum(%struct.udphdr* %0, i32 %1) {
%3 = bitcast %struct.udphdr* %0 to i8*
%4 = load i32, i32* @IPPROTO_UDP
%5 = tail call i64 @htons(i32 %4)
%6 = getelementptr inbounds %struct.udphdr, %struct.udphdr* %0, i64 0, i32 0
%7 = load i64, i64* %6
%8 = add i64 %7, %5
%9 = shl nsw i32 %1, 1
%10 = sext i32 %9 to i64
%11 = sub nsw i64 0, %10
%12 = getelementptr i8, i8* %3, i64 %11
%13 = tail call i32 @ntohs(i64 %7)
%14 = add nsw i32 %13, %9
%15 = sext i32 %14 to i64
%16 = lshr i64 %15, 1
%17 = tail call i32 @get_ip_csum(i8* %12, i64 %16, i64 %8)
ret i32 %17
}