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

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

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

    if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
        fprintf(stderr,"Autodetect failure\n");
        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();

    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
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2520
cmpl $0x2, -0x8(%rbp)
jge 0x238a
movq 0x5c81(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2c94(%rip), %rsi      # 0x5004
movb $0x0, %al
callq 0x21c0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x24f4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2c75(%rip), %rsi      # 0x500e
callq 0x2170
movq %rax, -0x78(%rbp)
jmp 0x23a4
movq -0x78(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x23e0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x24f4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2540
jmp 0x2509
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2db0
movb %al, -0x79(%rbp)
jmp 0x23f6
movb -0x79(%rbp), %al
testb $0x1, %al
jne 0x242a
jmp 0x23ff
movq 0x5bda(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2c01(%rip), %rsi      # 0x5011
movb $0x0, %al
callq 0x21c0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x24f4
leaq -0x20(%rbp), %rdi
callq 0x2cb0
jmp 0x2435
jmp 0x2437
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2b30
jmp 0x244a
leaq -0x20(%rbp), %rdi
callq 0x2c10
movl %eax, -0x80(%rbp)
jmp 0x2458
movl -0x80(%rbp), %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0x24a6
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x2d40
movl %eax, -0x84(%rbp)
jmp 0x2483
movl -0x84(%rbp), %esi
leaq 0x2b95(%rip), %rdi      # 0x5025
xorl %eax, %eax
callq 0x2050
jmp 0x2499
jmp 0x249b
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2465
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2b30
jmp 0x24b9
jmp 0x24bb
leaq -0x20(%rbp), %rdi
callq 0x2c10
movl %eax, -0x88(%rbp)
jmp 0x24cc
movl -0x88(%rbp), %eax
cmpl $0x0, %eax
ja 0x2437
movq -0x58(%rbp), %rdi
callq 0x2140
jmp 0x24e6
movl $0x0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2540
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2210
nopw %cs:(%rax,%rax)
nopl (%rax)