#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ parse_packet(unsigned char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  FILE *fd;
  int i;
  int in_packet = 0;
  int in_sequenece = 0;
  int flag = 0;
  unsigned char byte;
  int checksum = 0;
  unsigned char packet[32];
  int packet_length;
  int bytes_read;
  if (argc == 2)
    fd = fopen(argv[1], "rb");
  else
    fd = fopen("/dev/rfcomm0", "rb");
  if (!fd) {
    fprintf(stderr, "error couldnt open your mind.\n");
    return 1;
  }
  while (1) {
    checksum = 0;
    byte = fgetc(fd);
    if (byte == 255) {
      fprintf(stderr, "error could no longer read your mind.\n");
      return 1;
    }
    if (byte != 170) {
      continue;
    }
    byte = fgetc(fd);
    if (byte != 170) {
      continue;
    }
    byte = fgetc(fd);
    if (byte != 32) {
      fprintf(stderr, "packet claims wrong length %d not %d\n", byte, 32);
      continue;
    }
    bytes_read = fread(packet, 1, 32, fd);
    if (bytes_read != 32) {
      fprintf(stderr, "didnt get full packet got %d bytes.", bytes_read);
      continue;
    }
    for (i = 0; i < 32; i++) {
      checksum += packet[i];
    }
    checksum &= 0xFF;
    checksum = ~checksum & 0xFF;
    byte = fgetc(fd);
    if (byte == checksum) {
      parse_packet(packet);
      continue;
    } else {
      continue;
    }
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11a9 <main+0x59>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe65(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
jmp    11c0 <main+0x70>
lea    0xe53(%rip),%rdi
lea    0xe49(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ee <main+0x9e>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1360 <main+0x210>
jmp    11f3 <main+0xa3>
movl   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0xff,%eax
jne    1238 <main+0xe8>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1360 <main+0x210>
movzbl -0x29(%rbp),%eax
cmp    $0xaa,%eax
je     124c <main+0xfc>
jmp    11f3 <main+0xa3>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0xaa,%eax
je     126c <main+0x11c>
jmp    11f3 <main+0xa3>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    $0x20,%eax
je     12aa <main+0x15a>
mov    0x2d54(%rip),%rax
mov    (%rax),%edi
movzbl -0x29(%rbp),%edx
lea    0xdbd(%rip),%rsi
mov    $0x20,%ecx
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11f3 <main+0xa3>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20,%edx
call   1030 <fread@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x20,-0x58(%rbp)
je     12ed <main+0x19d>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
mov    -0x58(%rbp),%edx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    11f3 <main+0xa3>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x20,-0x1c(%rbp)
jge    131b <main+0x1cb>
movslq -0x1c(%rbp),%rax
movzbl -0x50(%rbp,%rax,1),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12f4 <main+0x1a4>
mov    -0x30(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
xor    $0xffffffff,%eax
and    $0xff,%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x29(%rbp)
movzbl -0x29(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    135b <main+0x20b>
lea    -0x50(%rbp),%rdi
call   1070 <parse_packet@plt>
jmp    11f3 <main+0xa3>
jmp    11f3 <main+0xa3>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x2,%edi
jne    1169 <main+0x19>
mov    0x8(%rsi),%rdi
jmp    1170 <main+0x20>
lea    0xe93(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     12e9 <main+0x199>
mov    %rax,%rbx
mov    %rsp,%r14
mov    0x2e4e(%rip),%r13
lea    0xee3(%rip),%r15
lea    0xeb6(%rip),%r12
jmp    11b8 <main+0x68>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0xff,%al
je     12dc <main+0x18c>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    $0xaa,%al
jne    11b0 <main+0x60>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    $0xaa,%al
jne    11b8 <main+0x68>
mov    %rbx,%rdi
call   1040 <fgetc@plt>
cmp    $0x20,%al
jne    12ac <main+0x15c>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
cmp    $0x20,%eax
jne    12c7 <main+0x177>
movdqa (%rsp),%xmm0
movdqa 0x10(%rsp),%xmm1
movdqa %xmm1,%xmm2
pxor   %xmm7,%xmm7
punpcklbw %xmm7,%xmm2
movdqa %xmm2,%xmm3
punpckhwd %xmm7,%xmm3
movdqa %xmm0,%xmm4
punpcklbw %xmm7,%xmm4
movdqa %xmm4,%xmm5
punpckhwd %xmm7,%xmm5
paddd  %xmm3,%xmm5
punpckhbw %xmm7,%xmm1
movdqa %xmm1,%xmm3
punpckhwd %xmm7,%xmm3
punpckhbw %xmm7,%xmm0
movdqa %xmm0,%xmm6
punpckhwd %xmm7,%xmm6
paddd  %xmm3,%xmm6
paddd  %xmm5,%xmm6
punpcklwd %xmm7,%xmm2
punpcklwd %xmm7,%xmm4
paddd  %xmm2,%xmm4
punpcklwd %xmm7,%xmm1
punpcklwd %xmm7,%xmm0
paddd  %xmm1,%xmm0
paddd  %xmm4,%xmm0
paddd  %xmm6,%xmm0
pshufd $0xee,%xmm0,%xmm1
paddd  %xmm0,%xmm1
pshufd $0x55,%xmm1,%xmm0
paddd  %xmm1,%xmm0
movd   %xmm0,%eax
not    %eax
movzbl %al,%ebp
mov    %rbx,%rdi
call   1040 <fgetc@plt>
movzbl %al,%eax
cmp    %eax,%ebp
jne    11b8 <main+0x68>
mov    %r14,%rdi
call   1070 <parse_packet@plt>
jmp    11b8 <main+0x68>
movzbl %al,%edx
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    $0x20,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11b8 <main+0x68>
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11b8 <main+0x68>
mov    0x0(%r13),%edi
lea    0xd48(%rip),%rsi
jmp    12f9 <main+0x1a9>
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [13 x i8] c"/dev/rfcomm0\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"error couldnt open your mind.\0A\00"
@.str.3 = [39 x i8] c"error could no longer read your mind.\0A\00"
@.str.4 = [38 x i8] c"packet claims wrong length %d not %d\0A\00"
@.str.5 = [36 x i8] c"didnt get full packet got %d bytes.\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i32
%13 = alloca [32 x i8]
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %12
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 2
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32* @fopen(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %22, i32** %6
br label %25
23: ; preds = %2
%24 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %6
br label %25
25: ; preds = %23, %18
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %31, label %28
28: ; preds = %25
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %105
31: ; preds = %25
br label %32
32: ; preds = %31, %45, %52, %59, %70, %101, %104
store i32 0, i32* %12
%33 = load i32*, i32** %6
%34 = call zeroext i8 @fgetc(i32* %33)
store i8 %34, i8* %11
%35 = load i8, i8* %11
%36 = zext i8 %35 to i32
%37 = icmp eq i32 %36, 255
br i1 %37, label %38, label %41
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %105
41: ; preds = %32
%42 = load i8, i8* %11
%43 = zext i8 %42 to i32
%44 = icmp ne i32 %43, 170
br i1 %44, label %45, label %46
45: ; preds = %41
br label %32
46: ; preds = %41
%47 = load i32*, i32** %6
%48 = call zeroext i8 @fgetc(i32* %47)
store i8 %48, i8* %11
%49 = load i8, i8* %11
%50 = zext i8 %49 to i32
%51 = icmp ne i32 %50, 170
br i1 %51, label %52, label %53
52: ; preds = %46
br label %32
53: ; preds = %46
%54 = load i32*, i32** %6
%55 = call zeroext i8 @fgetc(i32* %54)
store i8 %55, i8* %11
%56 = load i8, i8* %11
%57 = zext i8 %56 to i32
%58 = icmp ne i32 %57, 32
br i1 %58, label %59, label %64
59: ; preds = %53
%60 = load i32, i32* @stderr
%61 = load i8, i8* %11
%62 = zext i8 %61 to i32
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %62, i32 32)
br label %32
64: ; preds = %53
%65 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%66 = load i32*, i32** %6
%67 = call i32 @fread(i8* %65, i32 1, i32 32, i32* %66)
store i32 %67, i32* %15
%68 = load i32, i32* %15
%69 = icmp ne i32 %68, 32
br i1 %69, label %70, label %74
70: ; preds = %64
%71 = load i32, i32* @stderr
%72 = load i32, i32* %15
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %72)
br label %32
74: ; preds = %64
store i32 0, i32* %7
br label %75
75: ; preds = %86, %74
%76 = load i32, i32* %7
%77 = icmp slt i32 %76, 32
br i1 %77, label %78, label %89
78: ; preds = %75
%79 = load i32, i32* %7
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = zext i8 %82 to i32
%84 = load i32, i32* %12
%85 = add nsw i32 %84, %83
store i32 %85, i32* %12
br label %86
86: ; preds = %78
%87 = load i32, i32* %7
%88 = add nsw i32 %87, 1
store i32 %88, i32* %7
br label %75
89: ; preds = %75
%90 = load i32, i32* %12
%91 = and i32 %90, 255
store i32 %91, i32* %12
%92 = load i32, i32* %12
%93 = xor i32 %92, -1
%94 = and i32 %93, 255
store i32 %94, i32* %12
%95 = load i32*, i32** %6
%96 = call zeroext i8 @fgetc(i32* %95)
store i8 %96, i8* %11
%97 = load i8, i8* %11
%98 = zext i8 %97 to i32
%99 = load i32, i32* %12
%100 = icmp eq i32 %98, %99
br i1 %100, label %101, label %104
101: ; preds = %89
%102 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%103 = call i32 @parse_packet(i8* %102)
br label %32
104: ; preds = %89
br label %32
105: ; preds = %38, %28
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [13 x i8] c"/dev/rfcomm0\00"
@stderr = global i32 0
@.str.2 = [31 x i8] c"error couldnt open your mind.\0A\00"
@.str.3 = [39 x i8] c"error could no longer read your mind.\0A\00"
@.str.4 = [38 x i8] c"packet claims wrong length %d not %d\0A\00"
@.str.5 = [36 x i8] c"didnt get full packet got %d bytes.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca <32 x i8>
%4 = getelementptr inbounds <32 x i8>, <32 x i8>* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
br label %9
9: ; preds = %2, %6
%10 = phi i8* [ %8, %6 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %2 ]
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %49
16: ; preds = %9, %31
%17 = call zeroext i8 @fgetc(i32* %11)
switch i8 %17, label %31 [
i8 -1, label %18
i8 -86, label %21
]
18: ; preds = %16
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
br label %49
21: ; preds = %16
%22 = call zeroext i8 @fgetc(i32* %11)
%23 = icmp eq i8 %22, -86
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = call zeroext i8 @fgetc(i32* %11)
%26 = icmp eq i8 %25, 32
br i1 %26, label %32, label %27
27: ; preds = %24
%28 = zext i8 %25 to i32
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %28, i32 32)
br label %31
31: ; preds = %27, %44, %47, %21, %35, %16
br label %16
32: ; preds = %24
%33 = call i32 @fread(i8* %4, i32 1, i32 32, i32* %11)
%34 = icmp eq i32 %33, 32
br i1 %34, label %35, label %44
35: ; preds = %32
%36 = load <32 x i8>, <32 x i8>* %3
%37 = zext <32 x i8> %36 to <32 x i32>
%38 = call i32 @llvm.vector.reduce.add.v32i32(<32 x i32> %37)
%39 = and i32 %38, 255
%40 = xor i32 %39, 255
%41 = call zeroext i8 @fgetc(i32* %11)
%42 = zext i8 %41 to i32
%43 = icmp eq i32 %40, %42
br i1 %43, label %47, label %31
44: ; preds = %32
%45 = load i32, i32* @stderr
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i32 %33)
br label %31
47: ; preds = %35
%48 = call i32 @parse_packet(i8* %4)
br label %31
49: ; preds = %18, %13
ret i32 1
}