// File: /workspace/github_cpp_makefile_star2_O2/7shi[P]8086run/v2/keyboard.cpp
// Function: decodeKey(int)
uint16_t decodeKey(int ch) {
    if (ch == EOF) return 0;
#ifdef _WIN32
    static int stroke = -1;
    if (stroke == 0xe0 || stroke == 0) {
        stroke = EOF;
        return ch << 8;
    }
    if (ch == 0xe0 || ch == 0) {
        stroke = ch;
        return 0;
    }
    stroke = EOF;
#else
    static std::string stroke;
    if (!stroke.empty()) {
        stroke += ch;
        if (stroke == "\x1bO") { // NumLock
            return 0;
        } else if (stroke == "\x1bOP") { // NumLock (ignore)
            stroke.clear();
            return 0;
        } else if (stroke == "\x1b[") {
            return 0;
        } else if (stroke == "\x1b[A") { // up
            stroke.clear();
            return 'H' << 8;
        } else if (stroke == "\x1b[B") { // down
            stroke.clear();
            return 'P' << 8;
        } else if (stroke == "\x1b[C") { // right
            stroke.clear();
            return 'M' << 8;
        } else if (stroke == "\x1b[D") { // left
            stroke.clear();
            return 'K' << 8;
        } else if (stroke.length() > 2 && isdigit(ch)) { // F*
            return 0;
        } else if (stroke.length() > 3 && ch == '~') { // F*
            int num = atoi(&stroke[2]);
            stroke.clear();
            if (11 <= num && num <= 15) return (0x3b + (num - 11)) << 8;
            if (17 <= num && num <= 21) return (0x40 + (num - 17)) << 8;
            if (23 <= num && num <= 24) return (0x85 + (num - 23)) << 8;
            return 0;
        }
    }
    if (ch == 0x1b) {
        stroke = "\x1b";
        return 0;
    }
    stroke.clear();
#endif
    if (ch < 128 && kbscan[ch]) {
        return (kbscan[ch] << 8) | ch;
    }
    return 0;
}

// Assembly:
cmpl $-0x1, %edi
je 0x4d06
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %ebx
movb 0x1248c7(%rip), %al     # 0x129560
testb %al, %al
je 0x4ec1
cmpq $0x0, 0x12489f(%rip)    # 0x129548
je 0x4e43
movsbl %bl, %esi
leaq 0x124887(%rip), %r14    # 0x129540
movq %r14, %rdi
callq 0x10b0
leaq 0x1b4c(%rip), %rsi      # 0x6814
movq %r14, %rdi
callq 0x4f1d
testb %al, %al
jne 0x4e7e
leaq 0x124861(%rip), %rdi    # 0x129540
leaq 0x1b31(%rip), %rsi      # 0x6817
callq 0x4f1d
testb %al, %al
je 0x4d09
andq $0x0, 0x124851(%rip)    # 0x129548
movq 0x124842(%rip), %rax    # 0x129540
movb $0x0, (%rax)
jmp 0x4e7e
xorl %eax, %eax
retq
leaq 0x124830(%rip), %rdi    # 0x129540
leaq 0x1b04(%rip), %rsi      # 0x681b
callq 0x4f1d
testb %al, %al
jne 0x4e7e
leaq 0x124815(%rip), %rdi    # 0x129540
leaq 0x1aec(%rip), %rsi      # 0x681e
callq 0x4f1d
testb %al, %al
je 0x4d56
andq $0x0, 0x124805(%rip)    # 0x129548
movq 0x1247f6(%rip), %rax    # 0x129540
movb $0x0, (%rax)
movw $0x4800, %ax            # imm = 0x4800
jmp 0x4e80
leaq 0x1247e3(%rip), %rdi    # 0x129540
leaq 0x1abe(%rip), %rsi      # 0x6822
callq 0x4f1d
testb %al, %al
je 0x4d88
andq $0x0, 0x1247d3(%rip)    # 0x129548
movq 0x1247c4(%rip), %rax    # 0x129540
movb $0x0, (%rax)
movw $0x5000, %ax            # imm = 0x5000
jmp 0x4e80
leaq 0x1247b1(%rip), %rdi    # 0x129540
leaq 0x1a90(%rip), %rsi      # 0x6826
callq 0x4f1d
testb %al, %al
je 0x4dba
andq $0x0, 0x1247a1(%rip)    # 0x129548
movq 0x124792(%rip), %rax    # 0x129540
movb $0x0, (%rax)
movw $0x4d00, %ax            # imm = 0x4D00
jmp 0x4e80
leaq 0x12477f(%rip), %rdi    # 0x129540
leaq 0x1a62(%rip), %rsi      # 0x682a
callq 0x4f1d
testb %al, %al
je 0x4dec
andq $0x0, 0x12476f(%rip)    # 0x129548
movq 0x124760(%rip), %rax    # 0x129540
movb $0x0, (%rax)
movw $0x4b00, %ax            # imm = 0x4B00
jmp 0x4e80
movq 0x124755(%rip), %rax    # 0x129548
cmpq $0x3, %rax
setae %cl
leal -0x30(%rbx), %edx
cmpl $0xa, %edx
setb %dl
testb %cl, %dl
jne 0x4e7e
cmpq $0x4, %rax
setb %al
cmpl $0x7e, %ebx
setne %cl
orb %al, %cl
jne 0x4e43
movq 0x124721(%rip), %rbx    # 0x129540
leaq 0x2(%rbx), %rdi
callq 0x11d0
andq $0x0, 0x124718(%rip)    # 0x129548
movb $0x0, (%rbx)
leal -0xb(%rax), %ecx
cmpl $0x4, %ecx
ja 0x4e9d
orl $0x30, %eax
shll $0x8, %eax
jmp 0x4e80
cmpl $0x1b, %ebx
jne 0x4e5d
leaq 0x1246f1(%rip), %rdi    # 0x129540
leaq 0x19d8(%rip), %rsi      # 0x682e
callq 0x1210
jmp 0x4e7e
andq $0x0, 0x1246e3(%rip)    # 0x129548
movq 0x1246d4(%rip), %rax    # 0x129540
movb $0x0, (%rax)
cmpl $0x7f, %ebx
setg %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
je 0x4e88
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movslq %ebx, %rax
leaq 0x199e(%rip), %rcx      # 0x6830
movzbl (%rax,%rcx), %eax
shll $0x8, %eax
orl %ebx, %eax
jmp 0x4e80
leal -0x11(%rax), %ecx
cmpl $0x4, %ecx
ja 0x4eaf
shll $0x8, %eax
addl $0x2f00, %eax           # imm = 0x2F00
jmp 0x4e80
leal -0x17(%rax), %ecx
cmpl $0x1, %ecx
ja 0x4e7e
shll $0x8, %eax
addl $0x6e00, %eax           # imm = 0x6E00
jmp 0x4e80
leaq 0x124698(%rip), %rdi    # 0x129560
callq 0x1200
testl %eax, %eax
je 0x4ca1
leaq 0x124674(%rip), %rax    # 0x129550
movq %rax, 0x12465d(%rip)    # 0x129540
leaq 0x124656(%rip), %rsi    # 0x129540
andq $0x0, 0x124656(%rip)    # 0x129548
movb $0x0, 0x124657(%rip)    # 0x129550
movq 0x40d0(%rip), %rdi      # 0x8fd0
leaq 0x4219(%rip), %rdx      # 0x9120
callq 0x10c0
leaq 0x12464d(%rip), %rdi    # 0x129560
callq 0x1090
jmp 0x4ca1