#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 xmodem_crc(unsigned char *, int);
int check_packet(unsigned char *pkt, int pkt_num) {
  int crc;
  if (pkt[0] != 0x01)
    return -1;
  if (pkt[1] != pkt_num)
    return -2;
  if (pkt[2] != 255 - pkt_num)
    return -3;
  crc = xmodem_crc(pkt + 3, 128);
  if (crc != ((pkt[131] << 8) | pkt[132]))
    return -4;
  return 0;
}


x86 assembly (clang, -O0):
<check_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x1,%eax
je     113b <check_packet+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <check_packet+0xba>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
cmp    -0x14(%rbp),%eax
je     1158 <check_packet+0x48>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11ca <check_packet+0xba>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
mov    $0xff,%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
je     117c <check_packet+0x6c>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11ca <check_packet+0xba>
mov    -0x10(%rbp),%rdi
add    $0x3,%rdi
mov    $0x80,%esi
call   1030 <xmodem_crc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x10(%rbp),%rcx
movzbl 0x83(%rcx),%ecx
shl    $0x8,%ecx
mov    -0x10(%rbp),%rdx
movzbl 0x84(%rdx),%edx
or     %edx,%ecx
cmp    %ecx,%eax
je     11c3 <check_packet+0xb3>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11ca <check_packet+0xba>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_packet>:
push   %rbx
mov    $0xffffffff,%eax
cmpb   $0x1,(%rdi)
jne    1167 <check_packet+0x57>
mov    %rdi,%rbx
movzbl 0x1(%rdi),%ecx
mov    $0xfffffffe,%eax
cmp    %esi,%ecx
jne    1167 <check_packet+0x57>
movzbl 0x2(%rbx),%ecx
mov    $0xff,%edx
sub    %esi,%edx
mov    $0xfffffffd,%eax
cmp    %ecx,%edx
jne    1167 <check_packet+0x57>
lea    0x3(%rbx),%rdi
mov    $0x80,%esi
call   1030 <xmodem_crc@plt>
movzwl 0x83(%rbx),%ecx
shl    $0x10,%ecx
bswap  %ecx
xor    %edx,%edx
cmp    %ecx,%eax
sete   %dl
lea    -0x4(,%rdx,4),%eax
pop    %rbx
ret

LLVM IR (-O0):
define i32 @check_packet(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = load i8*, i8** %4
%8 = getelementptr inbounds i8, i8* %7, i64 0
%9 = load i8, i8* %8
%10 = zext i8 %9 to i32
%11 = icmp ne i32 %10, 1
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 -1, i32* %3
br label %48
13: ; preds = %2
%14 = load i8*, i8** %4
%15 = getelementptr inbounds i8, i8* %14, i64 1
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = load i32, i32* %5
%19 = icmp ne i32 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 -2, i32* %3
br label %48
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = getelementptr inbounds i8, i8* %22, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = load i32, i32* %5
%27 = sub nsw i32 255, %26
%28 = icmp ne i32 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 -3, i32* %3
br label %48
30: ; preds = %21
%31 = load i8*, i8** %4
%32 = getelementptr inbounds i8, i8* %31, i64 3
%33 = call i32 @xmodem_crc(i8* %32, i32 128)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 131
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = shl i32 %38, 8
%40 = load i8*, i8** %4
%41 = getelementptr inbounds i8, i8* %40, i64 132
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
%44 = or i32 %39, %43
%45 = icmp ne i32 %34, %44
br i1 %45, label %46, label %47
46: ; preds = %30
store i32 -4, i32* %3
br label %48
47: ; preds = %30
store i32 0, i32* %3
br label %48
48: ; preds = %47, %46, %29, %20, %12
%49 = load i32, i32* %3
ret i32 %49
}

LLVM IR (-O2):
define i32 @check_packet(i8* %0, i32 %1) {
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 1
br i1 %4, label %5, label %29
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 1
%7 = load i8, i8* %6
%8 = zext i8 %7 to i32
%9 = icmp eq i32 %8, %1
br i1 %9, label %10, label %29
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %0, i64 2
%12 = load i8, i8* %11
%13 = zext i8 %12 to i32
%14 = sub nsw i32 255, %1
%15 = icmp eq i32 %14, %13
br i1 %15, label %16, label %29
16: ; preds = %10
%17 = getelementptr inbounds i8, i8* %0, i64 3
%18 = tail call i32 @xmodem_crc(i8* %17, i32 128)
%19 = getelementptr inbounds i8, i8* %0, i64 131
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = shl nuw nsw i32 %21, 8
%23 = getelementptr inbounds i8, i8* %0, i64 132
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = or i32 %22, %25
%27 = icmp eq i32 %18, %26
%28 = select i1 %27, i32 0, i32 -4
br label %29
29: ; preds = %16, %10, %5, %2
%30 = phi i32 [ -1, %2 ], [ -2, %5 ], [ -3, %10 ], [ %28, %16 ]
ret i32 %30
}