#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 /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;

/* Variables and functions */
int /*<<< orphan*/ crc16_ccitt_finalize(int /*<<< orphan*/);
int /*<<< orphan*/ crc16_ccitt_init();
int /*<<< orphan*/ crc16_ccitt_update(int /*<<< orphan*/, int /*<<< orphan*/);
uint16_t crc_calculate_algorithm(const void *data, const size_t len) {
  uint8_t *b = (uint8_t *)data;
  size_t i;
  uint16_t crc;
  crc = crc16_ccitt_init();
  for (i = 0; i < len; i++) {
    crc = crc16_ccitt_update(b[i], crc);
  }
  crc = crc16_ccitt_finalize(crc);
  return crc;
}


x86 assembly (clang, -O0):
<crc_calculate_algorithm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <crc16_ccitt_init@plt>
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    118f <crc_calculate_algorithm+0x5f>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
mov    -0x24(%rbp),%esi
call   1050 <crc16_ccitt_update@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    115a <crc_calculate_algorithm+0x2a>
mov    -0x24(%rbp),%edi
call   1030 <crc16_ccitt_finalize@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crc_calculate_algorithm>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
xor    %eax,%eax
call   1040 <crc16_ccitt_init@plt>
test   %r15,%r15
je     1164 <crc_calculate_algorithm+0x34>
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    (%r14,%rbx,4),%edi
mov    %eax,%esi
call   1050 <crc16_ccitt_update@plt>
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1150 <crc_calculate_algorithm+0x20>
mov    %eax,%edi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <crc16_ccitt_finalize@plt>

LLVM IR (-O0):
define i32 @crc_calculate_algorithm(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%8 = load i8*, i8** %3
%9 = bitcast i8* %8 to i32*
store i32* %9, i32** %5
%10 = call i32 (...) @crc16_ccitt_init()
store i32 %10, i32* %7
store i64 0, i64* %6
br label %11
11: ; preds = %22, %2
%12 = load i64, i64* %6
%13 = load i64, i64* %4
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %11
%16 = load i32*, i32** %5
%17 = load i64, i64* %6
%18 = getelementptr inbounds i32, i32* %16, i64 %17
%19 = load i32, i32* %18
%20 = load i32, i32* %7
%21 = call i32 @crc16_ccitt_update(i32 %19, i32 %20)
store i32 %21, i32* %7
br label %22
22: ; preds = %15
%23 = load i64, i64* %6
%24 = add i64 %23, 1
store i64 %24, i64* %6
br label %11
25: ; preds = %11
%26 = load i32, i32* %7
%27 = call i32 @crc16_ccitt_finalize(i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
ret i32 %28
}

LLVM IR (-O2):
define i32 @crc_calculate_algorithm(i8* nocapture %0, i64 %1) {
%3 = bitcast i8* %0 to i32*
%4 = tail call i32 (...) @crc16_ccitt_init()
%5 = icmp eq i64 %1, 0
br i1 %5, label %14, label %6
6: ; preds = %2, %6
%7 = phi i32 [ %11, %6 ], [ %4, %2 ]
%8 = phi i64 [ %12, %6 ], [ 0, %2 ]
%9 = getelementptr inbounds i32, i32* %3, i64 %8
%10 = load i32, i32* %9
%11 = tail call i32 @crc16_ccitt_update(i32 %10, i32 %7)
%12 = add nuw i64 %8, 1
%13 = icmp eq i64 %12, %1
br i1 %13, label %14, label %6
14: ; preds = %6, %2
%15 = phi i32 [ %4, %2 ], [ %11, %6 ]
%16 = tail call i32 @crc16_ccitt_finalize(i32 %15)
ret i32 %16
}