// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x810, %rsp            # imm = 0x810
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
xorl %r12d, %r12d
movq %rdx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
andq $0x0, (%rbx)
cmpq $0x100, %r12            # imm = 0x100
je 0x3d60
andq $0x0, -0x830(%rbp,%r12,8)
incq %r12
jmp 0x3d2f
cmpb $0x1, 0x4(%r15)
jne 0x3d60
movl (%r15), %eax
cmpq $0xff, %rax
ja 0x3d60
incq -0x830(%rbp,%rax,8)
movq %r15, %rdi
movq %r14, %rsi
callq 0x3a28
testb %al, %al
jne 0x3d46
xorl %r15d, %r15d
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
pushq $-0x1
popq %rsi
xorl %eax, %eax
cmpq $0x100, %r15            # imm = 0x100
je 0x3db8
movq -0x830(%rbp,%r15,8), %rcx
cmpq %rcx, %rsi
movq %rcx, %rdx
cmovbq %rsi, %rdx
cmpq %rcx, %rax
movq %rcx, %rdi
cmovaq %rax, %rdi
testq %rcx, %rcx
cmovneq %rdx, %rsi
cmovneq %rdi, %rax
incq %r15
jmp 0x3d83
cmpq $-0x1, %rsi
sete %dl
leaq (,%rsi,4), %rcx
cmpq %rcx, %rax
setb %dil
xorl %ecx, %ecx
orb %dl, %dil
jne 0x3eba
subq %rsi, %rax
pushq $0x64
popq %rdi
xorl %edx, %edx
divq %rdi
addq %rsi, %rax
movl $0x100, %edx            # imm = 0x100
xorl %esi, %esi
cmpq $0x3, %rsi
je 0x3e5a
andl $0x0, -0x2c(%rbp,%rsi,4)
movl %ecx, %edi
cmpl $0x101, %ecx            # imm = 0x101
cmovbl %edx, %ecx
cmpq $0xff, %rdi
ja 0x3e1e
cmpq %rax, -0x830(%rbp,%rdi,8)
jae 0x3e1c
incq %rdi
jmp 0x3e04
movl %edi, %ecx
movl %ecx, %edi
cmpl $0x101, %ecx            # imm = 0x101
cmovbl %edx, %ecx
xorl %r8d, %r8d
cmpq $0xff, %rdi
ja 0x3e55
movq -0x830(%rbp,%rdi,8), %r9
cmpq %rax, %r9
jb 0x3e53
cmpq %r9, %r8
jae 0x3e4e
movl %edi, -0x2c(%rbp,%rsi,4)
movq %r9, %r8
incq %rdi
jmp 0x3e2c
movl %edi, %ecx
incq %rsi
jmp 0x3dee
movl -0x2c(%rbp), %eax
movl -0x28(%rbp), %esi
testl %eax, %eax
sete %cl
testl %esi, %esi
sete %dl
orb %cl, %dl
jne 0x3eb8
movl %esi, %ecx
subl %eax, %ecx
movl %ecx, 0xc(%rbx)
cmpl $0x8, %ecx
jb 0x3eb8
movl -0x24(%rbp), %edx
testl %edx, %edx
je 0x3eaa
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 0x3eaa
addq %rsi, %rdx
shrq %rdx
movl %edx, 0xc(%rbx)
movl %edx, %ecx
movl %eax, 0x8(%rbx)
subl %ecx, %eax
jbe 0x3eb8
movl %eax, 0x8(%rbx)
movb $0x1, %cl
jmp 0x3eba
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x810, %rsp            # imm = 0x810
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq