// File: /workspace/github_cpp_makefile_star2_O1/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 $0x828, %rsp            # imm = 0x828
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movq %rdx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2170
movq $0x0, (%rbx)
leaq -0x840(%rbp), %rdi
movl $0x800, %edx            # imm = 0x800
xorl %esi, %esi
callq 0x20d0
movq %r15, %rdi
movq %r14, %rsi
callq 0x3cf4
testb %al, %al
je 0x401a
cmpb $0x1, 0x4(%r15)
jne 0x3fef
movl (%r15), %eax
cmpq $0xff, %rax
ja 0x3fef
incq -0x840(%rbp,%rax,8)
jmp 0x3fef
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 -0x840(%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 0x4032
cmpq $-0x1, %rsi
sete %dl
leaq (,%rsi,4), %rcx
cmpq %rcx, %rax
setb %dil
xorl %ecx, %ecx
orb %dl, %dil
jne 0x417d
subq %rsi, %rax
shrq $0x2, %rax
movabsq $0x28f5c28f5c28f5c3, %rdx # imm = 0x28F5C28F5C28F5C3
mulq %rdx
shrq $0x2, %rdx
addq %rsi, %rdx
xorl %eax, %eax
movq $0x0, -0x40(%rbp,%rax,8)
movl $0x0, -0x24(%rbp,%rax,4)
cmpl $0xff, %ecx
ja 0x40df
movl %ecx, %ecx
cmpq %rdx, -0x840(%rbp,%rcx,8)
jae 0x40df
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x40c4
movl $0x100, %ecx            # imm = 0x100
cmpl $0xff, %ecx
ja 0x4117
movl %ecx, %ecx
movq -0x840(%rbp,%rcx,8), %rsi
cmpq %rdx, %rsi
jb 0x4117
cmpq %rsi, -0x40(%rbp,%rax,8)
jae 0x4106
movq %rsi, -0x40(%rbp,%rax,8)
movl %ecx, -0x24(%rbp,%rax,4)
incq %rcx
cmpq $0x100, %rcx            # imm = 0x100
jne 0x40e9
movl $0x100, %ecx            # imm = 0x100
incq %rax
cmpq $0x3, %rax
jne 0x40a9
movl -0x24(%rbp), %eax
movl -0x20(%rbp), %esi
testl %eax, %eax
sete %cl
testl %esi, %esi
sete %dl
orb %cl, %dl
jne 0x417b
movl %esi, %edx
subl %eax, %edx
movl %edx, 0xc(%rbx)
cmpl $0x8, %edx
jb 0x417b
movl -0x1c(%rbp), %ecx
testl %ecx, %ecx
je 0x416c
subl %esi, %ecx
movl %edx, %esi
movq %rcx, %rdi
subq %rsi, %rdi
movq %rdi, %r8
negq %r8
cmovsq %rdi, %r8
shrl $0x2, %edx
cmpl %edx, %r8d
jae 0x416c
addq %rsi, %rcx
shrq %rcx
movl %ecx, 0xc(%rbx)
movl %eax, 0x8(%rbx)
subl 0xc(%rbx), %eax
jbe 0x417b
movl %eax, 0x8(%rbx)
movb $0x1, %cl
jmp 0x417d
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x828, %rsp            # imm = 0x828
popq %rbx
popq %r14
popq %r15
popq %rbp
retq