#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 */
unsigned int CRCupdate(unsigned int, unsigned char);
unsigned int calcCRC(unsigned char *buffer) {
  int j;
  unsigned int CRC;
  CRC = 0xFFFF;
  for (j = 0; j < 256; j++)
    CRC = CRCupdate(CRC, buffer[j]);
  CRCupdate(CRC, 0);
  CRCupdate(CRC, 0);
  return ((~CRC) & 0xffff);
}


x86 assembly (clang, -O0):
<calcCRC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0xffff,-0x10(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x100,-0xc(%rbp)
jge    115c <calcCRC+0x4c>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
call   1030 <CRCupdate@plt>
mov    %eax,-0x10(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112a <calcCRC+0x1a>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1030 <CRCupdate@plt>
mov    -0x10(%rbp),%edi
xor    %esi,%esi
call   1030 <CRCupdate@plt>
mov    -0x10(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xffff,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calcCRC>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0xffff,%ebp
xor    %ebx,%ebx
xchg   %ax,%ax
movzbl (%r14,%rbx,1),%esi
mov    %ebp,%edi
call   1030 <CRCupdate@plt>
mov    %eax,%ebp
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    1120 <calcCRC+0x10>
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <CRCupdate@plt>
mov    %ebp,%ebx
not    %ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <CRCupdate@plt>
movzwl %bx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calcCRC(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 65535, i32* %4
store i32 0, i32* %3
br label %5
5: ; preds = %16, %1
%6 = load i32, i32* %3
%7 = icmp slt i32 %6, 256
br i1 %7, label %8, label %19
8: ; preds = %5
%9 = load i32, i32* %4
%10 = load i8*, i8** %2
%11 = load i32, i32* %3
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %10, i64 %12
%14 = load i8, i8* %13
%15 = call i32 @CRCupdate(i32 %9, i8 zeroext %14)
store i32 %15, i32* %4
br label %16
16: ; preds = %8
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %5
19: ; preds = %5
%20 = load i32, i32* %4
%21 = call i32 @CRCupdate(i32 %20, i8 zeroext 0)
%22 = load i32, i32* %4
%23 = call i32 @CRCupdate(i32 %22, i8 zeroext 0)
%24 = load i32, i32* %4
%25 = xor i32 %24, -1
%26 = and i32 %25, 65535
ret i32 %26
}

LLVM IR (-O2):
define i32 @calcCRC(i8* nocapture %0) {
br label %2
2: ; preds = %1, %2
%3 = phi i64 [ 0, %1 ], [ %8, %2 ]
%4 = phi i32 [ 65535, %1 ], [ %7, %2 ]
%5 = getelementptr inbounds i8, i8* %0, i64 %3
%6 = load i8, i8* %5
%7 = tail call i32 @CRCupdate(i32 %4, i8 zeroext %6)
%8 = add nuw nsw i64 %3, 1
%9 = icmp eq i64 %8, 256
br i1 %9, label %10, label %2
10: ; preds = %2
%11 = tail call i32 @CRCupdate(i32 %7, i8 zeroext 0)
%12 = tail call i32 @CRCupdate(i32 %7, i8 zeroext 0)
%13 = and i32 %7, 65535
%14 = xor i32 %13, 65535
ret i32 %14
}