#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 tcphdr {
  int dummy;
};
struct iphdr {
  unsigned long protocol;
  scalar_t__ saddr;
  scalar_t__ daddr;
  int /*<<< orphan*/ tot_len;
};

/* Variables and functions */
int /*<<< orphan*/ calc_csum(unsigned long, void *, unsigned long);
int /*<<< orphan*/ error(int, int /*<<< orphan*/, char *);
scalar_t__ htons(unsigned long);
int ntohs(int /*<<< orphan*/);
uint16_t calc_tcp_csum(struct iphdr *iph, struct tcphdr *tcph) {
  unsigned long sum = 0, tcplen;
  tcplen = ntohs(iph->tot_len) - sizeof(*iph);
  if (tcplen & 1)
    error(1, 0, "odd length: csum needs padding");
  sum += iph->daddr;
  sum += iph->saddr;
  sum += htons(iph->protocol);
  sum += htons(tcplen);
  return calc_csum(sum, (void *)tcph, tcplen >> 1);
}


x86 assembly (clang, -O0):
<calc_tcp_csum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x18(%rax),%edi
call   1050 <ntohs@plt>
cltq
sub    $0x20,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     1193 <calc_tcp_csum+0x53>
mov    $0x1,%edi
xor    %esi,%esi
lea    0xe72(%rip),%rdx
call   1060 <error@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <htons@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <htons@plt>
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
shr    $0x1,%rdx
call   1030 <calc_csum@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_tcp_csum>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    0x18(%rdi),%edi
call   1050 <ntohs@plt>
cltq
lea    -0x20(%rax),%r15
test   $0x1,%al
je     1173 <calc_tcp_csum+0x33>
lea    0xe99(%rip),%rdx
mov    $0x1,%edi
xor    %esi,%esi
call   1060 <error@plt>
mov    (%r12),%rdi
mov    0x8(%r12),%rbx
add    0x10(%r12),%rbx
call   1040 <htons@plt>
add    %rax,%rbx
mov    %r15,%rdi
call   1040 <htons@plt>
add    %rax,%rbx
shr    %r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <calc_csum@plt>

LLVM IR (-O0):
%struct.iphdr = type { i64, i64, i64, i32 }
%struct.tcphdr = type { i32 }
@.str = [31 x i8] c"odd length: csum needs padding\00"
define i32 @calc_tcp_csum(%struct.iphdr* %0, %struct.tcphdr* %1) {
%3 = alloca %struct.iphdr*
%4 = alloca %struct.tcphdr*
%5 = alloca i64
%6 = alloca i64
store %struct.iphdr* %0, %struct.iphdr** %3
store %struct.tcphdr* %1, %struct.tcphdr** %4
store i64 0, i64* %5
%7 = load %struct.iphdr*, %struct.iphdr** %3
%8 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %7, i32 0, i32 3
%9 = load i32, i32* %8
%10 = call i32 @ntohs(i32 %9)
%11 = sext i32 %10 to i64
%12 = sub i64 %11, 32
store i64 %12, i64* %6
%13 = load i64, i64* %6
%14 = and i64 %13, 1
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %16, %2
%19 = load %struct.iphdr*, %struct.iphdr** %3
%20 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %19, i32 0, i32 2
%21 = load i64, i64* %20
%22 = load i64, i64* %5
%23 = add i64 %22, %21
store i64 %23, i64* %5
%24 = load %struct.iphdr*, %struct.iphdr** %3
%25 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %24, i32 0, i32 1
%26 = load i64, i64* %25
%27 = load i64, i64* %5
%28 = add i64 %27, %26
store i64 %28, i64* %5
%29 = load %struct.iphdr*, %struct.iphdr** %3
%30 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = call i64 @htons(i64 %31)
%33 = load i64, i64* %5
%34 = add i64 %33, %32
store i64 %34, i64* %5
%35 = load i64, i64* %6
%36 = call i64 @htons(i64 %35)
%37 = load i64, i64* %5
%38 = add i64 %37, %36
store i64 %38, i64* %5
%39 = load i64, i64* %5
%40 = load %struct.tcphdr*, %struct.tcphdr** %4
%41 = bitcast %struct.tcphdr* %40 to i8*
%42 = load i64, i64* %6
%43 = lshr i64 %42, 1
%44 = call i32 @calc_csum(i64 %39, i8* %41, i64 %43)
ret i32 %44
}

LLVM IR (-O2):
%struct.iphdr = type { i64, i64, i64, i32 }
%struct.tcphdr = type { i32 }
@.str = [31 x i8] c"odd length: csum needs padding\00"
define i32 @calc_tcp_csum(%struct.iphdr* nocapture %0, %struct.tcphdr* %1) {
%3 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 3
%4 = load i32, i32* %3
%5 = tail call i32 @ntohs(i32 %4)
%6 = sext i32 %5 to i64
%7 = add nsw i64 %6, -32
%8 = and i64 %6, 1
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @error(i32 1, i32 0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %2
%13 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 2
%14 = load i64, i64* %13
%15 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
%16 = load i64, i64* %15
%17 = add i64 %16, %14
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 0
%19 = load i64, i64* %18
%20 = tail call i64 @htons(i64 %19)
%21 = add i64 %17, %20
%22 = tail call i64 @htons(i64 %7)
%23 = add i64 %21, %22
%24 = bitcast %struct.tcphdr* %1 to i8*
%25 = lshr i64 %7, 1
%26 = tail call i32 @calc_csum(i64 %23, i8* %24, i64 %25)
ret i32 %26
}