// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %r14
pushq %rbx
pushq %rax
cmpl $-0x1, %edi
je 0x6cb2
movl %edi, %ebx
movb 0x124a77(%rip), %al     # 0x12b560
testb %al, %al
je 0x6d66
cmpq $0x0, 0x124a4f(%rip)    # 0x12b548
je 0x6c35
movsbl %bl, %esi
leaq 0x124a37(%rip), %r14    # 0x12b540
movq %r14, %rdi
callq 0x10b0
leaq 0x1cf0(%rip), %rsi      # 0x8808
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x6cb2
leaq 0x124a11(%rip), %rdi    # 0x12b540
leaq 0x1cd5(%rip), %rsi      # 0x880b
callq 0x1040
testl %eax, %eax
je 0x6c9d
leaq 0x1249f6(%rip), %rdi    # 0x12b540
leaq 0x1cbe(%rip), %rsi      # 0x880f
callq 0x1040
testl %eax, %eax
je 0x6cb2
leaq 0x1249db(%rip), %rdi    # 0x12b540
leaq 0x1ca6(%rip), %rsi      # 0x8812
callq 0x1040
testl %eax, %eax
je 0x6cc0
leaq 0x1249c0(%rip), %rdi    # 0x12b540
leaq 0x1c8f(%rip), %rsi      # 0x8816
callq 0x1040
testl %eax, %eax
je 0x6cdc
leaq 0x1249a5(%rip), %rdi    # 0x12b540
leaq 0x1c78(%rip), %rsi      # 0x881a
callq 0x1040
testl %eax, %eax
je 0x6cf8
leaq 0x12498a(%rip), %rdi    # 0x12b540
leaq 0x1c61(%rip), %rsi      # 0x881e
callq 0x1040
testl %eax, %eax
je 0x6d14
movq 0x124977(%rip), %rax    # 0x12b548
cmpq $0x3, %rax
setae %cl
leal -0x30(%rbx), %edx
cmpl $0xa, %edx
setb %dl
testb %cl, %dl
jne 0x6cb2
cmpq $0x4, %rax
setb %al
cmpl $0x7e, %ebx
setne %cl
orb %al, %cl
jne 0x6c35
movq 0x12493f(%rip), %rbx    # 0x12b540
leaq 0x2(%rbx), %rdi
callq 0x11e0
movl %eax, %r14d
movq $0x0, 0x124930(%rip) # 0x12b548
movb $0x0, (%rbx)
leal -0xb(%r14), %eax
cmpl $0x4, %eax
ja 0x6d30
orl $0x30, %r14d
shll $0x8, %r14d
jmp 0x6cb5
cmpl $0x1b, %ebx
jne 0x6c61
movq 0x124907(%rip), %rdx    # 0x12b548
leaq 0x1248f8(%rip), %rdi    # 0x12b540
leaq 0x1bd3(%rip), %rcx      # 0x8822
xorl %r14d, %r14d
movl $0x1, %r8d
xorl %esi, %esi
callq 0x11d0
jmp 0x6cb5
movq $0x0, 0x1248dc(%rip) # 0x12b548
movq 0x1248cd(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
cmpl $0x7f, %ebx
setg %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0x6cb2
movslq %ebx, %rax
leaq 0x1ba1(%rip), %rcx      # 0x8830
movzbl (%rax,%rcx), %r14d
shll $0x8, %r14d
orl %ebx, %r14d
jmp 0x6cb5
movq $0x0, 0x1248a0(%rip) # 0x12b548
movq 0x124891(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq $0x0, 0x12487d(%rip) # 0x12b548
movq 0x12486e(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4800, %r14w          # imm = 0x4800
jmp 0x6cb5
movq $0x0, 0x124861(%rip) # 0x12b548
movq 0x124852(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x5000, %r14w          # imm = 0x5000
jmp 0x6cb5
movq $0x0, 0x124845(%rip) # 0x12b548
movq 0x124836(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4d00, %r14w          # imm = 0x4D00
jmp 0x6cb5
movq $0x0, 0x124829(%rip) # 0x12b548
movq 0x12481a(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4b00, %r14w          # imm = 0x4B00
jmp 0x6cb5
leal -0x11(%r14), %eax
cmpl $0x4, %eax
ja 0x6d49
shll $0x8, %r14d
addl $0x2f00, %r14d          # imm = 0x2F00
jmp 0x6cb5
leal -0x17(%r14), %eax
cmpl $0x1, %eax
ja 0x6cb2
shll $0x8, %r14d
addl $0x6e00, %r14d          # imm = 0x6E00
jmp 0x6cb5
callq 0x1240
jmp 0x6af1