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

/* Variables and functions */
uint32_t payload_checksum(uint32_t *read_ptr, int size) {
  int i;
  uint32_t checksum, data;
  uint32_t *last_ptr;
  checksum = 0;
  last_ptr = read_ptr + size;
  while (read_ptr != last_ptr) {
    data = *read_ptr++;
    checksum += (((uint32_t)(data & 0x000000FF) << 24) +
                 ((uint32_t)(data & 0x0000FF00) << 8) +
                 ((uint32_t)(data & 0x00FF0000) >> 8) +
                 ((uint32_t)(data & 0xFF000000) >> 24));
  }
  return (((uint32_t)(checksum & 0x000000FF) << 24) +
          ((uint32_t)(checksum & 0x0000FF00) << 8) +
          ((uint32_t)(checksum & 0x00FF0000) >> 8) +
          ((uint32_t)(checksum & 0xFF000000) >> 24));
}


x86 assembly (clang, -O0):
<payload_checksum>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1187 <payload_checksum+0x87>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x4,%rcx
mov    %rcx,-0x8(%rbp)
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
mov    -0x18(%rbp),%ecx
and    $0xff00,%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x18(%rbp),%ecx
and    $0xff0000,%ecx
sar    $0x8,%ecx
add    %ecx,%eax
mov    -0x18(%rbp),%ecx
and    $0xff000000,%ecx
sar    $0x18,%ecx
add    %ecx,%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    1125 <payload_checksum+0x25>
mov    -0x14(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
mov    -0x14(%rbp),%ecx
and    $0xff00,%ecx
shl    $0x8,%ecx
add    %ecx,%eax
mov    -0x14(%rbp),%ecx
and    $0xff0000,%ecx
sar    $0x8,%ecx
add    %ecx,%eax
mov    -0x14(%rbp),%ecx
and    $0xff000000,%ecx
sar    $0x18,%ecx
add    %ecx,%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<payload_checksum>:
test   %esi,%esi
je     1240 <payload_checksum+0x140>
movslq %esi,%r8
lea    -0x1(%r8),%rax
movabs $0x3fffffffffffffff,%rdx
ff ff 3f
and    %rax,%rdx
xor    %eax,%eax
mov    %rdi,%rcx
cmp    $0x7,%rdx
jb     120b <payload_checksum+0x10b>
add    $0x1,%rdx
mov    %rdx,%rsi
and    $0xfffffffffffffff8,%rsi
lea    (%rdi,%rsi,4),%rcx
pxor   %xmm0,%xmm0
xor    %eax,%eax
movdqa 0xeb7(%rip),%xmm8
pxor   %xmm2,%xmm2
pxor   %xmm1,%xmm1
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movdqu (%rdi,%rax,4),%xmm5
movdqu 0x10(%rdi,%rax,4),%xmm3
movdqa %xmm5,%xmm6
punpckhbw %xmm0,%xmm6
pshuflw $0x18,%xmm6,%xmm6
pshufhw $0x18,%xmm6,%xmm6
movdqa %xmm5,%xmm7
punpcklbw %xmm0,%xmm7
pshuflw $0x18,%xmm7,%xmm7
pshufhw $0x18,%xmm7,%xmm7
packuswb %xmm6,%xmm7
pand   %xmm8,%xmm7
movdqa %xmm3,%xmm6
punpckhbw %xmm0,%xmm6
pshuflw $0x18,%xmm6,%xmm6
pshufhw $0x18,%xmm6,%xmm6
movdqa %xmm3,%xmm4
punpcklbw %xmm0,%xmm4
pshuflw $0x18,%xmm4,%xmm4
pshufhw $0x18,%xmm4,%xmm4
packuswb %xmm6,%xmm4
pand   %xmm8,%xmm4
psrad  $0x18,%xmm5
paddd  %xmm5,%xmm2
paddd  %xmm7,%xmm2
psrad  $0x18,%xmm3
paddd  %xmm3,%xmm1
paddd  %xmm4,%xmm1
add    $0x8,%rax
cmp    %rax,%rsi
jne    1160 <payload_checksum+0x60>
paddd  %xmm2,%xmm1
pshufd $0xee,%xmm1,%xmm0
paddd  %xmm1,%xmm0
pshufd $0x55,%xmm0,%xmm1
paddd  %xmm0,%xmm1
movd   %xmm1,%eax
cmp    %rsi,%rdx
je     1242 <payload_checksum+0x142>
lea    (%rdi,%r8,4),%rdx
mov    %eax,%esi
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%rcx),%eax
add    $0x4,%rcx
mov    %eax,%edi
and    $0xffffff,%edi
bswap  %edi
sar    $0x18,%eax
add    %esi,%eax
add    %edi,%eax
mov    %eax,%esi
cmp    %rdx,%rcx
jne    1220 <payload_checksum+0x120>
jmp    1242 <payload_checksum+0x142>
xor    %eax,%eax
mov    %eax,%ecx
and    $0xffffff,%ecx
bswap  %ecx
sar    $0x18,%eax
add    %ecx,%eax
ret

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

