#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 EOF;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  FILE *fp;
  int c;
  int state = 0;
  unsigned int size, len;
  unsigned long offset;
  fp = fopen(argv[1], "r");
  while ((c = fgetc(fp)) != EOF) {
    switch (state) {
    case 0:
      switch (c) {
      case 0:
        printf("END\n");
        fclose(fp);
        return 0;
      case 1:
        printf("ERASE ALL\n");
        break;
      case 2:
        printf("ERASE BLOCK\n");
        state = 2;
        break;
      case 3:
        printf("PROGRAM ");
        state = 1;
        break;
      default:
        printf("Unknown Command!\n");
        return -1;
      }
      break;
    case 1:
      size = c & 0xff;
      state = 3;
      break;
    case 3:
      size |= (unsigned int)((c & 0xff) << 8);
      printf("%6.6d bytes", size);
      state = 4;
      break;
    case 4:
      offset = (unsigned long)(c & 0xff);
      state = 5;
      break;
    case 5:
      offset |= (unsigned long)((c & 0xff) << 8);
      state = 6;
      break;
    case 6:
      offset |= (unsigned long)((c & 0xff) << 16);
      printf(" at offset $%6.6x\n", offset);
      state = 7;
      len = 0;
      break;
    case 7:
      len++;
      if (len == size)
        state = 0;
      break;
    default:
      printf("error!\n");
      return -1;
    }
  }
  fclose(fp);
  return 0;
}


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,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xec4(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e60(%rip),%rcx
cmp    (%rcx),%eax
je     135d <main+0x21d>
mov    -0x20(%rbp),%eax
mov    %rax,-0x38(%rbp)
sub    $0x7,%rax
ja     133e <main+0x1fe>
mov    -0x38(%rbp),%rax
lea    0xe54(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x1c(%rbp),%eax
mov    %rax,-0x40(%rbp)
sub    $0x3,%rax
ja     1244 <main+0x104>
mov    -0x40(%rbp),%rax
lea    0xe4f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    136d <main+0x22d>
lea    0xe33(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    125e <main+0x11e>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x2,-0x20(%rbp)
jmp    125e <main+0x11e>
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x20(%rbp)
jmp    125e <main+0x11e>
lea    0xe0d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136d <main+0x22d>
jmp    1358 <main+0x218>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x24(%rbp)
movl   $0x3,-0x20(%rbp)
jmp    1358 <main+0x218>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
or     -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x4,-0x20(%rbp)
jmp    1358 <main+0x218>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
cltq
mov    %rax,-0x30(%rbp)
movl   $0x5,-0x20(%rbp)
jmp    1358 <main+0x218>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
shl    $0x8,%eax
cltq
or     -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x6,-0x20(%rbp)
jmp    1358 <main+0x218>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
shl    $0x10,%eax
cltq
or     -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xd73(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x7,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
jmp    1358 <main+0x218>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    1339 <main+0x1f9>
movl   $0x0,-0x20(%rbp)
jmp    1358 <main+0x218>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    136d <main+0x22d>
jmp    1175 <main+0x35>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
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
mov    0x8(%rsi),%rdi
lea    0xeca(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fgetc@plt>
mov    0x2e6b(%rip),%rcx
cmp    (%rcx),%eax
jne    119a <main+0x4a>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe5f(%rip),%r13
xor    %r12d,%r12d
jmp    11d3 <main+0x83>
add    $0x1,%ebp
cmp    %r15d,%ebp
mov    $0x7,%r12d
mov    $0x0,%eax
cmove  %eax,%r12d
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    0x2e19(%rip),%rcx
cmp    (%rcx),%eax
je     1181 <main+0x31>
cmp    $0x7,%r12d
ja     12c1 <main+0x171>
mov    %r12d,%ecx
movslq 0x0(%r13,%rcx,4),%rcx
add    %r13,%rcx
jmp    *%rcx
cmp    $0x3,%eax
ja     12ca <main+0x17a>
mov    %eax,%eax
lea    0xe24(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
xor    %r12d,%r12d
jmp    11c0 <main+0x70>
movzbl %al,%r15d
mov    $0x3,%r12d
jmp    11c0 <main+0x70>
shl    $0x8,%eax
movzwl %ax,%eax
or     %eax,%r15d
lea    0xe09(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%r12d
jmp    11c0 <main+0x70>
movzbl %al,%r14d
mov    $0x5,%r12d
jmp    11c0 <main+0x70>
shl    $0x8,%eax
movzwl %ax,%eax
or     %rax,%r14
mov    $0x6,%r12d
jmp    11c0 <main+0x70>
movzbl %al,%eax
shl    $0x10,%eax
or     %rax,%r14
xor    %ebp,%ebp
lea    0xdcb(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x7,%r12d
jmp    11c0 <main+0x70>
lea    0xdc2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%r12d
jmp    11c0 <main+0x70>
lea    0xd83(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r12d
jmp    11c0 <main+0x70>
lea    0xdbd(%rip),%rdi
jmp    12d1 <main+0x181>
lea    0xda3(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    118b <main+0x3b>
lea    0xd89(%rip),%rdi
call   1030 <puts@plt>
jmp    1181 <main+0x31>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.1 = [5 x i8] c"END\0A\00"
@.str.2 = [11 x i8] c"ERASE ALL\0A\00"
@.str.3 = [13 x i8] c"ERASE BLOCK\0A\00"
@.str.4 = [9 x i8] c"PROGRAM \00"
@.str.5 = [18 x i8] c"Unknown Command!\0A\00"
@.str.6 = [12 x i8] c"%6.6d bytes\00"
@.str.7 = [19 x i8] c" at offset $%6.6x\0A\00"
@.str.8 = [8 x i8] c"error!\0A\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 i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %6
br label %16
16: ; preds = %79, %2
%17 = load i32*, i32** %6
%18 = call i32 @fgetc(i32* %17)
store i32 %18, i32* %7
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %18, %19
br i1 %20, label %21, label %80
21: ; preds = %16
%22 = load i32, i32* %8
switch i32 %22, label %77 [
i32 0, label %23
i32 1, label %38
i32 3, label %41
i32 4, label %49
i32 5, label %53
i32 6, label %60
i32 7, label %69
]
23: ; preds = %21
%24 = load i32, i32* %7
switch i32 %24, label %35 [
i32 0, label %25
i32 1, label %29
i32 2, label %31
i32 3, label %33
]
25: ; preds = %23
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32*, i32** %6
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %3
br label %83
29: ; preds = %23
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
br label %37
31: ; preds = %23
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 2, i32* %8
br label %37
33: ; preds = %23
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %8
br label %37
35: ; preds = %23
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
37: ; preds = %33, %31, %29
br label %79
38: ; preds = %21
%39 = load i32, i32* %7
%40 = and i32 %39, 255
store i32 %40, i32* %9
store i32 3, i32* %8
br label %79
41: ; preds = %21
%42 = load i32, i32* %7
%43 = and i32 %42, 255
%44 = shl i32 %43, 8
%45 = load i32, i32* %9
%46 = or i32 %45, %44
store i32 %46, i32* %9
%47 = load i32, i32* %9
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %47)
store i32 4, i32* %8
br label %79
49: ; preds = %21
%50 = load i32, i32* %7
%51 = and i32 %50, 255
%52 = sext i32 %51 to i64
store i64 %52, i64* %11
store i32 5, i32* %8
br label %79
53: ; preds = %21
%54 = load i32, i32* %7
%55 = and i32 %54, 255
%56 = shl i32 %55, 8
%57 = sext i32 %56 to i64
%58 = load i64, i64* %11
%59 = or i64 %58, %57
store i64 %59, i64* %11
store i32 6, i32* %8
br label %79
60: ; preds = %21
%61 = load i32, i32* %7
%62 = and i32 %61, 255
%63 = shl i32 %62, 16
%64 = sext i32 %63 to i64
%65 = load i64, i64* %11
%66 = or i64 %65, %64
store i64 %66, i64* %11
%67 = load i64, i64* %11
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i64 %67)
store i32 7, i32* %8
store i32 0, i32* %10
br label %79
69: ; preds = %21
%70 = load i32, i32* %10
%71 = add i32 %70, 1
store i32 %71, i32* %10
%72 = load i32, i32* %10
%73 = load i32, i32* %9
%74 = icmp eq i32 %72, %73
br i1 %74, label %75, label %76
75: ; preds = %69
store i32 0, i32* %8
br label %76
76: ; preds = %75, %69
br label %79
77: ; preds = %21
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
store i32 -1, i32* %3
br label %83
79: ; preds = %76, %60, %53, %49, %41, %38, %37
br label %16
80: ; preds = %16
%81 = load i32*, i32** %6
%82 = call i32 @fclose(i32* %81)
store i32 0, i32* %3
br label %83
83: ; preds = %80, %77, %35, %25
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.4 = [9 x i8] c"PROGRAM \00"
@.str.6 = [12 x i8] c"%6.6d bytes\00"
@.str.7 = [19 x i8] c" at offset $%6.6x\0A\00"
@str = [12 x i8] c"ERASE BLOCK\00"
@str.9 = [10 x i8] c"ERASE ALL\00"
@str.10 = [4 x i8] c"END\00"
@str.11 = [17 x i8] c"Unknown Command!\00"
@str.12 = [7 x i8] c"error!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @fgetc(i32* %5)
%7 = load i32, i32* @EOF
%8 = icmp eq i32 %6, %7
br i1 %8, label %62, label %9
9: ; preds = %2, %54
%10 = phi i32 [ %59, %54 ], [ %6, %2 ]
%11 = phi i64 [ %58, %54 ], [ undef, %2 ]
%12 = phi i32 [ %57, %54 ], [ undef, %2 ]
%13 = phi i32 [ %56, %54 ], [ undef, %2 ]
%14 = phi i32 [ %55, %54 ], [ 0, %2 ]
switch i32 %14, label %52 [
i32 0, label %15
i32 1, label %27
i32 3, label %29
i32 4, label %34
i32 5, label %37
i32 6, label %42
i32 7, label %48
]
15: ; preds = %9
switch i32 %10, label %25 [
i32 0, label %16
i32 1, label %19
i32 2, label %21
i32 3, label %23
]
16: ; preds = %15
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str.10, i64 0, i64 0))
%18 = tail call i32 @fclose(i32* %5)
br label %64
19: ; preds = %15
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.9, i64 0, i64 0))
br label %54
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %54
23: ; preds = %15
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
br label %54
25: ; preds = %15
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.11, i64 0, i64 0))
br label %64
27: ; preds = %9
%28 = and i32 %10, 255
br label %54
29: ; preds = %9
%30 = shl i32 %10, 8
%31 = and i32 %30, 65280
%32 = or i32 %31, %13
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %32)
br label %54
34: ; preds = %9
%35 = and i32 %10, 255
%36 = zext i32 %35 to i64
br label %54
37: ; preds = %9
%38 = shl i32 %10, 8
%39 = and i32 %38, 65280
%40 = zext i32 %39 to i64
%41 = or i64 %11, %40
br label %54
42: ; preds = %9
%43 = shl i32 %10, 16
%44 = and i32 %43, 16711680
%45 = zext i32 %44 to i64
%46 = or i64 %11, %45
%47 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i64 %46)
br label %54
48: ; preds = %9
%49 = add i32 %12, 1
%50 = icmp eq i32 %49, %13
%51 = select i1 %50, i32 0, i32 7
br label %54
52: ; preds = %9
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.12, i64 0, i64 0))
br label %64
54: ; preds = %48, %19, %21, %23, %42, %37, %34, %29, %27
%55 = phi i32 [ 7, %42 ], [ 6, %37 ], [ 5, %34 ], [ 4, %29 ], [ 3, %27 ], [ 1, %23 ], [ 2, %21 ], [ 0, %19 ], [ %51, %48 ]
%56 = phi i32 [ %13, %42 ], [ %13, %37 ], [ %13, %34 ], [ %32, %29 ], [ %28, %27 ], [ %13, %23 ], [ %13, %21 ], [ %13, %19 ], [ %13, %48 ]
%57 = phi i32 [ 0, %42 ], [ %12, %37 ], [ %12, %34 ], [ %12, %29 ], [ %12, %27 ], [ %12, %23 ], [ %12, %21 ], [ %12, %19 ], [ %49, %48 ]
%58 = phi i64 [ %46, %42 ], [ %41, %37 ], [ %36, %34 ], [ %11, %29 ], [ %11, %27 ], [ %11, %23 ], [ %11, %21 ], [ %11, %19 ], [ %11, %48 ]
%59 = tail call i32 @fgetc(i32* %5)
%60 = load i32, i32* @EOF
%61 = icmp eq i32 %59, %60
br i1 %61, label %62, label %9
62: ; preds = %54, %2
%63 = tail call i32 @fclose(i32* %5)
br label %64
64: ; preds = %62, %52, %25, %16
%65 = phi i32 [ -1, %52 ], [ -1, %25 ], [ 0, %16 ], [ 0, %62 ]
ret i32 %65
}