// File: /workspace/github_cpp_makefile_star2_O1/7shi[P]8086run/v1/main.cpp
// Function: main
int main(int argc, char *argv[]) {
    inittty();

    if (argc < 2 || 3 < argc) {
        fprintf(stderr, "usage: %s fd1image [fd2image]\n", argv[0]);
        return 1;
    }
    for (int i = 1; i < argc; ++i) {
        if (!disks[i - 1].open(argv[i])) {
            fprintf(stderr, "can not open: %s\n", argv[i]);
            return 1;
        }
    }

    for (int i = 0; i < 256; ++i) {
        int n = 0;
        for (int j = 1; j < 256; j += j) {
            if (i & j) n++;
        }
        ptable[i] = (n & 1) == 0;
    }

    uint16_t tmp = 0x1234;
    uint8_t *p = (uint8_t *) r;
    if (*(uint8_t *) & tmp == 0x34) {
        for (int i = 0; i < 4; ++i) {
            r8[i] = p + i * 2;
            r8[i + 4] = r8[i] + 1;
        }
    } else {
        for (int i = 0; i < 4; ++i) {
            r8[i] = p + i * 2 + 1;
            r8[i + 4] = r8[i] - 1;
        }
    }

    for (int i = 0; i < 0x20; ++i) {
        write16(&mem[i << 2], i);
    }

    ES = CS = SS = DS = 0;
    IP = 0x7c00;
    if (fread(&mem[IP], 1, 512, disks[0].f)); // read MBR
    write16(&mem[0x41a], 0x1e); // keyboard queue head
    write16(&mem[0x41c], 0x1e); // keyboard queue tail
    int interval = 1 << 16, counter = interval, pitc = 0, trial = 4;
    clock_t next = clock();
    while (IP || *CS) {
        if (!--counter) {
            clock_t c = clock();
            if (c <= next && !--trial) {
                int old = interval;
                if ((interval <<= 1) < old) interval = old;
                trial = 4;
            }
            while (c > next) {
                intr(8);
                // 3600/65536 = 225/4096
                int t1 = pitc * 225000 / 4096;
                int t2 = (++pitc) * 225000 / 4096;
                if (pitc == 4096) pitc = 0;
                if (kbhit()) intr(9);
                next += CLOCKS_PER_SEC * (t2 - t1) / 1000;
                trial = 4;
            }
            counter = interval;
        }
        oldip = IP;
        step(0, NULL);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movl %edi, %r15d
callq 0x1390
leal -0x4(%r15), %eax
cmpl $-0x2, %eax
jb 0x59c6
xorl %ebp, %ebp
cmpl $0x2, %r15d
setge %bl
jl 0x57ac
movq 0x8(%r14), %rsi
leaq 0x123bfe(%rip), %rdi    # 0x129360
callq 0x6910
testb %al, %al
je 0x59d5
movl %r15d, %ebx
leaq 0x123bff(%rip), %r15    # 0x129378
movl $0x2, %r12d
cmpq %r12, %rbx
je 0x57a9
movq (%r14,%r12,8), %rsi
movq %r15, %rdi
callq 0x6910
addq $0x18, %r15
incq %r12
testb %al, %al
jne 0x577f
decq %r12
cmpq %rbx, %r12
setb %bl
jmp 0x59db
seta %bl
testb %bl, %bl
jne 0x59b5
xorl %eax, %eax
leaq 0x123a63(%rip), %rcx    # 0x129220
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
andl %eax, %edi
cmpl $0x1, %edi
sbbl $-0x1, %edx
leal (%rsi,%rsi), %edi
cmpl $0x80, %esi
movl %edi, %esi
jl 0x57c4
notb %dl
andb $0x1, %dl
movb %dl, (%rax,%rcx)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x57bd
leaq 0x1239cb(%rip), %rax    # 0x1291c0
movq $-0x4, %rcx
leaq 0x1239cd(%rip), %rdx    # 0x1291d0
movq %rax, 0x20(%rdx,%rcx,8)
leaq 0x1(%rax), %rsi
movq %rsi, 0x40(%rdx,%rcx,8)
addq $0x2, %rax
incq %rcx
jne 0x5803
xorl %ecx, %ecx
leaq 0x398d(%rip), %rax      # 0x91b0
movb %cl, (%rax,%rcx,4)
movb $0x0, 0x1(%rax,%rcx,4)
incq %rcx
cmpq $0x20, %rcx
jne 0x5823
movq %rax, 0x123b15(%rip)    # 0x129350
xorl %ecx, %ecx
movw %cx, 0x123b14(%rip)     # 0x129358
movq %rax, 0x123af5(%rip)    # 0x129340
movw %cx, 0x123af6(%rip)     # 0x129348
movq %rax, 0x123ad7(%rip)    # 0x129330
movw %cx, 0x123ad8(%rip)     # 0x129338
movq %rax, 0x123ab9(%rip)    # 0x129320
movw %cx, 0x123aba(%rip)     # 0x129328
movw $0x7c00, 0x123939(%rip) # imm = 0x7C00
movq 0x123ae2(%rip), %rcx    # 0x129360
leaq 0xb52b(%rip), %rdi      # 0x10db0
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1150
movl $0x1e001e, 0x3d2c(%rip) # imm = 0x1E001E
callq 0x1060
movq %rax, %r14
movzwl 0x123a8b(%rip), %eax    # 0x129338
orw 0x1238fc(%rip), %ax     # 0x1291b0
je 0x59b5
movl %ebp, 0xc(%rsp)
movl $0x10000, %r15d         # imm = 0x10000
movl $0x4, %ebx
movl $0x36ee8, %r13d         # imm = 0x36EE8
xorl %ebp, %ebp
movl $0x10000, %r12d         # imm = 0x10000
decl %r15d
jne 0x5986
callq 0x1060
movq %rax, %r15
cmpq %r14, %rax
jg 0x5901
decl %ebx
jne 0x5901
leal (%r12,%r12), %eax
cmpl %r12d, %eax
cmovgl %eax, %r12d
movl $0x4, %ebx
cmpq %r14, %r15
jle 0x5983
movq %r12, 0x10(%rsp)
movl $0x8, %edi
callq 0x1fb0
imull $0x36ee8, %ebp, %ebx    # imm = 0x36EE8
leal 0xfff(%rbx), %r12d
testl %ebx, %ebx
cmovnsl %ebx, %r12d
incl %ebp
leal (%rbx,%r13), %eax
addl $0x37ee7, %ebx          # imm = 0x37EE7
testl %eax, %eax
cmovnsl %eax, %ebx
cmpl $0x1000, %ebp           # imm = 0x1000
movl $0x0, %eax
cmovel %eax, %ebp
callq 0x12ed
testl %eax, %eax
je 0x595a
movl $0x9, %edi
callq 0x1fb0
sarl $0xc, %r12d
negl %r12d
sarl $0xc, %ebx
addl %r12d, %ebx
movslq %ebx, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
addq %rax, %r14
cmpq %r14, %r15
jg 0x590b
movl $0x4, %ebx
movq 0x10(%rsp), %r12
movl %r12d, %r15d
movzwl 0x123823(%rip), %eax    # 0x1291b0
movw %ax, 0x12381e(%rip)     # 0x1291b2
xorl %edi, %edi
xorl %esi, %esi
callq 0x20e8
movzwl 0x123994(%rip), %eax    # 0x129338
orw 0x123805(%rip), %ax     # 0x1291b0
jne 0x58d7
movl 0xc(%rsp), %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
callq 0x1190
movl $0x1, %ebp
jmp 0x59b5
movl $0x1, %r12d
movq 0x35fe(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq (%r14,%r12,8), %rdx
leaq 0x1d3f(%rip), %rsi      # 0x772f
xorl %eax, %eax
callq 0x1130
movl $0x1, %ebp
jmp 0x57ac