LLVM IR (-O2):
define i32 @payload_checksum(i32* %0, i32 %1) {
%3 = sext i32 %1 to i64
%4 = getelementptr inbounds i32, i32* %0, i64 %3
%5 = icmp eq i32 %1, 0
br i1 %5, label %54, label %6
6: ; preds = %2
%7 = add nsw i64 %3, 4611686018427387903
%8 = and i64 %7, 4611686018427387903
%9 = add nuw nsw i64 %8, 1
%10 = icmp ult i64 %8, 7
br i1 %10, label %40, label %11
11: ; preds = %6
%12 = and i64 %9, 9223372036854775800
%13 = getelementptr i32, i32* %0, i64 %12
br label %14
14: ; preds = %14, %11
%15 = phi i64 [ 0, %11 ], [ %34, %14 ]
%16 = phi <4 x i32> [ zeroinitializer, %11 ], [ %32, %14 ]
%17 = phi <4 x i32> [ zeroinitializer, %11 ], [ %33, %14 ]
%18 = getelementptr i32, i32* %0, i64 %15
%19 = bitcast i32* %18 to <4 x i32>*
%20 = load <4 x i32>, <4 x i32>* %19
%21 = getelementptr i32, i32* %18, i64 4
%22 = bitcast i32* %21 to <4 x i32>*
%23 = load <4 x i32>, <4 x i32>* %22
%24 = and <4 x i32> %20, <i32 16777215, i32 16777215, i32 16777215, i32 16777215>
%25 = and <4 x i32> %23, <i32 16777215, i32 16777215, i32 16777215, i32 16777215>
%26 = call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %24)
%27 = call <4 x i32> @llvm.bswap.v4i32(<4 x i32> %25)
%28 = ashr <4 x i32> %20, <i32 24, i32 24, i32 24, i32 24>
%29 = ashr <4 x i32> %23, <i32 24, i32 24, i32 24, i32 24>
%30 = add <4 x i32> %28, %16
%31 = add <4 x i32> %29, %17
%32 = add <4 x i32> %30, %26
%33 = add <4 x i32> %31, %27
%34 = add nuw i64 %15, 8
%35 = icmp eq i64 %34, %12
br i1 %35, label %36, label %14
36: ; preds = %14
%37 = add <4 x i32> %33, %32
%38 = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %37)
%39 = icmp eq i64 %9, %12
br i1 %39, label %54, label %40
40: ; preds = %6, %36
%41 = phi i32* [ %0, %6 ], [ %13, %36 ]
%42 = phi i32 [ 0, %6 ], [ %38, %36 ]
br label %43
43: ; preds = %40, %43
%44 = phi i32* [ %46, %43 ], [ %41, %40 ]
%45 = phi i32 [ %52, %43 ], [ %42, %40 ]
%46 = getelementptr inbounds i32, i32* %44, i64 1
%47 = load i32, i32* %44
%48 = and i32 %47, 16777215
%49 = tail call i32 @llvm.bswap.i32(i32 %48)
%50 = ashr i32 %47, 24
%51 = add i32 %50, %45
%52 = add i32 %51, %49
%53 = icmp eq i32* %46, %4
br i1 %53, label %54, label %43
54: ; preds = %43, %36, %2
%55 = phi i32 [ 0, %2 ], [ %38, %36 ], [ %52, %43 ]
%56 = and i32 %55, 16777215
%57 = tail call i32 @llvm.bswap.i32(i32 %56)
%58 = ashr i32 %55, 24
%59 = add nsw i32 %57, %58
ret i32 %59
}