#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 /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ new_file();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_state(int);
int /*<<< orphan*/ write_byte(int);
void handle_byte(int byte) {
  static int idx = 0;
  static int crc = 0;
  static int state = 0;
  switch (state) {
  case 0:
    if (byte == 0x02) {
      idx = 1;
      state = 1;
      crc = (0x80 ^ byte);
      write_byte(byte);
    }
    break;
  case 1:
    idx++;
    crc ^= (0x80 ^ byte);
    write_byte(byte);
    if ((byte == 0x03) && (idx > 2))
      state = 2;
    break;
  case 2:
    state = 0;
    write_byte(byte);
    if (crc == byte)
      printf("\nCRC ok.\n");
    else
      printf("\nCRC ERROR: calculated %02x, in file %02x\n", crc, byte);
    set_state((0));
    exit(0);
    new_file();
    return;
  }
  set_state((3));
}


x86 assembly (clang, -O0):
<handle_byte>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2efb(%rip),%eax
mov    %eax,-0x8(%rbp)
test   %eax,%eax
je     1183 <handle_byte+0x43>
jmp    1161 <handle_byte+0x21>
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
je     11bc <handle_byte+0x7c>
jmp    1172 <handle_byte+0x32>
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
je     120d <handle_byte+0xcd>
jmp    1266 <handle_byte+0x126>
cmpl   $0x2,-0x4(%rbp)
jne    11b7 <handle_byte+0x77>
movl   $0x1,0x2ead(%rip)
movl   $0x1,0x2eab(%rip)
mov    -0x4(%rbp),%eax
xor    $0x80,%eax
mov    %eax,0x2e99(%rip)
mov    -0x4(%rbp),%edi
call   1060 <write_byte@plt>
jmp    1266 <handle_byte+0x126>
mov    0x2e82(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e79(%rip)
mov    -0x4(%rbp),%eax
xor    $0x80,%eax
xor    0x2e6f(%rip),%eax
mov    %eax,0x2e69(%rip)
mov    -0x4(%rbp),%edi
call   1060 <write_byte@plt>
cmpl   $0x3,-0x4(%rbp)
jne    1208 <handle_byte+0xc8>
cmpl   $0x2,0x2e4c(%rip)
jle    1208 <handle_byte+0xc8>
movl   $0x2,0x2e44(%rip)
jmp    1266 <handle_byte+0x126>
movl   $0x0,0x2e35(%rip)
mov    -0x4(%rbp),%edi
call   1060 <write_byte@plt>
mov    0x2e23(%rip),%eax
cmp    -0x4(%rbp),%eax
jne    1241 <handle_byte+0x101>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1258 <handle_byte+0x118>
mov    0x2e01(%rip),%esi
mov    -0x4(%rbp),%edx
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1040 <set_state@plt>
xor    %edi,%edi
call   1050 <exit@plt>
mov    $0x3,%edi
call   1040 <set_state@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_byte>:
push   %rbx
mov    %edi,%ebx
mov    0x2efb(%rip),%eax
test   %eax,%eax
je     119b <handle_byte+0x4b>
cmp    $0x1,%eax
je     11ca <handle_byte+0x7a>
cmp    $0x2,%eax
jne    1203 <handle_byte+0xb3>
movl   $0x0,0x2edf(%rip)
mov    %ebx,%edi
call   1070 <write_byte@plt>
mov    0x2ece(%rip),%esi
cmp    %ebx,%esi
jne    120e <handle_byte+0xbe>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
jmp    121e <handle_byte+0xce>
cmp    $0x2,%ebx
jne    1203 <handle_byte+0xb3>
movl   $0x1,0x2ea2(%rip)
movl   $0x1,0x2ea0(%rip)
movl   $0x82,0x2e92(%rip)
mov    $0x2,%edi
call   1070 <write_byte@plt>
jmp    1203 <handle_byte+0xb3>
addl   $0x1,0x2e7b(%rip)
mov    0x2e79(%rip),%eax
xor    %ebx,%eax
xor    $0x80,%eax
mov    %eax,0x2e6c(%rip)
mov    %ebx,%edi
call   1070 <write_byte@plt>
cmp    $0x3,%ebx
jne    1203 <handle_byte+0xb3>
cmpl   $0x3,0x2e55(%rip)
jl     1203 <handle_byte+0xb3>
movl   $0x2,0x2e51(%rip)
mov    $0x3,%edi
pop    %rbx
jmp    1050 <set_state@plt>
lea    0xdeb(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <set_state@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@handle_byte.idx = internal global i32 0
@handle_byte.crc = internal global i32 0
@handle_byte.state = internal global i32 0
@.str = [10 x i8] c"\0ACRC ok.\0A\00"
@.str.1 = [43 x i8] c"\0ACRC ERROR: calculated %02x, in file %02x\0A\00"
define void @handle_byte(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = load i32, i32* @handle_byte.state
switch i32 %3, label %44 [
i32 0, label %4
i32 1, label %13
i32 2, label %29
]
4: ; preds = %1
%5 = load i32, i32* %2
%6 = icmp eq i32 %5, 2
br i1 %6, label %7, label %12
7: ; preds = %4
store i32 1, i32* @handle_byte.idx
store i32 1, i32* @handle_byte.state
%8 = load i32, i32* %2
%9 = xor i32 128, %8
store i32 %9, i32* @handle_byte.crc
%10 = load i32, i32* %2
%11 = call i32 @write_byte(i32 %10)
br label %12
12: ; preds = %7, %4
br label %44
13: ; preds = %1
%14 = load i32, i32* @handle_byte.idx
%15 = add nsw i32 %14, 1
store i32 %15, i32* @handle_byte.idx
%16 = load i32, i32* %2
%17 = xor i32 128, %16
%18 = load i32, i32* @handle_byte.crc
%19 = xor i32 %18, %17
store i32 %19, i32* @handle_byte.crc
%20 = load i32, i32* %2
%21 = call i32 @write_byte(i32 %20)
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %28
24: ; preds = %13
%25 = load i32, i32* @handle_byte.idx
%26 = icmp sgt i32 %25, 2
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 2, i32* @handle_byte.state
br label %28
28: ; preds = %27, %24, %13
br label %44
29: ; preds = %1
store i32 0, i32* @handle_byte.state
%30 = load i32, i32* %2
%31 = call i32 @write_byte(i32 %30)
%32 = load i32, i32* @handle_byte.crc
%33 = load i32, i32* %2
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %29
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %41
37: ; preds = %29
%38 = load i32, i32* @handle_byte.crc
%39 = load i32, i32* %2
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 %39)
br label %41
41: ; preds = %37, %35
%42 = call i32 @set_state(i32 0)
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %1, %28, %12
%45 = call i32 @set_state(i32 3)
ret void
}

LLVM IR (-O2):
@handle_byte.idx = internal unnamed_addr global i32 0
@handle_byte.crc = internal unnamed_addr global i32 0
@handle_byte.state = internal unnamed_addr global i32 0
@.str.1 = [43 x i8] c"\0ACRC ERROR: calculated %02x, in file %02x\0A\00"
@str = [9 x i8] c"\0ACRC ok.\00"
define void @handle_byte(i32 %0) {
%2 = load i32, i32* @handle_byte.state
switch i32 %2, label %30 [
i32 0, label %3
i32 1, label %7
i32 2, label %19
]
3: ; preds = %1
%4 = icmp eq i32 %0, 2
br i1 %4, label %5, label %30
5: ; preds = %3
store i32 1, i32* @handle_byte.idx
store i32 1, i32* @handle_byte.state
store i32 130, i32* @handle_byte.crc
%6 = tail call i32 @write_byte(i32 2)
br label %30
7: ; preds = %1
%8 = load i32, i32* @handle_byte.idx
%9 = add nsw i32 %8, 1
store i32 %9, i32* @handle_byte.idx
%10 = load i32, i32* @handle_byte.crc
%11 = xor i32 %10, %0
%12 = xor i32 %11, 128
store i32 %12, i32* @handle_byte.crc
%13 = tail call i32 @write_byte(i32 %0)
%14 = icmp eq i32 %0, 3
%15 = load i32, i32* @handle_byte.idx
%16 = icmp sgt i32 %15, 2
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %18, label %30
18: ; preds = %7
store i32 2, i32* @handle_byte.state
br label %30
19: ; preds = %1
store i32 0, i32* @handle_byte.state
%20 = tail call i32 @write_byte(i32 %0)
%21 = load i32, i32* @handle_byte.crc
%22 = icmp eq i32 %21, %0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %27
25: ; preds = %19
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0), i32 %21, i32 %0)
br label %27
27: ; preds = %25, %23
%28 = tail call i32 @set_state(i32 0)
%29 = tail call i32 @exit(i32 0)
unreachable
30: ; preds = %7, %18, %3, %5, %1
%31 = tail call i32 @set_state(i32 3)
ret void
}