// File: /workspace/github_cpp_makefile_star2_O1/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, -0x38(%rbp)
xorl %ecx, %ecx
movq %rcx, -0xf8(%rbp)
xorps %xmm0, %xmm0
movups %xmm0, -0x108(%rbp)
movq %rcx, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
leaq -0xe0(%rbp), %rax
movq %rax, -0x10(%rax)
movq %rcx, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x2, %edi
jl 0x310c
movq $0x0, -0x40(%rbp)
movl %edi, %r14d
movb $0x1, %r13b
movl $0x1, %r12d
leaq -0x1a0(%rbp), %rbx
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %r15
cmpb $0x2d, (%r15)
jne 0x2b0a
cmpb $0x2d, 0x1(%r15)
leaq 0x1(%r15), %r15
je 0x2ac9
movq %r15, %rdi
leaq 0x2639(%rip), %rsi      # 0x5117
callq 0x21c0
testl %eax, %eax
je 0x2b42
movq %r15, %rdi
leaq 0x262c(%rip), %rsi      # 0x511d
callq 0x21c0
testl %eax, %eax
jne 0x2b89
movl $0x9, 0x5630(%rip)      # 0x8138
jmp 0x2b4c
movq %rbx, %rdi
movq %r15, %rsi
leaq -0xc0(%rbp), %rdx
callq 0x2090
leaq -0x70(%rbp), %rdi
movq %rbx, %rsi
callq 0x3412
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
cmpq %rax, %rdi
je 0x2b6a
callq 0x2140
jmp 0x2b6a
movl $0x12, 0x55ec(%rip)     # 0x8138
movl $0x2, 0x55e6(%rip)      # 0x813c
movl $0x50, 0x55e0(%rip)     # 0x8140
movl $0x200, 0x55da(%rip)    # imm = 0x200
movb $0x1, %bl
testb %bl, %bl
leaq -0x1a0(%rbp), %rbx
je 0x2bb1
incq %r12
cmpq %r14, %r12
setb %r13b
jne 0x2abb
jmp 0x2bb1
movq 0x5450(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
xorl %ebx, %ebx
leaq 0x2586(%rip), %rsi      # 0x5122
movq %r15, %rdx
xorl %eax, %eax
callq 0x21d0
movl $0x1, %eax
movq %rax, -0x40(%rbp)
jmp 0x2b6c
testb $0x1, %r13b
movq -0x40(%rbp), %rbx
jne 0x323c
leaq 0x256f(%rip), %rdi      # 0x5135
leaq 0x2571(%rip), %rsi      # 0x513e
callq 0x2180
testq %rax, %rax
je 0x3115
movq %rax, %rbx
movl 0x5554(%rip), %esi      # 0x8138
imull 0x5551(%rip), %esi      # 0x813c
imull 0x554e(%rip), %esi      # 0x8140
leaq 0x9577(%rip), %rdi      # 0xc170
xorl %edx, %edx
callq 0x3312
movq -0x68(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x309f
leaq -0xb0(%rbp), %r15
leaq -0x80(%rbp), %r14
leaq -0x118(%rbp), %r13
movq $0x0, -0x48(%rbp)
movq %rbx, -0xd0(%rbp)
cmpl $0x0, 0x550a(%rip)      # 0x8140
je 0x307f
movl $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rax
shlq $0x5, %rax
movq %rax, -0xc8(%rbp)
cmpl $0x0, 0x54e3(%rip)      # 0x813c
je 0x306b
xorl %ecx, %ecx
leaq -0x1a0(%rbp), %rdi
leaq 0x24d2(%rip), %rsi      # 0x5141
movl -0x2c(%rbp), %edx
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x2050
movq -0x70(%rbp), %rax
leaq -0x90(%rbp), %rcx
movq %rcx, -0xa0(%rbp)
movq -0xc8(%rbp), %rcx
movq (%rax,%rcx), %rsi
movq 0x8(%rax,%rcx), %rdx
addq %rsi, %rdx
leaq -0xa0(%rbp), %r12
movq %r12, %rdi
callq 0x3c5a
movq %r12, %rdi
leaq 0x2498(%rip), %rsi      # 0x5152
callq 0x2230
movq %r12, %rdi
leaq -0x1a0(%rbp), %rsi
callq 0x2230
movq %r15, -0xc0(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x2cf7
movq %rdx, -0xc0(%rbp)
movq (%rcx), %rdx
movq %rdx, -0xb0(%rbp)
jmp 0x2cfe
movups (%rcx), %xmm0
movups %xmm0, (%r15)
movq 0x8(%rax), %rdx
movq %rdx, -0xb8(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xf0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x2190
movq -0xc0(%rbp), %rdi
cmpq %r15, %rdi
je 0x2d3c
callq 0x2140
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x2d54
callq 0x2140
movq -0xf0(%rbp), %rsi
leaq 0x23f2(%rip), %rdi      # 0x5154
xorl %eax, %eax
callq 0x2040
movq -0xf0(%rbp), %rdi
leaq 0x23e4(%rip), %rsi      # 0x515b
callq 0x2180
testq %rax, %rax
je 0x3016
movq %rax, %r15
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x3fb0
testb %al, %al
je 0x3061
movl -0x78(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movq -0x40(%rbp), %rax
leal -0x10(%rax), %r9d
cmpl $0x9, %eax
cmovll %eax, %r9d
movq -0x38(%rbp), %rcx
leal -0x10(%rcx), %eax
cmpl $0x9, %ecx
cmovll %ecx, %eax
movl %r9d, %ecx
movl $0x15, %r12d
addl -0x50(%rbp), %ecx
jle 0x2f95
movl %ecx, -0x78(%rbp)
movl %eax, %ecx
addl -0x4c(%rbp), %ecx
movl -0x30(%rbp), %edx
jle 0x2f95
movl %ecx, -0x74(%rbp)
movl 0x5339(%rip), %r11d     # 0x813c
movl 0x532e(%rip), %r8d      # 0x8138
testq %r8, %r8
je 0x2e46
movq 0x935a(%rip), %r10      # 0xc170
movl -0x2c(%rbp), %ecx
movl %ecx, %esi
imull %r11d, %esi
addl %edx, %esi
imull %r8d, %esi
xorl %ecx, %ecx
movq %r8, %rdi
movq %rsi, %r12
shrq $0x6, %r12
movq (%r10,%r12,8), %r12
btq %rsi, %r12
adcl $0x0, %ecx
incq %rsi
decq %rdi
jne 0x2e2a
jmp 0x2e48
xorl %ecx, %ecx
cmpl %r8d, %ecx
jae 0x2f6c
movl %eax, (%rsp)
leaq 0x233a(%rip), %rdi      # 0x5195
movl -0x2c(%rbp), %esi
xorl %eax, %eax
movq %r8, %r13
movq %r11, %r12
callq 0x2040
cmpl $0x0, 0x52c6(%rip)      # 0x8138
leaq 0x2355(%rip), %rbx      # 0x51ce
je 0x2ecc
movq %r12, %r8
imull -0x2c(%rbp), %r8d
addl -0x30(%rbp), %r8d
imull %r13d, %r8d
xorl %r12d, %r12d
leaq (%r8,%r12), %rax
movq 0x92d7(%rip), %rcx      # 0xc170
movq %rax, %rdx
shrq $0x6, %rdx
movq (%rcx,%rdx,8), %rcx
xorl %esi, %esi
btq %rax, %rcx
setb %sil
movq %rbx, %rdi
xorl %eax, %eax
movq %r8, %r13
callq 0x2040
movq %r13, %r8
incq %r12
movl 0x5271(%rip), %eax      # 0x8138
cmpq %rax, %r12
jb 0x2e8e
movl $0xa, %edi
callq 0x21b0
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq %r14, %rdi
callq 0x3f54
leaq -0x118(%rbp), %r13
movq -0xd0(%rbp), %rbx
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3e82
movq %r14, %rdi
callq 0x3f08
cmpl $0x10, %eax
jb 0x2f49
movq %r14, %rdi
movl $0x10, %esi
callq 0x3f62
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x2f3a
movq %rbx, %rdi
movq %r14, %rsi
movq %r13, %rdx
movq %r15, %rcx
callq 0x249c
jmp 0x2f06
movq %r14, %rdi
movl $0x1, %esi
callq 0x3f88
jmp 0x2f06
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x3e82
xorl %r12d, %r12d
testb %al, %al
je 0x2f95
movq %r14, %rdi
callq 0x3f08
testl %eax, %eax
jne 0x2ef8
jmp 0x2f95
leaq 0x21ff(%rip), %rdi      # 0x5172
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x2040
movl $0x13, %r12d
movl $0x63, %eax
movq %rax, -0x40(%rbp)
movl $0x63, %eax
movq %rax, -0x38(%rbp)
cmpl $0x15, %r12d
je 0x2fa0
testl %r12d, %r12d
jne 0x2fb4
movq -0x38(%rbp), %rcx
leal 0x1(%rcx), %eax
cmpl $0x10, %ecx
movq %rax, -0x38(%rbp)
jl 0x2dba
movq -0x40(%rbp), %rcx
leal 0x1(%rcx), %eax
movl $0x0, %edx
movq %rdx, -0x38(%rbp)
cmpl $0x10, %ecx
movq %rax, -0x40(%rbp)
jl 0x2dba
movl 0x5160(%rip), %r8d      # 0x8138
testq %r8, %r8
je 0x3024
movq 0x918c(%rip), %rax      # 0xc170
movl 0x5152(%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 0x2ffa
jmp 0x3026
leaq 0x2334(%rip), %rdi      # 0x5351
callq 0x21e0
jmp 0x3047
xorl %ecx, %ecx
cmpl %r8d, %ecx
jae 0x303f
leaq 0x219f(%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
cmpl 0x50ea(%rip), %ecx      # 0x813c
leaq -0xb0(%rbp), %r15
jb 0x2c61
jmp 0x306b
movq %r15, %rdi
callq 0x2260
jmp 0x3047
movl -0x2c(%rbp), %eax
incl %eax
movl %eax, -0x2c(%rbp)
cmpl 0x50c7(%rip), %eax      # 0x8140
jb 0x2c52
movq -0x48(%rbp), %rdx
incq %rdx
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
movq %rdx, -0x48(%rbp)
cmpq %rax, %rdx
jb 0x2c2f
movq 0x90da(%rip), %rdx      # 0xc180
movl 0x90dc(%rip), %ecx      # 0xc188
movq 0x90bd(%rip), %rax      # 0xc170
subq %rax, %rdx
shlq $0x3, %rdx
addq %rcx, %rdx
je 0x311f
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorl %esi, %esi
leaq 0x3f(%rcx), %rdi
xorl %r14d, %r14d
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, %r14
incq %rsi
cmpq %rdx, %rsi
jb 0x30d2
jmp 0x3122
movq -0x38(%rbp), %rdi
callq 0x2294
movl $0x1, %ebx
jmp 0x323c
xorl %r14d, %r14d
leaq 0x20d7(%rip), %rdi      # 0x5200
movq %r14, %rsi
xorl %eax, %eax
callq 0x2040
movq 0x9046(%rip), %rax      # 0xc180
movl 0x9048(%rip), %ecx      # 0xc188
subq 0x9029(%rip), %rax      # 0xc170
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r14
jae 0x3232
leaq 0x21e5(%rip), %rdi      # 0x5340
callq 0x21e0
movq 0x9019(%rip), %rcx      # 0xc180
movl 0x901b(%rip), %edx      # 0xc188
movq 0x8ffc(%rip), %rax      # 0xc170
subq %rax, %rcx
shlq $0x3, %rcx
addq %rdx, %rcx
je 0x3232
movabsq $-0x8000000000000000, %r12 # imm = 0x8000000000000000
leaq 0x3f(%r12), %r13
leaq 0x2090(%rip), %r14      # 0x522a
xorl %r15d, %r15d
leaq 0x3f(%r15), %rcx
testq %r15, %r15
cmovnsq %r15, %rcx
sarq $0x6, %rcx
leaq (%rax,%rcx,8), %rax
movq %r15, %rcx
andq %r13, %rcx
xorl %edx, %edx
cmpq %r12, %rcx
setbe %dl
movq -0x8(%rax,%rdx,8), %rax
btq %r15, %rax
jb 0x320b
movl 0x4f6d(%rip), %ecx      # 0x813c
movq %r15, %rax
xorl %edx, %edx
divq %rcx
movl 0x4f5b(%rip), %esi      # 0x8138
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
movq 0x8f6b(%rip), %rcx      # 0xc180
movl 0x8f6d(%rip), %edx      # 0xc188
movq 0x8f4e(%rip), %rax      # 0xc170
subq %rax, %rcx
leaq (%rdx,%rcx,8), %rcx
cmpq %rcx, %r15
jb 0x319d
movq %rbx, %rdi
callq 0x2150
xorl %ebx, %ebx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x3254
callq 0x2140
leaq -0x70(%rbp), %rdi
callq 0x3386
movq -0x108(%rbp), %rdi
testq %rdi, %rdi
je 0x326e
callq 0x2140
movl %ebx, %eax
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x32c0
movq %rax, %rbx
movq -0x1a0(%rbp), %rdi
leaq -0x190(%rbp), %rax
jmp 0x32ae
jmp 0x32c0
jmp 0x32c0
jmp 0x329d
movq %rax, %rbx
movq -0xa0(%rbp), %rdi
leaq -0x90(%rbp), %rax
cmpq %rax, %rdi
je 0x32c3
callq 0x2140
jmp 0x32c3
jmp 0x32c0
jmp 0x32c0
jmp 0x32c0
movq %rax, %rbx
movq -0xf0(%rbp), %rdi
leaq -0xe0(%rbp), %rax
cmpq %rax, %rdi
je 0x32db
callq 0x2140
leaq -0x70(%rbp), %rdi
callq 0x3386
movq -0x108(%rbp), %rdi
testq %rdi, %rdi
je 0x32f5
callq 0x2140
movq %rbx, %rdi
callq 0x2210
nop