#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 CRC16(unsigned int, unsigned char);
void appendCRC(unsigned char *buf, unsigned int len) {
  unsigned int Crc, i;
  Crc = 0xFFFF;
  for (i = 0; i < len - 2; i++) {
    Crc = CRC16(Crc, buf[i]);
  }
  buf[len - 2] = (Crc & 0x00FF);
  buf[len - 1] = (Crc & 0xFF00) / 256;
}


x86 assembly (clang, -O0):
<appendCRC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0xffff,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jae    1162 <appendCRC+0x52>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
movzbl (%rax,%rcx,1),%esi
call   1030 <CRC16@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112d <appendCRC+0x1d>
mov    -0x10(%rbp),%eax
and    $0xff,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
and    $0xff00,%eax
shr    $0x8,%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendCRC>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%eax
add    $0xfffffffe,%eax
je     114f <appendCRC+0x3f>
mov    %rsi,%r14
mov    %eax,%r15d
mov    $0xffff,%eax
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl (%rdi,%rbp,1),%esi
mov    %rdi,%rbx
mov    %eax,%edi
call   1030 <CRC16@plt>
mov    %rbx,%rdi
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1130 <appendCRC+0x20>
mov    %r14,%rsi
jmp    1157 <appendCRC+0x47>
xor    %r15d,%r15d
mov    $0xffff,%eax
mov    %al,(%rdi,%r15,1)
add    $0xffffffff,%esi
mov    %ah,(%rdi,%rsi,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @appendCRC(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 65535, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %20, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %4
%10 = sub i32 %9, 2
%11 = icmp ult i32 %8, %10
br i1 %11, label %12, label %23
12: ; preds = %7
%13 = load i32, i32* %5
%14 = load i8*, i8** %3
%15 = load i32, i32* %6
%16 = zext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = call i32 @CRC16(i32 %13, i8 zeroext %18)
store i32 %19, i32* %5
br label %20
20: ; preds = %12
%21 = load i32, i32* %6
%22 = add i32 %21, 1
store i32 %22, i32* %6
br label %7
23: ; preds = %7
%24 = load i32, i32* %5
%25 = and i32 %24, 255
%26 = trunc i32 %25 to i8
%27 = load i8*, i8** %3
%28 = load i32, i32* %4
%29 = sub i32 %28, 2
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 %26, i8* %31
%32 = load i32, i32* %5
%33 = and i32 %32, 65280
%34 = udiv i32 %33, 256
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %3
%37 = load i32, i32* %4
%38 = sub i32 %37, 1
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %36, i64 %39
store i8 %35, i8* %40
ret void
}

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