#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 ip6_hdr {
  size_t ip6_nxt;
  int /*<<< orphan*/ ip6_dst;
  int /*<<< orphan*/ ip6_src;
};

/* Variables and functions */
int htonl(size_t);
int htons(int);
scalar_t__ uint16_checksum(int *, size_t);
uint16_t tcp_checksum6(uint16_t *data, size_t n, struct ip6_hdr *ip6) {
  uint32_t phdr[2];
  uint32_t sum = 0;
  uint16_t sum2;
  sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_src, 16);
  sum += uint16_checksum((uint16_t *)(void *)&ip6->ip6_dst, 16);
  phdr[0] = htonl(n);
  phdr[1] = htonl(ip6->ip6_nxt);
  sum += uint16_checksum((uint16_t *)phdr, 8);
  sum += uint16_checksum(data, 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_checksum6>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
add    $0xc,%rdi
mov    $0x10,%esi
call   1040 <uint16_checksum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x8,%rdi
mov    $0x10,%esi
call   1040 <uint16_checksum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1050 <htonl@plt>
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0x8,%esi
call   1040 <uint16_checksum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <uint16_checksum@plt>
mov    %rax,%rcx
movslq -0x2c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0xffff,%eax
mov    -0x2c(%rbp),%ecx
sar    $0x10,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0xffff,%eax
mov    -0x2c(%rbp),%ecx
sar    $0x10,%ecx
add    %ecx,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
xor    $0xffffffff,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    1229 <tcp_checksum6+0xf9>
movl   $0xffff,-0x4(%rbp)
jmp    122f <tcp_checksum6+0xff>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_checksum6>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xc(%rdx),%rdi
mov    $0x10,%esi
call   1040 <uint16_checksum@plt>
mov    %rax,%rbx
lea    0x8(%r12),%rdi
mov    $0x10,%esi
call   1040 <uint16_checksum@plt>
add    %eax,%ebx
mov    %r14,%rdi
call   1050 <htonl@plt>
mov    %eax,(%rsp)
mov    (%r12),%rdi
call   1050 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    %rsp,%rdi
mov    $0x8,%esi
call   1040 <uint16_checksum@plt>
add    %eax,%ebx
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <uint16_checksum@plt>
add    %eax,%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
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.ip6_hdr = type { i64, i32, i32 }
define i32 @tcp_checksum6(i32* %0, i64 %1, %struct.ip6_hdr* %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = getelementptr inbounds %struct.ip6_hdr, %struct.ip6_hdr* %2, i64 0, i32 2
%7 = tail call i64 @uint16_checksum(i32* %6, i64 16)
%8 = getelementptr inbounds %struct.ip6_hdr, %struct.ip6_hdr* %2, i64 0, i32 1
%9 = tail call i64 @uint16_checksum(i32* %8, i64 16)
%10 = add i64 %9, %7
%11 = tail call i32 @htonl(i64 %1)
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.ip6_hdr, %struct.ip6_hdr* %2, i64 0, i32 0
%14 = load i64, i64* %13
%15 = tail call i32 @htonl(i64 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
store i32 %15, i32* %16
%17 = call i64 @uint16_checksum(i32* %12, i64 8)
%18 = add i64 %10, %17
%19 = call i64 @uint16_checksum(i32* %0, i64 %1)
%20 = add i64 %18, %19
%21 = trunc i64 %20 to i32
%22 = and i32 %21, 65535
%23 = ashr i32 %21, 16
%24 = add nsw i32 %22, %23
%25 = and i32 %24, 65535
%26 = ashr i32 %24, 16
%27 = add nsw i32 %25, %26
%28 = call i32 @htons(i32 %27)
%29 = icmp eq i32 %28, -1
%30 = xor i32 %28, -1
%31 = select i1 %29, i32 65535, i32 %30
ret i32 %31
}