#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 */

/* Variables and functions */
scalar_t__ endian_swap(unsigned int);
unsigned int checksum(unsigned int *buffer, unsigned int offset,
                      unsigned int length) {
  unsigned int x, result = 0;
  buffer = (unsigned int *)(buffer + offset);
  for (x = 0; x < length; x++) {
    result += endian_swap(buffer[x]);
  }
  return result;
}


x86 assembly (clang, -O0):
<checksum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
jae    1177 <checksum+0x67>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
call   1030 <endian_swap@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1142 <checksum+0x32>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<checksum>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     1146 <checksum+0x36>
mov    %esi,%eax
lea    (%rdi,%rax,4),%r14
mov    %edx,%r15d
xor    %ebx,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rbx,4),%edi
call   1030 <endian_swap@plt>
add    %eax,%ebp
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1130 <checksum+0x20>
jmp    1148 <checksum+0x38>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @checksum(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
store i32 0, i32* %8
%9 = load i32*, i32** %4
%10 = load i32, i32* %5
%11 = zext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %9, i64 %11
store i32* %12, i32** %4
store i32 0, i32* %7
br label %13
13: ; preds = %28, %3
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = icmp ult i32 %14, %15
br i1 %16, label %17, label %31
17: ; preds = %13
%18 = load i32*, i32** %4
%19 = load i32, i32* %7
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i32, i32* %18, i64 %20
%22 = load i32, i32* %21
%23 = call i64 @endian_swap(i32 %22)
%24 = load i32, i32* %8
%25 = zext i32 %24 to i64
%26 = add nsw i64 %25, %23
%27 = trunc i64 %26 to i32
store i32 %27, i32* %8
br label %28
28: ; preds = %17
%29 = load i32, i32* %7
%30 = add i32 %29, 1
store i32 %30, i32* %7
br label %13
31: ; preds = %13
%32 = load i32, i32* %8
ret i32 %32
}

LLVM IR (-O2):
define i32 @checksum(i32* nocapture %0, i32 %1, i32 %2) {
%4 = zext i32 %1 to i64
%5 = getelementptr inbounds i32, i32* %0, i64 %4
%6 = icmp eq i32 %2, 0
br i1 %6, label %19, label %7
7: ; preds = %3
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %17, %9 ]
%11 = phi i32 [ 0, %7 ], [ %16, %9 ]
%12 = getelementptr inbounds i32, i32* %5, i64 %10
%13 = load i32, i32* %12
%14 = tail call i64 @endian_swap(i32 %13)
%15 = trunc i64 %14 to i32
%16 = add i32 %11, %15
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %9
19: ; preds = %9, %3
%20 = phi i32 [ 0, %3 ], [ %16, %9 ]
ret i32 %20
}