// File: /workspace/github_cpp_makefile_star2_O3/joncampbell123[P]floppytools/kryoflux7.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 {
                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;

    captured.resize(heads * sectors * tracks);

    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++) {
                {
                    char tmp[128];

                    sprintf(tmp,"track%02u.%u.raw",track,head);
                    path = cappaths[capidx] + "/" + tmp;
                }

                printf("%s...\n",path.c_str());

                FILE *fp = fopen(path.c_str(),"rb");
                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;

                int adj_span = 8;
                int dadj_span = 8;

                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) {
                            printf("Track %u head %u already captured\n",track,head);
                            adj_c = 99;
                            dadj_c = 99;
                            break;
                        }
                        else {
                            printf("Track %u head %u capture progress: %u/%u adj=%d dadj=%d ",track,head,capcount,sectors,adj,dadj);
                            for (size_t i=0;i < sectors;i++) printf("%u",captured[i+snum]?1:0);
                            printf("\n");
                        }

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

                        do {
                            kryoflux_bits_refill(fb,ev,fp);

                            /*                                                      *            */
                            /*                                            1 0 1 0 0 0 0 1   (A1) */
                            /* look for A1 sync (100010010001). Look for '0100010010001001' */
                            /*                                            ................  16 bits */
                            /*                                            4-->4-->8-->9-->  */
                            /*                                            3210321032103210  */
                            while (fb.avail() >= MFM_A1_SYNC_LENGTH) {
                                if (fb.peek(MFM_A1_SYNC_LENGTH) == MFM_A1_SYNC) {
                                    process_sync(dsk_fp,fb,ev,fp);
                                }
                                else {
                                    fb.get(1);
                                }
                            }

                            if (!kryoflux_bits_refill(fb,ev,fp))
                                break;
                        } while (fb.avail() > 0);
                    }
                }

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

                    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);
            }
        }
    }

    fclose(dsk_fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x188, %rsp            # imm = 0x188
movq %rsi, %rbx
xorl %eax, %eax
movq %rax, -0x100(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x110(%rbp)
movq %rax, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
leaq -0xe8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb $0x0, (%rcx)
cmpl $0x2, %edi
jl 0x31b9
movl %edi, %r13d
movl $0x1, %r15d
leaq -0x1a0(%rbp), %r12
movq (%rbx,%r15,8), %r14
cmpb $0x2d, (%r14)
jne 0x2ae0
cmpb $0x2d, 0x1(%r14)
leaq 0x1(%r14), %r14
je 0x2a9f
movq %r14, %rdi
leaq 0x2663(%rip), %rsi      # 0x5117
callq 0x21c0
testl %eax, %eax
je 0x2b18
movq %r14, %rdi
leaq 0x2656(%rip), %rsi      # 0x511d
callq 0x21c0
testl %eax, %eax
jne 0x31c3
movl $0x9, 0x565a(%rip)      # 0x8138
jmp 0x2b22
movq %r12, %rdi
movq %r14, %rsi
leaq -0xb0(%rbp), %rdx
callq 0x2090
leaq -0x60(%rbp), %rdi
movq %r12, %rsi
callq 0x33be
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
cmpq %rax, %rdi
je 0x2b40
callq 0x2140
jmp 0x2b40
movl $0x12, 0x5616(%rip)     # 0x8138
movl $0x2, 0x5610(%rip)      # 0x813c
movl $0x50, 0x560a(%rip)     # 0x8140
movl $0x200, 0x5604(%rip)    # imm = 0x200
incq %r15
cmpq %r13, %r15
jne 0x2a95
leaq 0x25e2(%rip), %rdi      # 0x5135
leaq 0x25e4(%rip), %rsi      # 0x513e
callq 0x2180
testq %rax, %rax
je 0x31de
movq %rax, %rbx
movl 0x55c7(%rip), %esi      # 0x8138
imull 0x55c4(%rip), %esi      # 0x813c
imull 0x55c1(%rip), %esi      # 0x8140
leaq 0x95ea(%rip), %rdi      # 0xc170
xorl %edx, %edx
callq 0x32b4
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
cmpq %rcx, %rdx
je 0x3025
leaq -0x70(%rbp), %r15
leaq -0xa0(%rbp), %r12
movl 0x5591(%rip), %eax      # 0x8140
leaq -0x90(%rbp), %r14
leaq -0x120(%rbp), %r13
movl %eax, %esi
movq $0x0, -0x40(%rbp)
movq %rbx, -0xc8(%rbp)
testl %esi, %esi
je 0x3005
movl $0x0, -0x2c(%rbp)
movl 0x5559(%rip), %esi      # 0x813c
testl %esi, %esi
movl $0x0, %esi
je 0x2fe9
xorl %ecx, %ecx
leaq -0x1a0(%rbp), %rdi
leaq 0x2541(%rip), %rsi      # 0x5141
movl -0x2c(%rbp), %edx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x2050
movq -0x60(%rbp), %rax
movq -0x40(%rbp), %rcx
shlq $0x5, %rcx
movq %r15, -0x80(%rbp)
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq -0x80(%rbp), %r15
movq %r15, %rdi
callq 0x3c04
movq %r15, %rdi
leaq 0x2513(%rip), %rsi      # 0x5152
callq 0x2230
movq %r15, %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x2230
movq %r12, -0xb0(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2c7c
movq %rdx, -0xb0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0xa0(%rbp)
jmp 0x2c84
movups (%rcx), %xmm0
movups %xmm0, (%r12)
movq 0x8(%rax), %rdx
movq %rdx, -0xa8(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xf8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x2190
movq -0xb0(%rbp), %rdi
cmpq %r12, %rdi
je 0x2cc2
callq 0x2140
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
cmpq %rax, %rdi
je 0x2cd4
callq 0x2140
movq -0xf8(%rbp), %rsi
leaq 0x2472(%rip), %rdi      # 0x5154
xorl %eax, %eax
callq 0x2040
movq -0xf8(%rbp), %rdi
leaq 0x2464(%rip), %rsi      # 0x515b
callq 0x2180
testq %rax, %rax
je 0x2f8f
movq %rax, %r15
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x3f5c
testb %al, %al
je 0x2fd9
movl -0x88(%rbp), %eax
movq %rax, -0xb8(%rbp)
movl -0x84(%rbp), %eax
movl %eax, -0x44(%rbp)
xorl %eax, %eax
leal -0x10(%rax), %ecx
cmpl $0x9, %eax
movq %rax, -0xc0(%rbp)
cmovbl %eax, %ecx
movq -0xb8(%rbp), %rax
movq %rcx, -0xd0(%rbp)
addl %ecx, %eax
movl %eax, -0x34(%rbp)
xorl %eax, %eax
movq %rax, %rcx
addl $-0x10, %eax
cmpl $0x9, %ecx
movq %rcx, -0xd8(%rbp)
cmovbl %ecx, %eax
cmpl $0x0, -0x34(%rbp)
jle 0x2f0f
movl -0x34(%rbp), %ecx
movl %ecx, -0x88(%rbp)
movl %eax, %ecx
addl -0x44(%rbp), %ecx
jle 0x2f0f
movl %ecx, -0x84(%rbp)
movl 0x53a3(%rip), %r9d      # 0x813c
movl 0x5398(%rip), %r8d      # 0x8138
testq %r8, %r8
je 0x2ddd
movq 0x93c4(%rip), %rdx      # 0xc170
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
imull %r9d, %esi
addl -0x30(%rbp), %esi
imull %r8d, %esi
xorl %ecx, %ecx
movq %r8, %rdi
movq %rsi, %r10
shrq $0x6, %r10
movq (%rdx,%r10,8), %r10
btq %rsi, %r10
adcl $0x0, %ecx
incq %rsi
decq %rdi
jne 0x2dc1
jmp 0x2ddf
xorl %ecx, %ecx
cmpl %r8d, %ecx
jae 0x2f36
movl %eax, (%rsp)
leaq 0x23a3(%rip), %rdi      # 0x5195
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
movq %r9, %r12
movq -0xd0(%rbp), %r9
xorl %eax, %eax
movq %r8, %rbx
callq 0x2040
cmpl $0x0, 0x5325(%rip)      # 0x8138
leaq 0x23b4(%rip), %r13      # 0x51ce
je 0x2e6d
movq %r12, %r8
imull -0x2c(%rbp), %r8d
addl -0x30(%rbp), %r8d
imull %ebx, %r8d
xorl %r12d, %r12d
leaq (%r8,%r12), %rax
movq 0x9336(%rip), %rcx      # 0xc170
movq %rax, %rdx
shrq $0x6, %rdx
movq (%rcx,%rdx,8), %rcx
xorl %esi, %esi
btq %rax, %rcx
setb %sil
movq %r13, %rdi
xorl %eax, %eax
movq %r8, %rbx
callq 0x2040
movq %rbx, %r8
incq %r12
movl 0x52d0(%rip), %eax      # 0x8138
cmpq %rax, %r12
jb 0x2e2f
movl $0xa, %edi
callq 0x21b0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq %r14, %rdi
callq 0x3f00
leaq -0xa0(%rbp), %r12
leaq -0x120(%rbp), %r13
movq -0xc8(%rbp), %rbx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3e32
movq %r14, %rdi
callq 0x3eb4
cmpl $0x10, %eax
jb 0x2ef1
movq %r14, %rdi
movl $0x10, %esi
callq 0x3f0e
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x2ee2
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x249c
jmp 0x2eae
movq %r14, %rdi
movl $0x1, %esi
callq 0x3f34
jmp 0x2eae
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3e32
testb %al, %al
je 0x2f0f
movq %r14, %rdi
callq 0x3eb4
testl %eax, %eax
jne 0x2ea0
movq -0xd8(%rbp), %rax
incl %eax
cmpl $0x11, %eax
jne 0x2d5b
movq -0xc0(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl $0x10, %ecx
jb 0x2d36
jmp 0x2f4a
leaq 0x2235(%rip), %rdi      # 0x5172
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
xorl %eax, %eax
callq 0x2040
movl 0x51e7(%rip), %r8d      # 0x8138
testq %r8, %r8
je 0x2f9d
movq 0x9213(%rip), %rax      # 0xc170
movl 0x51d9(%rip), %edx      # 0x813c
imull -0x2c(%rbp), %edx
addl -0x30(%rbp), %edx
imull %r8d, %edx
xorl %ecx, %ecx
movq %r8, %rsi
movq %rdx, %rdi
shrq $0x6, %rdi
movq (%rax,%rdi,8), %rdi
btq %rdx, %rdi
adcl $0x0, %ecx
incq %rdx
decq %rsi
jne 0x2f73
jmp 0x2f9f
leaq 0x23bb(%rip), %rdi      # 0x5351
callq 0x21e0
jmp 0x2fc0
xorl %ecx, %ecx
cmpl %r8d, %ecx
jae 0x2fb8
leaq 0x2226(%rip), %rdi      # 0x51d1
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x2150
movl -0x30(%rbp), %ecx
incl %ecx
movl 0x5171(%rip), %esi      # 0x813c
cmpl %esi, %ecx
leaq -0x70(%rbp), %r15
jb 0x2bf2
jmp 0x2fe3
movq %r15, %rdi
callq 0x2260
jmp 0x2fc0
movl 0x5157(%rip), %eax      # 0x8140
movl -0x2c(%rbp), %edx
incl %edx
movl %edx, -0x2c(%rbp)
cmpl %eax, %edx
jb 0x2be3
movq -0x60(%rbp), %rcx
movq -0x58(%rbp), %rdx
movl %eax, %esi
jmp 0x3007
xorl %esi, %esi
movq -0x40(%rbp), %r9
incq %r9
movq %rdx, %rdi
subq %rcx, %rdi
sarq $0x5, %rdi
movq %r9, -0x40(%rbp)
cmpq %rdi, %r9
jb 0x2bce
movq 0x9154(%rip), %rdx      # 0xc180
movl 0x9156(%rip), %ecx      # 0xc188
movq 0x9137(%rip), %rax      # 0xc170
subq %rax, %rdx
shlq $0x3, %rdx
addq %rcx, %rdx
je 0x3092
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorl %esi, %esi
leaq 0x3f(%rcx), %rdi
xorl %r15d, %r15d
leaq 0x3f(%rsi), %r8
testq %rsi, %rsi
cmovnsq %rsi, %r8
sarq $0x6, %r8
leaq (%rax,%r8,8), %r8
movq %rsi, %r9
andq %rdi, %r9
xorl %r10d, %r10d
cmpq %rcx, %r9
setbe %r10b
movq -0x8(%r8,%r10,8), %r8
btq %rsi, %r8
adcq $0x0, %r15
incq %rsi
cmpq %rdx, %rsi
jb 0x3058
jmp 0x3095
xorl %r15d, %r15d
leaq 0x2164(%rip), %rdi      # 0x5200
xorl %r14d, %r14d
movq %r15, %rsi
xorl %eax, %eax
callq 0x2040
movq 0x90d0(%rip), %rax      # 0xc180
movl 0x90d2(%rip), %ecx      # 0xc188
subq 0x90b3(%rip), %rax      # 0xc170
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r15
jae 0x31af
leaq 0x226f(%rip), %rdi      # 0x5340
callq 0x21e0
movq 0x90a3(%rip), %rax      # 0xc180
movl 0x90a5(%rip), %ecx      # 0xc188
movq 0x9086(%rip), %rdx      # 0xc170
movq %rax, %rsi
subq %rdx, %rsi
shlq $0x3, %rsi
addq %rcx, %rsi
je 0x31af
movabsq $-0x8000000000000000, %r13 # imm = 0x8000000000000000
leaq 0x3f(%r13), %r15
xorl %r12d, %r12d
leaq 0x3f(%r12), %rsi
testq %r12, %r12
cmovnsq %r12, %rsi
sarq $0x6, %rsi
leaq (%rdx,%rsi,8), %rsi
movq %r12, %rdi
andq %r15, %rdi
xorl %r8d, %r8d
cmpq %r13, %rdi
setbe %r8b
movq -0x8(%rsi,%r8,8), %rsi
btq %r12, %rsi
jb 0x3197
movl 0x4ff9(%rip), %ecx      # 0x813c
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movl 0x4fe7(%rip), %esi      # 0x8138
xorl %edx, %edx
divq %rsi
movq %rax, %r9
movq %r12, %rax
xorl %edx, %edx
divq %rsi
movq %rdx, %r8
xorl %edx, %edx
divq %rcx
movq %rdx, %rcx
incq %r8
leaq 0x20b4(%rip), %rdi      # 0x522a
movq %r12, %rsi
movq %r9, %rdx
xorl %eax, %eax
callq 0x2040
movq 0x8ff6(%rip), %rax      # 0xc180
movl 0x8ff8(%rip), %ecx      # 0xc188
movq 0x8fd9(%rip), %rdx      # 0xc170
incq %r12
movq %rax, %rsi
subq %rdx, %rsi
movl %ecx, %edi
leaq (%rdi,%rsi,8), %rsi
cmpq %rsi, %r12
jb 0x310e
movq %rbx, %rdi
callq 0x2150
jmp 0x31e4
movq %rbx, %rdi
callq 0x2294
jmp 0x31de
movq 0x4e16(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x1f4e(%rip), %rsi      # 0x5122
movq %r14, %rdx
xorl %eax, %eax
callq 0x21d0
movl $0x1, %r14d
movq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rax
cmpq %rax, %rdi
je 0x31fc
callq 0x2140
leaq -0x60(%rbp), %rdi
callq 0x3328
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x3216
callq 0x2140
movl %r14d, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x3263
jmp 0x3263
jmp 0x3231
movq %rax, %rbx
movq -0x80(%rbp), %rdi
leaq -0x70(%rbp), %rax
jmp 0x3251
jmp 0x3263
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
cmpq %rax, %rdi
je 0x3266
callq 0x2140
jmp 0x3266
jmp 0x3263
jmp 0x3263
jmp 0x3263
movq %rax, %rbx
movq -0xf8(%rbp), %rdi
leaq -0xe8(%rbp), %rax
cmpq %rax, %rdi
je 0x327e
callq 0x2140
leaq -0x60(%rbp), %rdi
callq 0x3328
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x3298
callq 0x2140
movq %rbx, %rdi
callq 0x2210