// File: /workspace/github_c_makefile_star2_O1/serge-404[P]CPM32/cpm.c
// Function: cpm_getch
byte cpm_getch( void)
{
    int c, ch, ch2;
    if ( conin) {
#ifdef __linux__
	  if (( c=cpull()) == CON_BUF_EMPTY)		
	    if (( c = getch()) == 27) {		/* ESC */
		  if (kbhit()) {
			if ((ch=getch()) == '[') {
				    ch2=getch();
					switch ( ch2) {
                		case 'A':  /* Up arrow */
                        	return 'E' - '@';
                		case 'B':  /* Down arrow */
                        	return 'X' - '@';
                		case 'C':  /* Right arrow */
                        	return 'D' - '@';
                		case 'D':  /* Left arrow */
                        	return 8 /*'S' - '@'*/;
                		case '3':  /* Delete key */
                        	getch();
                        	return 'G' - '@';
                		case '2':  /* Insert key */
                        	getch();
                        	return 'V' - '@';
                		case '5':  /* PgUp */
                        	getch();
                        	return 'R' - '@';
                		case '6':  /* PgDn */
                        	getch();
                        	return 'C' - '@';
                		/*case '1':*/			/* CTRL+func.key : `Esc[1;5`+key */
						case '7':   /* Home */
                        	getch();
                		case 'H':   /* Home */
                        	cpush('s');
                       		return 'Q' - '@';	/* WORDSTAR Ctrl+Q+key */
                		case '4':
						case '8':   /* End */
                        	getch();
                		case 'F':   /* End */
                        	cpush('d');
                        	return 'Q' - '@';
                		default:
                        	cpush('[');
                        	cpush2(ch2);
                        	return 27;
                	}
			}
			else if (ch=='O') {
				ch2 = getch();
				switch ( ch2) {
                	case 'A':  /* Up arrow */
                        return 'E' - '@';
                	case 'B':  /* Down arrow */
                        return 'X' - '@';
                	case 'C':  /* Right arrow */
                        return 'D' - '@';
                	case 'D':  /* Left arrow */
                        return 'S' - '@';
                	case 'd':  /* Ctrl left arrow (rxvt) */
                        return 'A' - '@';
                	case 'c':  /* Ctrl right arrow (rxvt) */
                        return 'F' - '@';
                	case 'H':  /* Home */
                        cpush('s');
                        return 'Q' - '@';
                	case 'F':  /* End */
                        cpush('d');
                        return 'Q' - '@';
                	default:
                        cpush('O');
                        cpush2(ch2);
                        return 27;
                }
			}
			else {
				cpush(ch);
				return 27;
			}
		  }
        }
#else
	  if (( c = getch()) == 0) {
	    switch ( getch()) {
	    case 72: c = 'E' - 0x40; break; /* UP */
	    case 80: c = 'X' - 0x40; break; /* DOWN */
	    case 75: c = 8 /*'S' - 0x40*/; break; /* LEFT */            /* 2019 / ORION-128: 8 instead of ^S for LEFT */
	    case 77: c = 'D' - 0x40; break; /* RIGHT */
	    case 82: c = 'V' - 0x40; break; /* INS */
	    case 83: c = '\x7f';     break; /* DEL */ /* 2012.03 bug-fix by N.Fujita */
	    case 73: c = 'R' - 0x40; break; /* PgUP */
	    case 81: c = 'C' - 0x40; break; /* PgDOWN */
	    case 141: c = 'W' - 0x40; break; /* ^UP */
	    case 145: c = 'Z' - 0x40; break; /* ^DOWN */
	    case 115: c = 'A' - 0x40; break; /* ^LEFT */
	    case 116: c = 'F' - 0x40; break; /* ^RIGHT */
	    }
	  }
#endif
    } else if (( c = getchar()) == EOF) {
	  if ( --eofcount == 0) exit( 0);
	  c = '\x1a';
    }
    return (byte)c;
}

// Assembly:
subq $0x98, %rsp
cmpl $0x0, 0x10c25a(%rip)    # 0x116954
je 0xa7bd
movl 0x9b2e(%rip), %eax      # 0x14234
movl 0x9b2c(%rip), %ecx      # 0x14238
movl %ecx, 0x9b22(%rip)      # 0x14234
movl 0x9b24(%rip), %ecx      # 0x1423c
movl %ecx, 0x9b1a(%rip)      # 0x14238
movl $0xffffffff, 0x9b14(%rip) # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
jne 0xa891
callq 0x2bce
cmpl $0x1b, %eax
jne 0xa891
xorps %xmm0, %xmm0
movq %rsp, %r8
movaps %xmm0, (%r8)
leaq 0x10(%rsp), %rsi
movaps %xmm0, (%rsi)
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movq $0x1, (%rsi)
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2270
testl %eax, %eax
je 0xa7e3
callq 0x2bce
cmpl $0x4f, %eax
je 0xa7ed
cmpl $0x5b, %eax
jne 0xa811
callq 0x2bce
leal -0x32(%rax), %ecx
cmpl $0x16, %ecx
ja 0xa8a2
leaq 0x371f(%rip), %rdx      # 0xdecc
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
callq 0x2bce
jmp 0xa82c
callq 0x2200
cmpl $-0x1, %eax
jne 0xa891
decl 0x9a5f(%rip)            # 0x14230
movl $0x1a, %eax
jne 0xa891
xorl %edi, %edi
callq 0x23a0
movl $0x1b, %eax
jmp 0xa891
callq 0x2bce
leal -0x41(%rax), %ecx
cmpl $0x7, %ecx
ja 0xa838
leaq 0x36ab(%rip), %rdx      # 0xdeac
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movb $0x5, %al
jmp 0xa891
movl %eax, 0x9a1d(%rip)      # 0x14234
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x9a16(%rip)      # 0x14238
movl %eax, 0x9a14(%rip)      # 0x1423c
movb $0x1b, %al
jmp 0xa891
movl $0x64, 0x99fe(%rip)     # 0x14234
jmp 0xa87e
cmpl $0x63, %eax
je 0xa85e
cmpl $0x64, %eax
jne 0xa84e
movb $0x1, %al
jmp 0xa891
movb $0x18, %al
jmp 0xa891
movb $0x4, %al
jmp 0xa891
movl $0x4f, 0x99dc(%rip)     # 0x14234
jmp 0xa8ac
movb $0x13, %al
jmp 0xa891
movb $0x6, %al
jmp 0xa891
callq 0x2bce
movb $0x3, %al
jmp 0xa891
movb $0x8, %al
jmp 0xa891
callq 0x2bce
movl $0x73, 0x99b6(%rip)     # 0x14234
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x99af(%rip)      # 0x14238
movl %eax, 0x99ad(%rip)      # 0x1423c
movb $0x11, %al
addq $0x98, %rsp
retq
callq 0x2bce
movb $0x16, %al
jmp 0xa891
movl $0x5b, 0x9988(%rip)     # 0x14234
movl $0xffffffff, 0x9986(%rip) # imm = 0xFFFFFFFF
movl %eax, 0x997c(%rip)      # 0x14238
jmp 0xa828
callq 0x2bce
movb $0x7, %al
jmp 0xa891
callq 0x2bce
movb $0x12, %al
jmp 0xa891