// File: /workspace/github_cpp_makefile_star2_O3/joncampbell123[P]floppytools/kryocomm.cpp
// Function: autodetect_flux_bits_mfm(flux_bits&, kryoflux_event&, _IO_FILE*)
bool autodetect_flux_bits_mfm(struct flux_bits &fb,struct kryoflux_event &ev,FILE *fp) {
    fseek(fp,0,SEEK_SET);
    fb.clear();

    unsigned long hist[256];
    for (unsigned long i=0;i < 256;i++) hist[i] = 0;

    while (kryoflux_read(ev,fp)) {
        if (ev.message == MSG_FLUX) {
            unsigned long ofs = ev.flux_interval;
            if (ofs < 256ul) hist[ofs]++;
        }
    }

    fseek(fp,0,SEEK_SET);

    unsigned long min = ~0UL,max = 0;
    for (unsigned int i=0;i < 256;i++) {
        if (hist[i] != 0ul) {
            if (min > hist[i]) min = hist[i];
            if (max < hist[i]) max = hist[i];
        }
    }

    if (min == ~0UL)
        return false;

    if (max < (min * 4ul))
        return false;

    unsigned long cutoff = min + ((max - min) / 100ul);

    /* look for the first peak, which represents 1010101010101010101010 */
    unsigned int scan = 0;

    unsigned long peak_max[3];
    unsigned int peak_idx[3];

//  fprintf(stderr,"autoscan: min=%lu max=%lu cutoff=%lu\n",min,max,cutoff);

    for (unsigned int peak=0;peak < 3;peak++) {
        peak_max[peak] = 0;
        peak_idx[peak] = 0;
        while (scan < 256 && hist[scan] < cutoff) scan++;
        while (scan < 256 && hist[scan] >= cutoff) {
            if (peak_max[peak] < hist[scan]) {
                peak_max[peak] = hist[scan];
                peak_idx[peak] = scan;
            }
            scan++;
        }
    }

//    printf("autoscan: peaks at %u, %u, %u [%lu, %lu, %lu]\n",
//        peak_idx[0],peak_idx[1],peak_idx[2],
//        peak_max[0],peak_max[1],peak_max[2]);

    /* we require two peaks at least */
    if (peak_idx[0] == 0 || peak_idx[1] == 0)
        return false;

    fb.dist = peak_idx[1] - peak_idx[0];
    if (fb.dist < 8)
        return false;

    if (peak_idx[2] != 0) {
        unsigned long dist2 = peak_idx[2] - peak_idx[1];

//        printf("autoscan: dist=%lu dist2=%lu\n",fb.dist,dist2);
        if (labs((signed long)dist2 - (signed long)fb.dist) < (fb.dist / 4ul)) {
//            printf("autoscan: using peaks 1-2 to average dist (diff=%ld)\n",(signed long)dist2 - (signed long)fb.dist);
            fb.dist = (fb.dist + dist2) / 2ul;
        }
    }

    fb.shortest = peak_idx[0];
    if (fb.shortest <= fb.dist)
        return false;

    /* peak[0] represents 101010101010101010 so to get the shortest (1111111)
     * subtract dist from it */
    fb.shortest -= fb.dist;

//    printf("autoscan: final params shortest=%lu dist=%lu\n",fb.shortest,fb.dist);
    return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x818, %rsp            # imm = 0x818
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq %rdx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq $0x0, (%rbx)
leaq -0x830(%rbp), %rdi
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x20d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3ca4
testb %al, %al
je 0x3fc6
cmpb $0x1, 0x4(%r15)
jne 0x3f9b
movl (%r15), %eax
cmpq $0xff, %rax
ja 0x3f9b
incq -0x830(%rbp,%rax,8)
jmp 0x3f9b
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq $-0x1, %rsi
xorl %eax, %eax
movq %rsi, %rcx
movq %rax, %rdx
movq -0x830(%rbp,%r15,8), %rdi
cmpq %rdi, %rsi
movq %rdi, %rsi
cmovbq %rcx, %rsi
cmpq %rdi, %rax
movq %rdi, %rax
cmovaq %rdx, %rax
testq %rdi, %rdi
cmoveq %rcx, %rsi
cmoveq %rdx, %rax
incq %r15
cmpq $0x100, %r15            # imm = 0x100
jne 0x3fde
cmpq $-0x1, %rsi
sete %dl
leaq (,%rsi,4), %rcx
cmpq %rcx, %rax
setb %dil
xorl %ecx, %ecx
orb %dl, %dil
jne 0x411d
subq %rsi, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rdx # imm = 0x28F5C28F5C28F5C3
mulq %rdx
shrq $0x2, %rdx
addq %rsi, %rdx
xorl %eax, %eax
movl $0x0, -0x24(%rbp,%rax,4)
cmpl $0xff, %ecx
ja 0x40b4
movl %ecx, %ecx
cmpq %rdx, -0x830(%rbp,%rcx,8)
jae 0x407f
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x4067
jmp 0x40af
cmpq $0x100, %rcx            # imm = 0x100
jae 0x40b4
xorl %esi, %esi
movq -0x830(%rbp,%rcx,8), %rdi
cmpq %rdx, %rdi
jb 0x40b4
cmpq %rdi, %rsi
jae 0x40a3
movl %ecx, -0x24(%rbp,%rax,4)
movq %rdi, %rsi
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x408a
movl $0x100, %ecx            # imm = 0x100
incq %rax
cmpq $0x3, %rax
jne 0x4055
movl -0x24(%rbp), %eax
movl -0x20(%rbp), %esi
testl %eax, %eax
sete %cl
testl %esi, %esi
sete %dl
orb %cl, %dl
jne 0x411b
movl %esi, %ecx
subl %eax, %ecx
movl %ecx, 0xc(%rbx)
cmpl $0x8, %ecx
jb 0x411b
movl -0x1c(%rbp), %edx
testl %edx, %edx
je 0x410d
subl %esi, %edx
movl %ecx, %esi
movq %rdx, %rdi
subq %rsi, %rdi
movq %rdi, %r8
negq %r8
cmovsq %rdi, %r8
movl %ecx, %edi
shrl $0x2, %edi
cmpl %edi, %r8d
jae 0x410d
addq %rsi, %rdx
shrq %rdx
movl %edx, 0xc(%rbx)
movl %edx, %ecx
movl %eax, 0x8(%rbx)
subl %ecx, %eax
jbe 0x411b
movl %eax, 0x8(%rbx)
movb $0x1, %cl
jmp 0x411d
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x818, %rsp            # imm = 0x818
popq %rbx
popq %r14
popq %r15
popq %rbp
retq