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

/* Variables and functions */
unsigned char IPPROTO_TCP;
unsigned short csum(unsigned short *, int);
int /*<<< orphan*/ free(unsigned short *);
unsigned short htons(int);
unsigned short *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, ...);
unsigned short tcpcsum(struct iphdr *iph, struct tcphdr *tcph) {
  struct tcp_pseudo {
    unsigned long src_addr;
    unsigned long dst_addr;
    unsigned char zero;
    unsigned char proto;
    unsigned short length;
  } pseudohead;
  unsigned short total_len = iph->tot_len;
  pseudohead.src_addr = iph->saddr;
  pseudohead.dst_addr = iph->daddr;
  pseudohead.zero = 0;
  pseudohead.proto = IPPROTO_TCP;
  pseudohead.length = htons(sizeof(struct tcphdr));
  int totaltcp_len = sizeof(struct tcp_pseudo) + sizeof(struct tcphdr);
  unsigned short *tcp = malloc(totaltcp_len);
  memcpy((unsigned char *)tcp, &pseudohead, sizeof(struct tcp_pseudo));
  memcpy((unsigned char *)tcp + sizeof(struct tcp_pseudo),
         (unsigned char *)tcph, sizeof(struct tcphdr));
  unsigned short output = csum(tcp, totaltcp_len);
  free(tcp);
  return output;
}


x86 assembly (clang, -O0):
<tcpcsum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ax
mov    %ax,-0x2a(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
movb   $0x0,-0x18(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%al
mov    %al,-0x17(%rbp)
mov    $0x4,%edi
call   1050 <htons@plt>
mov    %ax,-0x16(%rbp)
movl   $0x1c,-0x30(%rbp)
mov    -0x30(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
lea    -0x28(%rbp),%rsi
mov    $0x18,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rdi
add    $0x18,%rdi
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1040 <csum@plt>
mov    %ax,-0x3a(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movzwl -0x3a(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpcsum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
movups 0x8(%rdi),%xmm0
movaps %xmm0,(%rsp)
movb   $0x0,0x10(%rsp)
mov    0x2e79(%rip),%rax
mov    (%rax),%al
mov    %al,0x11(%rsp)
mov    $0x4,%edi
call   1050 <htons@plt>
mov    %ax,0x12(%rsp)
mov    $0x1c,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    $0x18,%edx
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <memcpy@plt>
lea    0x18(%rbx),%rdi
mov    $0x4,%edx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    $0x1c,%esi
call   1040 <csum@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iphdr = type { i16, i64, i64 }
%struct.tcphdr = type { i32 }
%struct.tcp_pseudo = type { i64, i64, i8, i8, i16 }
@IPPROTO_TCP = global i8 0
define zeroext i16 @tcpcsum(%struct.iphdr* %0, %struct.tcphdr* %1) {
%3 = alloca %struct.iphdr*
%4 = alloca %struct.tcphdr*
%5 = alloca %struct.tcp_pseudo
%6 = alloca i16
%7 = alloca i32
%8 = alloca i16*
%9 = alloca i16
store %struct.iphdr* %0, %struct.iphdr** %3
store %struct.tcphdr* %1, %struct.tcphdr** %4
%10 = load %struct.iphdr*, %struct.iphdr** %3
%11 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %10, i32 0, i32 0
%12 = load i16, i16* %11
store i16 %12, i16* %6
%13 = load %struct.iphdr*, %struct.iphdr** %3
%14 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %13, i32 0, i32 1
%15 = load i64, i64* %14
%16 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %5, i32 0, i32 0
store i64 %15, i64* %16
%17 = load %struct.iphdr*, %struct.iphdr** %3
%18 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %17, i32 0, i32 2
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %5, i32 0, i32 1
store i64 %19, i64* %20
%21 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %5, i32 0, i32 2
store i8 0, i8* %21
%22 = load i8, i8* @IPPROTO_TCP
%23 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %5, i32 0, i32 3
store i8 %22, i8* %23
%24 = call zeroext i16 @htons(i32 4)
%25 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %5, i32 0, i32 4
store i16 %24, i16* %25
store i32 28, i32* %7
%26 = load i32, i32* %7
%27 = call i16* @malloc(i32 %26)
store i16* %27, i16** %8
%28 = load i16*, i16** %8
%29 = bitcast i16* %28 to i8*
%30 = call i32 (i8*, ...) @memcpy(i8* %29, %struct.tcp_pseudo* %5, i64 24)
%31 = load i16*, i16** %8
%32 = bitcast i16* %31 to i8*
%33 = getelementptr inbounds i8, i8* %32, i64 24
%34 = load %struct.tcphdr*, %struct.tcphdr** %4
%35 = bitcast %struct.tcphdr* %34 to i8*
%36 = call i32 (i8*, ...) @memcpy(i8* %33, i8* %35, i64 4)
%37 = load i16*, i16** %8
%38 = load i32, i32* %7
%39 = call zeroext i16 @csum(i16* %37, i32 %38)
store i16 %39, i16* %9
%40 = load i16*, i16** %8
%41 = call i32 @free(i16* %40)
%42 = load i16, i16* %9
ret i16 %42
}

LLVM IR (-O2):
%struct.iphdr = type { i16, i64, i64 }
%struct.tcphdr = type { i32 }
%struct.tcp_pseudo = type { i64, i64, i8, i8, i16 }
@IPPROTO_TCP = global i8 0
define zeroext i16 @tcpcsum(%struct.iphdr* nocapture %0, %struct.tcphdr* %1) {
%3 = alloca %struct.tcp_pseudo
%4 = bitcast %struct.tcp_pseudo* %3 to i8*
%5 = getelementptr inbounds %struct.iphdr, %struct.iphdr* %0, i64 0, i32 1
%6 = bitcast i64* %5 to <2 x i64>*
%7 = load <2 x i64>, <2 x i64>* %6
%8 = bitcast %struct.tcp_pseudo* %3 to <2 x i64>*
store <2 x i64> %7, <2 x i64>* %8
%9 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %3, i64 0, i32 2
store i8 0, i8* %9
%10 = load i8, i8* @IPPROTO_TCP
%11 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %3, i64 0, i32 3
store i8 %10, i8* %11
%12 = tail call zeroext i16 @htons(i32 4)
%13 = getelementptr inbounds %struct.tcp_pseudo, %struct.tcp_pseudo* %3, i64 0, i32 4
store i16 %12, i16* %13
%14 = tail call i16* @malloc(i32 28)
%15 = bitcast i16* %14 to i8*
%16 = call i32 (i8*, ...) @memcpy(i8* %15, %struct.tcp_pseudo* %3, i64 24)
%17 = getelementptr inbounds i16, i16* %14, i64 12
%18 = bitcast i16* %17 to i8*
%19 = call i32 (i8*, ...) @memcpy(i8* %18, %struct.tcphdr* %1, i64 4)
%20 = call zeroext i16 @csum(i16* %14, i32 28)
%21 = call i32 @free(i16* %14)
ret i16 %20
}