#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*/ u_int32_t;
struct tcphdr {
  scalar_t__ check;
};
struct iphdr {
  scalar_t__ protocol;
  int ihl;
  scalar_t__ check;
  int /*<<< orphan*/ daddr;
  int /*<<< orphan*/ saddr;
  int /*<<< orphan*/ tot_len;
};
typedef scalar_t__ __u16;

/* Variables and functions */
scalar_t__ IPPROTO_TCP;
scalar_t__ ip_sum_calc(int, unsigned short *);
scalar_t__ ntohs(int /*<<< orphan*/);
scalar_t__ tcp_sum_calc(scalar_t__, unsigned short *, unsigned short *,
                        unsigned short *);
void checksum(unsigned char *packet) {
  struct iphdr *iph = NULL;
  struct tcphdr *tcph = NULL;
  __u16 tcplen;
  iph = (struct iphdr *)packet;
  if (iph->protocol == IPPROTO_TCP) {
    tcph = (struct tcphdr *)(((u_int32_t *)packet) + iph->ihl);
    tcplen = ntohs(iph->tot_len) - iph->ihl * 4;
    tcph->check = 0;
    tcph->check =
        tcp_sum_calc(tcplen, (unsigned short *)&iph->saddr,
                     (unsigned short *)&iph->daddr, (unsigned short *)tcph);
    iph->check = 0;
    iph->check = ip_sum_calc(iph->ihl * 4, (unsigned short *)iph);
  }
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e86(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <checksum+0xce>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq 0x8(%rcx),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%edi
call   1040 <ntohs@plt>
mov    -0x10(%rbp),%rcx
mov    0x8(%rcx),%ecx
shl    $0x2,%ecx
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1c,%rsi
mov    -0x10(%rbp),%rdx
add    $0x18,%rdx
mov    -0x18(%rbp),%rcx
call   1050 <tcp_sum_calc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%edi
shl    $0x2,%edi
mov    -0x10(%rbp),%rsi
call   1030 <ip_sum_calc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x10(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %r15
push   %r14
push   %rbx
mov    (%rdi),%rax
mov    0x2ea9(%rip),%rcx
cmp    (%rcx),%rax
jne    119b <checksum+0x6b>
mov    %rdi,%rbx
movslq 0x8(%rdi),%r15
lea    (%rdi,%r15,4),%r14
mov    0x20(%rdi),%edi
call   1040 <ntohs@plt>
movslq 0x8(%rbx),%rcx
shl    $0x2,%rcx
sub    %rcx,%rax
movq   $0x0,(%rbx,%r15,4)
lea    0x1c(%rbx),%rsi
lea    0x18(%rbx),%rdx
mov    %rax,%rdi
mov    %r14,%rcx
call   1050 <tcp_sum_calc@plt>
mov    %rax,(%rbx,%r15,4)
movq   $0x0,0x10(%rbx)
mov    0x8(%rbx),%edi
shl    $0x2,%edi
mov    %rbx,%rsi
call   1030 <ip_sum_calc@plt>
mov    %rax,0x10(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.iphdr = type { i64, i32, i64, i32, i32, i32 }
%struct.tcphdr = type { i64 }
@IPPROTO_TCP = global i64 0
define void @checksum(i8* %0) {
%2 = bitcast i8* %0 to i64*
%3 = load i64, i64* %2
%4 = load i64, i64* @IPPROTO_TCP
%5 = icmp eq i64 %3, %4
br i1 %5, label %6, label %34
6: ; preds = %1
%7 = bitcast i8* %0 to i32*
%8 = getelementptr inbounds i8, i8* %0, i64 8
%9 = bitcast i8* %8 to i32*
%10 = load i32, i32* %9
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %7, i64 %11
%13 = getelementptr inbounds i8, i8* %0, i64 32
%14 = bitcast i8* %13 to i32*
%15 = load i32, i32* %14
%16 = tail call i64 @ntohs(i32 %15)
%17 = load i32, i32* %9
%18 = shl nsw i32 %17, 2
%19 = sext i32 %18 to i64
%20 = sub nsw i64 %16, %19
%21 = bitcast i32* %12 to i64*
store i64 0, i64* %21
%22 = getelementptr inbounds i8, i8* %0, i64 28
%23 = bitcast i8* %22 to i16*
%24 = getelementptr inbounds i8, i8* %0, i64 24
%25 = bitcast i8* %24 to i16*
%26 = bitcast i32* %12 to i16*
%27 = tail call i64 @tcp_sum_calc(i64 %20, i16* %23, i16* %25, i16* %26)
store i64 %27, i64* %21
%28 = getelementptr inbounds i8, i8* %0, i64 16
%29 = bitcast i8* %28 to i64*
store i64 0, i64* %29
%30 = load i32, i32* %9
%31 = shl nsw i32 %30, 2
%32 = bitcast i8* %0 to i16*
%33 = tail call i64 @ip_sum_calc(i32 %31, i16* %32)
store i64 %33, i64* %29
br label %34
34: ; preds = %6, %1
ret void
}