// File: /workspace/github_cpp_makefile_star2_O3/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 0x136a
leal -0x4(%r14), %eax
cmpl $-0x2, %eax
jb 0x5d41
movl %r14d, %r12d
addq $0x8, %rbx
decq %r12
leaq 0x124895(%rip), %r15    # 0x12a360
leaq 0x1cc5(%rip), %r14      # 0x7797
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x6a02
testb %al, %al
je 0x5d48
addq $0x8, %rbx
addq $0x18, %r15
decq %r12
jne 0x5ad2
xorl %eax, %eax
leaq 0x124725(%rip), %rcx    # 0x12a220
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
andl %eax, %edi
cmpl $0x1, %edi
sbbb $-0x1, %dl
leal (%rsi,%rsi), %edi
cmpl $0x80, %esi
movl %edi, %esi
jl 0x5b02
notb %dl
andb $0x1, %dl
movb %dl, (%rax,%rcx)
incq %rax
cmpq $0x100, %rax            # imm = 0x100
jne 0x5afb
leaq 0x12468d(%rip), %rax    # 0x12a1c0
movq %rax, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
movdqa 0x1a4b(%rip), %xmm1     # 0x7590
paddq %xmm0, %xmm1
movdqa %xmm1, 0x12467f(%rip)   # 0x12a1d0
movdqa 0x1a47(%rip), %xmm1     # 0x75a0
paddq %xmm0, %xmm1
movdqa %xmm1, 0x12468b(%rip)   # 0x12a1f0
movdqa 0x1a43(%rip), %xmm1     # 0x75b0
paddq %xmm0, %xmm1
paddq 0x1a47(%rip), %xmm0     # 0x75c0
movdqa %xmm1, 0x12465f(%rip)   # 0x12a1e0
movdqa %xmm0, 0x124677(%rip)   # 0x12a200
xorl %ecx, %ecx
leaq 0x461e(%rip), %rax      # 0xa1b0
movb %cl, (%rax,%rcx,4)
movb $0x0, 0x1(%rax,%rcx,4)
incq %rcx
cmpq $0x20, %rcx
jne 0x5b92
movq %rax, 0x1247a6(%rip)    # 0x12a350
xorl %r14d, %r14d
movw %r14w, 0x1247a3(%rip)   # 0x12a358
movq %rax, 0x124784(%rip)    # 0x12a340
movw %r14w, 0x124784(%rip)   # 0x12a348
movq %rax, 0x124765(%rip)    # 0x12a330
movw %r14w, 0x124765(%rip)   # 0x12a338
movq %rax, 0x124746(%rip)    # 0x12a320
movw %r14w, 0x124746(%rip)   # 0x12a328
movw $0x7c00, 0x1245c5(%rip) # imm = 0x7C00
movq 0x12476e(%rip), %rcx    # 0x12a360
leaq 0xc1b7(%rip), %rdi      # 0x11db0
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x1150
movl $0x1e001e, 0x49b8(%rip) # imm = 0x1E001E
callq 0x1060
movq %rax, %rbx
movzwl 0x124717(%rip), %eax    # 0x12a338
orw 0x124588(%rip), %ax     # 0x12a1b0
je 0x5d65
movl $0x10000, %r13d         # imm = 0x10000
movl $0x4, 0xc(%rsp)
movl $0x36ee8, %r15d         # imm = 0x36EE8
xorl %r12d, %r12d
movl $0x10000, %ebp          # imm = 0x10000
decl %r13d
jne 0x5d11
callq 0x1060
cmpq %rbx, %rax
jle 0x5cec
movq %rax, %r14
movq %rbp, 0x10(%rsp)
movl $0x8, %edi
callq 0x1f79
imull $0x36ee8, %r12d, %r13d  # imm = 0x36EE8
leal 0xfff(%r13), %ebp
testl %r13d, %r13d
cmovnsl %r13d, %ebp
incl %r12d
leal (%r15,%r13), %eax
addl $0x37ee7, %r13d         # imm = 0x37EE7
testl %eax, %eax
cmovnsl %eax, %r13d
cmpl $0x1000, %r12d          # imm = 0x1000
movl $0x0, %eax
cmovel %eax, %r12d
callq 0x12c7
testl %eax, %eax
je 0x5cbf
movl $0x9, %edi
callq 0x1f79
sarl $0xc, %ebp
negl %ebp
sarl $0xc, %r13d
addl %ebp, %r13d
movslq %r13d, %rax
imulq $0x3e8, %rax, %rax      # imm = 0x3E8
addq %rax, %rbx
cmpq %rbx, %r14
jg 0x5c69
movl $0x4, 0xc(%rsp)
movq 0x10(%rsp), %rbp
jmp 0x5d0e
movl 0xc(%rsp), %eax
decl %eax
je 0x5cfa
movl %eax, 0xc(%rsp)
jmp 0x5d0e
leal (,%rbp,2), %eax
cmpl %ebp, %eax
cmovgl %eax, %ebp
movl $0x4, 0xc(%rsp)
movl %ebp, %r13d
movzwl 0x124498(%rip), %eax    # 0x12a1b0
movw %ax, 0x124493(%rip)     # 0x12a1b2
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x20b8
movzwl 0x124606(%rip), %eax    # 0x12a338
orw 0x124477(%rip), %ax     # 0x12a1b0
jne 0x5c4a
jmp 0x5d65
leaq 0x1a30(%rip), %r14      # 0x7778
movq 0x4291(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movq (%rbx), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1130
movl $0x1, %r14d
movl %r14d, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq