// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
cmpl $0x0, 0x10d155(%rip)    # 0x11daf4
je 0x10c24
callq 0x103b0
movl %eax, -0x8(%rbp)
cmpl $-0x1, %eax
jne 0x10c22
callq 0x2660
movl %eax, -0x8(%rbp)
cmpl $0x1b, %eax
jne 0x10c20
callq 0x25c0
cmpl $0x0, %eax
je 0x10c1e
callq 0x2660
movl %eax, -0xc(%rbp)
cmpl $0x5b, %eax
jne 0x10b05
callq 0x2660
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
addl $-0x32, %eax
movl %eax, %ecx
movq %rcx, -0x18(%rbp)
subl $0x16, %eax
ja 0x10acb
movq -0x18(%rbp), %rax
leaq 0x484e(%rip), %rcx      # 0x1525c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x5, -0x1(%rbp)
jmp 0x10c5b
movb $0x18, -0x1(%rbp)
jmp 0x10c5b
movb $0x4, -0x1(%rbp)
jmp 0x10c5b
movb $0x8, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movb $0x7, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movb $0x16, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movb $0x12, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movb $0x3, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movl $0x73, 0xa7ca(%rip)     # 0x1b24c
movl $0xffffffff, 0xa7c4(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa7be(%rip) # imm = 0xFFFFFFFF
movb $0x11, -0x1(%rbp)
jmp 0x10c5b
callq 0x2660
movl $0x64, 0xa79e(%rip)     # 0x1b24c
movl $0xffffffff, 0xa798(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa792(%rip) # imm = 0xFFFFFFFF
movb $0x11, -0x1(%rbp)
jmp 0x10c5b
movl $0x5b, 0xa777(%rip)     # 0x1b24c
movl $0xffffffff, 0xa771(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa76b(%rip) # imm = 0xFFFFFFFF
movl -0x10(%rbp), %eax
movl %eax, 0xa75e(%rip)      # 0x1b250
movl $0xffffffff, 0xa758(%rip) # imm = 0xFFFFFFFF
movb $0x1b, -0x1(%rbp)
jmp 0x10c5b
cmpl $0x4f, -0xc(%rbp)
jne 0x10bfb
callq 0x2660
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
addl $-0x41, %eax
movl %eax, %ecx
movq %rcx, -0x20(%rbp)
subl $0x23, %eax
ja 0x10bc4
movq -0x20(%rbp), %rax
leaq 0x4695(%rip), %rcx      # 0x151cc
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movb $0x5, -0x1(%rbp)
jmp 0x10c5b
movb $0x18, -0x1(%rbp)
jmp 0x10c5b
movb $0x4, -0x1(%rbp)
jmp 0x10c5b
movb $0x13, -0x1(%rbp)
jmp 0x10c5b
movb $0x1, -0x1(%rbp)
jmp 0x10c5b
movb $0x6, -0x1(%rbp)
jmp 0x10c5b
movl $0x73, 0xa6cc(%rip)     # 0x1b24c
movl $0xffffffff, 0xa6c6(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa6c0(%rip) # imm = 0xFFFFFFFF
movb $0x11, -0x1(%rbp)
jmp 0x10c5b
movl $0x64, 0xa6a5(%rip)     # 0x1b24c
movl $0xffffffff, 0xa69f(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa699(%rip) # imm = 0xFFFFFFFF
movb $0x11, -0x1(%rbp)
jmp 0x10c5b
movl $0x4f, 0xa67e(%rip)     # 0x1b24c
movl $0xffffffff, 0xa678(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa672(%rip) # imm = 0xFFFFFFFF
movl -0x10(%rbp), %eax
movl %eax, 0xa665(%rip)      # 0x1b250
movl $0xffffffff, 0xa65f(%rip) # imm = 0xFFFFFFFF
movb $0x1b, -0x1(%rbp)
jmp 0x10c5b
movl -0xc(%rbp), %eax
movl %eax, 0xa648(%rip)      # 0x1b24c
movl $0xffffffff, 0xa642(%rip) # imm = 0xFFFFFFFF
movl $0xffffffff, 0xa63c(%rip) # imm = 0xFFFFFFFF
movb $0x1b, -0x1(%rbp)
jmp 0x10c5b
jmp 0x10c20
jmp 0x10c22
jmp 0x10c55
callq 0x2210
movl %eax, -0x8(%rbp)
cmpl $-0x1, %eax
jne 0x10c53
movl 0xa611(%rip), %eax      # 0x1b248
addl $-0x1, %eax
movl %eax, 0xa608(%rip)      # 0x1b248
cmpl $0x0, %eax
jne 0x10c4c
xorl %edi, %edi
callq 0x23c0
movl $0x1a, -0x8(%rbp)
jmp 0x10c55
movl -0x8(%rbp), %eax
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)