#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*/ dumpCore();
int /*<<< orphan*/ flipMode();
char getch_screen();
int /*<<< orphan*/ loadBasic();
int /*<<< orphan*/ loadCore();
int /*<<< orphan*/ resetM6502();
int /*<<< orphan*/ resetMemory();
int /*<<< orphan*/ resetPia6820();
int /*<<< orphan*/ resetScreen();
int /*<<< orphan*/ writeKbd(unsigned char);
int /*<<< orphan*/ writeKbdCr(int);
int handleInput(void) {
  char tmp;
  tmp = '\0';
  while ((tmp = getch_screen()) == '\0')
    ;
  if (tmp == 'B') {
    loadBasic();
    resetPia6820();
    resetM6502();
    return 1;
  } else if (tmp == 'L') {
    loadCore();
    resetPia6820();
    resetM6502();
    return 1;
  } else if (tmp == 'D') {
    dumpCore();
    resetPia6820();
    resetM6502();
    return 1;
  } else if (tmp == 'R') {
    resetPia6820();
    resetM6502();
    return 1;
  } else if (tmp == 'H') {
    resetScreen();
    resetPia6820();
    resetMemory();
    resetM6502();
    return 1;
  } else if (tmp == 'Q') {
    return 0;
  } else if (tmp == 'M') {
    flipMode();
    resetPia6820();
    resetM6502();
    return 1;
  } else if (tmp == '\n') {
    tmp = '\r';
  } else if (tmp == '\b') {
    tmp = 0x5f;
  } else if (tmp >= 'a' && tmp <= 'z') {
    tmp = tmp - 'a' + 'A';
  }
  writeKbd((unsigned char)(tmp + 0x80));
  writeKbdCr(0xA7);
  return 1;
}


