// File: /workspace/github_cpp_makefile_star2_O2/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, %rbx
movl %edi, %r14d
callq 0x1359
leal -0x4(%r14), %eax
cmpl $-0x2, %eax
jb 0x433d
movl %r14d, %r12d
decq %r12
leaq 0x12423b(%rip), %r15    # 0x128360
leaq 0x160b(%rip), %r14      # 0x5737
subq $0x1, %r12
jb 0x414f
movq 0x8(%rbx), %rsi
addq $0x8, %rbx
movq %r15, %rdi
callq 0x4af0
addq $0x18, %r15
testb %al, %al
jne 0x412c
jmp 0x4344
xorl %eax, %eax
pushq $0x1
popq %rcx
leaq 0x1240c5(%rip), %rdx    # 0x128220
cmpq $0x100, %rax            # imm = 0x100
je 0x4191
xorl %esi, %esi
movl %ecx, %edi
cmpl $0x100, %edi            # imm = 0x100
jge 0x4181
movl %edi, %r8d
andl %eax, %r8d
cmpl $0x1, %r8d
sbbb $-0x1, %sil
addl %edi, %edi
jmp 0x4167
notb %sil
andb $0x1, %sil
movb %sil, (%rax,%rdx)
incq %rax
jmp 0x415b
pushq $-0x20
popq %rax
leaq 0x124025(%rip), %rcx    # 0x1281c0
leaq 0x12402e(%rip), %rdx    # 0x1281d0
testq %rax, %rax
je 0x41bf
movq %rcx, 0x20(%rax,%rdx)
leaq 0x1(%rcx), %rsi
movq %rsi, 0x40(%rax,%rdx)
addq $0x8, %rax
addq $0x2, %rcx
jmp 0x41a2
xorl %ecx, %ecx
leaq 0x3fe8(%rip), %rax      # 0x81b0
cmpq $0x20, %rcx
je 0x41db
movb %cl, (%rax,%rcx,4)
movb $0x0, 0x1(%rax,%rcx,4)
incq %rcx
jmp 0x41c8
movq %rax, 0x12416e(%rip)    # 0x128350
andw $0x0, 0x12416e(%rip)    # 0x128358
movq %rax, 0x12414f(%rip)    # 0x128340
andw $0x0, 0x12414f(%rip)    # 0x128348
movq %rax, 0x124130(%rip)    # 0x128330
andw $0x0, 0x124130(%rip)    # 0x128338
movq %rax, 0x124111(%rip)    # 0x128320
andw $0x0, 0x124111(%rip)    # 0x128328
movw $0x7c00, 0x123f90(%rip) # imm = 0x7C00
movq 0x124139(%rip), %rcx    # 0x128360
leaq 0xbb82(%rip), %rdi      # 0xfdb0
pushq $0x1
popq %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x1150
movl $0x1e001e, 0x4385(%rip) # imm = 0x1E001E
pushq $0x4
popq %r15
movq $0x0, 0x10(%rsp)
movl $0x10000, %eax          # imm = 0x10000
movq %rax, 0x8(%rsp)
callq 0x1060
movq %rax, %r14
movl $0x1000, %ebp           # imm = 0x1000
movl $0x10000, %r12d         # imm = 0x10000
xorl %ebx, %ebx
movzwl 0x123f38(%rip), %eax    # 0x1281b0
movzwl 0x1240b9(%rip), %ecx    # 0x128338
orw %ax, %cx
je 0x4363
decl %r12d
jne 0x4328
callq 0x1060
movq %rax, %r12
cmpq %r14, %rax
jg 0x42b7
decl %r15d
jne 0x42b7
movq 0x8(%rsp), %rcx
leal (%rcx,%rcx), %eax
cmpl %ecx, %eax
cmovgl %eax, %ecx
movq %rcx, 0x8(%rsp)
jmp 0x4313
cmpq %r14, %r12
jle 0x4319
pushq $0x8
popq %rdi
callq 0x1e23
imull $0x36ee8, %ebx, %ecx    # imm = 0x36EE8
movl %ecx, %eax
cltd
movl $0xfffff000, %esi       # imm = 0xFFFFF000
idivl %esi
movl %eax, %r13d
incl %ebx
movl $0x36ee8, %eax          # imm = 0x36EE8
addl %eax, %ecx
movl %ecx, %eax
cltd
idivl %ebp
movl %eax, %r15d
cmpl %ebp, %ebx
movl $0x0, %eax
cmovel %eax, %ebx
callq 0x12be
testl %eax, %eax
je 0x4303
pushq $0x9
popq %rdi
callq 0x1e23
addl %r13d, %r15d
movslq %r15d, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
addq %rax, %r14
pushq $0x4
popq %r15
jmp 0x42b7
movzwl 0x123e90(%rip), %eax    # 0x1281b0
movq 0x8(%rsp), %rcx
movl %ecx, %r12d
movw %ax, 0x123e83(%rip)     # 0x1281b2
xorl %edi, %edi
xorl %esi, %esi
callq 0x1eb4
jmp 0x4271
leaq 0x13d4(%rip), %r14      # 0x5718
movq 0x3c95(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq (%rbx), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1130
pushq $0x1
popq %rax
movq %rax, 0x10(%rsp)
movq 0x10(%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq