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

/* Variables and functions */
int pppfcs16(int, int const *, int);
int hdlc_check_fcs(const uint8_t *buf, uint16_t size) {
  uint16_t fcs = pppfcs16(0xffff, &buf[1], size - 4);
  fcs ^= 0xffff;
  uint16_t expected = buf[size - 2];
  expected = (expected << 8) + buf[size - 3];
  return (expected == fcs) ? 0 : -6;
}


x86 assembly (clang, -O0):
<hdlc_check_fcs>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rsi
add    $0x4,%rsi
mov    -0xc(%rbp),%edx
sub    $0x4,%edx
mov    $0xffff,%edi
call   1030 <pppfcs16@plt>
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
xor    $0xffff,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
mov    -0x8(%rbp),%rcx
mov    -0xc(%rbp),%edx
sub    $0x3,%edx
movslq %edx,%rdx
add    (%rcx,%rdx,4),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%esi
mov    $0xfffffffa,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hdlc_check_fcs>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x4(%rdi),%rsi
lea    -0x4(%r14),%edx
mov    $0xffff,%edi
call   1030 <pppfcs16@plt>
xor    $0xffff,%eax
movslq %r14d,%rcx
mov    -0x8(%rbx,%rcx,4),%edx
shl    $0x8,%edx
add    -0xc(%rbx,%rcx,4),%edx
xor    %ecx,%ecx
cmp    %eax,%edx
mov    $0xfffffffa,%eax
cmove  %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @hdlc_check_fcs(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
%7 = load i32*, i32** %3
%8 = getelementptr inbounds i32, i32* %7, i64 1
%9 = load i32, i32* %4
%10 = sub nsw i32 %9, 4
%11 = call i32 @pppfcs16(i32 65535, i32* %8, i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = xor i32 %12, 65535
store i32 %13, i32* %5
%14 = load i32*, i32** %3
%15 = load i32, i32* %4
%16 = sub nsw i32 %15, 2
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i32, i32* %14, i64 %17
%19 = load i32, i32* %18
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = shl i32 %20, 8
%22 = load i32*, i32** %3
%23 = load i32, i32* %4
%24 = sub nsw i32 %23, 3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %22, i64 %25
%27 = load i32, i32* %26
%28 = add nsw i32 %21, %27
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = load i32, i32* %5
%31 = icmp eq i32 %29, %30
%32 = zext i1 %31 to i64
%33 = select i1 %31, i32 0, i32 -6
ret i32 %33
}

LLVM IR (-O2):
define i32 @hdlc_check_fcs(i32* %0, i32 %1) {
%3 = getelementptr inbounds i32, i32* %0, i64 1
%4 = add nsw i32 %1, -4
%5 = tail call i32 @pppfcs16(i32 65535, i32* %3, i32 %4)
%6 = xor i32 %5, 65535
%7 = add nsw i32 %1, -2
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i32, i32* %0, i64 %8
%10 = load i32, i32* %9
%11 = shl i32 %10, 8
%12 = add nsw i32 %1, -3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = add nsw i32 %11, %15
%17 = icmp eq i32 %16, %6
%18 = select i1 %17, i32 0, i32 -6
ret i32 %18
}