// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: wcns_vote(overlap_region*, All_reads*, char*, unsigned long, UC_Read*, bit_extz_t*, unsigned long*, unsigned long, unsigned long, unsigned long, ul_ov_t*, cc_idx_t*, unsigned long, double, overlap_region*, asg32_v*, cns_gfa*, unsigned int, unsigned long*)
uint64_t wcns_vote(overlap_region* ol, All_reads *rref, char* qstr, uint64_t ql, UC_Read* tu, bit_extz_t *exz, uint64_t *id_a, uint64_t id_n, uint64_t s, uint64_t e, ul_ov_t *c_idx, cc_idx_t *occ, uint64_t occ_tot, double occ_exact, overlap_region *aux_o, asg32_v* b32, cns_gfa *cns, uint32_t rid, uint64_t *nec)
{
    uint64_t k, q[2], rr = 0, os, oe, wl, oc[2], fI; ul_ov_t *p, *gp; overlap_region *z; 
    // uint64_t *ct = occ->idx->a;///occ->idx->a[0, wl<<1)
    for (k = 0; k < id_n; k++) {
        p = &(c_idx[id_a[k]]); z = &(ol[ovlp_id(*p)]); 
        q[0] = z->w_list.a[ovlp_cur_wid(*p)].x_start+ovlp_bd(*p);
        q[1] = z->w_list.a[ovlp_cur_wid(*p)].x_end+1-ovlp_bd(*p);
        if(q[1] <= e) rr = 1;
        os = MAX(q[0], s); oe = MIN(q[1], e);
        if(oe > os) {
            ///prepare for CNS
            gp = &(occ->c_idx[id_a[k]]);
            ovlp_cur_xoff(*gp) = ovlp_cur_xoff(*p); ovlp_cur_yoff(*gp) = ovlp_cur_yoff(*p); ovlp_cur_coff(*gp) = ovlp_cur_coff(*p); ovlp_cur_ylen(*gp) = ovlp_cur_ylen(*p);
            // assert(ovlp_cur_wid(*p) == ovlp_cur_wid(*gp));
            // assert(ovlp_id(*p) == ovlp_id(*gp));
            // fprintf(stderr, "[M::%s] tid::%u\t%.*s\twid::%u\tq::[%u, %u)\tos::%lu\toe::%lu\n", __func__, ol[ovlp_id(*p)].y_id, (int)Get_NAME_LENGTH(R_INF, ol[ovlp_id(*p)].y_id), Get_NAME(R_INF, ol[ovlp_id(*p)].y_id),
            // ovlp_cur_wid(*p), ol[ovlp_id(*p)].w_list.a[ovlp_cur_wid(*p)].x_start, ol[ovlp_id(*p)].w_list.a[ovlp_cur_wid(*p)].x_end+1, os, oe);
            extract_sub_cigar_mm(z, os, oe, p, occ->idx->a + os - s);
        }
    }

    wl = e - s;
    os = occ->mms; oe = occ->mme;

    // fprintf(stderr, "[M::%s] s::%lu\te::%lu\n", __func__, s, e);
    
    for (k = 0; k < wl; k++) {
        //+1 for the reference read
        oc[0] = (occ->idx->a[(k<<1)]>>32) + 1;
        oc[1] = ((uint32_t)occ->idx->a[(k<<1)]) + 1;
        // fprintf(stderr, "-0-p::%lu\toc[0]::%lu\toc[1]::%lu\tgoc[0]::%lu\tgoc[1]::%u\n", s + k, oc[0], oc[1], (ct[(k<<1)+1]>>32) + 1, ((uint32_t)ct[(k<<1)+1]) + 1);
        // if(oc[1] < occ_tot || oc[0] <= 1) {
        //     ct[(k<<1)] = ct[(k<<1)+1] = 0;
        //     continue;
        // }
        // fprintf(stderr, "-1-p::%lu\toc[0]::%lu\toc[1]::%lu\n", s + k, oc[0], oc[1]);

        ///a) pass coverage check; b) no enough coverage
        if(((oc[0] > (oc[1]*occ_exact)) && (oc[0] > (oc[1]-oc[0])) && (oc[1] >= occ_tot) && (oc[0] > 1)) ||
            (oc[1] < occ_tot)) {
            ///note: there might be insertions at q[k-1, k], insead if q[k, k+1]
            fI = 1;
            ///make sure there is no insertion
            //+1 for the reference read
            oc[0] = (occ->idx->a[(k<<1)+1]>>32) + 1;
            oc[1] = ((uint32_t)occ->idx->a[(k<<1)+1]) + 1;
            ///a) pass coverage check; b) no enough coverage
            if((((oc[0] > (oc[1]*occ_exact)) && (oc[0] > (oc[1]-oc[0])) && (oc[1] >= occ_tot) && (oc[0] > 1))) || 
                (oc[1] < occ_tot)) {
                fI = 0;
            }

            if(fI) {
                // fprintf(stderr, "-1-p::%lu\toc[0]::%lu\toc[1]::%u\tgoc[0]::%lu\tgoc[1]::%u\n", s + k, (occ->idx->a[(k<<1)]>>32) + 1, ((uint32_t)occ->idx->a[(k<<1)]) + 1, (occ->idx->a[(k<<1)+1]>>32) + 1, ((uint32_t)occ->idx->a[(k<<1)+1]) + 1);
                if(oe > os && os != ((uint64_t)-1)) {///push previous intervals
                    (*nec) += push_cns_anchor(ol, rref, os, oe, qstr, ql, tu, exz, occ, aux_o, 0, occ_tot, occ_exact, b32, cns, rid);
                }
                os = oe = (uint64_t)-1;
            }

            //+1 for the reference read
            oc[0] = (occ->idx->a[(k<<1)]>>32) + 1;
            oc[1] = ((uint32_t)occ->idx->a[(k<<1)]) + 1;
            if((s+k) == oe) {
                oe++;
            } else {
                if(oe > os && os != ((uint64_t)-1)) {///push previous intervals
                    (*nec) += push_cns_anchor(ol, rref, os, oe, qstr, ql, tu, exz, occ, aux_o, 0, occ_tot, occ_exact, b32, cns, rid);
                }
                os = s+k; oe = s+k+1;
            }
        } else {
            // fprintf(stderr, "-2-p::%lu\toc[0]::%lu\toc[1]::%u\tgoc[0]::%lu\tgoc[1]::%u\n", s + k, (occ->idx->a[(k<<1)]>>32) + 1, ((uint32_t)occ->idx->a[(k<<1)]) + 1, (occ->idx->a[(k<<1)+1]>>32) + 1, ((uint32_t)occ->idx->a[(k<<1)+1]) + 1);
            if(oe > os && os != ((uint64_t)-1)) {///push previous intervals
                (*nec) += push_cns_anchor(ol, rref, os, oe, qstr, ql, tu, exz, occ, aux_o, 0, occ_tot, occ_exact, b32, cns, rid);
            }
            os = oe = (uint64_t)-1;
        }
        occ->idx->a[(k<<1)] = occ->idx->a[(k<<1)+1] = 0;
    }

    occ->mms = occ->mme = (uint64_t)-1;
    if(oe > os && os != ((uint64_t)-1)) {
        occ->mms = os; occ->mme = oe;
    }
    return rr;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbp
movq 0x98(%rsp), %r11
movq 0x80(%rsp), %r12
movq %r12, %rax
negq %rax
movq %rax, 0x8(%rsp)
movq 0x90(%rsp), %r15
movq 0x78(%rsp), %r9
movq 0x70(%rsp), %r10
xorl %ebx, %ebx
pushq $0x1
popq %r13
xorl %r14d, %r14d
movsd %xmm0, (%rsp)
cmpq %r14, %r9
je 0x141eed
imulq $0x1c, (%r10,%r14,8), %rax
movl (%r15,%rax), %ecx
movl 0xc(%r15,%rax), %edx
imulq $0xa8, %rdx, %rdi
movq 0x58(%rbp,%rdi), %rdx
shlq $0x5, %rcx
movslq (%rdx,%rcx), %rsi
movl 0x18(%r15,%rax), %r8d
andl $0x3fffffff, %r8d       # imm = 0x3FFFFFFF
addq %r8, %rsi
cmpq %r12, %rsi
ja 0x141e46
movq %r12, %rsi
movl 0x4(%rdx,%rcx), %ecx
subl %r8d, %ecx
incl %ecx
movslq %ecx, %rdx
movq 0x88(%rsp), %rcx
cmpq %rcx, %rdx
cmovbeq %r13, %rbx
cmovaeq %rcx, %rdx
cmpq %rsi, %rdx
jbe 0x141ee5
leaq (%r15,%rax), %rcx
addq %rbp, %rdi
movq (%r11), %r8
movl 0x10(%rcx), %r9d
movl %r9d, 0x10(%r8,%rax)
movq 0x4(%rcx), %r9
movq %r9, 0x4(%r8,%rax)
movl 0x14(%rcx), %r9d
movl %r9d, 0x14(%r8,%rax)
movq 0x8(%r11), %rax
leaq (,%rsi,8), %r8
addq 0x10(%rax), %r8
movq 0x8(%rsp), %rax
leaq (%r8,%rax,8), %r8
movq %r13, %r15
movq %r12, %r13
movq %rbx, %r12
movq %rbp, %rbx
movq %r10, %rbp
callq 0x13d4d8
movq %rbp, %r10
movq %rbx, %rbp
movq %r12, %rbx
movq %r13, %r12
movq %r15, %r13
movq 0x78(%rsp), %r9
movq 0x90(%rsp), %r15
movq 0x98(%rsp), %r11
movsd (%rsp), %xmm0
incq %r14
jmp 0x141e04
leaq 0x38(%r11), %rax
movq %rax, 0x8(%rsp)
movq 0x38(%r11), %rdx
movq 0x40(%r11), %rcx
pushq $0x8
popq %r15
pushq $-0x1
popq %r13
xorps %xmm3, %xmm3
movq 0x30(%rsp), %r14
cmpq %r12, 0x88(%rsp)
je 0x1421a1
movq 0x8(%r11), %rax
movq 0x10(%rax), %rax
movq -0x8(%rax,%r15), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
leaq 0x1(%rdi), %r8
incl %esi
cvtsi2sd %r8, %xmm2
cvtsi2sd %rsi, %xmm1
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
setbe %r9b
movq %rsi, %r10
subq %r8, %r10
cmpq %r10, %rdi
setb %r8b
orb %r9b, %r8b
je 0x141ff3
cmpq 0xa0(%rsp), %rsi
jb 0x14200f
movq %r13, %rax
movq %r13, %r8
cmpq %rdx, %rcx
jbe 0x142181
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r11
pushq %r14
pushq 0x70(%rsp)
callq 0x14198f
xorps %xmm3, %xmm3
movq 0xe8(%rsp), %r11
movsd 0x50(%rsp), %xmm0
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
movq %r13, %rax
movq %r13, %r8
jmp 0x142181
testq %rdi, %rdi
setne %dil
cmpq 0xa0(%rsp), %rsi
setb %sil
orb %dil, %sil
je 0x141f70
movq (%rax,%r15), %rax
movq %rax, %rsi
shrq $0x20, %rsi
leaq 0x1(%rsi), %rdi
incl %eax
cvtsi2sd %rdi, %xmm2
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
setbe %r8b
movq %rax, %r9
subq %rdi, %r9
cmpq %r9, %rsi
setb %dil
orb %r8b, %dil
je 0x1420da
cmpq 0xa0(%rsp), %rax
jb 0x1420f5
movq %r13, %rax
movq %r13, %r8
cmpq %rdx, %rcx
jbe 0x1420fb
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r11
pushq %r14
pushq 0x70(%rsp)
callq 0x14198f
xorps %xmm3, %xmm3
movq 0xe8(%rsp), %r11
movsd 0x50(%rsp), %xmm0
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
movq %r13, %rax
movq %r13, %r8
jmp 0x1420fb
testq %rsi, %rsi
setne %sil
cmpq 0xa0(%rsp), %rax
setb %al
orb %sil, %al
je 0x14205a
movq %rcx, %rax
movq %rdx, %r8
cmpq %rax, %r12
jne 0x142105
incq %rax
jmp 0x142181
cmpq %r8, %rax
jbe 0x142179
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq %r8, %rdx
movq %rax, %rcx
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r11
pushq %r14
pushq 0x70(%rsp)
callq 0x14198f
xorps %xmm3, %xmm3
movq 0xe8(%rsp), %r11
movsd 0x50(%rsp), %xmm0
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
leaq 0x1(%r12), %rax
movq %r12, %r8
movq 0x8(%r11), %rcx
movq 0x10(%rcx), %rcx
movups %xmm3, -0x8(%rcx,%r15)
incq %r12
addq $0x10, %r15
movq %rax, %rcx
movq %r8, %rdx
jmp 0x141f0e
pcmpeqd %xmm0, %xmm0
movq 0x8(%rsp), %rax
movdqu %xmm0, (%rax)
cmpq %rdx, %rcx
jbe 0x1421bb
movq %rdx, 0x38(%r11)
movq %rcx, 0x40(%r11)
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq