#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 */
int crc_update_bits(unsigned char, int, int);
int crc16bits(unsigned char *data, int nbits) {
  int i;
  int crc = 0xffff;
  int nbytes = nbits >> 3;
  nbits = nbits & 7;
  for (i = 0; i < nbytes; i++)
    crc = crc_update_bits(data[i], crc, 8);
  crc = crc_update_bits(data[i], crc, nbits);
  return crc & 0xffff;
}


x86 assembly (clang, -O0):
<crc16bits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffff,-0x14(%rbp)
mov    -0xc(%rbp),%eax
sar    $0x3,%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1177 <crc16bits+0x67>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%esi
mov    $0x8,%edx
movzbl %al,%edi
call   1030 <crc_update_bits@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    113f <crc16bits+0x2f>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    -0x14(%rbp),%esi
mov    -0xc(%rbp),%edx
movzbl %al,%edi
call   1030 <crc_update_bits@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0xffff,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc16bits>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
cmp    $0x8,%esi
jl     114c <crc16bits+0x3c>
mov    %r14d,%ebp
shr    $0x3,%ebp
mov    $0xffff,%eax
xor    %ebx,%ebx
nop
movzbl (%r15,%rbx,1),%edi
mov    %eax,%esi
mov    $0x8,%edx
call   1030 <crc_update_bits@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1130 <crc16bits+0x20>
jmp    1153 <crc16bits+0x43>
mov    $0xffff,%eax
xor    %ebp,%ebp
and    $0x7,%r14d
movzbl (%r15,%rbp,1),%edi
mov    %eax,%esi
mov    %r14d,%edx
call   1030 <crc_update_bits@plt>
movzwl %ax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crc16bits(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 65535, i32* %6
%8 = load i32, i32* %4
%9 = ashr i32 %8, 3
store i32 %9, i32* %7
%10 = load i32, i32* %4
%11 = and i32 %10, 7
store i32 %11, i32* %4
store i32 0, i32* %5
br label %12
12: ; preds = %24, %2
%13 = load i32, i32* %5
%14 = load i32, i32* %7
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i8*, i8** %3
%18 = load i32, i32* %5
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = load i32, i32* %6
%23 = call i32 @crc_update_bits(i8 zeroext %21, i32 %22, i32 8)
store i32 %23, i32* %6
br label %24
24: ; preds = %16
%25 = load i32, i32* %5
%26 = add nsw i32 %25, 1
store i32 %26, i32* %5
br label %12
27: ; preds = %12
%28 = load i8*, i8** %3
%29 = load i32, i32* %5
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = load i8, i8* %31
%33 = load i32, i32* %6
%34 = load i32, i32* %4
%35 = call i32 @crc_update_bits(i8 zeroext %32, i32 %33, i32 %34)
store i32 %35, i32* %6
%36 = load i32, i32* %6
%37 = and i32 %36, 65535
ret i32 %37
}

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