// File: /workspace/github_cpp_makefile_star2_O1/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
movsd %xmm0, (%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %rdi, %rbp
movq 0x88(%rsp), %r11
movq 0x80(%rsp), %r13
movq 0x78(%rsp), %r9
testq %r9, %r9
je 0x17ef47
movq 0x90(%rsp), %r15
movq 0x70(%rsp), %r10
movq %r13, %rax
negq %rax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movl $0x1, %r12d
xorl %ebx, %ebx
movq (%r10,%r14,8), %rcx
leaq (%rcx,%rcx,8), %rax
leaq (%rax,%rax,2), %rax
addq %rcx, %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 %r13, %rsi
ja 0x17ee8e
movq %r13, %rsi
movl 0x4(%rdx,%rcx), %ecx
subl %r8d, %ecx
incl %ecx
movslq %ecx, %rdx
cmpq %r11, %rdx
cmovbeq %r12, %rbx
cmovaeq %r11, %rdx
cmpq %rsi, %rdx
jbe 0x17ef39
leaq (%r15,%rax), %rcx
addq %rbp, %rdi
movq 0x98(%rsp), %r8
movq %rbx, %r12
movq %r10, %rbx
movq %r8, %r10
movq (%r8), %r8
movq %r15, %r13
movq %rbp, %r15
movq %r9, %rbp
movl 0x4(%rcx), %r9d
movl %r9d, 0x4(%r8,%rax)
movl 0x10(%rcx), %r9d
movl %r9d, 0x10(%r8,%rax)
movl 0x8(%rcx), %r9d
movl %r9d, 0x8(%r8,%rax)
movl 0x14(%rcx), %r9d
movl %r9d, 0x14(%r8,%rax)
movq 0x8(%r10), %rax
leaq (,%rsi,8), %r8
addq 0x10(%rax), %r8
movq 0x30(%rsp), %rax
leaq (%r8,%rax,8), %r8
callq 0x1795d4
movq %rbx, %r10
movq %r12, %rbx
movl $0x1, %r12d
movq %rbp, %r9
movq %r15, %rbp
movq %r13, %r15
movq 0x80(%rsp), %r13
movq 0x88(%rsp), %r11
incq %r14
cmpq %r14, %r9
jne 0x17ee4b
jmp 0x17ef49
xorl %ebx, %ebx
movq 0x98(%rsp), %r15
leaq 0x38(%r15), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%r15), %rdx
movq 0x40(%r15), %rcx
cmpq %r13, %r11
jne 0x17ef6f
movq %rcx, %r14
jmp 0x17f207
movl $0x8, %r12d
xorps %xmm2, %xmm2
movq 0x8(%r15), %rax
movq 0x10(%rax), %rax
movq -0x8(%rax,%r12), %rsi
movq %rsi, %rdi
shrq $0x20, %rdi
leaq 0x1(%rdi), %r8
incl %esi
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
mulsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
setbe %r9b
movq %rsi, %r10
subq %r8, %r10
cmpq %r10, %rdi
setb %r8b
orb %r9b, %r8b
je 0x17f056
cmpq 0xa0(%rsp), %rsi
jb 0x17f072
movq $-0x1, %r14
cmpq %rdx, %rcx
jbe 0x17f04a
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r15
pushq 0x68(%rsp)
pushq 0x68(%rsp)
callq 0x17eb0f
xorps %xmm2, %xmm2
movq 0xd8(%rsp), %r11
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
movq $-0x1, %rdx
jmp 0x17f1e6
testq %rdi, %rdi
setne %dil
cmpq 0xa0(%rsp), %rsi
setb %sil
orb %dil, %sil
je 0x17efd3
movq (%rax,%r12), %rax
movq %rax, %rsi
shrq $0x20, %rsi
leaq 0x1(%rsi), %rdi
incl %eax
xorps %xmm0, %xmm0
cvtsi2sd %rdi, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
setbe %r8b
movq %rax, %r9
subq %rdi, %r9
cmpq %r9, %rsi
setb %dil
orb %r8b, %dil
je 0x17f144
cmpq 0xa0(%rsp), %rax
jb 0x17f15f
movq $-0x1, %r14
cmpq %rdx, %rcx
jbe 0x17f13b
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r15
pushq 0x68(%rsp)
pushq 0x68(%rsp)
callq 0x17eb0f
xorps %xmm2, %xmm2
movq 0xd8(%rsp), %r11
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
movq $-0x1, %rdx
jmp 0x17f162
testq %rsi, %rsi
setne %sil
cmpq 0xa0(%rsp), %rax
setb %al
orb %sil, %al
je 0x17f0c4
movq %rcx, %r14
cmpq %r14, %r13
jne 0x17f16c
incq %r14
jmp 0x17f1e6
cmpq %rdx, %r14
jbe 0x17f1df
subq $0x8, %rsp
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rcx
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %r9
movsd 0x8(%rsp), %xmm0
movl 0xc8(%rsp), %eax
pushq %rax
pushq 0xc8(%rsp)
pushq 0xc8(%rsp)
pushq 0xc0(%rsp)
pushq $0x0
pushq 0xd8(%rsp)
pushq %r15
pushq 0x68(%rsp)
pushq 0x68(%rsp)
callq 0x17eb0f
xorps %xmm2, %xmm2
movq 0xd8(%rsp), %r11
addq $0x50, %rsp
movq 0xc8(%rsp), %rcx
addq %rax, (%rcx)
leaq 0x1(%r13), %r14
movq %r13, %rdx
movq 0x8(%r15), %rax
movq 0x10(%rax), %rax
movups %xmm2, -0x8(%rax,%r12)
incq %r13
addq $0x10, %r12
movq %r14, %rcx
cmpq %r13, %r11
jne 0x17ef78
pcmpeqd %xmm0, %xmm0
movq 0x30(%rsp), %rax
movdqu %xmm0, (%rax)
cmpq %rdx, %r14
jbe 0x17f221
movq %rdx, 0x38(%r15)
movq %r14, 0x40(%r15)
movq %rbx, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq