#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 scalar_t__ uint16_t;

/* Variables and functions */
scalar_t__ calcCRC16(void *, int);
int /*<<< orphan*/ debug(int, char *, scalar_t__, scalar_t__);
int packetCheck16(void *buf, int size) {
  uint16_t rcrc16, crc16;
  if (!size)
    return -1;
  crc16 = calcCRC16(buf, size - sizeof(uint16_t));
  rcrc16 = *((uint16_t *)(((uint8_t *)buf) + (size - sizeof(uint16_t))));
  debug(4, "Rx CRC16: 0x%04X, Calc CRC16 0x%04X", rcrc16, crc16);
  if (crc16 == rcrc16)
    return 0;
  else
    return -1;
}


x86 assembly (clang, -O0):
<packetCheck16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1145 <packetCheck16+0x25>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <packetCheck16+0x8a>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
sub    $0x8,%rax
mov    %eax,%esi
call   1030 <calcCRC16@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
sub    $0x8,%rcx
mov    (%rax,%rcx,4),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x4,%edi
lea    0xe7c(%rip),%rsi
call   1040 <debug@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    11a3 <packetCheck16+0x83>
movl   $0x0,-0x4(%rbp)
jmp    11aa <packetCheck16+0x8a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<packetCheck16>:
test   %esi,%esi
je     1167 <packetCheck16+0x47>
push   %r15
push   %r14
push   %rbx
mov    %rdi,%rbx
movslq %esi,%r15
lea    -0x8(%r15),%rsi
call   1030 <calcCRC16@plt>
mov    %rax,%r14
mov    -0x20(%rbx,%r15,4),%rbx
lea    0xeb9(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1040 <debug@plt>
xor    %eax,%eax
cmp    %rbx,%r14
setne  %al
neg    %eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
@.str = [36 x i8] c"Rx CRC16: 0x%04X, Calc CRC16 0x%04X\00"
define i32 @packetCheck16(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %11, label %10
10: ; preds = %2
store i32 -1, i32* %3
br label %34
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = sext i32 %13 to i64
%15 = sub i64 %14, 8
%16 = trunc i64 %15 to i32
%17 = call i64 @calcCRC16(i8* %12, i32 %16)
store i64 %17, i64* %7
%18 = load i8*, i8** %4
%19 = bitcast i8* %18 to i32*
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = sub i64 %21, 8
%23 = getelementptr inbounds i32, i32* %19, i64 %22
%24 = bitcast i32* %23 to i64*
%25 = load i64, i64* %24
store i64 %25, i64* %6
%26 = load i64, i64* %6
%27 = load i64, i64* %7
%28 = call i32 @debug(i32 4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %26, i64 %27)
%29 = load i64, i64* %7
%30 = load i64, i64* %6
%31 = icmp eq i64 %29, %30
br i1 %31, label %32, label %33
32: ; preds = %11
store i32 0, i32* %3
br label %34
33: ; preds = %11
store i32 -1, i32* %3
br label %34
34: ; preds = %33, %32, %10
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@.str = [36 x i8] c"Rx CRC16: 0x%04X, Calc CRC16 0x%04X\00"
define i32 @packetCheck16(i8* %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %16, label %4
4: ; preds = %2
%5 = sext i32 %1 to i64
%6 = add nsw i64 %5, -8
%7 = trunc i64 %6 to i32
%8 = tail call i64 @calcCRC16(i8* %0, i32 %7)
%9 = bitcast i8* %0 to i32*
%10 = getelementptr inbounds i32, i32* %9, i64 %6
%11 = bitcast i32* %10 to i64*
%12 = load i64, i64* %11
%13 = tail call i32 @debug(i32 4, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %12, i64 %8)
%14 = icmp ne i64 %8, %12
%15 = sext i1 %14 to i32
br label %16
16: ; preds = %4, %2
%17 = phi i32 [ -1, %2 ], [ %15, %4 ]
ret i32 %17
}