#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 */
short HandleInstruction(char, char, char, char *, short);
char *ReadBinary(char *, int *);
int /*<<< orphan*/ free(char *);
int getchar();
scalar_t__ kbhit();
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ set_conio_terminal_mode();
int /*<<< orphan*/ updateButtons(int, char *);
int /*<<< orphan*/ updateDisplay(char *);
int /*<<< orphan*/ usleep(int);
int main(int argc, char **argv) {
  char *binaryFile;
  int bufferSize, c;
  char *registers = (char *)malloc(sizeof(char) * 27);
  if (!registers) {
    printf("malloc failed!\n");
    return -1;
  }
  if (argc != 2) {
    printf("Usage: %s <binary file.bin>\n", argv[0]);
    return -1;
  }
  binaryFile = ReadBinary(argv[1], &bufferSize);
  if (!binaryFile) {
    free(registers);
    registers = NULL;
    return -1;
  }
  set_conio_terminal_mode();
  printf("\x1B[?25l");
  printf("\x1B[2J");
  for (short i = 0; i < bufferSize; i = i + 3) {
    i = HandleInstruction(binaryFile[i], binaryFile[i + 1], binaryFile[i + 2],
                          registers, i);
    if (kbhit()) {
      c = getchar();
      if (c == 113)
        break;
      updateButtons(c, registers);
    }
    updateDisplay(registers);
    usleep(20);
  }
  free(registers);
  registers = NULL;
  free(binaryFile);
  binaryFile = NULL;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x1b,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f9 <main+0x49>
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <main+0x1b4>
cmpl   $0x2,-0x8(%rbp)
je     1224 <main+0x74>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <main+0x1b4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x1c(%rbp),%rsi
call   10a0 <ReadBinary@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1261 <main+0xb1>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    1364 <main+0x1b4>
mov    $0x0,%al
call   1040 <set_conio_terminal_mode@plt>
lea    0xdbe(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xdb7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movw   $0x0,-0x2a(%rbp)
movswl -0x2a(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    133b <main+0x18b>
mov    -0x18(%rbp),%rax
movswq -0x2a(%rbp),%rcx
mov    (%rax,%rcx,1),%sil
mov    -0x18(%rbp),%rax
movswl -0x2a(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x18(%rbp),%rax
movswl -0x2a(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x28(%rbp),%rcx
movsbl %sil,%edi
movsbl %dl,%esi
movsbl %al,%edx
movswl -0x2a(%rbp),%r8d
call   10c0 <HandleInstruction@plt>
mov    %ax,-0x2a(%rbp)
mov    $0x0,%al
call   1070 <kbhit@plt>
cmp    $0x0,%rax
je     1318 <main+0x168>
mov    $0x0,%al
call   1080 <getchar@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x71,-0x20(%rbp)
jne    130c <main+0x15c>
jmp    133b <main+0x18b>
mov    -0x20(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <updateButtons@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <updateDisplay@plt>
mov    $0x14,%edi
call   10d0 <usleep@plt>
movswl -0x2a(%rbp),%eax
add    $0x3,%eax
mov    %ax,-0x2a(%rbp)
jmp    128a <main+0xda>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1b,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12b5 <main+0xf5>
cmp    $0x2,%ebp
jne    12c3 <main+0x103>
mov    %rax,%r15
mov    0x8(%rbx),%rdi
lea    0x4(%rsp),%rsi
call   10b0 <ReadBinary@plt>
test   %rax,%rax
je     12d6 <main+0x116>
mov    %rax,%rbp
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <set_conio_terminal_mode@plt>
lea    0xe07(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
cmpl   $0x0,0x4(%rsp)
jle    12a3 <main+0xe3>
xor    %eax,%eax
xor    %ebx,%ebx
jmp    1268 <main+0xa8>
nopl   0x0(%rax,%rax,1)
mov    %eax,%edi
mov    %r15,%rsi
call   1060 <updateButtons@plt>
mov    %r15,%rdi
call   10c0 <updateDisplay@plt>
mov    $0x14,%edi
call   10e0 <usleep@plt>
add    $0x3,%ebx
movswl %bx,%eax
cmp    %eax,0x4(%rsp)
jle    12a3 <main+0xe3>
movswq %bx,%rcx
movsbl 0x0(%rbp,%rcx,1),%edi
cltq
movsbl 0x1(%rax,%rbp,1),%esi
movsbl 0x2(%rax,%rbp,1),%edx
movswl %cx,%r8d
mov    %r15,%rcx
call   10d0 <HandleInstruction@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <kbhit@plt>
test   %rax,%rax
je     124a <main+0x8a>
xor    %eax,%eax
call   1090 <getchar@plt>
cmp    $0x71,%eax
jne    1240 <main+0x80>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    12e4 <main+0x124>
lea    0xd6d(%rip),%rdi
call   1050 <puts@plt>
jmp    12de <main+0x11e>
mov    (%rbx),%rsi
lea    0xd33(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12de <main+0x11e>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"malloc failed!\0A\00"
@.str.1 = [29 x i8] c"Usage: %s <binary file.bin>\0A\00"
@.str.2 = [7 x i8] c"\1B[?25l\00"
@.str.3 = [5 x i8] c"\1B[2J\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i16
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i64 @malloc(i32 27)
%12 = inttoptr i64 %11 to i8*
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %92
17: ; preds = %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 2
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 -1, i32* %3
br label %92
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = call i8* @ReadBinary(i8* %28, i32* %7)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %35, label %32
32: ; preds = %25
%33 = load i8*, i8** %9
%34 = call i32 @free(i8* %33)
store i8* null, i8** %9
store i32 -1, i32* %3
br label %92
35: ; preds = %25
%36 = call i32 (...) @set_conio_terminal_mode()
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
store i16 0, i16* %10
br label %39
39: ; preds = %82, %35
%40 = load i16, i16* %10
%41 = sext i16 %40 to i32
%42 = load i32, i32* %7
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %87
44: ; preds = %39
%45 = load i8*, i8** %6
%46 = load i16, i16* %10
%47 = sext i16 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
%49 = load i8, i8* %48
%50 = load i8*, i8** %6
%51 = load i16, i16* %10
%52 = sext i16 %51 to i32
%53 = add nsw i32 %52, 1
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %50, i64 %54
%56 = load i8, i8* %55
%57 = load i8*, i8** %6
%58 = load i16, i16* %10
%59 = sext i16 %58 to i32
%60 = add nsw i32 %59, 2
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %57, i64 %61
%63 = load i8, i8* %62
%64 = load i8*, i8** %9
%65 = load i16, i16* %10
%66 = call signext i16 @HandleInstruction(i8 signext %49, i8 signext %56, i8 signext %63, i8* %64, i16 signext %65)
store i16 %66, i16* %10
%67 = call i64 (...) @kbhit()
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %44
%70 = call i32 (...) @getchar()
store i32 %70, i32* %8
%71 = load i32, i32* %8
%72 = icmp eq i32 %71, 113
br i1 %72, label %73, label %74
73: ; preds = %69
br label %87
74: ; preds = %69
%75 = load i32, i32* %8
%76 = load i8*, i8** %9
%77 = call i32 @updateButtons(i32 %75, i8* %76)
br label %78
78: ; preds = %74, %44
%79 = load i8*, i8** %9
%80 = call i32 @updateDisplay(i8* %79)
%81 = call i32 @usleep(i32 20)
br label %82
82: ; preds = %78
%83 = load i16, i16* %10
%84 = sext i16 %83 to i32
%85 = add nsw i32 %84, 3
%86 = trunc i32 %85 to i16
store i16 %86, i16* %10
br label %39
87: ; preds = %73, %39
%88 = load i8*, i8** %9
%89 = call i32 @free(i8* %88)
store i8* null, i8** %9
%90 = load i8*, i8** %6
%91 = call i32 @free(i8* %90)
store i8* null, i8** %6
store i32 0, i32* %3
br label %92
92: ; preds = %87, %32, %20, %15
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@.str.1 = [29 x i8] c"Usage: %s <binary file.bin>\0A\00"
@.str.2 = [7 x i8] c"\1B[?25l\00"
@.str.3 = [5 x i8] c"\1B[2J\00"
@str = [15 x i8] c"malloc failed!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i64 @malloc(i32 27)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
br label %60
10: ; preds = %2
%11 = icmp eq i32 %0, 2
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = load i8*, i8** %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %13)
br label %60
15: ; preds = %10
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = call i8* @ReadBinary(i8* %17, i32* %3)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @free(i8* %6)
br label %60
22: ; preds = %15
%23 = call i32 (...) @set_conio_terminal_mode()
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %57
28: ; preds = %22, %50
%29 = phi i32 [ %54, %50 ], [ 0, %22 ]
%30 = phi i16 [ %53, %50 ], [ 0, %22 ]
%31 = sext i16 %30 to i64
%32 = getelementptr inbounds i8, i8* %18, i64 %31
%33 = load i8, i8* %32
%34 = add nsw i32 %29, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %18, i64 %35
%37 = load i8, i8* %36
%38 = add nsw i32 %29, 2
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %18, i64 %39
%41 = load i8, i8* %40
%42 = call signext i16 @HandleInstruction(i8 signext %33, i8 signext %37, i8 signext %41, i8* %6, i16 signext %30)
%43 = call i64 (...) @kbhit()
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %28
%46 = call i32 (...) @getchar()
%47 = icmp eq i32 %46, 113
br i1 %47, label %57, label %48
48: ; preds = %45
%49 = call i32 @updateButtons(i32 %46, i8* %6)
br label %50
50: ; preds = %48, %28
%51 = call i32 @updateDisplay(i8* %6)
%52 = call i32 @usleep(i32 20)
%53 = add i16 %42, 3
%54 = sext i16 %53 to i32
%55 = load i32, i32* %3
%56 = icmp sgt i32 %55, %54
br i1 %56, label %28, label %57
57: ; preds = %50, %45, %22
%58 = call i32 @free(i8* %6)
%59 = call i32 @free(i8* %18)
br label %60
60: ; preds = %57, %20, %12, %8
%61 = phi i32 [ -1, %12 ], [ 0, %57 ], [ -1, %20 ], [ -1, %8 ]
ret i32 %61
}