#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*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, ...);
int serialReadEscape(int, unsigned char *);
unsigned char expectPacketType(int fd, unsigned char _byte) {
  unsigned char b;
  int size;
  printf("{Packettype ");
  size = serialReadEscape(fd, &b);
  switch (b) {
  case 0x00:
    printf("VERSION}");
    break;
  case 0x01:
    printf("READ Flash}");
    break;
  case 0x02:
    printf("WRITE Flash}");
    break;
  case 0x03:
    printf("ERASE Flash}");
    break;
  case 0x04:
    printf("READ EE}");
    break;
  case 0x05:
    printf("WRITE EE}");
    break;
  case 0x06:
    printf("READ CONFIG}");
    break;
  case 0x07:
    printf("WRITE CONFIG}");
    break;
  default:
    printf("UNKNOWN}");
    break;
  }
  if (b != _byte) {
    printf("\n[ERROR, expected PacketType 0x%02x, but got 0x%02x]\n", b, _byte);
    close(fd);
    exit(-1);
  }
  return size;
}


x86 assembly (clang, -O0):
<expectPacketType>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
lea    0xec8(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x6(%rbp),%rsi
call   1050 <serialReadEscape@plt>
mov    %eax,-0xc(%rbp)
movzbl -0x6(%rbp),%eax
mov    %rax,-0x18(%rbp)
sub    $0x7,%rax
ja     122c <expectPacketType+0xec>
mov    -0x18(%rbp),%rax
lea    0xe75(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe88(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe81(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe75(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    123a <expectPacketType+0xfa>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movzbl -0x6(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
je     1272 <expectPacketType+0x132>
movzbl -0x6(%rbp),%esi
movzbl -0x5(%rbp),%edx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
movzbl %al,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expectPacketType>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebp
mov    %edi,%r14d
lea    0xeac(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xf(%rsp),%rsi
mov    %r14d,%edi
call   1050 <serialReadEscape@plt>
mov    %eax,%ebx
movsbq 0xf(%rsp),%rax
cmp    $0x7,%rax
ja     1186 <expectPacketType+0x46>
lea    0xf2f(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    118d <expectPacketType+0x4d>
lea    0xedd(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movzbl 0xf(%rsp),%esi
cmp    %bpl,%sil
jne    11a9 <expectPacketType+0x69>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
movzbl %bpl,%edx
lea    0xebf(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r14d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [13 x i8] c"{Packettype \00"
@.str.1 = [9 x i8] c"VERSION}\00"
@.str.2 = [12 x i8] c"READ Flash}\00"
@.str.3 = [13 x i8] c"WRITE Flash}\00"
@.str.4 = [13 x i8] c"ERASE Flash}\00"
@.str.5 = [9 x i8] c"READ EE}\00"
@.str.6 = [10 x i8] c"WRITE EE}\00"
@.str.7 = [13 x i8] c"READ CONFIG}\00"
@.str.8 = [14 x i8] c"WRITE CONFIG}\00"
@.str.9 = [9 x i8] c"UNKNOWN}\00"
@.str.10 = [54 x i8] c"\0A[ERROR, expected PacketType 0x%02x, but got 0x%02x]\0A\00"
define zeroext i8 @expectPacketType(i32 %0, i8 zeroext %1) {
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
store i32 %0, i32* %3
store i8 %1, i8* %4
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* %3
%9 = call i32 @serialReadEscape(i32 %8, i8* %5)
store i32 %9, i32* %6
%10 = load i8, i8* %5
%11 = zext i8 %10 to i32
switch i32 %11, label %28 [
i32 0, label %12
i32 1, label %14
i32 2, label %16
i32 3, label %18
i32 4, label %20
i32 5, label %22
i32 6, label %24
i32 7, label %26
]
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %30
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %30
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %30
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
br label %30
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %30
22: ; preds = %2
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0))
br label %30
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %30
26: ; preds = %2
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.8, i64 0, i64 0))
br label %30
28: ; preds = %2
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
br label %30
30: ; preds = %28, %26, %24, %22, %20, %18, %16, %14, %12
%31 = load i8, i8* %5
%32 = zext i8 %31 to i32
%33 = load i8, i8* %4
%34 = zext i8 %33 to i32
%35 = icmp ne i32 %32, %34
br i1 %35, label %36, label %45
36: ; preds = %30
%37 = load i8, i8* %5
%38 = zext i8 %37 to i32
%39 = load i8, i8* %4
%40 = zext i8 %39 to i32
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.10, i64 0, i64 0), i32 %38, i32 %40)
%42 = load i32, i32* %3
%43 = call i32 @close(i32 %42)
%44 = call i32 @exit(i32 -1)
unreachable
45: ; preds = %30
%46 = load i32, i32* %6
%47 = trunc i32 %46 to i8
ret i8 %47
}

LLVM IR (-O2):
@.str = [13 x i8] c"{Packettype \00"
@.str.1 = [9 x i8] c"VERSION}\00"
@.str.2 = [12 x i8] c"READ Flash}\00"
@.str.3 = [13 x i8] c"WRITE Flash}\00"
@.str.4 = [13 x i8] c"ERASE Flash}\00"
@.str.5 = [9 x i8] c"READ EE}\00"
@.str.6 = [10 x i8] c"WRITE EE}\00"
@.str.7 = [13 x i8] c"READ CONFIG}\00"
@.str.8 = [14 x i8] c"WRITE CONFIG}\00"
@.str.9 = [9 x i8] c"UNKNOWN}\00"
@.str.10 = [54 x i8] c"\0A[ERROR, expected PacketType 0x%02x, but got 0x%02x]\0A\00"
@reltable.expectPacketType = [8 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.1 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([12 x i8]* @.str.2 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([13 x i8]* @.str.3 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([13 x i8]* @.str.4 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([9 x i8]* @.str.5 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([10 x i8]* @.str.6 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([13 x i8]* @.str.7 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([14 x i8]* @.str.8 to i64), i64 ptrtoint ([8 x i32]* @reltable.expectPacketType to i64)) to i32)]
define zeroext i8 @expectPacketType(i32 %0, i8 zeroext %1) {
%3 = alloca i8
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%5 = call i32 @serialReadEscape(i32 %0, i8* %3)
%6 = load i8, i8* %3
%7 = icmp ult i8 %6, 8
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = sext i8 %6 to i64
%10 = shl i64 %9, 2
%11 = call i8* @llvm.load.relative.i64(i8* bitcast ([8 x i32]* @reltable.expectPacketType to i8*), i64 %10)
br label %12
12: ; preds = %2, %8
%13 = phi i8* [ %11, %8 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), %2 ]
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %13)
%15 = load i8, i8* %3
%16 = icmp eq i8 %15, %1
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = zext i8 %1 to i32
%19 = zext i8 %15 to i32
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.10, i64 0, i64 0), i32 %19, i32 %18)
%21 = call i32 @close(i32 %0)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %12
%24 = trunc i32 %5 to i8
ret i8 %24
}