#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 uint8_t;
typedef int uint32_t;

/* Variables and functions */
uint32_t xmega_nvm_crc32(uint8_t *buffer, uint32_t buffer_length) {
  uint32_t address;
  uint32_t data_reg, help_a, help_b;
  uint32_t crc_reg = 0;
  for (address = 0; address < buffer_length; address += 2) {
    help_a = crc_reg << 1;
    help_a &= 0x00FFFFFE;
    help_b = crc_reg & (1 << 23);
    if (help_b > 0)
      help_b = 0x00FFFFFF;
    data_reg =
        (uint32_t)((buffer[address + 1] << 8) | buffer[address] & 0xFFFF);
    crc_reg = (help_a ^ data_reg) ^ (help_b & 0x0080001B);
    crc_reg = crc_reg & 0x00FFFFFF;
  }
  return crc_reg;
}


x86 assembly (clang, -O0):
<xmega_nvm_crc32>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ab <xmega_nvm_crc32+0xab>
mov    -0x20(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xfffffe,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
and    $0x800000,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1155 <xmega_nvm_crc32+0x55>
movl   $0xffffff,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
movslq -0x10(%rbp),%rdx
mov    (%rcx,%rdx,4),%ecx
and    $0xffff,%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
xor    -0x14(%rbp),%eax
mov    -0x1c(%rbp),%ecx
and    $0x80001b,%ecx
xor    %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
and    $0xffffff,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
jmp    1119 <xmega_nvm_crc32+0x19>
mov    -0x20(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<xmega_nvm_crc32>:
test   %esi,%esi
jle    1143 <xmega_nvm_crc32+0x43>
mov    %esi,%r8d
xor    %edx,%edx
xor    %esi,%esi
nopl   0x0(%rax,%rax,1)
lea    (%rsi,%rsi,1),%r9d
shl    $0x8,%esi
sar    $0x1f,%esi
and    $0x80001b,%esi
mov    0x4(%rdi,%rdx,4),%ecx
shl    $0x8,%ecx
movzwl (%rdi,%rdx,4),%eax
or     %ecx,%eax
xor    %r9d,%eax
and    $0xffffff,%eax
xor    %esi,%eax
add    $0x2,%rdx
mov    %eax,%esi
cmp    %r8,%rdx
jb     1110 <xmega_nvm_crc32+0x10>
ret
xor    %eax,%eax
ret

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

LLVM IR (-O2):
define i32 @xmega_nvm_crc32(i32* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %26
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %4, %6
%7 = phi i64 [ 0, %4 ], [ %24, %6 ]
%8 = phi i32 [ 0, %4 ], [ %23, %6 ]
%9 = shl nuw nsw i32 %8, 1
%10 = and i32 %8, 8388608
%11 = icmp eq i32 %10, 0
%12 = select i1 %11, i32 0, i32 8388635
%13 = or i64 %7, 1
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = shl i32 %15, 8
%17 = getelementptr inbounds i32, i32* %0, i64 %7
%18 = load i32, i32* %17
%19 = and i32 %18, 65535
%20 = or i32 %19, %16
%21 = xor i32 %20, %9
%22 = and i32 %21, 16777215
%23 = xor i32 %22, %12
%24 = add nuw nsw i64 %7, 2
%25 = icmp ult i64 %24, %5
br i1 %25, label %6, label %26
26: ; preds = %6, %2
%27 = phi i32 [ 0, %2 ], [ %23, %6 ]
ret i32 %27
}