#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 */
scalar_t__ ntohs(int /*<<< orphan*/);
void checksum(const void *buffer, int bytes, uint32_t *total, int finalize) {
  const uint16_t *ptr;
  int words;
  ptr = (uint16_t *)buffer;
  words = (bytes + 1) / 2;
  while (words--)
    *total += ntohs(*ptr++);
  if (finalize) {
    while (*total & 0xffff0000)
      *total = (*total >> 16) + (*total & 0xffff);
    *total = *total ^ 0xffff;
  }
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x2c(%rbp)
cmp    $0x0,%eax
je     117d <checksum+0x6d>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq (%rax),%rcx
add    %rdx,%rcx
mov    %ecx,(%rax)
jmp    113f <checksum+0x2f>
cmpl   $0x0,-0x1c(%rbp)
je     11d3 <checksum+0xc3>
jmp    118c <checksum+0x7c>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0xffff0000,%eax
cmp    $0x0,%eax
je     11c1 <checksum+0xb1>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
sar    $0x10,%ecx
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
and    $0xffff,%eax
add    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    118c <checksum+0x7c>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
xor    $0xffff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r14d
mov    %rdx,%r15
lea    0x2(%rsi),%eax
cmp    $0x3,%eax
jb     1154 <checksum+0x44>
mov    %rdi,%r12
mov    %esi,%eax
add    $0x1,%eax
shr    $0x1f,%eax
lea    (%rsi,%rax,1),%ebx
add    $0x1,%ebx
sar    %ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r12,%rbp,4),%edi
call   1030 <ntohs@plt>
add    %eax,(%r15)
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1140 <checksum+0x30>
test   %r14d,%r14d
je     1189 <checksum+0x79>
mov    (%r15),%eax
cmp    $0x10000,%eax
jb     1181 <checksum+0x71>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %eax,%ecx
sar    $0x10,%ecx
movzwl %ax,%eax
add    %ecx,%eax
cmp    $0xffff,%eax
ja     1170 <checksum+0x60>
xor    $0xffff,%eax
mov    %eax,(%r15)
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @checksum(i8* %0, i32 %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = load i8*, i8** %5
%12 = bitcast i8* %11 to i32*
store i32* %12, i32** %9
%13 = load i32, i32* %6
%14 = add nsw i32 %13, 1
%15 = sdiv i32 %14, 2
store i32 %15, i32* %10
br label %16
16: ; preds = %20, %4
%17 = load i32, i32* %10
%18 = add nsw i32 %17, -1
store i32 %18, i32* %10
%19 = icmp ne i32 %17, 0
br i1 %19, label %20, label %30
20: ; preds = %16
%21 = load i32*, i32** %9
%22 = getelementptr inbounds i32, i32* %21, i32 1
store i32* %22, i32** %9
%23 = load i32, i32* %21
%24 = call i64 @ntohs(i32 %23)
%25 = load i32*, i32** %7
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
%28 = add nsw i64 %27, %24
%29 = trunc i64 %28 to i32
store i32 %29, i32* %25
br label %16
30: ; preds = %16
%31 = load i32, i32* %8
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %53
33: ; preds = %30
br label %34
34: ; preds = %39, %33
%35 = load i32*, i32** %7
%36 = load i32, i32* %35
%37 = and i32 %36, -65536
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %48
39: ; preds = %34
%40 = load i32*, i32** %7
%41 = load i32, i32* %40
%42 = ashr i32 %41, 16
%43 = load i32*, i32** %7
%44 = load i32, i32* %43
%45 = and i32 %44, 65535
%46 = add nsw i32 %42, %45
%47 = load i32*, i32** %7
store i32 %46, i32* %47
br label %34
48: ; preds = %34
%49 = load i32*, i32** %7
%50 = load i32, i32* %49
%51 = xor i32 %50, 65535
%52 = load i32*, i32** %7
store i32 %51, i32* %52
br label %53
53: ; preds = %48, %30
ret void
}

LLVM IR (-O2):
define void @checksum(i8* nocapture %0, i32 %1, i32* nocapture %2, i32 %3) {
%5 = add i32 %1, 2
%6 = icmp ult i32 %5, 3
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = add nuw nsw i32 %1, 1
%9 = sdiv i32 %8, 2
%10 = bitcast i8* %0 to i32*
br label %11
11: ; preds = %7, %11
%12 = phi i32 [ %14, %11 ], [ %9, %7 ]
%13 = phi i32* [ %15, %11 ], [ %10, %7 ]
%14 = add nsw i32 %12, -1
%15 = getelementptr inbounds i32, i32* %13, i64 1
%16 = load i32, i32* %13
%17 = tail call i64 @ntohs(i32 %16)
%18 = load i32, i32* %2
%19 = trunc i64 %17 to i32
%20 = add i32 %18, %19
store i32 %20, i32* %2
%21 = icmp eq i32 %14, 0
br i1 %21, label %22, label %11
22: ; preds = %11, %4
%23 = icmp eq i32 %3, 0
br i1 %23, label %36, label %24
24: ; preds = %22
%25 = load i32, i32* %2
%26 = icmp ult i32 %25, 65536
br i1 %26, label %33, label %27
27: ; preds = %24, %27
%28 = phi i32 [ %31, %27 ], [ %25, %24 ]
%29 = ashr i32 %28, 16
%30 = and i32 %28, 65535
%31 = add nsw i32 %29, %30
%32 = icmp ult i32 %31, 65536
br i1 %32, label %33, label %27
33: ; preds = %27, %24
%34 = phi i32 [ %25, %24 ], [ %31, %27 ]
%35 = xor i32 %34, 65535
store i32 %35, i32* %2
br label %36
36: ; preds = %33, %22
ret void
}