#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 */
int packet_byte(int, char *);
int check_crc(char *packet, int len) {
  int crc, b;
  int bytes;
  if (len >= 20 && (len & 0x07) == 2) {
    bytes = (len - 2) >> 3;
    crc = packet_byte(--bytes, packet);
    while (bytes-- > 0) {
      b = packet_byte(bytes, packet);
      crc -= b;
    }
    return crc & 0xff;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<check_crc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x14,-0x14(%rbp)
jl     119c <check_crc+0x8c>
mov    -0x14(%rbp),%eax
and    $0x7,%eax
cmp    $0x2,%eax
jne    119c <check_crc+0x8c>
mov    -0x14(%rbp),%eax
sub    $0x2,%eax
sar    $0x3,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
add    $0xffffffff,%edi
mov    %edi,-0x20(%rbp)
mov    -0x10(%rbp),%rsi
call   1030 <packet_byte@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x20(%rbp)
cmp    $0x0,%eax
jle    118c <check_crc+0x7c>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <packet_byte@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x18(%rbp)
jmp    1159 <check_crc+0x49>
mov    -0x18(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x4(%rbp)
jmp    11a3 <check_crc+0x93>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_crc>:
push   %rbp
push   %r14
push   %rbx
mov    $0xffffffff,%eax
cmp    $0x14,%esi
jl     1165 <check_crc+0x55>
mov    %esi,%ebx
mov    %esi,%ecx
and    $0x7,%ecx
cmp    $0x2,%ecx
jne    1165 <check_crc+0x55>
mov    %rdi,%r14
add    $0xfffffffe,%ebx
shr    $0x3,%ebx
lea    -0x1(%rbx),%edi
mov    %r14,%rsi
call   1030 <packet_byte@plt>
mov    %eax,%ebp
add    $0xfffffffe,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <packet_byte@plt>
sub    %eax,%ebp
add    $0xffffffff,%ebx
jb     1150 <check_crc+0x40>
movzbl %bpl,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @check_crc(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp sge i32 %9, 20
br i1 %10, label %11, label %37
11: ; preds = %2
%12 = load i32, i32* %5
%13 = and i32 %12, 7
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %37
15: ; preds = %11
%16 = load i32, i32* %5
%17 = sub nsw i32 %16, 2
%18 = ashr i32 %17, 3
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = add nsw i32 %19, -1
store i32 %20, i32* %8
%21 = load i8*, i8** %4
%22 = call i32 @packet_byte(i32 %20, i8* %21)
store i32 %22, i32* %6
br label %23
23: ; preds = %27, %15
%24 = load i32, i32* %8
%25 = add nsw i32 %24, -1
store i32 %25, i32* %8
%26 = icmp sgt i32 %24, 0
br i1 %26, label %27, label %34
27: ; preds = %23
%28 = load i32, i32* %8
%29 = load i8*, i8** %4
%30 = call i32 @packet_byte(i32 %28, i8* %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, %31
store i32 %33, i32* %6
br label %23
34: ; preds = %23
%35 = load i32, i32* %6
%36 = and i32 %35, 255
store i32 %36, i32* %3
br label %38
37: ; preds = %11, %2
store i32 -1, i32* %3
br label %38
38: ; preds = %37, %34
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
define i32 @check_crc(i8* %0, i32 %1) {
%3 = icmp sgt i32 %1, 19
%4 = and i32 %1, 7
%5 = icmp eq i32 %4, 2
%6 = and i1 %3, %5
br i1 %6, label %7, label %22
7: ; preds = %2
%8 = add nsw i32 %1, -2
%9 = lshr i32 %8, 3
%10 = add nsw i32 %9, -1
%11 = tail call i32 @packet_byte(i32 %10, i8* %0)
%12 = add nsw i32 %9, -2
br label %13
13: ; preds = %7, %13
%14 = phi i32 [ %18, %13 ], [ %12, %7 ]
%15 = phi i32 [ %17, %13 ], [ %11, %7 ]
%16 = tail call i32 @packet_byte(i32 %14, i8* %0)
%17 = sub nsw i32 %15, %16
%18 = add nsw i32 %14, -1
%19 = icmp eq i32 %14, 0
br i1 %19, label %20, label %13
20: ; preds = %13
%21 = and i32 %17, 255
br label %22
22: ; preds = %2, %20
%23 = phi i32 [ %21, %20 ], [ -1, %2 ]
ret i32 %23
}