#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 /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int htons(int);
scalar_t__ ntohs(int /*<<< orphan*/ const);
uint32_t ip_check(const void *data, size_t size) {
  assert(size % 2 == 0);
  size /= 2;
  const uint16_t *d = data;
  uint32_t sum = 0;
  while (size) {
    sum += ntohs(*d);
    d++;
    size--;
  }
  return htons(0xFFFF - ((sum & 0xFFFF) + ((sum & 0xFFFF0000) >> 16)));
}


x86 assembly (clang, -O0):
<ip_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
and    $0x1,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rax
shr    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11b4 <ip_check+0x84>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1050 <ntohs@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
jmp    1174 <ip_check+0x44>
mov    -0x1c(%rbp),%eax
and    $0xffff,%eax
mov    -0x1c(%rbp),%ecx
and    $0xffff0000,%ecx
shr    $0x10,%ecx
add    %ecx,%eax
mov    $0xffff,%edi
sub    %eax,%edi
call   1030 <htons@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ip_check>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
mov    %r15d,%edi
not    %edi
and    $0x1,%edi
call   1040 <assert@plt>
xor    %ebp,%ebp
cmp    $0x2,%r15
jb     1174 <ip_check+0x44>
shr    %r15
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%edi
call   1050 <ntohs@plt>
add    %eax,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1160 <ip_check+0x30>
movzwl %bp,%eax
not    %ebp
shr    $0x10,%ebp
sub    %eax,%ebp
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <htons@plt>

LLVM IR (-O0):
define i32 @ip_check(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = urem i64 %7, 2
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
%11 = call i32 @assert(i32 %10)
%12 = load i64, i64* %4
%13 = udiv i64 %12, 2
store i64 %13, i64* %4
%14 = load i8*, i8** %3
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %5
store i32 0, i32* %6
br label %16
16: ; preds = %19, %2
%17 = load i64, i64* %4
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %16
%20 = load i32*, i32** %5
%21 = load i32, i32* %20
%22 = call i64 @ntohs(i32 %21)
%23 = load i32, i32* %6
%24 = sext i32 %23 to i64
%25 = add nsw i64 %24, %22
%26 = trunc i64 %25 to i32
store i32 %26, i32* %6
%27 = load i32*, i32** %5
%28 = getelementptr inbounds i32, i32* %27, i32 1
store i32* %28, i32** %5
%29 = load i64, i64* %4
%30 = add i64 %29, -1
store i64 %30, i64* %4
br label %16
31: ; preds = %16
%32 = load i32, i32* %6
%33 = and i32 %32, 65535
%34 = load i32, i32* %6
%35 = and i32 %34, -65536
%36 = lshr i32 %35, 16
%37 = add i32 %33, %36
%38 = sub i32 65535, %37
%39 = call i32 @htons(i32 %38)
ret i32 %39
}

LLVM IR (-O2):
define i32 @ip_check(i8* nocapture %0, i64 %1) {
%3 = trunc i64 %1 to i32
%4 = and i32 %3, 1
%5 = xor i32 %4, 1
%6 = tail call i32 @assert(i32 %5)
%7 = icmp ult i64 %1, 2
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = bitcast i8* %0 to i32*
%10 = lshr i64 %1, 1
br label %11
11: ; preds = %8, %11
%12 = phi i32 [ %18, %11 ], [ 0, %8 ]
%13 = phi i32* [ %19, %11 ], [ %9, %8 ]
%14 = phi i64 [ %20, %11 ], [ %10, %8 ]
%15 = load i32, i32* %13
%16 = tail call i64 @ntohs(i32 %15)
%17 = trunc i64 %16 to i32
%18 = add i32 %12, %17
%19 = getelementptr inbounds i32, i32* %13, i64 1
%20 = add nsw i64 %14, -1
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %11
22: ; preds = %11, %2
%23 = phi i32 [ 0, %2 ], [ %18, %11 ]
%24 = and i32 %23, 65535
%25 = xor i32 %23, -1
%26 = lshr i32 %25, 16
%27 = sub nsw i32 %26, %24
%28 = tail call i32 @htons(i32 %27)
ret i32 %28
}