// File: 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
subq $0x90, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x25f0
cmpl $0x4, -0x8(%rbp)
jge 0x2446
movq 0x5c6d(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x2c80(%rip), %rsi      # 0x5004
movb $0x0, %al
callq 0x21c0
movq 0x5c4e(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2c84(%rip), %rsi      # 0x5020
movb $0x0, %al
callq 0x21c0
movq 0x5c36(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2c93(%rip), %rsi      # 0x5047
movb $0x0, %al
callq 0x21c0
movq 0x5c1e(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2cc7(%rip), %rsi      # 0x5093
movb $0x0, %al
callq 0x21c0
movq 0x5c06(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2d03(%rip), %rsi      # 0x50e7
movb $0x0, %al
callq 0x21c0
movq 0x5bee(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2d32(%rip), %rsi      # 0x512e
movb $0x0, %al
callq 0x21c0
movq 0x5bd6(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2cd1(%rip), %rsi      # 0x50e5
movb $0x0, %al
callq 0x21c0
movq 0x5bbe(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x2d3f(%rip), %rsi      # 0x516b
movb $0x0, %al
callq 0x21c0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x25c4
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2d53(%rip), %rsi      # 0x51a8
callq 0x2170
movq %rax, -0x78(%rbp)
jmp 0x2460
movq -0x78(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x249c
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x25c4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2610
jmp 0x25d9
leaq -0x20(%rbp), %rdi
callq 0x2d80
jmp 0x24a7
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x21e0
movl %eax, -0x18(%rbp)
cmpl $0xa, -0x18(%rbp)
jb 0x24c6
cmpl $0x1f4, -0x18(%rbp)     # imm = 0x1F4
jbe 0x24d9
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x25c4
movq -0x10(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x21e0
movl %eax, -0x14(%rbp)
cmpl $0x3, -0x14(%rbp)
jb 0x24f8
cmpl $0x190, -0x14(%rbp)     # imm = 0x190
jbe 0x250b
movl $0x1, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
jmp 0x25c4
jmp 0x250d
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2c00
jmp 0x2520
leaq -0x20(%rbp), %rdi
callq 0x2ce0
movl %eax, -0x7c(%rbp)
jmp 0x252e
movl -0x7c(%rbp), %eax
movl %eax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x68(%rbp), %eax
jae 0x2576
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x2e10
movl %eax, -0x80(%rbp)
jmp 0x2556
movl -0x80(%rbp), %esi
leaq 0x2c4b(%rip), %rdi      # 0x51ab
xorl %eax, %eax
callq 0x2050
jmp 0x2569
jmp 0x256b
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x253b
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2c00
jmp 0x2589
jmp 0x258b
leaq -0x20(%rbp), %rdi
callq 0x2ce0
movl %eax, -0x84(%rbp)
jmp 0x259c
movl -0x84(%rbp), %eax
cmpl $0x0, %eax
ja 0x250d
movq -0x58(%rbp), %rdi
callq 0x2140
jmp 0x25b6
movl $0x0, -0x4(%rbp)
movl $0x1, -0x4c(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2610
movl -0x4(%rbp), %eax
addq $0x90, %rsp
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2220
nopw %cs:(%rax,%rax)
nopl (%rax)