#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 */
#define EOF 128
int getchar();
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ sscanf(char *, char *, int *);
main(int argc, char **argv) {
  int cc;
  int cksum_orig = 0;
  int cksum = '$';
  int state = 0;
  char buf[3];
  int option_r = 0;
  int option_m = 1;
  while (state < 6) {
    cc = getchar();
    switch (cc) {
    case '$':
      state = 1;
      break;
    case '*':
      if (state == 1)
        state = 2;
      break;
    case '\r':
      if (state == 5)
        state = 0;
      break;
    case '\n':
      if (state == 5)
        state = 0;
      break;
    case EOF:
      state = 6;
      break;
    }
    switch (state) {
    case 0:
      putchar(cc);
      cksum = '$';
      break;
    case 1:
      putchar(cc);
      cksum ^= cc;
      break;
    case 2:
      putchar(cc);
      state++;
      break;
    case 3:
      state++;
      buf[0] = cc;
      break;
    case 4:
      state++;
      buf[1] = cc;
      buf[2] = 0;
      sscanf(buf, "%2X", &cksum_orig);
      if (option_r) {
        printf("%02X", cksum);
      } else {
        printf("%02X", cksum_orig);
      }
      if (option_m && (cksum_orig != cksum))
        printf(" # wrong:%02X # right:%02X # ", cksum_orig, cksum);
      break;
    case 5:
      printf("%c # unexpected # ", cc);
      state = 0;
      break;
    case 6:
      putchar(cc);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x24,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
cmpl   $0x6,-0x20(%rbp)
jge    1366 <main+0x226>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xa,%eax
je     121f <main+0xdf>
jmp    11a1 <main+0x61>
mov    -0x30(%rbp),%eax
sub    $0xd,%eax
je     1209 <main+0xc9>
jmp    11b2 <main+0x72>
mov    -0x30(%rbp),%eax
sub    $0x24,%eax
je     11e7 <main+0xa7>
jmp    11c3 <main+0x83>
mov    -0x30(%rbp),%eax
sub    $0x2a,%eax
je     11f3 <main+0xb3>
jmp    11d4 <main+0x94>
mov    -0x30(%rbp),%eax
sub    $0x80,%eax
je     1235 <main+0xf5>
jmp    123c <main+0xfc>
movl   $0x1,-0x20(%rbp)
jmp    123c <main+0xfc>
cmpl   $0x1,-0x20(%rbp)
jne    1204 <main+0xc4>
movl   $0x2,-0x20(%rbp)
jmp    123c <main+0xfc>
cmpl   $0x5,-0x20(%rbp)
jne    121a <main+0xda>
movl   $0x0,-0x20(%rbp)
jmp    123c <main+0xfc>
cmpl   $0x5,-0x20(%rbp)
jne    1230 <main+0xf0>
movl   $0x0,-0x20(%rbp)
jmp    123c <main+0xfc>
movl   $0x6,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0x6,%rax
ja     1361 <main+0x221>
mov    -0x38(%rbp),%rax
lea    0xda8(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
movl   $0x24,-0x1c(%rbp)
jmp    1361 <main+0x221>
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x14(%rbp),%eax
xor    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1361 <main+0x221>
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1361 <main+0x221>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,-0x23(%rbp)
jmp    1361 <main+0x221>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %al,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
lea    -0x23(%rbp),%rdi
lea    0xd49(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1060 <sscanf@plt>
cmpl   $0x0,-0x28(%rbp)
je     12fc <main+0x1bc>
mov    -0x1c(%rbp),%esi
lea    0xd30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    130d <main+0x1cd>
mov    -0x18(%rbp),%esi
lea    0xd1a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0x2c(%rbp)
je     1337 <main+0x1f7>
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     1337 <main+0x1f7>
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xcf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1361 <main+0x221>
mov    -0x14(%rbp),%esi
lea    0xcfd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x20(%rbp)
jmp    1361 <main+0x221>
mov    -0x14(%rbp),%edi
call   1030 <putchar@plt>
jmp    1179 <main+0x39>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movl   $0x0,0x4(%rsp)
mov    $0x24,%ebp
lea    0xea1(%rip),%r15
lea    0xf36(%rip),%rbx
lea    0xf47(%rip),%r14
xor    %r12d,%r12d
jmp    119b <main+0x5b>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r13d,%edi
call   1030 <putchar@plt>
xor    %r13d,%ebp
mov    $0x1,%r12d
cmp    $0x6,%r12d
jge    12ce <main+0x18e>
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%r13d
lea    -0xa(%r13),%eax
cmp    $0x20,%eax
ja     11f4 <main+0xb4>
movslq (%r15,%rax,4),%rax
add    %r15,%rax
jmp    *%rax
cmp    $0x5,%r12d
ja     1191 <main+0x51>
mov    %r12d,%eax
lea    0xebd(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
cmp    $0x5,%r12d
ja     1191 <main+0x51>
mov    %r12d,%eax
movslq (%rbx,%rax,4),%rax
add    %rbx,%rax
jmp    *%rax
cmp    $0x5,%r12d
ja     1191 <main+0x51>
mov    %r12d,%eax
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
cmp    $0x80,%r13d
je     12c4 <main+0x184>
cmp    $0x5,%r12d
ja     1191 <main+0x51>
mov    %r12d,%eax
lea    0xebb(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %r13d,%edi
call   1030 <putchar@plt>
mov    $0x24,%ebp
xor    %r12d,%r12d
jmp    1191 <main+0x51>
mov    %r13d,%edi
call   1030 <putchar@plt>
mov    $0x3,%r12d
jmp    1191 <main+0x51>
mov    %r13b,0x1(%rsp)
mov    $0x4,%r12d
jmp    1191 <main+0x51>
mov    %r13b,0x2(%rsp)
movb   $0x0,0x3(%rsp)
lea    0x1(%rsp),%rdi
lea    0xe7c(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1060 <sscanf@plt>
mov    0x4(%rsp),%esi
lea    0xe6b(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x4(%rsp),%esi
mov    $0x5,%r12d
cmp    %ebp,%esi
je     1191 <main+0x51>
lea    0xe50(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1191 <main+0x51>
xor    %r12d,%r12d
lea    0xe56(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1191 <main+0x51>
mov    $0x80,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%2X\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [30 x i8] c" # wrong:%02X # right:%02X # \00"
@.str.3 = [19 x i8] c"%c # unexpected # \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 [3 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %7
store i32 36, i32* %8
store i32 0, i32* %9
store i32 0, i32* %11
store i32 1, i32* %12
br label %13
13: ; preds = %93, %2
%14 = load i32, i32* %9
%15 = icmp slt i32 %14, 6
br i1 %15, label %16, label %94
16: ; preds = %13
%17 = call i32 (...) @getchar()
store i32 %17, i32* %6
%18 = load i32, i32* %6
switch i32 %18, label %36 [
i32 36, label %19
i32 42, label %20
i32 13, label %25
i32 10, label %30
i32 128, label %35
]
19: ; preds = %16
store i32 1, i32* %9
br label %36
20: ; preds = %16
%21 = load i32, i32* %9
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 2, i32* %9
br label %24
24: ; preds = %23, %20
br label %36
25: ; preds = %16
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, 5
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 0, i32* %9
br label %29
29: ; preds = %28, %25
br label %36
30: ; preds = %16
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 5
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 0, i32* %9
br label %34
34: ; preds = %33, %30
br label %36
35: ; preds = %16
store i32 6, i32* %9
br label %36
36: ; preds = %16, %35, %34, %29, %24, %19
%37 = load i32, i32* %9
switch i32 %37, label %93 [
i32 0, label %38
i32 1, label %41
i32 2, label %47
i32 3, label %52
i32 4, label %58
i32 5, label %87
i32 6, label %90
]
38: ; preds = %36
%39 = load i32, i32* %6
%40 = call i32 @putchar(i32 %39)
store i32 36, i32* %8
br label %93
41: ; preds = %36
%42 = load i32, i32* %6
%43 = call i32 @putchar(i32 %42)
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = xor i32 %45, %44
store i32 %46, i32* %8
br label %93
47: ; preds = %36
%48 = load i32, i32* %6
%49 = call i32 @putchar(i32 %48)
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %93
52: ; preds = %36
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
store i32 %54, i32* %9
%55 = load i32, i32* %6
%56 = trunc i32 %55 to i8
%57 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
store i8 %56, i8* %57
br label %93
58: ; preds = %36
%59 = load i32, i32* %9
%60 = add nsw i32 %59, 1
store i32 %60, i32* %9
%61 = load i32, i32* %6
%62 = trunc i32 %61 to i8
%63 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 1
store i8 %62, i8* %63
%64 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 2
store i8 0, i8* %64
%65 = getelementptr inbounds [3 x i8], [3 x i8]* %10, i64 0, i64 0
%66 = call i32 @sscanf(i8* %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %7)
%67 = load i32, i32* %11
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %58
%70 = load i32, i32* %8
%71 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %70)
br label %75
72: ; preds = %58
%73 = load i32, i32* %7
%74 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %72, %69
%76 = load i32, i32* %12
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %86
78: ; preds = %75
%79 = load i32, i32* %7
%80 = load i32, i32* %8
%81 = icmp ne i32 %79, %80
br i1 %81, label %82, label %86
82: ; preds = %78
%83 = load i32, i32* %7
%84 = load i32, i32* %8
%85 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %83, i32 %84)
br label %86
86: ; preds = %82, %78, %75
br label %93
87: ; preds = %36
%88 = load i32, i32* %6
%89 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %88)
store i32 0, i32* %9
br label %93
90: ; preds = %36
%91 = load i32, i32* %6
%92 = call i32 @putchar(i32 %91)
br label %93
93: ; preds = %36, %90, %87, %86, %52, %47, %41, %38
br label %13
94: ; preds = %13
%95 = load i32, i32* %3
ret i32 %95
}

LLVM IR (-O2):
@.str = [4 x i8] c"%2X\00"
@.str.1 = [5 x i8] c"%02X\00"
@.str.2 = [30 x i8] c" # wrong:%02X # right:%02X # \00"
@.str.3 = [19 x i8] c"%c # unexpected # \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [3 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
%8 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
br label %9
9: ; preds = %2, %39
%10 = phi i32 [ 36, %2 ], [ %41, %39 ]
%11 = phi i32 [ 0, %2 ], [ %40, %39 ]
%12 = call i32 (...) @getchar()
switch i32 %12, label %16 [
i32 36, label %19
i32 42, label %13
i32 13, label %14
i32 10, label %15
i32 128, label %37
]
13: ; preds = %9
switch i32 %11, label %39 [
i32 1, label %22
i32 0, label %17
i32 2, label %22
i32 3, label %24
i32 4, label %26
i32 5, label %35
]
14: ; preds = %9
switch i32 %11, label %39 [
i32 5, label %17
i32 0, label %17
i32 1, label %19
i32 2, label %22
i32 3, label %24
i32 4, label %26
]
15: ; preds = %9
switch i32 %11, label %39 [
i32 5, label %17
i32 0, label %17
i32 1, label %19
i32 2, label %22
i32 3, label %24
i32 4, label %26
]
16: ; preds = %9
switch i32 %11, label %39 [
i32 0, label %17
i32 1, label %19
i32 2, label %22
i32 3, label %24
i32 4, label %26
i32 5, label %35
]
17: ; preds = %13, %14, %14, %15, %15, %16
%18 = call i32 @putchar(i32 %12)
br label %39
19: ; preds = %14, %15, %9, %16
%20 = call i32 @putchar(i32 %12)
%21 = xor i32 %12, %10
br label %39
22: ; preds = %13, %13, %14, %15, %16
%23 = call i32 @putchar(i32 %12)
br label %39
24: ; preds = %13, %14, %15, %16
%25 = trunc i32 %12 to i8
store i8 %25, i8* %6
br label %39
26: ; preds = %13, %14, %15, %16
%27 = trunc i32 %12 to i8
store i8 %27, i8* %7
store i8 0, i8* %8
%28 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %3)
%29 = load i32, i32* %3
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %3
%32 = icmp eq i32 %31, %10
br i1 %32, label %39, label %33
33: ; preds = %26
%34 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %10)
br label %39
35: ; preds = %13, %16
%36 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %12)
br label %39
37: ; preds = %9
%38 = call i32 @putchar(i32 128)
br label %43
39: ; preds = %13, %14, %15, %26, %33, %16, %35, %24, %22, %19, %17
%40 = phi i32 [ %11, %16 ], [ 0, %35 ], [ 5, %33 ], [ 5, %26 ], [ 4, %24 ], [ 3, %22 ], [ 1, %19 ], [ 0, %17 ], [ %11, %15 ], [ %11, %14 ], [ %11, %13 ]
%41 = phi i32 [ %10, %16 ], [ %10, %35 ], [ %10, %33 ], [ %10, %26 ], [ %10, %24 ], [ %10, %22 ], [ %21, %19 ], [ 36, %17 ], [ %10, %15 ], [ %10, %14 ], [ %10, %13 ]
%42 = icmp slt i32 %40, 6
br i1 %42, label %9, label %43
43: ; preds = %39, %37
ret i32 0
}