// File: /workspace/github_c_makefile_star2_O0/mist64[P]cbmbasic/runtime.c
// Function: CHROUT
static void
CHROUT() {
#if 0
int a = *(unsigned short*)(&RAM[0x0100+S+1]) + 1;
int b = *(unsigned short*)(&RAM[0x0100+S+3]) + 1;
int c = *(unsigned short*)(&RAM[0x0100+S+5]) + 1;
int d = *(unsigned short*)(&RAM[0x0100+S+7]) + 1;
printf("CHROUT: %d @ %x,%x,%x,%x\n", A, a, b, c, d);
#endif
	if (!interactive) {
		if (stack4(0xe10f,0xab4a,0xab30,0xe430)) {
			/* COMMODORE 64 BASIC V2 */
			C = 0;
			return;
		}
		if (stack4(0xe10f,0xab4a,0xab30,0xe43d)) {
			/* 38911 */
			C = 0;
			return;
		}
		if (stack4(0xe10f,0xab4a,0xab30,0xe444)) {
			/* BASIC BYTES FREE */
			C = 0;
			return;
		}
	}
	if (stack4(0xe10f,0xab4a,0xab30,0xa47b)) {
		/* READY */
		if (A=='R') readycount++;
		if (!interactive) {
			C = 0;
			return;
		}
	}
	if (stack4(0xe10f,0xab4a,0xaadc,0xa486)) {
		/*
		 * CR after each entered numbered program line:
		 * The CBM screen editor returns CR when the user
		 * hits return, but does not print the character,
		 * therefore CBMBASIC does. On UNIX, the terminal
		 * prints all input characters, so we have to avoid
		 * printing it again
		 */
		C = 0;
		return;
	}
	
#if 0
	printf("CHROUT: %c (%d)\n", A, A);
#else
    if (kernal_output) {
        if (fputc(A, kernal_files[kernal_output]) == EOF) {
            C = 1;
            A = KERN_ERR_NOT_OUTPUT_FILE;
        } else
            C = 0;
    } else {
        if (kernal_quote) {
            if (A == '"' || A == '\n' || A == '\r') kernal_quote = 0;
            putchar(A);
        } else {
            switch (A) {
                case 5:
                    set_color(COLOR_WHITE);
                    break;
                case 10:
                    break;
                case 13:
                    putchar(13);
                    putchar(10);
                    break;
                case 17: /* CSR DOWN */
                    down_cursor();
                    break;
		case 18: /* REV ON */
		    reverse_screen(1);
		    break;
                case 19: /* CSR HOME */
                    move_cursor(0, 0);
                    break;
                case 28:
                    set_color(COLOR_RED);
                    break;
                case 29: /* CSR RIGHT */
                    right_cursor();
                    break;
                case 30:
                    set_color(COLOR_GREEN);
                    break;
                case 31:
                    set_color(COLOR_BLUE);
                    break;
                case 129:
                    set_color(COLOR_ORANGE);
                    break;
                case 144:
                    set_color(COLOR_BLACK);
                    break;
                case 145: /* CSR UP */
                    up_cursor();
                    break;
		case 146: /* REV OFF */
		    reverse_screen(0);
                    break;
                case 147: /* clear screen */
#ifndef NO_CLRHOME
                    clear_screen();
#endif
                    break;
                case 149:
                    set_color(COLOR_BROWN);
                    break;
                case 150:
                    set_color(COLOR_LTRED);
                    break;
                case 151:
                    set_color(COLOR_GREY1);
                    break;
                case 152:
                    set_color(COLOR_GREY2);
                    break;
                case 153:
                    set_color(COLOR_LTGREEN);
                    break;
                case 154:
                    set_color(COLOR_LTBLUE);
                    break;
                case 155:
                    set_color(COLOR_GREY3);
                    break;
                case 156:
                    set_color(COLOR_PURPLE);
                    break;
                case 158:
                    set_color(COLOR_YELLOW);
                    break;
                case 159:
                    set_color(COLOR_CYAN);
                    break;
                case 157: /* CSR LEFT */
                    left_cursor();
                    break;
                case '"':
                    kernal_quote = 1;
                    // fallthrough
                default:
                    putchar(A);
            }
        }
#endif
		fflush(stdout);
		C = 0;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
cmpl $0x0, 0x1df25(%rip)     # 0x5ab94
jne 0x3ccfe
movl $0xe10f, %edi           # imm = 0xE10F
movl $0xab4a, %esi           # imm = 0xAB4A
movl $0xab30, %edx           # imm = 0xAB30
movl $0xe430, %ecx           # imm = 0xE430
callq 0x3b640
cmpl $0x0, %eax
je 0x3cca2
leaq 0x1de1c(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
movl $0xe10f, %edi           # imm = 0xE10F
movl $0xab4a, %esi           # imm = 0xAB4A
movl $0xab30, %edx           # imm = 0xAB30
movl $0xe43d, %ecx           # imm = 0xE43D
callq 0x3b640
cmpl $0x0, %eax
je 0x3cccf
leaq 0x1ddef(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
movl $0xe10f, %edi           # imm = 0xE10F
movl $0xab4a, %esi           # imm = 0xAB4A
movl $0xab30, %edx           # imm = 0xAB30
movl $0xe444, %ecx           # imm = 0xE444
callq 0x3b640
cmpl $0x0, %eax
je 0x3ccfc
leaq 0x1ddc2(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
jmp 0x3ccfe
movl $0xe10f, %edi           # imm = 0xE10F
movl $0xab4a, %esi           # imm = 0xAB4A
movl $0xab30, %edx           # imm = 0xAB30
movl $0xa47b, %ecx           # imm = 0xA47B
callq 0x3b640
cmpl $0x0, %eax
je 0x3cd54
leaq 0x1dd92(%rip), %rax     # 0x5aab5
movzbl (%rax), %eax
cmpl $0x52, %eax
jne 0x3cd3a
movl 0x1de5f(%rip), %eax     # 0x5ab90
addl $0x1, %eax
movl %eax, 0x1de56(%rip)     # 0x5ab90
cmpl $0x0, 0x1de53(%rip)     # 0x5ab94
jne 0x3cd52
leaq 0x1dd6c(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
jmp 0x3cd54
movl $0xe10f, %edi           # imm = 0xE10F
movl $0xab4a, %esi           # imm = 0xAB4A
movl $0xaadc, %edx           # imm = 0xAADC
movl $0xa486, %ecx           # imm = 0xA486
callq 0x3b640
cmpl $0x0, %eax
je 0x3cd81
leaq 0x1dd3d(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
cmpb $0x0, 0x1dd40(%rip)     # 0x5aac8
je 0x3cdd7
leaq 0x1dd24(%rip), %rax     # 0x5aab5
movzbl (%rax), %edi
movzbl 0x1dd2d(%rip), %eax     # 0x5aac8
movl %eax, %ecx
leaq 0x1dd2c(%rip), %rax     # 0x5aad0
movq (%rax,%rcx,8), %rsi
callq 0x1120
cmpl $-0x1, %eax
jne 0x3cdc8
leaq 0x1dcfd(%rip), %rax     # 0x5aab6
movb $0x1, (%rax)
leaq 0x1dcf2(%rip), %rax     # 0x5aab5
movb $0x7, (%rax)
jmp 0x3cdd2
leaq 0x1dce7(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
jmp 0x3cfe6
cmpl $0x0, 0x1dce6(%rip)     # 0x5aac4
je 0x3ce2b
leaq 0x1dcce(%rip), %rax     # 0x5aab5
movzbl (%rax), %eax
cmpl $0x22, %eax
je 0x3ce0d
leaq 0x1dcbf(%rip), %rax     # 0x5aab5
movzbl (%rax), %eax
cmpl $0xa, %eax
je 0x3ce0d
leaq 0x1dcb0(%rip), %rax     # 0x5aab5
movzbl (%rax), %eax
cmpl $0xd, %eax
jne 0x3ce17
movl $0x0, 0x1dcad(%rip)     # 0x5aac4
leaq 0x1dc97(%rip), %rax     # 0x5aab5
movzbl (%rax), %edi
callq 0x1030
jmp 0x3cfcd
leaq 0x1dc83(%rip), %rax     # 0x5aab5
movzbl (%rax), %eax
addl $-0x5, %eax
movl %eax, %ecx
movq %rcx, -0x8(%rbp)
subl $0x9a, %eax
ja 0x3cfbc
movq -0x8(%rbp), %rax
leaq 0x2a0c(%rip), %rcx      # 0x3f860
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %edi, %edi
callq 0x3e700
jmp 0x3cfcb
jmp 0x3cfcb
movl $0xd, %edi
callq 0x1030
movl $0xa, %edi
callq 0x1030
jmp 0x3cfcb
movb $0x0, %al
callq 0x3e5f0
jmp 0x3cfcb
movl $0x1, %edi
callq 0x3e6b0
jmp 0x3cfcb
xorl %esi, %esi
movl %esi, %edi
callq 0x3e650
jmp 0x3cfcb
movl $0x1, %edi
callq 0x3e700
jmp 0x3cfcb
movb $0x0, %al
callq 0x3e630
jmp 0x3cfcb
movl $0x2, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x3, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x8, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x4, %edi
callq 0x3e700
jmp 0x3cfcb
movb $0x0, %al
callq 0x3e5d0
jmp 0x3cfcb
xorl %edi, %edi
callq 0x3e6b0
jmp 0x3cfcb
movb $0x0, %al
callq 0x3e5b0
jmp 0x3cfcb
movl $0x9, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xa, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xb, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xc, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xd, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xe, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0xf, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x5, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x6, %edi
callq 0x3e700
jmp 0x3cfcb
movl $0x7, %edi
callq 0x3e700
jmp 0x3cfcb
movb $0x0, %al
callq 0x3e610
jmp 0x3cfcb
movl $0x1, 0x1db08(%rip)     # 0x5aac4
leaq 0x1daf2(%rip), %rax     # 0x5aab5
movzbl (%rax), %edi
callq 0x1030
jmp 0x3cfcd
movq 0x500c(%rip), %rax      # 0x41fe0
movq (%rax), %rdi
callq 0x11b0
leaq 0x1dad3(%rip), %rax     # 0x5aab6
movb $0x0, (%rax)
addq $0x10, %rsp
popq %rbp
retq
nopl (%rax)