// File: 7shi[P]8086run/v2/bios.cpp
// Function: bios(int)
void bios(int n) {
    void intr(int);
    static uint8_t buf[512];
    static FILE *fhcopy;
    static bool hread;
    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;
                uint16_t scanasc = decodeKey(getch());
                if (scanasc) {
                    write16(&mem[0x41e + tail], scanasc);
                    write16(&mem[0x41c], 0x1e + next);
                }
            }
            return;
        case 0x10: // video
            switch (AH) {
                case 0x00:
#ifdef _WIN32
                    system("cls");
#else
                    printf("\x1b[H\x1b[J");
                    fflush(stdout);
#endif
                    cleared = true;
                    return;
                case 0x02:
                {
#ifdef _WIN32
                    HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
                    COORD pos = {DL, DH};
                    SetConsoleCursorPosition(hStd, pos);
#else
                    printf("\x1b[%d;%dH", DH + 1, DL + 1);
                    fflush(stdout);
#endif
                    return;
                }
                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
        {
            switch (AH) { // ORIGINAL EXTENSIONS
                case 0xfe: // hopen
                    if (fhcopy) fclose(fhcopy);
                    hread = AL != 1;
                    CF = !(fhcopy = fopen((char *) &DS[DX], hread ? "rb" : "wb"));
                    return;
                case 0xff: // hcopy
                    if (fhcopy) {
                        if (hread) {
                            CX = fread(&DS[DX], 1, 512, fhcopy);
                        } else {
                            CX = fwrite(&DS[DX], 1, CX, fhcopy);
                        }
                        if (!CX) {
                            fclose(fhcopy);
                            fhcopy = NULL;
                        }
                    } else {
                        CX = 0;
                    }
                    return;
            }
            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 0x14: // serial port
            CF = 1; // error
            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 0x17: // parallel port
            CF = 1; // error
            return;
        case 0x18: // boot fault
            error("Boot failed (INT 18H)");
            return;
        case 0x19: // bootstrap
            error("Bootstrap (INT 19H) not implemented");
            return;
        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 0x1b: // Control-Break handler
            return;
        case 0x1c: // timer handler
            return;
    }
    error("not implemented: int %02x,%02x", n, AH);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movl %edi, -0x4(%rbp)
movl -0x4(%rbp), %eax
movq %rax, -0x50(%rbp)
subq $0x1c, %rax
ja 0x3101
movq -0x50(%rbp), %rax
leaq 0x7b1a(%rip), %rcx      # 0xa004
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x7bfe(%rip), %rdi      # 0xa0f8
movb $0x0, %al
callq 0x3690
jmp 0x3101
leaq 0xaf33(%rip), %rdi      # 0xd440
addq $0x46c, %rdi            # imm = 0x46C
callq 0x31c0
movw %ax, -0x6(%rbp)
movw -0x6(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x6(%rbp)
cmpw $0x0, %ax
jne 0x2592
leaq 0xaf0a(%rip), %rdi      # 0xd440
addq $0x46e, %rdi            # imm = 0x46E
callq 0x31c0
movw %ax, -0x8(%rbp)
movw -0x8(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x8(%rbp)
movzwl %ax, %eax
cmpl $0x18, %eax
jl 0x257b
movzwl -0x8(%rbp), %eax
movl $0x18, %ecx
cltd
idivl %ecx
movw %dx, %ax
movw %ax, -0x8(%rbp)
leaq 0xaecc(%rip), %rax      # 0xd440
movb $0x1, 0x470(%rax)
leaq 0xaebe(%rip), %rdi      # 0xd440
addq $0x46e, %rdi            # imm = 0x46E
movzwl -0x8(%rbp), %esi
callq 0x31e0
leaq 0xaea7(%rip), %rdi      # 0xd440
addq $0x46c, %rdi            # imm = 0x46C
movzwl -0x6(%rbp), %esi
callq 0x31e0
movl $0x1c, %edi
callq 0x37c0
jmp 0x3120
jmp 0x25ba
callq 0x8e70
cmpl $0x0, %eax
je 0x266b
leaq 0xae71(%rip), %rdi      # 0xd440
addq $0x41a, %rdi            # imm = 0x41A
callq 0x31c0
movzwl %ax, %eax
subl $0x1e, %eax
movl %eax, -0xc(%rbp)
leaq 0xae55(%rip), %rdi      # 0xd440
addq $0x41c, %rdi            # imm = 0x41C
callq 0x31c0
movzwl %ax, %eax
subl $0x1e, %eax
movl %eax, -0x10(%rbp)
movl -0x10(%rbp), %eax
addl $0x2, %eax
andl $0x1f, %eax
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
cmpl -0xc(%rbp), %eax
jne 0x2616
jmp 0x266b
callq 0x8e00
movl %eax, %edi
callq 0x8570
movw %ax, -0x16(%rbp)
cmpw $0x0, -0x16(%rbp)
je 0x2666
movl -0x10(%rbp), %eax
addl $0x41e, %eax            # imm = 0x41E
cltq
leaq 0xae02(%rip), %rdi      # 0xd440
addq %rax, %rdi
movzwl -0x16(%rbp), %esi
callq 0x31e0
movl -0x14(%rbp), %eax
addl $0x1e, %eax
leaq 0xade9(%rip), %rdi      # 0xd440
addq $0x41c, %rdi            # imm = 0x41C
movzwl %ax, %esi
callq 0x31e0
jmp 0x25ba
jmp 0x3120
leaq 0x12ade9(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x54(%rbp)
testl %eax, %eax
je 0x269e
jmp 0x2687
movl -0x54(%rbp), %eax
subl $0x2, %eax
je 0x26ca
jmp 0x2691
movl -0x54(%rbp), %eax
subl $0xe, %eax
je 0x270e
jmp 0x2734
leaq 0x7a6b(%rip), %rdi      # 0xa110
movb $0x0, %al
callq 0x2030
movq 0xa90d(%rip), %rax      # 0xcfc0
movq (%rax), %rdi
callq 0x2140
leaq 0x12aef0(%rip), %rax    # 0x12d5b2
movb $0x1, (%rax)
jmp 0x3120
leaq 0x12ad8f(%rip), %rax    # 0x12d460
movq 0x30(%rax), %rax
movzbl (%rax), %esi
addl $0x1, %esi
leaq 0x12ad7e(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movzbl (%rax), %edx
addl $0x1, %edx
leaq 0x7a24(%rip), %rdi      # 0xa117
movb $0x0, %al
callq 0x2030
movq 0xa8bf(%rip), %rax      # 0xcfc0
movq (%rax), %rdi
callq 0x2140
jmp 0x3120
leaq 0x12ad4b(%rip), %rax    # 0x12d460
movq (%rax), %rsi
movl $0x1, %edi
movl $0x1, %edx
callq 0x2060
cmpq $0x0, %rax
je 0x272f
jmp 0x272f
jmp 0x3120
jmp 0x3120
movq 0xaad8(%rip), %rdx      # 0xd218
movl $0x1, %eax
movl $0x41, %ecx
cmpq $0x0, %rdx
cmovnel %ecx, %eax
movw %ax, %cx
leaq 0x12acf5(%rip), %rax    # 0x12d450
movw %cx, (%rax)
jmp 0x3120
leaq 0x12ace6(%rip), %rax    # 0x12d450
movw $0x280, (%rax)          # imm = 0x280
jmp 0x3120
leaq 0x12ace5(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x58(%rbp)
subl $0xfe, %eax
je 0x27a1
jmp 0x278e
movl -0x58(%rbp), %eax
subl $0xff, %eax
je 0x2843
jmp 0x2921
cmpq $0x0, 0xac87(%rip)      # 0xd430
je 0x27b7
movq 0xac7e(%rip), %rdi      # 0xd430
callq 0x2100
leaq 0x12aca2(%rip), %rax    # 0x12d460
movq (%rax), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
setne %al
andb $0x1, %al
movb %al, 0xac66(%rip)       # 0xd438
leaq 0x12ac77(%rip), %rax    # 0x12d450
movzwl 0x4(%rax), %esi
leaq 0x12addc(%rip), %rdi    # 0x12d5c0
addq $0x30, %rdi
callq 0x3210
movq %rax, -0x60(%rbp)
testb $0x1, 0xac40(%rip)      # 0xd438
je 0x2807
leaq 0x791f(%rip), %rax      # 0xa120
movq %rax, -0x68(%rbp)
jmp 0x2814
leaq 0x7915(%rip), %rax      # 0xa123
movq %rax, -0x68(%rbp)
jmp 0x2814
movq -0x60(%rbp), %rdi
movq -0x68(%rbp), %rsi
callq 0x2150
movq %rax, 0xac08(%rip)      # 0xd430
cmpq $0x0, %rax
setne %cl
xorb $-0x1, %cl
andb $0x1, %cl
leaq 0x12ac6c(%rip), %rax    # 0x12d4a8
movb %cl, (%rax)
jmp 0x3120
cmpq $0x0, 0xabe5(%rip)      # 0xd430
je 0x290f
testb $0x1, 0xabe0(%rip)      # 0xd438
je 0x289e
leaq 0x12abef(%rip), %rax    # 0x12d450
movzwl 0x4(%rax), %esi
leaq 0x12ad54(%rip), %rdi    # 0x12d5c0
addq $0x30, %rdi
callq 0x3210
movq %rax, %rdi
movq 0xabb1(%rip), %rcx      # 0xd430
movl $0x1, %esi
movl $0x200, %edx            # imm = 0x200
callq 0x2210
movw %ax, %cx
leaq 0x12abb8(%rip), %rax    # 0x12d450
movw %cx, 0x2(%rax)
jmp 0x28e8
leaq 0x12abab(%rip), %rax    # 0x12d450
movzwl 0x4(%rax), %esi
leaq 0x12ad10(%rip), %rdi    # 0x12d5c0
addq $0x30, %rdi
callq 0x3210
movq %rax, %rdi
leaq 0x12ab8d(%rip), %rax    # 0x12d450
movzwl 0x2(%rax), %eax
movl %eax, %edx
movq 0xab60(%rip), %rcx      # 0xd430
movl $0x1, %esi
callq 0x2280
movw %ax, %cx
leaq 0x12ab6c(%rip), %rax    # 0x12d450
movw %cx, 0x2(%rax)
leaq 0x12ab61(%rip), %rax    # 0x12d450
cmpw $0x0, 0x2(%rax)
jne 0x290d
movq 0xab33(%rip), %rdi      # 0xd430
callq 0x2100
movq $0x0, 0xab23(%rip) # 0xd430
jmp 0x291c
leaq 0x12ab3a(%rip), %rax    # 0x12d450
movw $0x0, 0x2(%rax)
jmp 0x3120
leaq 0x12ab38(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movzbl (%rax), %eax
cmpl $0x1, %eax
jg 0x2958
leaq 0x12ab25(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movzbl (%rax), %eax
movl %eax, %ecx
leaq 0xa8b5(%rip), %rax      # 0xd200
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jne 0x2975
leaq 0x12ab49(%rip), %rax    # 0x12d4a8
movb $0x1, (%rax)
leaq 0x12aaf7(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x1, (%rax)
jmp 0x3120
leaq 0x12aae4(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rcx
movzbl (%rcx), %ecx
leaq (%rcx,%rcx,2), %rdx
leaq 0xa872(%rip), %rcx      # 0xd200
leaq (%rcx,%rdx,8), %rcx
movq %rcx, -0x20(%rbp)
movq 0x8(%rax), %rcx
movq 0x28(%rax), %rdx
movzbl (%rdx), %edx
movzbl (%rcx), %esi
andl $-0x40, %esi
movl %edx, %ecx
movl %esi, %edx
leal (%rcx,%rdx,4), %ecx
movl %ecx, -0x24(%rbp)
movq 0x8(%rax), %rcx
movzbl (%rcx), %ecx
andl $0x3f, %ecx
decl %ecx
movl %ecx, -0x28(%rbp)
movq -0x20(%rbp), %rcx
movl 0x10(%rcx), %edx
movl 0x14(%rcx), %ecx
movl -0x24(%rbp), %esi
imull %esi, %edx
movq 0x30(%rax), %rsi
movzbl (%rsi), %esi
addl %esi, %edx
imull %edx, %ecx
movl -0x28(%rbp), %edx
addl %edx, %ecx
movl %ecx, -0x2c(%rbp)
movq 0x20(%rax), %rax
movzbl (%rax), %eax
movq %rax, -0x70(%rbp)
subq $0x18, %rax
ja 0x2d17
movq -0x70(%rbp), %rax
leaq 0x768c(%rip), %rcx      # 0xa090
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x12aa94(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
leaq 0x12aa42(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x0, (%rax)
jmp 0x3120
movl -0x24(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0xc(%rcx), %eax
jge 0x2a5f
leaq 0x12aa23(%rip), %rax    # 0x12d460
movq 0x30(%rax), %rax
movzbl (%rax), %eax
movq -0x20(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jge 0x2a5f
cmpl $0x0, -0x28(%rbp)
jl 0x2a5f
movl -0x28(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jl 0x2a7c
leaq 0x12aa42(%rip), %rax    # 0x12d4a8
movb $0x1, (%rax)
leaq 0x12a9f0(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x4, (%rax)
jmp 0x3120
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %eax
shll $0x9, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2120
leaq 0x12a9c6(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
cmpl $0x2, %eax
jne 0x2b1c
leaq 0x12a9a3(%rip), %rax    # 0x12d450
movzwl 0x6(%rax), %esi
leaq 0x12ab08(%rip), %rdi    # 0x12d5c0
callq 0x3210
movq %rax, %rdi
leaq 0x12a999(%rip), %rax    # 0x12d460
movq (%rax), %rax
movzbl (%rax), %eax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x200, %esi            # imm = 0x200
callq 0x2210
cmpq $0x1, %rax
jae 0x2b1a
leaq 0x12a963(%rip), %rax    # 0x12d450
movzwl 0x6(%rax), %esi
leaq 0x12aac8(%rip), %rdi    # 0x12d5c0
callq 0x3210
movq %rax, %rdi
leaq 0x12a959(%rip), %rax    # 0x12d460
movq (%rax), %rax
movzbl (%rax), %eax
shll $0x9, %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x2070
jmp 0x2b56
leaq 0x12a92d(%rip), %rax    # 0x12d450
movzwl 0x6(%rax), %esi
leaq 0x12aa92(%rip), %rdi    # 0x12d5c0
callq 0x3210
movq %rax, %rdi
leaq 0x12a923(%rip), %rax    # 0x12d460
movq (%rax), %rax
movzbl (%rax), %eax
movl %eax, %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movl $0x200, %esi            # imm = 0x200
callq 0x2280
leaq 0x12a94b(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
leaq 0x12a8f9(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x0, (%rax)
jmp 0x3120
movq -0x20(%rbp), %rax
movq (%rax), %rdi
movl -0x2c(%rbp), %eax
subl -0x28(%rbp), %eax
shll $0x9, %eax
movslq %eax, %rsi
xorl %edx, %edx
callq 0x2120
movl $0x0, -0x30(%rbp)
movl -0x30(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x14(%rcx), %eax
jge 0x2bc8
movq -0x20(%rbp), %rax
movq (%rax), %rcx
leaq 0xa682(%rip), %rdi      # 0xd230
movl $0x200, %esi            # imm = 0x200
movl $0x1, %edx
callq 0x2280
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2b94
leaq 0x12a8d9(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
leaq 0x12a887(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x0, (%rax)
jmp 0x3120
leaq 0x12a864(%rip), %rax    # 0x12d450
movw $0x0, (%rax)
movq -0x20(%rbp), %rax
movl 0x8(%rax), %eax
movb %al, %cl
leaq 0x12a85f(%rip), %rax    # 0x12d460
movq 0x18(%rax), %rax
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
subl $0x1, %eax
movb %al, %cl
leaq 0x12a846(%rip), %rax    # 0x12d460
movq 0x28(%rax), %rax
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl 0x14(%rax), %eax
movq -0x20(%rbp), %rcx
movl 0xc(%rcx), %ecx
sarl $0x2, %ecx
andl $0xc0, %ecx
orl %ecx, %eax
movb %al, %cl
leaq 0x12a81e(%rip), %rax    # 0x12d460
movq 0x8(%rax), %rax
movb %cl, (%rax)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movb %al, %cl
leaq 0x12a805(%rip), %rax    # 0x12d460
movq 0x30(%rax), %rax
movb %cl, (%rax)
leaq 0x12a7f8(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movb $0x1, (%rax)
leaq 0x12a7da(%rip), %rax    # 0x12d450
movw $0x0, 0xe(%rax)
leaq 0x12a93d(%rip), %rdi    # 0x12d5c0
xorl %esi, %esi
callq 0x3230
leaq 0x12a817(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
jmp 0x3120
leaq 0x12a7c0(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x1, (%rax)
leaq 0x12a7fa(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
jmp 0x3120
leaq 0x12a7eb(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
jmp 0x3120
movl -0x24(%rbp), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0xc(%rax)
movl -0x28(%rbp), %ecx
addl $0x1, %ecx
movq -0x20(%rbp), %rax
movl %ecx, 0x14(%rax)
leaq 0x12a76a(%rip), %rax    # 0x12d450
movw $0x0, 0xe(%rax)
leaq 0x12a8cd(%rip), %rdi    # 0x12d5c0
xorl %esi, %esi
callq 0x3230
leaq 0x12a7a7(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
leaq 0x12a755(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb $0x0, (%rax)
jmp 0x3120
leaq 0x12a78a(%rip), %rax    # 0x12d4a8
movb $0x1, (%rax)
jmp 0x3120
leaq 0x12a77b(%rip), %rax    # 0x12d4a8
movb $0x1, (%rax)
jmp 0x3120
leaq 0x12a724(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movb (%rax), %al
subb $-0x78, %al
jne 0x2d63
jmp 0x2d48
leaq 0x12a759(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
leaq 0x12a6f7(%rip), %rax    # 0x12d450
movw $0x0, (%rax)
jmp 0x3120
jmp 0x3101
leaq 0xa6d1(%rip), %rdi      # 0xd440
movq %rdi, -0x80(%rbp)
addq $0x41a, %rdi            # imm = 0x41A
callq 0x31c0
movq -0x80(%rbp), %rdi
movzwl %ax, %eax
movl %eax, -0x34(%rbp)
addq $0x41c, %rdi            # imm = 0x41C
callq 0x31c0
movzwl %ax, %eax
movl %eax, -0x38(%rbp)
leaq 0x12a6be(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
movl %eax, -0x74(%rbp)
testl %eax, %eax
je 0x2dc3
jmp 0x2db2
movl -0x74(%rbp), %eax
subl $0x1, %eax
je 0x2e3f
jmp 0x2e95
jmp 0x2dc5
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
jne 0x2df2
movl $0x9, %edi
callq 0x24c0
leaq 0xa662(%rip), %rdi      # 0xd440
addq $0x41c, %rdi            # imm = 0x41C
callq 0x31c0
movzwl %ax, %eax
movl %eax, -0x38(%rbp)
jmp 0x2dc5
movl -0x34(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
cltq
leaq 0xa63d(%rip), %rdi      # 0xd440
addq %rax, %rdi
callq 0x31c0
movw %ax, %cx
leaq 0x12a63b(%rip), %rax    # 0x12d450
movw %cx, (%rax)
movl -0x34(%rbp), %eax
subl $0x1c, %eax
andl $0x1f, %eax
addl $0x1e, %eax
leaq 0xa615(%rip), %rdi      # 0xd440
addq $0x41a, %rdi            # imm = 0x41A
movzwl %ax, %esi
callq 0x31e0
jmp 0x3120
movl -0x34(%rbp), %eax
cmpl -0x38(%rbp), %eax
sete %al
movb %al, %dl
andb $0x1, %dl
leaq 0x12a651(%rip), %rcx    # 0x12d4a5
movb %dl, (%rcx)
testb $0x1, %al
jne 0x2e5c
jmp 0x2e6a
leaq 0x12a5ed(%rip), %rax    # 0x12d450
movw $0x0, (%rax)
jmp 0x2e90
movl -0x34(%rbp), %eax
addl $0x400, %eax            # imm = 0x400
cltq
leaq 0xa5c5(%rip), %rdi      # 0xd440
addq %rax, %rdi
callq 0x31c0
movw %ax, %cx
leaq 0x12a5c3(%rip), %rax    # 0x12d450
movw %cx, (%rax)
jmp 0x3120
jmp 0x3101
leaq 0x12a607(%rip), %rax    # 0x12d4a8
movb $0x1, (%rax)
jmp 0x3120
leaq 0x7276(%rip), %rdi      # 0xa126
movb $0x0, %al
callq 0x3690
jmp 0x3120
leaq 0x7279(%rip), %rdi      # 0xa13c
movb $0x0, %al
callq 0x3690
jmp 0x3120
leaq 0x12a58a(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
movq %rax, -0x88(%rbp)
subq $0x5, %rax
ja 0x30fb
movq -0x88(%rbp), %rax
leaq 0x717c(%rip), %rcx      # 0xa078
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0xa534(%rip), %rax      # 0xd440
movb 0x470(%rax), %cl
leaq 0x12a547(%rip), %rax    # 0x12d460
movq (%rax), %rax
movb %cl, (%rax)
leaq 0xa51b(%rip), %rdi      # 0xd440
addq $0x46e, %rdi            # imm = 0x46E
callq 0x31c0
movw %ax, %cx
leaq 0x12a515(%rip), %rax    # 0x12d450
movw %cx, 0x2(%rax)
leaq 0xa4fa(%rip), %rdi      # 0xd440
addq $0x46c, %rdi            # imm = 0x46C
callq 0x31c0
movw %ax, %cx
leaq 0x12a4f4(%rip), %rax    # 0x12d450
movw %cx, 0x4(%rax)
leaq 0xa4d9(%rip), %rax      # 0xd440
movb $0x0, 0x470(%rax)
jmp 0x3120
leaq 0xa4c6(%rip), %rdi      # 0xd440
addq $0x46e, %rdi            # imm = 0x46E
leaq 0x12a4c8(%rip), %rax    # 0x12d450
movzwl 0x2(%rax), %esi
callq 0x31e0
leaq 0xa4a8(%rip), %rdi      # 0xd440
addq $0x46c, %rdi            # imm = 0x46C
leaq 0x12a4aa(%rip), %rax    # 0x12d450
movzwl 0x4(%rax), %esi
callq 0x31e0
leaq 0xa48a(%rip), %rax      # 0xd440
movb $0x0, 0x470(%rax)
jmp 0x3120
xorl %eax, %eax
movl %eax, %edi
callq 0x20f0
movq %rax, -0x40(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x20b0
movq %rax, -0x48(%rbp)
leaq 0x12a47d(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %eax
cmpl $0x2, %eax
jne 0x3067
movq -0x48(%rbp), %rax
movl 0x8(%rax), %edi
callq 0x3130
movb %al, %cl
leaq 0x12a45c(%rip), %rax    # 0x12d460
movq 0x28(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl 0x4(%rax), %edi
callq 0x3130
movb %al, %cl
leaq 0x12a441(%rip), %rax    # 0x12d460
movq 0x8(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl (%rax), %edi
callq 0x3130
movb %al, %cl
leaq 0x12a427(%rip), %rax    # 0x12d460
movq 0x30(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl 0x20(%rax), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x1, %edx
cmovel %ecx, %eax
movb %al, %cl
leaq 0x12a404(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movb %cl, (%rax)
jmp 0x30ed
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %eax, %edi
addl $0x13, %edi
callq 0x3130
movb %al, %cl
leaq 0x12a3d7(%rip), %rax    # 0x12d460
movq 0x28(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl 0x14(%rax), %eax
movl $0x64, %ecx
cltd
idivl %ecx
movl %edx, %edi
callq 0x3130
movb %al, %cl
leaq 0x12a3b2(%rip), %rax    # 0x12d460
movq 0x8(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl 0x10(%rax), %edi
addl $0x1, %edi
callq 0x3130
movb %al, %cl
leaq 0x12a394(%rip), %rax    # 0x12d460
movq 0x30(%rax), %rax
movb %cl, (%rax)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %edi
callq 0x3130
movb %al, %cl
leaq 0x12a379(%rip), %rax    # 0x12d460
movq 0x10(%rax), %rax
movb %cl, (%rax)
leaq 0x12a3b4(%rip), %rax    # 0x12d4a8
movb $0x0, (%rax)
jmp 0x3120
jmp 0x3120
jmp 0x3101
jmp 0x3120
jmp 0x3120
movl -0x4(%rbp), %esi
leaq 0x12a355(%rip), %rax    # 0x12d460
movq 0x20(%rax), %rax
movzbl (%rax), %edx
leaq 0x7047(%rip), %rdi      # 0xa160
movb $0x0, %al
callq 0x3690
addq $0x90, %rsp
popq %rbp
retq
nopl (%rax)