// File: /workspace/github_c_makefile_star2_O3/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, 0x10bd7f(%rip)    # 0x116954
je 0xac9d
movl 0x964b(%rip), %eax      # 0x1422c
movl 0x9649(%rip), %ecx      # 0x14230
movl %ecx, 0x963f(%rip)      # 0x1422c
movl 0x9641(%rip), %ecx      # 0x14234
movl %ecx, 0x9637(%rip)      # 0x14230
movl $0xffffffff, 0x9631(%rip) # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
jne 0xad71
callq 0x2bc3
cmpl $0x1b, %eax
jne 0xad71
xorps %xmm0, %xmm0
movq %rsp, %r8
movaps %xmm0, (%r8)
leaq 0x18(%rsp), %rsi
movups %xmm0, 0x68(%rsi)
movups %xmm0, 0x58(%rsi)
movups %xmm0, 0x48(%rsi)
movups %xmm0, 0x38(%rsi)
movups %xmm0, 0x28(%rsi)
movups %xmm0, 0x18(%rsi)
movups %xmm0, 0x8(%rsi)
movq $0x0, 0x78(%rsi)
movq $0x1, (%rsi)
movl $0x1, %edi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x2260
testl %eax, %eax
je 0xacc3
callq 0x2bc3
cmpl $0x4f, %eax
je 0xaccd
cmpl $0x5b, %eax
jne 0xacf1
callq 0x2bc3
leal -0x32(%rax), %ecx
cmpl $0x16, %ecx
ja 0xad82
leaq 0x323f(%rip), %rdx      # 0xdecc
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
callq 0x2bc3
jmp 0xad0c
callq 0x21f0
cmpl $-0x1, %eax
jne 0xad71
decl 0x9577(%rip)            # 0x14228
movl $0x1a, %eax
jne 0xad71
xorl %edi, %edi
callq 0x2390
movl $0x1b, %eax
jmp 0xad71
callq 0x2bc3
leal -0x41(%rax), %ecx
cmpl $0x7, %ecx
ja 0xad18
leaq 0x31cb(%rip), %rdx      # 0xdeac
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
movb $0x5, %al
jmp 0xad71
movl %eax, 0x9535(%rip)      # 0x1422c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x952e(%rip)      # 0x14230
movl %eax, 0x952c(%rip)      # 0x14234
movb $0x1b, %al
jmp 0xad71
movl $0x64, 0x9516(%rip)     # 0x1422c
jmp 0xad5e
cmpl $0x63, %eax
je 0xad3e
cmpl $0x64, %eax
jne 0xad2e
movb $0x1, %al
jmp 0xad71
movb $0x18, %al
jmp 0xad71
movb $0x4, %al
jmp 0xad71
movl $0x4f, 0x94f4(%rip)     # 0x1422c
jmp 0xad8c
movb $0x13, %al
jmp 0xad71
movb $0x6, %al
jmp 0xad71
callq 0x2bc3
movb $0x3, %al
jmp 0xad71
movb $0x8, %al
jmp 0xad71
callq 0x2bc3
movl $0x73, 0x94ce(%rip)     # 0x1422c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, 0x94c7(%rip)      # 0x14230
movl %eax, 0x94c5(%rip)      # 0x14234
movb $0x11, %al
addq $0x98, %rsp
retq
callq 0x2bc3
movb $0x16, %al
jmp 0xad71
movl $0x5b, 0x94a0(%rip)     # 0x1422c
movl %eax, 0x949e(%rip)      # 0x14230
movl $0xffffffff, 0x9498(%rip) # imm = 0xFFFFFFFF
jmp 0xad08
callq 0x2bc3
movb $0x7, %al
jmp 0xad71
callq 0x2bc3
movb $0x12, %al
jmp 0xad71