#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 short crc_update(unsigned short, unsigned char);
unsigned short crc16(void *data, unsigned short cnt) {
  unsigned short crc = 0xff;
  unsigned char *ptr = (unsigned char *)data;
  int i;
  for (i = 0; i < cnt; i++) {
    crc = crc_update(crc, *ptr);
    ptr++;
  }
  return crc;
}


x86 assembly (clang, -O0):
<crc16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
movw   $0xff,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
movzwl -0xa(%rbp),%ecx
cmp    %ecx,%eax
jge    1178 <crc16+0x68>
mov    -0xc(%rbp),%cx
mov    -0x18(%rbp),%rax
movzwl %cx,%edi
movzbl (%rax),%esi
call   1030 <crc_update@plt>
mov    %ax,-0xc(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1138 <crc16+0x28>
movzwl -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc16>:
push   %r15
push   %r14
push   %rbx
test   %esi,%esi
je     1148 <crc16+0x38>
mov    %rdi,%r14
movzwl %si,%r15d
mov    $0xff,%ax
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%esi
movzwl %ax,%edi
call   1030 <crc_update@plt>
add    $0x1,%rbx
cmp    %ebx,%r15d
jne    1130 <crc16+0x20>
jmp    114c <crc16+0x3c>
mov    $0xff,%ax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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