// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: win_cluster_fliter(window_list*, unsigned long, unsigned long, double)
uint64_t win_cluster_fliter(window_list *wa, uint64_t wn, uint64_t min_ovlp, double o_rate)
{
    uint64_t k, ws = (uint64_t)-1, we = (uint64_t)-1, aln_ol, sk;
    for (k = 0; k < wn && wa[k].y_end == -1; k++);
    if(k >= wn) return 0; ws = wa[k].x_start;

    for (aln_ol = 0, sk = k; k < wn; k++) {
        we = wa[k].x_end+1; 
        if(wa[k].y_end != -1) {
            aln_ol += wa[k].x_end+1-wa[k].x_start;
        }
        if(we >= ws + min_ovlp) break;
    }
    // fprintf(stderr, "[M::%s::] aln_ol::%lu, w::[%lu, %lu), min_ovlp::%lu, o_rate::%f\n", 
    // __func__, aln_ol, ws, we, min_ovlp, o_rate);
    if((aln_ol >= (we-ws)*o_rate) && (we-ws >= min_ovlp)) return 1;

    for (k++; k < wn; k++) {
        we = wa[k].x_end+1; 
        if(wa[k].y_end != -1) {
            aln_ol += wa[k].x_end+1-wa[k].x_start;
        }
        for (; sk < wn && wa[sk].x_start + min_ovlp < we; sk++) {
            if(wa[sk].y_end != -1) aln_ol -= wa[sk].x_end+1-wa[sk].x_start;
        }
        ws = wa[sk].x_start; 
        // fprintf(stderr, "+[M::%s::] aln_ol::%lu, w::[%lu, %lu), min_ovlp::%lu, o_rate::%f, k::[%lu, %lu)\n", 
        //     __func__, aln_ol, ws, we, min_ovlp, o_rate, sk, k);
        if((aln_ol >= (we-ws)*o_rate) && (we-ws >= min_ovlp)) return 1;
        if((sk > 0) && (we-ws < min_ovlp)) {
            sk--; if(wa[sk].y_end != -1) aln_ol += wa[sk].x_end+1-wa[sk].x_start;
            ws = wa[sk].x_start;  
            // fprintf(stderr, "-[M::%s::] aln_ol::%lu, w::[%lu, %lu), min_ovlp::%lu, o_rate::%f, k::[%lu, %lu)\n", 
            // __func__, aln_ol, ws, we, min_ovlp, o_rate, sk, k);
            if((aln_ol >= (we-ws)*o_rate) && (we-ws >= min_ovlp)) return 1;
        }
    }
    return 0;
}

// Assembly:
testq %rsi, %rsi
je 0x821c2
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
xorl %eax, %eax
movq %rdi, %r11
xorl %ecx, %ecx
cmpl $-0x1, 0xc(%r11)
jne 0x821c5
incq %rcx
addq $0x20, %r11
cmpq %rcx, %rsi
jne 0x821aa
jmp 0x823c2
xorl %eax, %eax
retq
movslq (%r11), %rax
cmpq %rsi, %rcx
jae 0x82207
leaq (%rax,%rdx), %rbx
xorl %r8d, %r8d
movq %rcx, %r9
movslq 0x4(%r11), %r10
incq %r10
cmpl $-0x1, 0xc(%r11)
je 0x821f1
movslq (%r11), %r14
movslq %r10d, %r15
subq %r14, %r15
addq %r15, %r8
cmpq %r10, %rbx
jbe 0x82214
incq %r9
addq $0x20, %r11
cmpq %rsi, %r9
jb 0x821d7
movq %rsi, %r9
jmp 0x82214
movq $-0x1, %r10
xorl %r8d, %r8d
movq %rcx, %r9
movq 0x1af064(%rip), %xmm1   # 0x231280
movapd 0x1af06c(%rip), %xmm2   # 0x231290
subq %rax, %r10
movl $0x1, %eax
cmpq %rdx, %r10
jb 0x82271
movq %r8, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
movq %r10, %xmm3
punpckldq %xmm1, %xmm3    # xmm3 = xmm3[0],xmm1[0],xmm3[1],xmm1[1]
subpd %xmm2, %xmm3
movapd %xmm3, %xmm5
unpckhpd %xmm3, %xmm5            # xmm5 = xmm5[1],xmm3[1]
addsd %xmm3, %xmm5
mulsd %xmm0, %xmm5
ucomisd %xmm5, %xmm4
jae 0x823c2
incq %r9
cmpq %rsi, %r9
jae 0x823c0
movapd 0x1af00b(%rip), %xmm3   # 0x231290
movq %r9, %r11
shlq $0x5, %r11
movslq 0x4(%rdi,%r11), %r10
incq %r10
cmpl $-0x1, 0xc(%rdi,%r11)
je 0x822ab
addq %rdi, %r11
movslq (%r11), %r11
movslq %r10d, %rbx
subq %r11, %rbx
addq %rbx, %r8
cmpq %rsi, %rcx
jae 0x822ea
movq %rcx, %r11
shlq $0x5, %r11
addq %rdi, %r11
movslq (%r11), %rbx
leaq (%rbx,%rdx), %r14
cmpq %r10, %r14
jae 0x822ea
cmpl $-0x1, 0xc(%r11)
je 0x822db
movl 0x4(%r11), %ebp
subl %ebx, %ebp
incl %ebp
movslq %ebp, %rbx
subq %rbx, %r8
incq %rcx
addq $0x20, %r11
cmpq %rcx, %rsi
jne 0x822ba
movq %rsi, %rcx
movq %rcx, %r11
shlq $0x5, %r11
movslq (%rdi,%r11), %rbx
movq %r8, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm2, %xmm5
movapd %xmm5, %xmm4
unpckhpd %xmm5, %xmm4            # xmm4 = xmm4[1],xmm5[1]
addsd %xmm5, %xmm4
movq %r10, %r11
subq %rbx, %r11
cmpq %rdx, %r11
jb 0x82340
movq %r11, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm2, %xmm5
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
addsd %xmm5, %xmm6
mulsd %xmm0, %xmm6
ucomisd %xmm6, %xmm4
jae 0x823c2
testq %rcx, %rcx
je 0x823b4
cmpq %rdx, %r11
jae 0x823b4
decq %rcx
movq %rcx, %rbx
shlq $0x5, %rbx
cmpl $-0x1, 0xc(%rdi,%rbx)
movslq (%rdi,%rbx), %r11
je 0x82389
addq %rdi, %rbx
movl 0x4(%rbx), %ebx
subl %r11d, %ebx
incl %ebx
movslq %ebx, %rbx
addq %rbx, %r8
movq %r8, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm3, %xmm5
movapd %xmm5, %xmm4
unpckhpd %xmm5, %xmm4            # xmm4 = xmm4[1],xmm5[1]
addsd %xmm5, %xmm4
subq %r11, %r10
movq %r10, %xmm5
punpckldq %xmm1, %xmm5    # xmm5 = xmm5[0],xmm1[0],xmm5[1],xmm1[1]
subpd %xmm3, %xmm5
cmpq %rdx, %r10
jb 0x823b4
movapd %xmm5, %xmm6
unpckhpd %xmm5, %xmm6            # xmm6 = xmm6[1],xmm5[1]
addsd %xmm5, %xmm6
mulsd %xmm0, %xmm6
ucomisd %xmm6, %xmm4
jae 0x823c2
incq %r9
cmpq %rsi, %r9
jne 0x82285
xorl %eax, %eax
popq %rbx
popq %r14
popq %r15
popq %rbp
retq