#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 uint32_t;
typedef int uint16_t;
struct iphdr {
  scalar_t__ protocol;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
};

/* Variables and functions */
int htons(int);
int uint16_checksum(int *, int);
uint16_t tcp_checksum(uint16_t *data, size_t n, struct iphdr *ip) {
  uint32_t sum = uint16_checksum(data, n);
  uint16_t sum2;
  sum += uint16_checksum((uint16_t *)(void *)&ip->saddr, sizeof(ip->saddr));
  sum += uint16_checksum((uint16_t *)(void *)&ip->daddr, sizeof(ip->daddr));
  sum += ip->protocol + n;
  sum = (sum & 0xFFFF) + (sum >> 16);
  sum = (sum & 0xFFFF) + (sum >> 16);
  sum2 = htons(sum);
  sum2 = ~sum2;
  if (sum2 == 0) {
    return 0xFFFF;
  }
  return sum2;
}


x86 assembly (clang, -O0):
<tcp_checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    %eax,%esi
call   1040 <uint16_checksum@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
add    $0xc,%rdi
mov    $0x4,%esi
call   1040 <uint16_checksum@plt>
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    $0x4,%esi
call   1040 <uint16_checksum@plt>
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
add    -0x18(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xffff,%eax
mov    -0x24(%rbp),%ecx
sar    $0x10,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
and    $0xffff,%eax
mov    -0x24(%rbp),%ecx
sar    $0x10,%ecx
add    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11db <tcp_checksum+0xbb>
movl   $0xffff,-0x4(%rbp)
jmp    11e1 <tcp_checksum+0xc1>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_checksum>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
call   1040 <uint16_checksum@plt>
mov    %eax,%ebp
lea    0xc(%r14),%rdi
mov    $0x4,%esi
call   1040 <uint16_checksum@plt>
add    %eax,%ebp
lea    0x8(%r14),%rdi
mov    $0x4,%esi
call   1040 <uint16_checksum@plt>
add    (%r14),%ebx
add    %eax,%ebp
add    %ebp,%ebx
movzwl %bx,%eax
sar    $0x10,%ebx
add    %eax,%ebx
movzwl %bx,%eax
sar    $0x10,%ebx
add    %eax,%ebx
mov    %ebx,%edi
call   1030 <htons@plt>
cmp    $0xffffffff,%eax
not    %eax
mov    $0xffff,%ecx
cmove  %ecx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.iphdr = type { i64, i32, i32 }
define i32 @tcp_checksum(i32* %0, i64 %1, %struct.iphdr* %2) {
%4 = trunc i64 %1 to i32
%5 = tail call i32 @uint16_checksum(i32* %0, i32 %4)
%6 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %2, i64 0, i32 2
%7 = tail call i32 @uint16_checksum(i32* %6, i32 4)
%8 = add nsw i32 %7, %5
%9 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %2, i64 0, i32 1
%10 = tail call i32 @uint16_checksum(i32* %9, i32 4)
%11 = add nsw i32 %8, %10
%12 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %2, i64 0, i32 0
%13 = load i64, i64* %12
%14 = add i64 %13, %1
%15 = trunc i64 %14 to i32
%16 = add i32 %11, %15
%17 = and i32 %16, 65535
%18 = ashr i32 %16, 16
%19 = add nsw i32 %17, %18
%20 = and i32 %19, 65535
%21 = ashr i32 %19, 16
%22 = add nsw i32 %20, %21
%23 = tail call i32 @htons(i32 %22)
%24 = icmp eq i32 %23, -1
%25 = xor i32 %23, -1
%26 = select i1 %24, i32 65535, i32 %25
ret i32 %26
}