// File: /workspace/github_cpp_makefile_star2_O3/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 0x6cee
movl %edi, %ebx
movb 0x124a33(%rip), %al     # 0x12b560
testb %al, %al
je 0x6da2
cmpq $0x0, 0x124a0b(%rip)    # 0x12b548
je 0x6c76
movsbl %bl, %esi
leaq 0x1249f3(%rip), %r14    # 0x12b540
movq %r14, %rdi
callq 0x10b0
leaq 0x1cd8(%rip), %rsi      # 0x8834
movq %r14, %rdi
callq 0x1040
testl %eax, %eax
je 0x6cee
leaq 0x1249cd(%rip), %rdi    # 0x12b540
leaq 0x1cbd(%rip), %rsi      # 0x8837
callq 0x1040
testl %eax, %eax
je 0x6cd9
leaq 0x1249b2(%rip), %rdi    # 0x12b540
leaq 0x1ca6(%rip), %rsi      # 0x883b
callq 0x1040
testl %eax, %eax
je 0x6cee
leaq 0x124997(%rip), %rdi    # 0x12b540
leaq 0x1c8e(%rip), %rsi      # 0x883e
callq 0x1040
testl %eax, %eax
je 0x6cfc
leaq 0x12497c(%rip), %rdi    # 0x12b540
leaq 0x1c77(%rip), %rsi      # 0x8842
callq 0x1040
testl %eax, %eax
je 0x6d18
leaq 0x124961(%rip), %rdi    # 0x12b540
leaq 0x1c60(%rip), %rsi      # 0x8846
callq 0x1040
testl %eax, %eax
je 0x6d34
leaq 0x124946(%rip), %rdi    # 0x12b540
leaq 0x1c49(%rip), %rsi      # 0x884a
callq 0x1040
testl %eax, %eax
je 0x6d50
movq 0x124933(%rip), %rdx    # 0x12b548
cmpq $0x3, %rdx
setae %al
leal -0x30(%rbx), %ecx
cmpl $0xa, %ecx
setb %cl
testb %al, %cl
jne 0x6cee
cmpq $0x4, %rdx
setb %al
cmpl $0x7e, %ebx
setne %cl
orb %al, %cl
jne 0x6c78
movq 0x1248fb(%rip), %rbx    # 0x12b540
leaq 0x2(%rbx), %rdi
callq 0x11e0
movl %eax, %r14d
movq $0x0, 0x1248ec(%rip) # 0x12b548
movb $0x0, (%rbx)
leal -0xb(%r14), %eax
cmpl $0x4, %eax
ja 0x6d6c
orl $0x30, %r14d
shll $0x8, %r14d
jmp 0x6cf1
xorl %edx, %edx
cmpl $0x1b, %ebx
jne 0x6c9d
leaq 0x1248bc(%rip), %rdi    # 0x12b540
leaq 0x1bc3(%rip), %rcx      # 0x884e
xorl %r14d, %r14d
movl $0x1, %r8d
xorl %esi, %esi
callq 0x11d0
jmp 0x6cf1
movq $0x0, 0x1248a0(%rip) # 0x12b548
movq 0x124891(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
cmpl $0x7f, %ebx
setg %al
testl %ebx, %ebx
sete %cl
orb %al, %cl
jne 0x6cee
movslq %ebx, %rax
leaq 0x1b85(%rip), %rcx      # 0x8850
movzbl (%rax,%rcx), %r14d
shll $0x8, %r14d
orl %ebx, %r14d
jmp 0x6cf1
movq $0x0, 0x124864(%rip) # 0x12b548
movq 0x124855(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x8, %rsp
popq %rbx
popq %r14
retq
movq $0x0, 0x124841(%rip) # 0x12b548
movq 0x124832(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4800, %r14w          # imm = 0x4800
jmp 0x6cf1
movq $0x0, 0x124825(%rip) # 0x12b548
movq 0x124816(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x5000, %r14w          # imm = 0x5000
jmp 0x6cf1
movq $0x0, 0x124809(%rip) # 0x12b548
movq 0x1247fa(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4d00, %r14w          # imm = 0x4D00
jmp 0x6cf1
movq $0x0, 0x1247ed(%rip) # 0x12b548
movq 0x1247de(%rip), %rax    # 0x12b540
movb $0x0, (%rax)
movw $0x4b00, %r14w          # imm = 0x4B00
jmp 0x6cf1
leal -0x11(%r14), %eax
cmpl $0x4, %eax
ja 0x6d85
shll $0x8, %r14d
addl $0x2f00, %r14d          # imm = 0x2F00
jmp 0x6cf1
leal -0x17(%r14), %eax
cmpl $0x1, %eax
ja 0x6cee
shll $0x8, %r14d
addl $0x6e00, %r14d          # imm = 0x6E00
jmp 0x6cf1
callq 0x1240
jmp 0x6b35