#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 uint16_t;

/* Variables and functions */
int bswap_16(int);
uint16_t add_ipchksums(unsigned long offset, uint16_t sum, uint16_t new) {
  unsigned long checksum;
  sum = ~sum & 0xFFFF;
  new = ~new & 0xFFFF;
  if (offset & 1) {
    new = bswap_16(new);
  }
  checksum = sum + new;
  if (checksum > 0xFFFF) {
    checksum -= 0xFFFF;
  }
  return (~checksum) & 0xFFFF;
}


x86 assembly (clang, -O0):
<add_ipchksums>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xffff,%eax
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
je     115b <add_ipchksums+0x4b>
mov    -0x10(%rbp),%edi
call   1030 <bswap_16@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    -0x10(%rbp),%eax
cltq
mov    %rax,-0x18(%rbp)
cmpq   $0xffff,-0x18(%rbp)
jbe    1183 <add_ipchksums+0x73>
mov    -0x18(%rbp),%rax
sub    $0xffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
xor    $0xffffffffffffffff,%rax
and    $0xffff,%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_ipchksums>:
push   %rbx
mov    %esi,%ebx
not    %edx
movzwl %dx,%eax
test   $0x1,%dil
je     1125 <add_ipchksums+0x15>
mov    %eax,%edi
call   1030 <bswap_16@plt>
not    %ebx
movzwl %bx,%ecx
add    %eax,%ecx
cmp    $0x10000,%ecx
sbb    $0xffffffff,%ecx
not    %ecx
movzwl %cx,%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @add_ipchksums(i64 %0, i32 %1, i32 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i64 %0, i64* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = load i32, i32* %5
%9 = xor i32 %8, -1
%10 = and i32 %9, 65535
store i32 %10, i32* %5
%11 = load i32, i32* %6
%12 = xor i32 %11, -1
%13 = and i32 %12, 65535
store i32 %13, i32* %6
%14 = load i64, i64* %4
%15 = and i64 %14, 1
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i32, i32* %6
%19 = call i32 @bswap_16(i32 %18)
store i32 %19, i32* %6
br label %20
20: ; preds = %17, %3
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = add nsw i32 %21, %22
%24 = sext i32 %23 to i64
store i64 %24, i64* %7
%25 = load i64, i64* %7
%26 = icmp ugt i64 %25, 65535
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i64, i64* %7
%29 = sub i64 %28, 65535
store i64 %29, i64* %7
br label %30
30: ; preds = %27, %20
%31 = load i64, i64* %7
%32 = xor i64 %31, -1
%33 = and i64 %32, 65535
%34 = trunc i64 %33 to i32
ret i32 %34
}

LLVM IR (-O2):
define i32 @add_ipchksums(i64 %0, i32 %1, i32 %2) {
%4 = and i32 %2, 65535
%5 = xor i32 %4, 65535
%6 = and i64 %0, 1
%7 = icmp eq i64 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %3
%9 = tail call i32 @bswap_16(i32 %5)
br label %10
10: ; preds = %8, %3
%11 = phi i32 [ %9, %8 ], [ %5, %3 ]
%12 = and i32 %1, 65535
%13 = xor i32 %12, 65535
%14 = add nsw i32 %11, %13
%15 = icmp ugt i32 %14, 65535
%16 = zext i1 %15 to i32
%17 = add i32 %14, %16
%18 = and i32 %17, 65535
%19 = xor i32 %18, 65535
ret i32 %19
}