#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 crc16(unsigned short, unsigned char *, unsigned short);
unsigned char crcGetHexFromASCII(unsigned char *, int);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ malloc(unsigned short);
unsigned short crca16(unsigned short preset, unsigned char *dataP,
                      unsigned short n) {
  unsigned short checkLen;
  unsigned char *check;
  unsigned int ix, iy;
  unsigned short ret;
  if (n < 1)
    return 0;
  iy = 0;
  checkLen = n / 2;
  check = (unsigned char *)malloc(checkLen * sizeof(unsigned char));
  for (ix = 0; ix < checkLen; ix++) {
    check[ix] = crcGetHexFromASCII(&dataP[iy], 2);
    iy += 2;
  }
  ret = crc16(preset, check, checkLen);
  free(check);
  return ret;
}


x86 assembly (clang, -O0):
<crca16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dx,%ax
mov    %di,%cx
mov    %cx,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0x1,%eax
jge    1172 <crca16+0x32>
movw   $0x0,-0x2(%rbp)
jmp    1213 <crca16+0xd3>
movl   $0x0,-0x28(%rbp)
movzwl -0x12(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %ax,-0x14(%rbp)
movzwl -0x14(%rbp),%eax
shl    $0x0,%rax
movzwl %ax,%edi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
movzwl -0x14(%rbp),%ecx
cmp    %ecx,%eax
jae    11ea <crca16+0xaa>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%eax
add    %rax,%rdi
mov    $0x2,%esi
call   1060 <crcGetHexFromASCII@plt>
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a4 <crca16+0x64>
mov    -0x4(%rbp),%ax
mov    -0x20(%rbp),%rsi
movzwl %ax,%edi
movzwl -0x14(%rbp),%edx
call   1050 <crc16@plt>
mov    %ax,-0x2a(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2a(%rbp),%ax
mov    %ax,-0x2(%rbp)
movzwl -0x2(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crca16>:
test   %edx,%edx
je     11d9 <crca16+0x99>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r14d
movzwl %dx,%ebp
mov    %ebp,%r15d
shr    %r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
mov    %rax,%r12
cmp    $0x1,%bp
je     11ae <crca16+0x6e>
cmp    $0x2,%r15w
mov    $0x1,%eax
cmovae %r15d,%eax
movzwl %ax,%r13d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x2,%esi
call   1060 <crcGetHexFromASCII@plt>
mov    %al,(%r12,%rbp,1)
add    $0x1,%rbp
add    $0x2,%rbx
cmp    %rbp,%r13
jne    1190 <crca16+0x50>
movzwl %r14w,%edi
movzwl %r15w,%edx
mov    %r12,%rsi
call   1050 <crc16@plt>
mov    %eax,%ebx
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define zeroext i16 @crca16(i16 zeroext %0, i8* %1, i16 zeroext %2) {
%4 = alloca i16
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i16
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i16
store i16 %0, i16* %5
store i8* %1, i8** %6
store i16 %2, i16* %7
%13 = load i16, i16* %7
%14 = zext i16 %13 to i32
%15 = icmp slt i32 %14, 1
br i1 %15, label %16, label %17
16: ; preds = %3
store i16 0, i16* %4
br label %56
17: ; preds = %3
store i32 0, i32* %11
%18 = load i16, i16* %7
%19 = zext i16 %18 to i32
%20 = sdiv i32 %19, 2
%21 = trunc i32 %20 to i16
store i16 %21, i16* %8
%22 = load i16, i16* %8
%23 = zext i16 %22 to i64
%24 = mul i64 %23, 1
%25 = trunc i64 %24 to i16
%26 = call i64 @malloc(i16 zeroext %25)
%27 = inttoptr i64 %26 to i8*
store i8* %27, i8** %9
store i32 0, i32* %10
br label %28
28: ; preds = %45, %17
%29 = load i32, i32* %10
%30 = load i16, i16* %8
%31 = zext i16 %30 to i32
%32 = icmp ult i32 %29, %31
br i1 %32, label %33, label %48
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = load i32, i32* %11
%36 = zext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = call zeroext i8 @crcGetHexFromASCII(i8* %37, i32 2)
%39 = load i8*, i8** %9
%40 = load i32, i32* %10
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %39, i64 %41
store i8 %38, i8* %42
%43 = load i32, i32* %11
%44 = add i32 %43, 2
store i32 %44, i32* %11
br label %45
45: ; preds = %33
%46 = load i32, i32* %10
%47 = add i32 %46, 1
store i32 %47, i32* %10
br label %28
48: ; preds = %28
%49 = load i16, i16* %5
%50 = load i8*, i8** %9
%51 = load i16, i16* %8
%52 = call zeroext i16 @crc16(i16 zeroext %49, i8* %50, i16 zeroext %51)
store i16 %52, i16* %12
%53 = load i8*, i8** %9
%54 = call i32 @free(i8* %53)
%55 = load i16, i16* %12
store i16 %55, i16* %4
br label %56
56: ; preds = %48, %16
%57 = load i16, i16* %4
ret i16 %57
}

LLVM IR (-O2):
define zeroext i16 @crca16(i16 zeroext %0, i8* %1, i16 zeroext %2) {
%4 = icmp eq i16 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3
%6 = lshr i16 %2, 1
%7 = tail call i64 @malloc(i16 zeroext %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i16 %2, 1
br i1 %9, label %22, label %10
10: ; preds = %5
%11 = call i16 @llvm.umax.i16(i16 %6, i16 1)
%12 = zext i16 %11 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %20, %13 ]
%15 = phi i64 [ 0, %10 ], [ %19, %13 ]
%16 = getelementptr inbounds i8, i8* %1, i64 %15
%17 = tail call zeroext i8 @crcGetHexFromASCII(i8* %16, i32 2)
%18 = getelementptr inbounds i8, i8* %8, i64 %14
store i8 %17, i8* %18
%19 = add nuw nsw i64 %15, 2
%20 = add nuw nsw i64 %14, 1
%21 = icmp eq i64 %20, %12
br i1 %21, label %22, label %13
22: ; preds = %13, %5
%23 = tail call zeroext i16 @crc16(i16 zeroext %0, i8* %8, i16 zeroext %6)
%24 = tail call i32 @free(i8* %8)
br label %25
25: ; preds = %3, %22
%26 = phi i16 [ %23, %22 ], [ 0, %3 ]
ret i16 %26
}