x86 assembly (clang, -O0):
<handleInput>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movb   $0x0,-0x5(%rbp)
mov    $0x0,%al
call   1060 <getch_screen@plt>
mov    %al,-0x5(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
jne    11d7 <handleInput+0x27>
jmp    11bc <handleInput+0xc>
movsbl -0x5(%rbp),%eax
cmp    $0x42,%eax
jne    1205 <handleInput+0x55>
mov    $0x0,%al
call   1070 <loadBasic@plt>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x4c,%eax
jne    1233 <handleInput+0x83>
mov    $0x0,%al
call   1030 <loadCore@plt>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x44,%eax
jne    1261 <handleInput+0xb1>
mov    $0x0,%al
call   10c0 <dumpCore@plt>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x52,%eax
jne    1288 <handleInput+0xd8>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x48,%eax
jne    12bd <handleInput+0x10d>
mov    $0x0,%al
call   10b0 <resetScreen@plt>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10a0 <resetMemory@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x51,%eax
jne    12d6 <handleInput+0x126>
movl   $0x0,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0x4d,%eax
jne    1304 <handleInput+0x154>
mov    $0x0,%al
call   1050 <flipMode@plt>
mov    $0x0,%al
call   1090 <resetPia6820@plt>
mov    $0x0,%al
call   10d0 <resetM6502@plt>
movl   $0x1,-0x4(%rbp)
jmp    13a6 <handleInput+0x1f6>
movsbl -0x5(%rbp),%eax
cmp    $0xa,%eax
jne    131a <handleInput+0x16a>
movb   $0xd,-0x5(%rbp)
jmp    1361 <handleInput+0x1b1>
movsbl -0x5(%rbp),%eax
cmp    $0x8,%eax
jne    1330 <handleInput+0x180>
movb   $0x5f,-0x5(%rbp)
jmp    135c <handleInput+0x1ac>
movsbl -0x5(%rbp),%eax
cmp    $0x61,%eax
jl     1357 <handleInput+0x1a7>
movsbl -0x5(%rbp),%eax
cmp    $0x7a,%eax
jg     1357 <handleInput+0x1a7>
movsbl -0x5(%rbp),%eax
sub    $0x61,%eax
add    $0x41,%eax
mov    %al,-0x5(%rbp)
jmp    135c <handleInput+0x1ac>
jmp    1361 <handleInput+0x1b1>
jmp    1366 <handleInput+0x1b6>
jmp    136b <handleInput+0x1bb>
jmp    1370 <handleInput+0x1c0>
jmp    1375 <handleInput+0x1c5>
jmp    137a <handleInput+0x1ca>
jmp    137f <handleInput+0x1cf>
jmp    1384 <handleInput+0x1d4>
movsbl -0x5(%rbp),%eax
add    $0x80,%eax
movzbl %al,%edi
call   1040 <writeKbd@plt>
mov    $0xa7,%edi
call   1080 <writeKbdCr@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handleInput>:
push   %rax
xor    %eax,%eax
call   1060 <getch_screen@plt>
test   %al,%al
je     11b1 <handleInput+0x1>
lea    -0x8(%rax),%ecx
cmp    $0x4a,%cl
ja     11db <handleInput+0x2b>
movzbl %cl,%ecx
lea    0xe32(%rip),%rdx
movslq (%rdx,%rcx,4),%rcx
add    %rdx,%rcx
jmp    *%rcx
mov    $0xdf,%al
jmp    11f3 <handleInput+0x43>
lea    -0x61(%rax),%ecx
lea    -0x20(%rax),%edx
cmp    $0x1a,%cl
movzbl %al,%ecx
movzbl %dl,%eax
cmovae %ecx,%eax
xor    $0x80,%al
jmp    11f3 <handleInput+0x43>
mov    $0x8d,%al
movzbl %al,%edi
call   1040 <writeKbd@plt>
mov    $0xa7,%edi
call   1080 <writeKbdCr@plt>
jmp    124e <handleInput+0x9e>
xor    %eax,%eax
call   1070 <loadBasic@plt>
jmp    1240 <handleInput+0x90>
xor    %eax,%eax
call   10c0 <dumpCore@plt>
jmp    1240 <handleInput+0x90>
xor    %eax,%eax
call   10b0 <resetScreen@plt>
xor    %eax,%eax
call   1090 <resetPia6820@plt>
xor    %eax,%eax
call   10a0 <resetMemory@plt>
jmp    1247 <handleInput+0x97>
xor    %eax,%eax
call   1030 <loadCore@plt>
jmp    1240 <handleInput+0x90>
xor    %eax,%eax
call   1050 <flipMode@plt>
xor    %eax,%eax
call   1090 <resetPia6820@plt>
xor    %eax,%eax
call   10d0 <resetM6502@plt>
mov    $0x1,%eax
pop    %rcx
ret
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
define i32 @handleInput() {
%1 = alloca i32
%2 = alloca i8
store i8 0, i8* %2
br label %3
3: ; preds = %7, %0
%4 = call signext i8 (...) @getch_screen()
store i8 %4, i8* %2
%5 = sext i8 %4 to i32
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %8
7: ; preds = %3
br label %3
8: ; preds = %3
%9 = load i8, i8* %2
%10 = sext i8 %9 to i32
%11 = icmp eq i32 %10, 66
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = call i32 (...) @loadBasic()
%14 = call i32 (...) @resetPia6820()
%15 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
16: ; preds = %8
%17 = load i8, i8* %2
%18 = sext i8 %17 to i32
%19 = icmp eq i32 %18, 76
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = call i32 (...) @loadCore()
%22 = call i32 (...) @resetPia6820()
%23 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
24: ; preds = %16
%25 = load i8, i8* %2
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 68
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = call i32 (...) @dumpCore()
%30 = call i32 (...) @resetPia6820()
%31 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
32: ; preds = %24
%33 = load i8, i8* %2
%34 = sext i8 %33 to i32
%35 = icmp eq i32 %34, 82
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 (...) @resetPia6820()
%38 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
39: ; preds = %32
%40 = load i8, i8* %2
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 72
br i1 %42, label %43, label %48
43: ; preds = %39
%44 = call i32 (...) @resetScreen()
%45 = call i32 (...) @resetPia6820()
%46 = call i32 (...) @resetMemory()
%47 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
48: ; preds = %39
%49 = load i8, i8* %2
%50 = sext i8 %49 to i32
%51 = icmp eq i32 %50, 81
br i1 %51, label %52, label %53
52: ; preds = %48
store i32 0, i32* %1
br label %101
53: ; preds = %48
%54 = load i8, i8* %2
%55 = sext i8 %54 to i32
%56 = icmp eq i32 %55, 77
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = call i32 (...) @flipMode()
%59 = call i32 (...) @resetPia6820()
%60 = call i32 (...) @resetM6502()
store i32 1, i32* %1
br label %101
61: ; preds = %53
%62 = load i8, i8* %2
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %66
65: ; preds = %61
store i8 13, i8* %2
br label %87
66: ; preds = %61
%67 = load i8, i8* %2
%68 = sext i8 %67 to i32
%69 = icmp eq i32 %68, 8
br i1 %69, label %70, label %71
70: ; preds = %66
store i8 95, i8* %2
br label %86
71: ; preds = %66
%72 = load i8, i8* %2
%73 = sext i8 %72 to i32
%74 = icmp sge i32 %73, 97
br i1 %74, label %75, label %85
75: ; preds = %71
%76 = load i8, i8* %2
%77 = sext i8 %76 to i32
%78 = icmp sle i32 %77, 122
br i1 %78, label %79, label %85
79: ; preds = %75
%80 = load i8, i8* %2
%81 = sext i8 %80 to i32
%82 = sub nsw i32 %81, 97
%83 = add nsw i32 %82, 65
%84 = trunc i32 %83 to i8
store i8 %84, i8* %2
br label %85
85: ; preds = %79, %75, %71
br label %86
86: ; preds = %85, %70
br label %87
87: ; preds = %86, %65
br label %88
88: ; preds = %87
br label %89
89: ; preds = %88
br label %90
90: ; preds = %89
br label %91
91: ; preds = %90
br label %92
92: ; preds = %91
br label %93
93: ; preds = %92
br label %94
94: ; preds = %93
%95 = load i8, i8* %2
%96 = sext i8 %95 to i32
%97 = add nsw i32 %96, 128
%98 = trunc i32 %97 to i8
%99 = call i32 @writeKbd(i8 zeroext %98)
%100 = call i32 @writeKbdCr(i32 167)
store i32 1, i32* %1
br label %101
101: ; preds = %94, %57, %52, %43, %36, %28, %20, %12
%102 = load i32, i32* %1
ret i32 %102
}

LLVM IR (-O2):
define i32 @handleInput() {
br label %1
1: ; preds = %1, %0
%2 = tail call signext i8 (...) @getch_screen()
switch i8 %2, label %27 [
i8 0, label %1
i8 66, label %3
i8 76, label %7
i8 68, label %11
i8 82, label %15
i8 72, label %18
i8 81, label %38
i8 77, label %23
i8 10, label %33
i8 8, label %34
]
3: ; preds = %1
%4 = tail call i32 (...) @loadBasic()
%5 = tail call i32 (...) @resetPia6820()
%6 = tail call i32 (...) @resetM6502()
br label %38
7: ; preds = %1
%8 = tail call i32 (...) @loadCore()
%9 = tail call i32 (...) @resetPia6820()
%10 = tail call i32 (...) @resetM6502()
br label %38
11: ; preds = %1
%12 = tail call i32 (...) @dumpCore()
%13 = tail call i32 (...) @resetPia6820()
%14 = tail call i32 (...) @resetM6502()
br label %38
15: ; preds = %1
%16 = tail call i32 (...) @resetPia6820()
%17 = tail call i32 (...) @resetM6502()
br label %38
18: ; preds = %1
%19 = tail call i32 (...) @resetScreen()
%20 = tail call i32 (...) @resetPia6820()
%21 = tail call i32 (...) @resetMemory()
%22 = tail call i32 (...) @resetM6502()
br label %38
23: ; preds = %1
%24 = tail call i32 (...) @flipMode()
%25 = tail call i32 (...) @resetPia6820()
%26 = tail call i32 (...) @resetM6502()
br label %38
27: ; preds = %1
%28 = add i8 %2, -97
%29 = icmp ult i8 %28, 26
%30 = add i8 %2, -32
%31 = select i1 %29, i8 %30, i8 %2
%32 = xor i8 %31, -128
br label %34
33: ; preds = %1
br label %34
34: ; preds = %1, %33, %27
%35 = phi i8 [ %32, %27 ], [ -115, %33 ], [ -33, %1 ]
%36 = tail call i32 @writeKbd(i8 zeroext %35)
%37 = tail call i32 @writeKbdCr(i32 167)
br label %38
38: ; preds = %1, %34, %23, %18, %15, %11, %7, %3
%39 = phi i32 [ 1, %3 ], [ 1, %7 ], [ 1, %11 ], [ 1, %15 ], [ 1, %18 ], [ 1, %23 ], [ 1, %34 ], [ 0, %1 ]
ret i32 %39
}