// File: /workspace/github_cpp_makefile_star2_O2/joncampbell123[P]floppytools/kryo_ibm.cpp
// Function: main
int main(int argc,char **argv) {
    struct flux_bits fb;
    struct kryoflux_event ev;
    std::vector<std::string> cappaths;
    std::string path;
    FILE *dsk_fp;
    int c;

    if (argc < 2) {
        fprintf(stderr,"%s <raw capture directory> [dir...]\n",argv[0]);
        return 1;
    }

    for (int i=1;i < argc;i++) {
        char *a = argv[i];

        if (*a == '-') {
            do { a++; } while (*a == '-');

            if (!strcmp(a,"1.4mb")) {
                sectors = 18;
                heads = 2;
                tracks = 80;
                sector_size = 512;
            }
            else if (!strcmp(a,"720k")) {
                sectors = 9;
                heads = 2;
                tracks = 80;
                sector_size = 512;
            }
            else if (!strcmp(a,"sside")) {
                heads = 1;
            }
            else if (!strcmp(a,"dside")) {
                heads = 2;
            }
            else {
                fprintf(stderr,"Unknown switch %s\n",a);
                return 1;
            }
        }
        else {
            cappaths.push_back(argv[i]);
        }
    }

    dsk_fp = fopen("disk.img","wb");
    if (dsk_fp == NULL) return 1;

    /* sector size auto-detect */
    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        if (sector_size == 0) {
            printf("Auto-detecting sector size...\n");

            FILE *fp = kryo_fopen(cappaths[capidx],0/*track*/,0/*head*/);
            if (fp == NULL)
                continue;

            if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
                fclose(fp);
                continue;
            }

            flux_bits ofb = fb;

            fseek(fp,0,SEEK_SET);
            fb.clear();

            static const unsigned int max_sector = 40; /* more than enough for even 2.88MB formats */
            unsigned long count_sszcode[max_sector] = {0};

            while (mfm_find_sync(fb,ev,fp)) {
                mfm_sector_id sid;

                if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) continue;
                if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) continue;

                /* this is a read from track 0 head 0, make sure it matches */
                if (sid.track != 0 || sid.side != 0) continue;
                if (sid.sector_size_code < max_sector) count_sszcode[sid.sector_size_code]++;
            }

            /* scan the histogram for the most common sector size */
            {
                int sel = -1;
                unsigned long sel_count = 0;
                for (unsigned int i=0;i < max_sector;i++) {
                    if (sel_count < count_sszcode[i]) {
                        sel_count = count_sszcode[i];
                        sel = i;
                    }
                }

                if (sel_count >= 6 && sel >= 0) {
                    sector_size = 128u << (unsigned int)sel;
                }
            }

            fclose(fp);
        }
    }

    /* double track detect. Read track 2 and see how they are marked. This is needed to support 360KB DD floppies imaged by a 1.2MB HD drive. */
    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        if (sector_size != 0 && double_track == 0) {
            printf("Auto-detecting double/single track...\n");

            FILE *fp = kryo_fopen(cappaths[capidx],2/*track*/,0/*head*/);
            if (fp == NULL)
                continue;

            if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
                fclose(fp);
                continue;
            }

            flux_bits ofb = fb;

            fseek(fp,0,SEEK_SET);
            fb.clear();

            unsigned long count_1x = 0;
            unsigned long count_2x = 0;

            while (mfm_find_sync(fb,ev,fp)) {
                mfm_sector_id sid;

                if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) continue;
                if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) continue;

                /* this is a read from track 0 head 0, make sure it matches */
                if (sid.side != 0) continue;
                if (sid.sector_size() != sector_size) continue;

                if (sid.track == 1/*DD in HD case*/) count_2x++;
                else if (sid.track == 2/*HD in HD case or normal floppy*/) count_1x++;
            }

            /* scan the histogram for the most common sector size */
            if (count_1x >= 4 || count_2x >= 4) {
                if (count_2x > (count_1x / 2u))
                    double_track = 2;
                else if (count_1x > (count_2x / 2u))
                    double_track = 1;
            }

            fclose(fp);
        }
    }

    /* sectors per track autodetect. scan multiple tracks in case of missing/unreadable sectors in track 0 */
    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        if (sectors == 0 && sector_size != 0 && double_track != 0) {
            printf("Auto-detecting sectors per track...\n");

            for (unsigned int track=0;track < 20;track++) {
                FILE *fp = kryo_fopen(cappaths[capidx],track*double_track/*track*/,0/*head*/);
                if (fp == NULL)
                    continue;

                if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
                    fclose(fp);
                    continue;
                }

                flux_bits ofb = fb;

                fseek(fp,0,SEEK_SET);
                fb.clear();

                while (mfm_find_sync(fb,ev,fp)) {
                    mfm_sector_id sid;

                    if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) continue;
                    if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) continue;

                    /* this is a read from track <track> head 0, make sure it matches */
                    if (sid.track != track || sid.side != 0 || sid.sector_size() != sector_size) continue;

                    /* reject copy protection sectors that most likely have out of range numbers */
                    if (sid.sector > 24) continue; /* more than enough for 1.44MB and even DMF */

                    if (sectors < sid.sector)
                        sectors = sid.sector;
                }

                fclose(fp);
            }
        }
    }

    /* number of heads. look at side 2 (head == 1) and make sure the format matches. */
    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        if (sectors != 0 && sector_size != 0 && double_track != 0 && heads == 0) {
            unsigned int sectors2 = 0;

            printf("Auto-detecting heads...\n");

            for (unsigned int track=0;track < 20;track++) {
                FILE *fp = kryo_fopen(cappaths[capidx],track*double_track/*track*/,1/*head*/);
                if (fp == NULL)
                    continue;

                if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
                    fclose(fp);
                    continue;
                }

                flux_bits ofb = fb;

                fseek(fp,0,SEEK_SET);
                fb.clear();

                while (mfm_find_sync(fb,ev,fp)) {
                    mfm_sector_id sid;

                    if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) continue;
                    if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) continue;

                    /* this is a read from track <track> head 1, make sure it matches */
                    if (sid.track != track || sid.side != 1 || sid.sector_size() != sector_size) continue;

                    /* reject copy protection sectors that most likely have out of range numbers */
                    if (sid.sector > 24) continue; /* more than enough for 1.44MB and even DMF */

                    if (sectors2 < sid.sector)
                        sectors2 = sid.sector;
                }

                fclose(fp);
            }

            if (sectors2 == sectors)
                heads = 2;
            else
                heads = 1;
        }
    }

    /* number of tracks */
    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        if (sectors != 0 && sector_size != 0 && double_track != 0 && heads != 0 && tracks == 0) {
            printf("Auto-detecting tracks...\n");

            for (unsigned int track=0;track < (84/double_track);track++) {
                unsigned long sectcount = 0;
                unsigned long hsectmin = 4;
                unsigned long sectmin = hsectmin * heads; /* in case of damaged or lost sectors */

                for (unsigned int head=0;head < heads;head++) {
                    unsigned long hsectcount = 0;

		    printf("\x0D" "t=%u h=%u ",track,head);
		    fflush(stdout);

                    FILE *fp = kryo_fopen(cappaths[capidx],track*double_track/*track*/,head/*head*/);
                    if (fp == NULL)
                        continue;

                    if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
                        fclose(fp);
                        continue;
                    }

                    flux_bits ofb = fb;

                    fseek(fp,0,SEEK_SET);
                    fb.clear();

                    while (mfm_find_sync(fb,ev,fp)) {
                        mfm_sector_id sid;

                        if (flux_bits_mfm_read_sync_and_byte(fb,ev,fp) != 0xFE) continue;
                        if (flux_bits_mfm_read_sector_id(sid,fb,ev,fp) < 0) continue;

                        /* this is a read from track <track> head <head>, make sure it matches */
                        if (sid.track != track || sid.side != head || sid.sector_size() != sector_size || sid.sector > sectors) continue;

                        sectcount++;
                        hsectcount++;
                        if (hsectcount >= hsectmin) break;
                    }

                    fclose(fp);
                }

		printf("\x0D            \x0D");
		fflush(stdout);

                if (sectcount >= sectmin) {
                    if (tracks <= track)
                        tracks = track + 1;
                }
            }
        }
    }

    printf("Using disk geometry C/H/S/Sz %u/%u/%u/%u doubletrack=%u\n",tracks,heads,sectors,sector_size,double_track);
    if (heads == 0 || sectors == 0 || tracks == 0 || sector_size == 0 || double_track == 0) {
        fprintf(stderr,"Unable to detect format\n");
        return 1;
    }

    captured.resize(heads * sectors * tracks);

    for (size_t pass=0;pass < 3;pass++) {
	    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
		    for (unsigned int track=0;track < tracks;track++) {
			    for (unsigned int head=0;head < heads;head++) {
				    printf("Track %u head %u\n",track,head);

				    FILE *fp = kryo_fopen(cappaths[capidx],track*double_track,head);
				    if (fp == NULL) {
					    printf("Failed to open\n");
					    continue;
				    }

				    if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
					    fprintf(stderr,"Autodetect failure\n");
					    fclose(fp);
					    continue;
				    }

				    flux_bits ofb = fb;
				    unsigned int capcount = 0;

				    int adj_span;
				    int dadj_span;

				    switch (pass) {
					    case 0:
						    adj_span = 0;
						    dadj_span = 0;
						    break;
					    case 1:
						    adj_span = 2;
						    dadj_span = 2;
						    break;
					    case 2:
						    adj_span = 4;
						    dadj_span = 4;
						    break;
					    default:
						    abort();
				    }

				    for (int adj_c = 0;adj_c <= (adj_span*2);adj_c++) {
					    for (int dadj_c = 0;dadj_c <= (dadj_span*2);dadj_c++) {
						    unsigned int capcount = 0;

						    int adj = adj_c;
						    int dadj = dadj_c;

						    if (adj >= (adj_span+1)) adj -= adj_span*2;
						    if (dadj >= (dadj_span+1)) dadj -= dadj_span*2;

						    if ((int)ofb.shortest+(int)adj <= 0)
							    continue;
						    fb.shortest = ofb.shortest + adj;

						    if ((int)ofb.dist+(int)dadj <= 0)
							    continue;
						    fb.dist = ofb.dist + dadj;

						    unsigned long snum = ((track * heads) + head) * sectors;

						    for (size_t i=0;i < sectors;i++)
							    capcount += captured[i+snum];

						    if (capcount >= sectors)
							    break;

						    fseek(fp,0,SEEK_SET);
						    fb.clear();

						    while (mfm_find_sync(fb,ev,fp))
							    process_sync(dsk_fp,fb,ev,fp,track,head);
					    }
				    }

				    {
					    unsigned int capcount = 0;
					    {
						    unsigned long snum = ((track * heads) + head) * sectors;
						    for (size_t i=0;i < sectors;i++)
							    capcount += captured[i+snum];
					    }

					    unsigned long snum = ((track * heads) + head) * sectors;
					    printf("Track %u head %u capture progress: %u/%u ",track,head,capcount,sectors);
					    for (size_t i=0;i < sectors;i++) printf("%u",captured[i+snum]?1:0);
					    printf("\n");

					    if (capcount < sectors) {
						    printf("Track %u head %u not captured fully (%u < %u)\n",track,head,capcount,sectors);
					    }
				    }

				    fclose(fp);
			    }
		    }
	    }
    }

    {
        size_t count = 0;

        for (size_t i=0;i < captured.size();i++) {
            if (captured[i]) count++;
        }

        printf("Capture report: %zu captured / %zu total\n",count,captured.size());

        if (count < captured.size()) {
            printf("Missing sectors:\n");
            for (size_t i=0;i < captured.size();i++) {
                if (!captured[i])
                    printf(" %zu (C/H/S %u/%u/%u)\n",
                        i,
                        i / heads / sectors,
                        (i / sectors) % heads,
                        (i % sectors) + 1);
            }
        }
    }

    printf("Using disk geometry C/H/S/Sz %u/%u/%u/%u doubletrack=%u\n",tracks,heads,sectors,sector_size,double_track);
    fclose(dsk_fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x318, %rsp            # imm = 0x318
movq %rsi, %rbx
leaq -0xc0(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
andq $0x0, -0x60(%rbp)
leaq -0xf8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, -0x70(%rbp)
movb $0x0, (%rax)
pushq $0x1
popq %rax
movl %eax, -0x44(%rbp)
cmpl $0x2, %edi
jl 0x354f
movl %edi, %r12d
pushq $0x1
popq %r15
leaq -0x250(%rbp), %r13
cmpq %r12, %r15
je 0x2775
movq (%rbx,%r15,8), %r14
cmpb $0x2d, (%r14)
jne 0x2708
cmpb $0x2d, 0x1(%r14)
leaq 0x1(%r14), %r14
je 0x269d
movq %r14, %rdi
leaq 0x3a39(%rip), %rsi      # 0x60eb
callq 0x21e0
testl %eax, %eax
je 0x272d
movq %r14, %rdi
leaq 0x3a2c(%rip), %rsi      # 0x60f1
callq 0x21e0
testl %eax, %eax
je 0x2739
movq %r14, %rdi
leaq 0x3a1e(%rip), %rsi      # 0x60f6
callq 0x21e0
testl %eax, %eax
je 0x2769
movq %r14, %rdi
leaq 0x3a0d(%rip), %rsi      # 0x60fc
callq 0x21e0
testl %eax, %eax
jne 0x370e
movl $0x2, 0x6a5e(%rip)      # 0x9164
jmp 0x2761
movq %r13, %rdi
movq %r14, %rsi
leaq -0x49(%rbp), %rdx
callq 0x2090
leaq -0x70(%rbp), %rdi
movq %r13, %rsi
callq 0x3b02
movq %r13, %rdi
callq 0x20b0
jmp 0x2761
movl $0x12, 0x6a29(%rip)     # 0x9160
jmp 0x2743
movl $0x9, 0x6a1d(%rip)      # 0x9160
movl $0x2, 0x6a17(%rip)      # 0x9164
movl $0x50, 0x6a11(%rip)     # 0x9168
movl $0x200, 0x6a0b(%rip)    # imm = 0x200
incq %r15
jmp 0x268a
movl $0x1, 0x69f1(%rip)      # 0x9164
jmp 0x2761
leaq 0x3999(%rip), %rdi      # 0x6115
leaq 0x399b(%rip), %rsi      # 0x611e
callq 0x2180
movq %rax, -0xa8(%rbp)
testq %rax, %rax
je 0x36d8
xorl %esi, %esi
leaq -0x88(%rbp), %r15
leaq -0x340(%rbp), %r14
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rdx, %rsi
jae 0x2927
movq %rsi, %r13
cmpl $0x0, 0x699f(%rip)      # 0x916c
jne 0x291c
leaq 0x3b49(%rip), %rdi      # 0x6323
callq 0x2200
movq %r13, %rdi
shlq $0x5, %rdi
addq -0x70(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4aa9
movq %rax, %rbx
testq %rax, %rax
je 0x291c
movq %r15, %rdi
leaq -0xd0(%rbp), %rsi
movq %rbx, %rdx
callq 0x4575
testb %al, %al
je 0x2914
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r15, %rdi
callq 0x44f0
movl $0x140, %edx            # imm = 0x140
leaq -0x250(%rbp), %rdi
xorl %esi, %esi
callq 0x20e0
movq %r14, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3832
movq %r15, %rdi
movq %r14, %r12
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4b79
movl %eax, %r14d
leaq -0x330(%rbp), %rdi
callq 0x3972
testb %r14b, %r14b
je 0x28d6
leaq -0x49(%rbp), %rdi
callq 0x4d1a
movq %r12, %r14
movq %r15, %rdi
leaq -0xd0(%rbp), %rsi
movq %rbx, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x2840
leaq -0x49(%rbp), %rdi
movq %r15, %rsi
leaq -0xd0(%rbp), %rdx
movq %rbx, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x2840
movb -0x48(%rbp), %al
orb -0x49(%rbp), %al
jne 0x2840
movzbl -0x46(%rbp), %eax
cmpq $0x27, %rax
ja 0x2840
incq -0x250(%rbp,%rax,8)
jmp 0x2840
xorl %edx, %edx
xorl %eax, %eax
pushq $-0x1
popq %rcx
movq %r12, %r14
cmpq $0x28, %rdx
je 0x28fd
movq -0x250(%rbp,%rdx,8), %rsi
cmpq %rsi, %rax
cmovbeq %rsi, %rax
cmovbl %edx, %ecx
incq %rdx
jmp 0x28e0
cmpq $0x6, %rax
jb 0x2914
testl %ecx, %ecx
js 0x2914
movl $0x80, %eax
shll %cl, %eax
movl %eax, 0x6858(%rip)      # 0x916c
movq %rbx, %rdi
callq 0x2140
movq %r13, %rsi
incq %rsi
jmp 0x27a8
xorl %esi, %esi
leaq -0x88(%rbp), %r12
leaq -0xd0(%rbp), %r13
leaq -0x318(%rbp), %r14
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x5, %rdx
movq %rsi, -0x30(%rbp)
cmpq %rdx, %rsi
jae 0x2ae2
cmpl $0x0, 0x6810(%rip)      # 0x916c
je 0x2ace
cmpl $0x0, 0x6807(%rip)      # 0x9170
jne 0x2ace
leaq 0x3987(%rip), %rdi      # 0x62fd
callq 0x2200
movq -0x30(%rbp), %rdi
shlq $0x5, %rdi
addq -0x70(%rbp), %rdi
pushq $0x2
popq %rsi
xorl %edx, %edx
callq 0x4aa9
movq %rax, %rbx
testq %rax, %rax
je 0x2ace
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4575
testb %al, %al
je 0x2ac6
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r12, %rdi
callq 0x44f0
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x3832
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4b79
movl %eax, %r15d
leaq -0x308(%rbp), %rdi
callq 0x3972
testb %r15b, %r15b
je 0x2a8a
leaq -0x250(%rbp), %rdi
callq 0x4d1a
movq %r12, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x29d7
leaq -0x250(%rbp), %rdi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x29d7
cmpb $0x0, -0x24f(%rbp)
jne 0x29d7
leaq -0x250(%rbp), %rdi
callq 0x4d36
cmpl 0x670f(%rip), %eax      # 0x916c
jne 0x29d7
movzbl -0x250(%rbp), %eax
cmpl $0x2, %eax
je 0x2a81
cmpl $0x1, %eax
jne 0x29d7
incq -0x38(%rbp)
jmp 0x29d7
incq -0x40(%rbp)
jmp 0x29d7
movq -0x40(%rbp), %rsi
cmpq $0x4, %rsi
setae %al
movq -0x38(%rbp), %rdx
cmpq $0x4, %rdx
setae %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2ac6
movq %rsi, %rcx
shrq %rcx
pushq $0x2
popq %rax
cmpq %rcx, %rdx
ja 0x2ac0
shrq %rdx
pushq $0x1
popq %rax
cmpq %rdx, %rsi
jbe 0x2ac6
movl %eax, 0x66aa(%rip)      # 0x9170
movq %rbx, %rdi
callq 0x2140
movq -0x30(%rbp), %rsi
incq %rsi
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
jmp 0x293e
xorl %esi, %esi
leaq -0x88(%rbp), %r15
leaq -0xd0(%rbp), %r12
leaq -0x2f0(%rbp), %r13
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x5, %rdx
movq %rsi, -0x40(%rbp)
cmpq %rdx, %rsi
jae 0x2c90
cmpl $0x0, 0x6649(%rip)      # 0x9160
jne 0x2c84
cmpl $0x0, 0x6648(%rip)      # 0x916c
je 0x2c84
cmpl $0x0, 0x663f(%rip)      # 0x9170
je 0x2c84
leaq 0x379b(%rip), %rdi      # 0x62d9
callq 0x2200
xorl %eax, %eax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
movq %rcx, -0x38(%rbp)
cmpl $0x14, %eax
je 0x2c7c
movq -0x70(%rbp), %rdi
addq -0x38(%rbp), %rdi
movl 0x6608(%rip), %esi      # 0x9170
movl %eax, -0x30(%rbp)
imull %eax, %esi
xorl %edx, %edx
callq 0x4aa9
movq %rax, %rbx
testq %rax, %rax
je 0x2c72
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4575
testb %al, %al
je 0x2c6a
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r15, %rdi
callq 0x44f0
movq %r13, %rdi
movq %r12, %rsi
callq 0x3832
movq %r15, %rdi
movq %r13, %rsi
movq %rbx, %rdx
callq 0x4b79
movl %eax, %r14d
leaq -0x2e0(%rbp), %rdi
callq 0x3972
testb %r14b, %r14b
je 0x2c6a
leaq -0x250(%rbp), %rdi
callq 0x4d1a
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x2bab
leaq -0x250(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x2bab
movzbl -0x250(%rbp), %eax
cmpl %eax, -0x30(%rbp)
jne 0x2bab
cmpb $0x0, -0x24f(%rbp)
jne 0x2bab
leaq -0x250(%rbp), %rdi
callq 0x4d36
cmpl 0x652f(%rip), %eax      # 0x916c
jne 0x2bab
movzbl -0x24e(%rbp), %eax
cmpl $0x18, %eax
ja 0x2bab
cmpl %eax, 0x6507(%rip)      # 0x9160
jae 0x2bab
movl %eax, 0x64fb(%rip)      # 0x9160
jmp 0x2bab
movq %rbx, %rdi
callq 0x2140
movl -0x30(%rbp), %eax
incl %eax
jmp 0x2b51
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x40(%rbp), %rsi
incq %rsi
jmp 0x2af9
xorl %esi, %esi
pushq $0x1
popq %rdx
movl %edx, -0x44(%rbp)
leaq -0x88(%rbp), %r15
leaq -0xd0(%rbp), %r12
leaq -0x2c8(%rbp), %r14
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x5, %rdx
movq %rsi, -0x58(%rbp)
cmpq %rdx, %rsi
jae 0x2e75
cmpl $0x0, 0x6495(%rip)      # 0x9160
je 0x2e69
cmpl $0x0, 0x6494(%rip)      # 0x916c
je 0x2e69
cmpl $0x0, 0x648b(%rip)      # 0x9170
je 0x2e69
cmpl $0x0, 0x6472(%rip)      # 0x9164
jne 0x2e69
leaq 0x35c2(%rip), %rdi      # 0x62c1
callq 0x2200
movl $0x0, -0x40(%rbp)
movq -0x58(%rbp), %rax
shlq $0x5, %rax
movq %rax, -0x38(%rbp)
xorl %eax, %eax
cmpl $0x14, %eax
je 0x2e4b
movq -0x70(%rbp), %rdi
addq -0x38(%rbp), %rdi
movl 0x6440(%rip), %esi      # 0x9170
movl %eax, -0x30(%rbp)
imull %eax, %esi
pushq $0x1
popq %rdx
callq 0x4aa9
movq %rax, %rbx
testq %rax, %rax
je 0x2e41
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4575
testb %al, %al
je 0x2e39
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r15, %rdi
callq 0x44f0
movq %r14, %rdi
movq %r12, %rsi
callq 0x3832
movq %r15, %rdi
movq %r14, %r13
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4b79
movl %eax, %r14d
leaq -0x2b8(%rbp), %rdi
callq 0x3972
testb %r14b, %r14b
je 0x2e36
leaq -0x250(%rbp), %rdi
callq 0x4d1a
movq %r13, %r14
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x2d74
leaq -0x250(%rbp), %rdi
movq %r15, %rsi
movq %r12, %rdx
movq %rbx, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x2d74
movzbl -0x250(%rbp), %eax
cmpl %eax, -0x30(%rbp)
jne 0x2d74
cmpb $0x1, -0x24f(%rbp)
jne 0x2d74
leaq -0x250(%rbp), %rdi
callq 0x4d36
cmpl 0x635c(%rip), %eax      # 0x916c
jne 0x2d74
movzbl -0x24e(%rbp), %eax
cmpl $0x18, %eax
ja 0x2d74
movl -0x40(%rbp), %ecx
cmpl %eax, %ecx
cmovbel %eax, %ecx
movl %ecx, -0x40(%rbp)
jmp 0x2d74
movq %r13, %r14
movq %rbx, %rdi
callq 0x2140
movl -0x30(%rbp), %eax
incl %eax
jmp 0x2d19
xorl %eax, %eax
movl -0x40(%rbp), %ecx
cmpl 0x630a(%rip), %ecx      # 0x9160
sete %al
incl %eax
movl %eax, 0x6303(%rip)      # 0x9164
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x58(%rbp), %rsi
incq %rsi
jmp 0x2cad
leaq -0x88(%rbp), %r14
leaq -0xd0(%rbp), %r12
leaq -0x2a0(%rbp), %r15
xorl %esi, %esi
movq %rcx, %rdx
subq %rax, %rdx
sarq $0x5, %rdx
cmpq %rdx, %rsi
jae 0x311b
movq %rsi, %rdx
cmpl $0x0, 0x62b7(%rip)      # 0x9160
je 0x3110
cmpl $0x0, 0x62b6(%rip)      # 0x916c
je 0x3110
cmpl $0x0, 0x62ad(%rip)      # 0x9170
je 0x3110
cmpl $0x0, 0x6294(%rip)      # 0x9164
je 0x3110
cmpl $0x0, 0x628b(%rip)      # 0x9168
jne 0x3110
leaq 0x33be(%rip), %rdi      # 0x62a8
movq %rdx, %rbx
callq 0x2200
xorl %eax, %eax
movq %rbx, -0x90(%rbp)
shlq $0x5, %rbx
movq %rbx, -0x40(%rbp)
movl %eax, %ecx
pushq $0x54
popq %rax
xorl %edx, %edx
divl 0x6260(%rip)            # 0x9170
cmpl %eax, %ecx
jae 0x3101
movq %rcx, -0x38(%rbp)
movl 0x6242(%rip), %eax      # 0x9164
movq %rax, %rcx
shlq $0x2, %rcx
movq %rcx, -0x98(%rbp)
movq $0x0, -0x78(%rbp)
xorl %r13d, %r13d
cmpl %eax, %r13d
jae 0x30b5
leaq 0x31d6(%rip), %rdi      # 0x6121
movq -0x38(%rbp), %rbx
movl %ebx, %esi
movl %r13d, %edx
xorl %eax, %eax
callq 0x2040
movq 0x6066(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
callq 0x2170
movq -0x70(%rbp), %rdi
addq -0x40(%rbp), %rdi
movl 0x61f8(%rip), %esi      # 0x9170
imull %ebx, %esi
movl %r13d, -0x30(%rbp)
movl %r13d, %edx
callq 0x4aa9
movq %rax, %r13
testq %rax, %rax
je 0x30a3
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4575
testb %al, %al
je 0x309b
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r14, %rdi
callq 0x44f0
movq $0x0, -0x58(%rbp)
movq %r15, %rdi
movq %r12, %rsi
callq 0x3832
movq %r14, %rdi
movq %r15, %rbx
movq %r15, %rsi
movq %r13, %rdx
callq 0x4b79
movl %eax, %r15d
leaq -0x290(%rbp), %rdi
callq 0x3972
testb %r15b, %r15b
je 0x3098
leaq -0x250(%rbp), %rdi
callq 0x4d1a
movq %rbx, %r15
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4cd4
cmpl $0xfe, %eax
jne 0x2fc5
leaq -0x250(%rbp), %rdi
movq %r14, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x4d4e
testl %eax, %eax
js 0x2fc5
movzbl -0x250(%rbp), %eax
cmpl %eax, -0x38(%rbp)
jne 0x2fc5
movzbl -0x24f(%rbp), %eax
cmpl %eax, -0x30(%rbp)
jne 0x2fc5
leaq -0x250(%rbp), %rdi
callq 0x4d36
cmpl 0x6108(%rip), %eax      # 0x916c
jne 0x2fc5
movzbl -0x24e(%rbp), %eax
cmpl %eax, 0x60e9(%rip)      # 0x9160
jb 0x2fc5
incq -0x78(%rbp)
movq -0x58(%rbp), %rcx
incq %rcx
movq %rcx, -0x58(%rbp)
cmpq $0x3, %rcx
jbe 0x2fc5
jmp 0x309b
movq %rbx, %r15
movq %r13, %rdi
callq 0x2140
movl -0x30(%rbp), %r13d
incl %r13d
movl 0x60b4(%rip), %eax      # 0x9164
jmp 0x2f3b
leaq 0x3071(%rip), %rdi      # 0x612d
xorl %eax, %eax
callq 0x2040
movq 0x5efe(%rip), %rax      # 0x8fc8
movq (%rax), %rdi
callq 0x2170
movq -0x38(%rbp), %rcx
leal 0x1(%rcx), %eax
movq -0x78(%rbp), %rdx
cmpq -0x98(%rbp), %rdx
jb 0x2f03
cmpl %ecx, 0x6078(%rip)      # 0x9168
ja 0x2f03
movl %eax, 0x606c(%rip)      # 0x9168
jmp 0x2f03
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
movq -0x90(%rbp), %rdx
incq %rdx
movq %rdx, %rsi
jmp 0x2e8c
movl 0x6047(%rip), %esi      # 0x9168
movl 0x603d(%rip), %edx      # 0x9164
movl 0x6033(%rip), %ecx      # 0x9160
movl 0x6038(%rip), %r8d      # 0x916c
movl 0x6035(%rip), %r9d      # 0x9170
leaq 0x2ffa(%rip), %rdi      # 0x613c
xorl %eax, %eax
callq 0x2040
movl 0x6015(%rip), %eax      # 0x9164
testl %eax, %eax
je 0x356f
movl 0x6003(%rip), %esi      # 0x9160
testl %esi, %esi
je 0x356f
movl 0x5ffd(%rip), %ecx      # 0x9168
testl %ecx, %ecx
je 0x356f
cmpl $0x0, 0x5ff2(%rip)      # 0x916c
je 0x356f
cmpl $0x0, 0x5fe9(%rip)      # 0x9170
je 0x356f
imull %eax, %esi
imull %ecx, %esi
leaq 0x9ff6(%rip), %rdi      # 0xd190
xorl %edx, %edx
callq 0x386c
leaq -0x88(%rbp), %rbx
leaq -0xd0(%rbp), %r14
xorl %eax, %eax
cmpq $0x3, %rax
je 0x3590
leal (%rax,%rax), %ecx
movl %ecx, -0x98(%rbp)
leal (,%rax,4), %ecx
movl %ecx, -0x90(%rbp)
movq %rax, -0xd8(%rbp)
leal 0x1(,%rax,4), %eax
movl %eax, -0x78(%rbp)
xorl %ecx, %ecx
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
cmpq %rax, %rcx
jae 0x3540
xorl %esi, %esi
movq %rcx, %rax
shlq $0x5, %rax
movq %rax, -0xe8(%rbp)
movq %rcx, -0xe0(%rbp)
cmpl 0x5f52(%rip), %esi      # 0x9168
jae 0x3538
xorl %r12d, %r12d
movl %esi, -0x38(%rbp)
cmpl 0x5f3b(%rip), %r12d     # 0x9164
jae 0x3531
leaq 0x2f58(%rip), %rdi      # 0x618e
movl %r12d, %edx
movl %esi, %r15d
xorl %eax, %eax
callq 0x2040
movq -0x70(%rbp), %rdi
addq -0xe8(%rbp), %rdi
movl 0x5f1c(%rip), %esi      # 0x9170
imull %r15d, %esi
movl %r12d, -0x30(%rbp)
movl %r12d, %edx
callq 0x4aa9
movq %rax, %r13
testq %rax, %rax
je 0x34e9
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x4575
testb %al, %al
je 0x350b
movl -0x80(%rbp), %eax
movl %eax, -0x9c(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0x44(%rbp)
xorl %ecx, %ecx
cmpl -0x78(%rbp), %ecx
je 0x33f1
cmpl -0x98(%rbp), %ecx
movl $0x0, %eax
cmoval -0x90(%rbp), %eax
movl %ecx, -0xa0(%rbp)
subl %eax, %ecx
xorl %eax, %eax
addl -0x9c(%rbp), %ecx
movl %ecx, -0x58(%rbp)
movl %eax, -0x40(%rbp)
cmpl -0x78(%rbp), %eax
je 0x33e4
cmpl $0x0, -0x58(%rbp)
jle 0x33da
movl -0x40(%rbp), %eax
cmpl -0x98(%rbp), %eax
movl $0x0, %ecx
cmoval -0x90(%rbp), %ecx
subl %ecx, %eax
movl -0x58(%rbp), %ecx
movl %ecx, -0x80(%rbp)
addl -0x44(%rbp), %eax
jle 0x33da
movl %eax, -0x7c(%rbp)
movl 0x5e56(%rip), %eax      # 0x9160
movl 0x5e54(%rip), %ebx      # 0x9164
imull -0x38(%rbp), %ebx
addl -0x30(%rbp), %ebx
imull %eax, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
movl %eax, %ecx
cmpq %rcx, %r15
jae 0x334d
leaq (%rbx,%r15), %rsi
leaq 0x9e5e(%rip), %rdi      # 0xd190
callq 0x3810
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbl $-0x1, %r14d
incq %r15
movl 0x5e15(%rip), %eax      # 0x9160
jmp 0x3320
cmpl %eax, %r14d
leaq -0x268(%rbp), %r15
leaq -0x88(%rbp), %r14
jae 0x33e4
movq %r13, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r14, %rdi
callq 0x44f0
leaq -0xd0(%rbp), %rbx
leaq -0x278(%rbp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3832
movq %r14, %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x4b79
movl %eax, %ebx
movq %r15, %rdi
callq 0x3972
testb %bl, %bl
je 0x33da
movq -0xa8(%rbp), %rdi
movq %r14, %rsi
leaq -0xd0(%rbp), %rbx
movq %rbx, %rdx
movq %r13, %rcx
movl -0x38(%rbp), %r8d
movl -0x30(%rbp), %r9d
callq 0x23ac
leaq -0x278(%rbp), %r12
jmp 0x3386
movl -0x40(%rbp), %eax
incl %eax
jmp 0x32c5
movl -0xa0(%rbp), %ecx
incl %ecx
jmp 0x3297
movl 0x5d68(%rip), %r12d     # 0x9160
movl 0x5d66(%rip), %ebx      # 0x9164
imull -0x38(%rbp), %ebx
addl -0x30(%rbp), %ebx
imull %r12d, %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
movl %r12d, %eax
cmpq %rax, %r14
jae 0x343e
leaq (%rbx,%r14), %rsi
leaq 0x9d6e(%rip), %rdi      # 0xd190
callq 0x3810
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbl $-0x1, %r15d
incq %r14
movl 0x5d24(%rip), %r12d     # 0x9160
jmp 0x340f
movl 0x5d20(%rip), %ebx      # 0x9164
movl -0x38(%rbp), %esi
imull %esi, %ebx
leaq 0x2d63(%rip), %rdi      # 0x61b4
movl -0x30(%rbp), %r14d
movl %r14d, %edx
movl %r15d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x2040
addl %r14d, %ebx
imull %r12d, %ebx
xorl %r14d, %r14d
movl 0x5ceb(%rip), %eax      # 0x9160
cmpq %rax, %r14
jae 0x34a6
leaq (%rbx,%r14), %rsi
leaq 0x9d0b(%rip), %rdi      # 0xd190
callq 0x3810
xorl %esi, %esi
testq %rdx, (%rax)
setne %sil
leaq 0x2d44(%rip), %rdi      # 0x61de
xorl %eax, %eax
callq 0x2040
incq %r14
jmp 0x346f
pushq $0xa
popq %rdi
callq 0x21d0
movl 0x5cab(%rip), %r8d      # 0x9160
cmpl %r8d, %r15d
jae 0x34d1
leaq 0x2d20(%rip), %rdi      # 0x61e1
movl -0x38(%rbp), %esi
movl -0x30(%rbp), %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2040
movq %r13, %rdi
callq 0x2140
leaq -0x88(%rbp), %rbx
leaq -0xd0(%rbp), %r14
jmp 0x34f5
leaq 0x2da9(%rip), %rdi      # 0x6299
callq 0x2200
movl -0x30(%rbp), %r12d
incl %r12d
movq -0xe0(%rbp), %rcx
movl -0x38(%rbp), %esi
jmp 0x3222
movq 0x5ace(%rip), %rax      # 0x8fe0
movq (%rax), %rcx
leaq 0x2c84(%rip), %rdi      # 0x61a0
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x2250
movq %r13, %rdi
callq 0x2140
jmp 0x34f5
incl %esi
jmp 0x3210
incq %rcx
jmp 0x31e4
movq -0xd8(%rbp), %rax
incq %rax
jmp 0x31b1
movq 0x5a8a(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq (%rbx), %rdx
leaq 0x2b63(%rip), %rsi      # 0x60c6
xorl %eax, %eax
callq 0x21f0
jmp 0x36d8
movq 0x5a6a(%rip), %rax      # 0x8fe0
movq (%rax), %rcx
leaq 0x2bf5(%rip), %rdi      # 0x6175
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x2250
jmp 0x36d8
xorl %ebx, %ebx
leaq 0x9bf7(%rip), %r14      # 0xd190
xorl %r15d, %r15d
movq 0x9bfd(%rip), %rax      # 0xd1a0
movl 0x9bff(%rip), %ecx      # 0xd1a8
subq 0x9be0(%rip), %rax      # 0xd190
leaq (%rcx,%rax,8), %rdx
cmpq %rdx, %r15
jae 0x35d4
movq %r14, %rdi
movq %r15, %rsi
callq 0x3810
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbq $-0x1, %rbx
incq %r15
jmp 0x359c
leaq 0x2c35(%rip), %rdi      # 0x6210
movl $0x0, -0x44(%rbp)
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
movq 0x9bad(%rip), %rax      # 0xd1a0
movl 0x9baf(%rip), %ecx      # 0xd1a8
subq 0x9b90(%rip), %rax      # 0xd190
leaq (%rcx,%rax,8), %rax
cmpq %rax, %rbx
jae 0x369e
leaq 0x2c74(%rip), %rdi      # 0x6288
callq 0x2200
leaq 0x9b70(%rip), %rbx      # 0xd190
leaq 0x2c13(%rip), %r14      # 0x623a
xorl %r15d, %r15d
movq 0x9b6f(%rip), %rax      # 0xd1a0
movl 0x9b71(%rip), %ecx      # 0xd1a8
subq 0x9b52(%rip), %rax      # 0xd190
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r15
jae 0x369e
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3810
testq %rdx, (%rax)
jne 0x3699
movl 0x5b07(%rip), %ecx      # 0x9164
movq %r15, %rax
xorl %edx, %edx
divq %rcx
movl 0x5af5(%rip), %esi      # 0x9160
xorl %edx, %edx
divq %rsi
movq %rax, %r9
movq %r15, %rax
xorl %edx, %edx
divq %rsi
movq %rdx, %r8
xorl %edx, %edx
divq %rcx
movq %rdx, %rcx
incq %r8
movq %r14, %rdi
movq %r15, %rsi
movq %r9, %rdx
xorl %eax, %eax
callq 0x2040
incq %r15
jmp 0x362a
movl 0x5ac4(%rip), %esi      # 0x9168
movl 0x5aba(%rip), %edx      # 0x9164
movl 0x5ab0(%rip), %ecx      # 0x9160
movl 0x5ab5(%rip), %r8d      # 0x916c
movl 0x5ab2(%rip), %r9d      # 0x9170
leaq 0x2a77(%rip), %rdi      # 0x613c
xorl %eax, %eax
callq 0x2040
movq -0xa8(%rbp), %rdi
callq 0x2140
leaq -0x108(%rbp), %rdi
callq 0x20b0
leaq -0x70(%rbp), %rdi
callq 0x38e4
leaq -0xc0(%rbp), %rdi
callq 0x3972
movl -0x44(%rbp), %eax
addq $0x318, %rsp            # imm = 0x318
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x58cb(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x29e3(%rip), %rsi      # 0x6102
movq %r14, %rdx
jmp 0x3563
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x250(%rbp), %rdi
callq 0x20b0
jmp 0x37d3
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x330(%rbp), %rdi
jmp 0x37c5
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x308(%rbp), %rdi
jmp 0x37c5
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x2b8(%rbp), %rdi
jmp 0x37c5
jmp 0x37d0
movq %rax, %r14
leaq -0x2e0(%rbp), %rdi
jmp 0x37c5
jmp 0x37d0
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x290(%rbp), %rdi
jmp 0x37c5
jmp 0x37d0
movq %rax, %r14
movq %r15, %rdi
callq 0x3972
jmp 0x37d3
jmp 0x37d0
jmp 0x37d0
movq %rax, %r14
leaq -0x108(%rbp), %rdi
callq 0x20b0
leaq -0x70(%rbp), %rdi
callq 0x38e4
leaq -0xc0(%rbp), %rdi
callq 0x3972
movq %r14, %rdi
callq 0x2230