// File: /workspace/github_cpp_makefile_star2_O2/joncampbell123[P]floppytools/kryoflux3.cpp
// Function: main
int main(int argc,char **argv) {
    struct flux_bits fb;
    struct kryoflux_event ev;

    if (argc < 4) {
        fprintf(stderr,"%s <raw> <shortest> <dist>\n",argv[0]);
        fprintf(stderr," shortest = distance of one bit cell.\n");
        fprintf(stderr,"            use the histogram function in kryoflux2.cpp to determine this.\n");
        fprintf(stderr,"            for an MFM encoded disk, the 1010101010 distance will be the shortest,\n");
        fprintf(stderr,"            therefore take that and subtract distance and apply here.\n");
        fprintf(stderr," dist =     distance between 10101010101 and 1001001001001.\n");
        fprintf(stderr,"\n");
        fprintf(stderr,"In my tests with a 1.44MB MFM, shortest = 26 and dist = 23.\n");
        return 1;
    }

    FILE *fp = fopen(argv[1],"rb");
    if (fp == NULL) return 1;

    /* TODO: autodetection is possible (for MFM at least).
     *       look at the histogram code in kryoflux2.cpp */

    /* from my tests:
     *
     * distance of 10101010101010 = 49
     * change in distance to encode 1001001001001001 = 23.
     *
     * shortest is therefore 49 - 23 = 26 which is the distance to encode 111111111111 */

    fb.clear();

    fb.shortest = atoi(argv[2]);
    if (fb.shortest < 10 || fb.shortest > 500) return 1;

    fb.dist = atoi(argv[3]);
    if (fb.dist < 3 || fb.dist > 400) return 1;

    do {
        kryoflux_bits_refill(fb,ev,fp);

        unsigned int c = fb.avail();
        for (unsigned int i=0;i < c;i++) printf("%u",fb.get(1));

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

    fclose(fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %r15
leaq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
cmpl $0x3, %edi
jle 0x13da
movq 0x8(%r15), %rdi
leaq 0x1e95(%rip), %rsi      # 0x31a8
callq 0x1110
pushq $0x1
popq %rbx
testq %rax, %rax
je 0x1490
movq %rax, %r14
leaq -0x38(%rbp), %rdi
callq 0x172c
movq 0x10(%r15), %rdi
callq 0x1180
movl %eax, -0x30(%rbp)
addl $0xfffffe0b, %eax       # imm = 0xFFFFFE0B
cmpl $0xfffffe15, %eax       # imm = 0xFFFFFE15
jb 0x1490
movq 0x18(%r15), %rdi
callq 0x1180
movl %eax, -0x2c(%rbp)
addl $0xfffffe6f, %eax       # imm = 0xFFFFFE6F
cmpl $0xfffffe72, %eax       # imm = 0xFFFFFE72
jb 0x1490
leaq -0x38(%rbp), %r15
pushq $0x1
popq %r13
leaq 0x1e34(%rip), %rbx      # 0x31ab
movq %r15, %rdi
leaq -0x60(%rbp), %rsi
movq %r14, %rdx
callq 0x1640
movq %r15, %rdi
callq 0x16ca
movl %eax, %r12d
subl $0x1, %r12d
jb 0x13b0
movq %r15, %rdi
movl %r13d, %esi
callq 0x1772
movq %rbx, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
jmp 0x1391
movq %r15, %rdi
leaq -0x60(%rbp), %rsi
movq %r14, %rdx
callq 0x1640
movq %r15, %rdi
callq 0x16ca
testl %eax, %eax
jne 0x1377
movq %r14, %rdi
callq 0x10e0
xorl %ebx, %ebx
jmp 0x1490
movq 0x3bff(%rip), %r12      # 0x4fe0
movq (%r12), %rdi
movq (%r15), %rdx
leaq 0x1c15(%rip), %rsi      # 0x3004
xorl %eax, %eax
callq 0x1170
movq (%r12), %rcx
leaq 0x1c1f(%rip), %rdi      # 0x3020
pushq $0x26
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1c2d(%rip), %rdi      # 0x3047
pushq $0x4b
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1c63(%rip), %rdi      # 0x3093
pushq $0x53
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1ca1(%rip), %rdi      # 0x30e7
pushq $0x46
popq %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rcx
leaq 0x1cd2(%rip), %rdi      # 0x312e
pushq $0x3c
popq %r14
movq %r14, %rsi
movq %rbx, %rdx
callq 0x11d0
movq (%r12), %rsi
pushq $0xa
popq %rdi
callq 0x1140
movq (%r12), %rcx
leaq 0x1ce9(%rip), %rdi      # 0x316b
movq %r14, %rsi
movq %rbx, %rdx
callq 0x11d0
pushq $0x1
popq %rbx
leaq -0x50(%rbp), %rdi
callq 0x14c4
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x14b0
jmp 0x14b0
jmp 0x14b0
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x14c4
movq %rbx, %rdi
callq 0x11b0