// File: /workspace/github_cpp_makefile_star2_O3/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
xorps %xmm0, %xmm0
movups %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x3, %edi
jle 0x152a
movq 0x8(%r15), %rdi
leaq 0x1d55(%rip), %rsi      # 0x31a8
callq 0x1110
movl $0x1, %r14d
testq %rax, %rax
je 0x1538
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
callq 0x17c8
movq 0x10(%r15), %rdi
callq 0x1160
movl %eax, -0x30(%rbp)
addl $0xfffffe0b, %eax       # imm = 0xFFFFFE0B
cmpl $0xfffffe15, %eax       # imm = 0xFFFFFE15
jb 0x1538
movq 0x18(%r15), %rdi
callq 0x1160
movl %eax, -0x2c(%rbp)
addl $0xfffffe6f, %eax       # imm = 0xFFFFFE6F
cmpl $0xfffffe72, %eax       # imm = 0xFFFFFE72
jb 0x1538
leaq -0x38(%rbp), %r14
leaq -0x60(%rbp), %r15
leaq 0x1cf1(%rip), %r12      # 0x31ab
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x16fa
movq %r14, %rdi
callq 0x177c
movl %eax, %r13d
testl %eax, %eax
je 0x14f5
movq %r14, %rdi
movl $0x1, %esi
callq 0x17fc
movq %r12, %rdi
movl %eax, %esi
xorl %eax, %eax
callq 0x1040
decl %r13d
jne 0x14d7
movq %r14, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x16fa
movq %r14, %rdi
callq 0x177c
testl %eax, %eax
jne 0x14ba
movq %rbx, %rdi
callq 0x10e0
movq -0x50(%rbp), %rdi
xorl %r14d, %r14d
testq %rdi, %rdi
je 0x1538
callq 0x10d0
jmp 0x1538
movq %r15, %rdi
callq 0x11e0
movl $0x1, %r14d
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1550
jmp 0x1550
jmp 0x1550
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x1561
callq 0x10d0
movq %rbx, %rdi
callq 0x1190
nopl (%rax)