// File: /workspace/github_cpp_makefile_star2_O3/7shi[P]8086run/v1/main.cpp
// Function: bios(int)
void bios(int n) {
    void intr(int);
    static uint8_t buf[512];
    switch (n) {
        case 0x00: // #DE
            error("#DE: division exception");
            break;
        case 0x08: // timer
        {
            uint16_t low = read16(&mem[0x46c]);
            if (!++low) {
                uint16_t high = read16(&mem[0x46e]);
                if (++high >= 24) {
                    high %= 24;
                    mem[0x470] = 1;
                }
                write16(&mem[0x46e], high);
            }
            write16(&mem[0x46c], low);
            intr(0x1c);
            return;
        }
        case 0x09: // keyboard
            while (kbhit()) {
                int head = read16(&mem[0x41a]) - 0x1e;
                int tail = read16(&mem[0x41c]) - 0x1e;
                int next = (tail + 2) & 0x1f;
                if (next == head) break;
                int ch = getch();
                if (ch != EOF && kbscan[ch]) {
                    mem[0x41e + tail] = ch;
                    mem[0x41f + tail] = kbscan[ch];
                    write16(&mem[0x41c], 0x1e + next);
                }
            }
            return;
        case 0x10: // video
            switch (AH) {
                case 0x0e:
                    if (write(STDOUT_FILENO, &AL, 1));
                    return;
            }
            return; // ignore
        case 0x11: // get equipment list
            AX = disks[1].f ? 0x41 : 1;
            return;
        case 0x12: // get memory size
            AX = 640;
            return;
        case 0x13: // disk
        {
            if (DL > 1 || !disks[DL].f) {
                CF = 1; // error
                AH = 1; // invalid
                return;
            }
            Disk *d = &disks[DL];
            int c = CH | ((CL & 0xc0) << 2), s = (CL & 0x3f) - 1;
            int lba = d->s * (d->h * c + DH) + s;
            switch (AH) {
                case 0x00: // reset disk system
                    AH = CF = 0;
                    return;
                case 0x02: // read sectors
                case 0x03: // write sectors
                    if (c >= d->c || DH >= d->h
                            || s < 0 || s >= d->s) {
                        CF = 1; // error
                        AH = 4; // sector
                        return;
                    }
                    fseek(d->f, lba << 9, SEEK_SET);
                    if (AH == 2) {
                        if (fread(&ES[BX], 512, AL, d->f) < 1) {
                            memset(&ES[BX], 0, AL << 9);
                        }
                    } else {
                        fwrite(&ES[BX], 512, AL, d->f);
                    }
                    AH = CF = 0;
                    return;
                case 0x05: // format
                    fseek(d->f, (lba - s) << 9, SEEK_SET);
                    for (int i = 0; i < d->s; ++i) {
                        fwrite(&buf, 512, 1, d->f);
                    }
                    AH = CF = 0;
                    return;
                case 0x08: // get drive params
                    AX = 0;
                    BL = d->type;
                    CH = d->c - 1;
                    CL = d->s | ((d->c >> 2) & 0xc0);
                    DH = d->h - 1;
                    DL = 1;
                    ES = DI = 0;
                    CF = 0;
                    return;
                case 0x15: // get disk type
                    AH = 1;
                    CF = 0;
                    return;
                case 0x17: // set disk type
                    CF = 0; // ignore
                    return;
                case 0x18: // set media type
                    d->c = c + 1;
                    d->s = s + 1;
                    ES = DI = 0;
                    AH = CF = 0;
                    return;
            }
            CF = 1;
            return;
        }
        case 0x15: // system
            switch (AH) {
                case 0x88: // get extended memory size
                    CF = 0;
                    AX = 0;
                    return;
            }
            break;
        case 0x16: // keyboard
        {
            int head = read16(&mem[0x41a]);
            int tail = read16(&mem[0x41c]);
            switch (AH) {
                case 0x00: // get keystroke
                    while (head == tail) {
                        bios(9);
                        tail = read16(&mem[0x41c]);
                    }
                    AX = read16(&mem[0x400 + head]);
                    write16(&mem[0x41a], 0x1e + ((head - 0x1c) & 0x1f));
                    return;
                case 0x01: // check for keystroke
                    if ((ZF = head == tail)) {
                        AX = 0; // empty
                    } else {
                        AX = read16(&mem[0x400 + head]);
                    }
                    return;
            }
            break;
        }
        case 0x1a: // time
            switch (AH) {
                case 0x00: // get system time
                    AL = mem[0x470];
                    CX = read16(&mem[0x46e]);
                    DX = read16(&mem[0x46c]);
                    mem[0x470] = 0;
                    return;
                case 0x01: // set system time
                    write16(&mem[0x46e], CX);
                    write16(&mem[0x46c], DX);
                    mem[0x470] = 0;
                    return;
                case 0x02: // get RTC time
                case 0x04: // get RTC date
                {
                    time_t t = time(NULL);
                    tm *lt = localtime(&t);
                    if (AH == 2) {
                        CH = bcd(lt->tm_hour);
                        CL = bcd(lt->tm_min);
                        DH = bcd(lt->tm_sec);
                        DL = lt->tm_isdst == 1 ? 1 : 0;
                    } else {
                        CH = bcd(lt->tm_year / 100 + 19);
                        CL = bcd(lt->tm_year % 100);
                        DH = bcd(lt->tm_mon + 1);
                        DL = bcd(lt->tm_mday);
                    }
                    CF = 0;
                    return;
                }
                case 0x03: // set RTC time
                case 0x05: // set RTC date
                    return; // ignore
            }
            break;
        case 0x1c: // timer handler
            return;
    }
    error("not implemented: int %02x,%02x", n, AH);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
pushq %rax
movl %edi, %esi
leal -0x8(%rsi), %eax
cmpl $0x14, %eax
ja 0x1f4d
leaq 0x57a7(%rip), %rcx      # 0x7004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movzwl 0x8daf(%rip), %eax      # 0xa61c
incw %ax
jne 0x1bf5
movzwl 0x8da1(%rip), %eax      # 0xa61e
incl %eax
cmpw $0x18, %ax
jb 0x18a0
movzwl %ax, %ecx
imull $0xaaab, %ecx, %ecx     # imm = 0xAAAB
shrl $0x11, %ecx
andl $-0x8, %ecx
leal (%rcx,%rcx,2), %ecx
subl %ecx, %eax
movb $0x1, 0x8d80(%rip)      # 0xa620
movb %al, 0x8d78(%rip)       # 0xa61e
xorl %eax, %eax
movb %al, 0x8d71(%rip)       # 0xa61f
movb %al, 0x8d68(%rip)       # 0xa61c
jmp 0x1bfe
movq 0x128930(%rip), %rax    # 0x12a1f0
movzbl (%rax), %eax
cmpq $0x5, %rax
ja 0x1f68
leaq 0x5784(%rip), %rcx      # 0x7058
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
xorl %edi, %edi
callq 0x1090
movq %rsp, %rdi
movq %rax, (%rdi)
callq 0x1070
movq 0x1288fa(%rip), %rcx    # 0x12a1f0
cmpb $0x2, (%rcx)
jne 0x1c3f
movslq 0x8(%rax), %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
orl %esi, %ecx
movq 0x1288cc(%rip), %rdx    # 0x12a1f8
movb %cl, (%rdx)
movslq 0x4(%rax), %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
orl %esi, %ecx
movq 0x12887d(%rip), %rdx    # 0x12a1d8
movb %cl, (%rdx)
movslq (%rax), %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
orl %esi, %ecx
movq 0x128877(%rip), %rdx    # 0x12a200
movb %cl, (%rdx)
cmpl $0x1, 0x20(%rax)
sete %al
jmp 0x1d33
movzwl 0x8c2c(%rip), %ebx      # 0xa5ca
movzwl 0x8c27(%rip), %ecx      # 0xa5cc
movq 0x128844(%rip), %rax    # 0x12a1f0
movzbl (%rax), %eax
cmpl $0x1, %eax
je 0x1c18
testl %eax, %eax
jne 0x1f68
cmpw %cx, %bx
jne 0x19d8
movl $0x9, %edi
callq 0x1841
cmpw 0x8bf6(%rip), %bx       # 0xa5cc
je 0x19c5
leaq 0x87d1(%rip), %rax      # 0xa1b0
movzwl 0x400(%rbx,%rax), %eax
movw %ax, 0x1287d2(%rip)     # 0x12a1c0
leal 0x4(%rbx), %eax
andb $0x1f, %al
addb $0x1e, %al
movb %al, 0x8bcf(%rip)       # 0xa5ca
movb $0x0, 0x8bc9(%rip)      # 0xa5cb
jmp 0x1d93
xorl %eax, %eax
cmpq $0x0, 0x128967(%rip)    # 0x12a378
setne %al
shll $0x6, %eax
incl %eax
movw %ax, 0x1287a0(%rip)     # 0x12a1c0
jmp 0x1d93
movw $0x280, 0x128792(%rip)  # imm = 0x280
jmp 0x1d93
callq 0x12c7
testl %eax, %eax
je 0x1d93
leaq 0x8699(%rip), %rbx      # 0xa0e0
leaq 0x8762(%rip), %r14      # 0xa1b0
movzwl 0x8b75(%rip), %eax      # 0xa5ca
addl $-0x1e, %eax
movzwl 0x8b6c(%rip), %r15d     # 0xa5cc
leal 0x4(%r15), %ebp
andl $0x1f, %ebp
cmpl %eax, %ebp
je 0x1d93
callq 0x127c
cmpl $-0x1, %eax
je 0x1aa5
movslq %eax, %rcx
movb (%rcx,%rbx), %cl
testb %cl, %cl
je 0x1aa5
movb %al, 0x400(%r15,%r14)
movb %cl, 0x401(%r15,%r14)
addb $0x1e, %bpl
movb %bpl, 0x8b2e(%rip)      # 0xa5cc
movb $0x0, 0x8b28(%rip)      # 0xa5cd
callq 0x12c7
testl %eax, %eax
jne 0x1a4e
jmp 0x1d93
movq 0x128736(%rip), %rax    # 0x12a1f0
movb (%rax), %al
cmpb $-0x78, %al
jne 0x1f68
movb $0x0, 0x12874d(%rip)    # 0x12a218
movw $0x0, 0x1286ec(%rip)    # 0x12a1c0
jmp 0x1d93
movq 0x128710(%rip), %rax    # 0x12a1f0
cmpb $0xe, (%rax)
jne 0x1d93
movq 0x1286e0(%rip), %rsi    # 0x12a1d0
movl $0x1, %edx
movl $0x1, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1030
movq 0x1286d0(%rip), %rax    # 0x12a1e0
movzbl (%rax), %eax
cmpq $0x1, %rax
ja 0x1bdf
leaq (%rax,%rax,2), %rax
leaq 0x128838(%rip), %rcx    # 0x12a360
movq (%rcx,%rax,8), %rdi
testq %rdi, %rdi
je 0x1bdf
leaq (%rcx,%rax,8), %r14
movq 0x1286b8(%rip), %rax    # 0x12a1f8
movq 0x128691(%rip), %rcx    # 0x12a1d8
movzbl (%rcx), %ecx
leal (,%rcx,4), %edx
movb (%rax), %dl
andl $0x3f, %ecx
movq 0x128693(%rip), %rax    # 0x12a1f0
movzbl (%rax), %r8d
cmpl $0x14, %r8d
jg 0x1d9e
cmpl $0x8, %r8d
ja 0x1eb2
movl 0x10(%r14), %r10d
movl 0x14(%r14), %r9d
movl %edx, %esi
imull %r10d, %esi
movq 0x128676(%rip), %r11    # 0x12a200
movzbl (%r11), %r11d
addl %r11d, %esi
imull %r9d, %esi
movl %r8d, %r8d
leaq 0x54d1(%rip), %rbx      # 0x7070
movslq (%rbx,%r8,4), %r8
addq %rbx, %r8
jmpq *%r8
cmpl 0xc(%r14), %edx
jge 0x1bd0
cmpl %r11d, %r10d
setle %dl
testl %ecx, %ecx
sete %r8b
orb %dl, %r8b
cmpl %r9d, %ecx
setg %dl
orb %r8b, %dl
cmpb $0x1, %dl
jne 0x1ebe
movb $0x1, 0x128641(%rip)    # 0x12a218
movb $0x4, (%rax)
jmp 0x1d93
movb $0x1, %al
movb %al, 0x128631(%rip)     # 0x12a218
movq 0x128602(%rip), %rcx    # 0x12a1f0
movb %al, (%rcx)
jmp 0x1d93
movb %al, 0x8a21(%rip)       # 0xa61c
shrl $0x8, %eax
movb %al, 0x8a19(%rip)       # 0xa61d
movl $0x1c, %edi
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x1f79
cmpw %cx, %bx
sete 0x1285f3(%rip)          # 0x12a215
jne 0x1c2b
xorl %eax, %eax
jmp 0x1a19
leaq 0x857e(%rip), %rax      # 0xa1b0
movzwl 0x400(%rbx,%rax), %eax
jmp 0x1a19
movslq 0x14(%rax), %rcx
imulq $0x51eb851f, %rcx, %rcx # imm = 0x51EB851F
movq %rcx, %rdx
sarq $0x25, %rdx
shrq $0x3f, %rcx
addl %edx, %ecx
addl $0x13, %ecx
movslq %ecx, %rcx
imulq $0x66666667, %rcx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subl %edx, %ecx
orl %esi, %ecx
movq 0x128572(%rip), %rdx    # 0x12a1f8
movb %cl, (%rdx)
movslq 0x14(%rax), %rcx
imulq $0x51eb851f, %rcx, %rdx # imm = 0x51EB851F
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x25, %rdx
addl %esi, %edx
imull $0x64, %edx, %edx
subl %edx, %ecx
movsbl %cl, %edx
imull $0x67, %edx, %edx
movzwl %dx, %edx
movswl %dx, %esi
shrl $0xf, %edx
shrl $0xa, %esi
addb %dl, %sil
movzbl %sil, %edx
shlb $0x4, %sil
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
subb %dl, %cl
orb %sil, %cl
movq 0x128505(%rip), %rdx    # 0x12a1d8
movb %cl, (%rdx)
movl 0x10(%rax), %ecx
leal 0x1(%rcx), %edx
movslq %edx, %rdx
imulq $0x66666667, %rdx, %rdx # imm = 0x66666667
movq %rdx, %rsi
shrq $0x3f, %rsi
sarq $0x22, %rdx
addl %esi, %edx
movl %edx, %esi
shll $0x4, %esi
addl %edx, %edx
leal (%rdx,%rdx,4), %edx
negl %edx
addl %edx, %ecx
incl %ecx
orl %esi, %ecx
movq 0x1284f5(%rip), %rdx    # 0x12a200
movb %cl, (%rdx)
movslq 0xc(%rax), %rax
imulq $0x66666667, %rax, %rcx # imm = 0x66666667
movq %rcx, %rdx
shrq $0x3f, %rdx
sarq $0x22, %rcx
addl %edx, %ecx
movl %ecx, %edx
shll $0x4, %edx
addl %ecx, %ecx
leal (%rcx,%rcx,4), %ecx
subl %ecx, %eax
orl %edx, %eax
movq 0x1284a6(%rip), %rcx    # 0x12a1e0
movb %al, (%rcx)
movb $0x0, 0x1284d5(%rip)    # 0x12a218
jmp 0x1d93
movzwl 0x128476(%rip), %eax    # 0x12a1c2
movw %ax, 0x88cb(%rip)       # 0xa61e
movzwl 0x12846a(%rip), %eax    # 0x12a1c4
movb %al, 0x88bc(%rip)       # 0xa61c
movb %ah, 0x88b7(%rip)       # 0xa61d
jmp 0x1d8c
movb 0x88b2(%rip), %al       # 0xa620
movq 0x12845b(%rip), %rcx    # 0x12a1d0
movb %al, (%rcx)
movd 0x889d(%rip), %xmm0     # 0xa61c
pshuflw $0xe1, %xmm0, %xmm0     # xmm0 = xmm0[1,0,2,3,4,5,6,7]
movd %xmm0, 0x128436(%rip)   # 0x12a1c2
movb $0x0, 0x888d(%rip)      # 0xa620
addq $0x8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x15, %r8d
je 0x1eaa
cmpl $0x17, %r8d
je 0x1d3c
cmpl $0x18, %r8d
jne 0x1eb2
incl %edx
movl %edx, 0xc(%r14)
movl %ecx, 0x14(%r14)
xorl %ecx, %ecx
movw %cx, 0x128403(%rip)     # 0x12a1ce
leaq 0x83de(%rip), %rdx      # 0xa1b0
movq %rdx, 0x128547(%rip)    # 0x12a320
movw %cx, 0x128548(%rip)     # 0x12a328
jmp 0x1de4
xorl %ecx, %ecx
movb %cl, 0x12842e(%rip)     # 0x12a218
movb %cl, (%rax)
jmp 0x1d93
shll $0x9, %esi
movslq %esi, %rsi
xorl %ebp, %ebp
xorl %edx, %edx
callq 0x10b0
cmpl $0x0, 0x14(%r14)
jle 0x1f37
leaq 0x128581(%rip), %rbx    # 0x12a390
movq (%r14), %rcx
movl $0x200, %esi            # imm = 0x200
movl $0x1, %edx
movq %rbx, %rdi
callq 0x1170
incl %ebp
cmpl 0x14(%r14), %ebp
jl 0x1e0f
jmp 0x1f37
xorl %eax, %eax
movw %ax, 0x128386(%rip)     # 0x12a1c0
movb 0x8(%r14), %cl
movq 0x1283a3(%rip), %rdx    # 0x12a1e8
movb %cl, (%rdx)
movb 0xc(%r14), %cl
decb %cl
movq 0x1283a4(%rip), %rdx    # 0x12a1f8
movb %cl, (%rdx)
movl 0xc(%r14), %ecx
shrl $0x2, %ecx
andl $0xc0, %ecx
orl 0x14(%r14), %ecx
movq 0x12836a(%rip), %rdx    # 0x12a1d8
movb %cl, (%rdx)
movb 0x10(%r14), %cl
decb %cl
movq 0x128383(%rip), %rdx    # 0x12a200
movb %cl, (%rdx)
movq 0x12835a(%rip), %rcx    # 0x12a1e0
movb $0x1, (%rcx)
movw %ax, 0x12833e(%rip)     # 0x12a1ce
leaq 0x8319(%rip), %rcx      # 0xa1b0
movq %rcx, 0x128482(%rip)    # 0x12a320
movw %ax, 0x128483(%rip)     # 0x12a328
jmp 0x1d3c
movb $0x1, (%rax)
jmp 0x1d3c
movb $0x1, 0x12835f(%rip)    # 0x12a218
jmp 0x1d93
leal (%rcx,%rsi), %eax
addl $0x7fffff, %eax         # imm = 0x7FFFFF
shll $0x9, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x10b0
movq 0x128316(%rip), %rax    # 0x12a1f0
movzwl 0x1282e5(%rip), %edi    # 0x12a1c6
movq 0x1282e8(%rip), %rcx    # 0x12a1d0
movzbl (%rcx), %edx
movq (%r14), %rcx
addq 0x12842b(%rip), %rdi    # 0x12a320
cmpb $0x2, (%rax)
jne 0x1f2d
movl $0x200, %esi            # imm = 0x200
callq 0x1150
testq %rax, %rax
jne 0x1f37
movzwl 0x1282b6(%rip), %edi    # 0x12a1c6
addq 0x128409(%rip), %rdi    # 0x12a320
movq 0x1282b2(%rip), %rax    # 0x12a1d0
movzbl (%rax), %edx
shll $0x9, %edx
xorl %esi, %esi
callq 0x1040
jmp 0x1f37
movl $0x200, %esi            # imm = 0x200
callq 0x1170
xorl %eax, %eax
movb %al, 0x1282d9(%rip)     # 0x12a218
movq 0x1282aa(%rip), %rcx    # 0x12a1f0
movb %al, (%rcx)
jmp 0x1d93
testl %esi, %esi
jne 0x1f5f
leaq 0x57cb(%rip), %rdi      # 0x7723
xorl %eax, %eax
callq 0x16c2
movq 0x12828a(%rip), %rax    # 0x12a1f0
movb (%rax), %al
movzbl %al, %edx
leaq 0x57c9(%rip), %rdi      # 0x773b
xorl %eax, %eax
callq 0x16c2