// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x68(%rbp), %rax
movl 0x60(%rbp), %eax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq 0x18(%rbp), %rax
jae 0x253ebb
movq 0x30(%rbp), %rax
movq 0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq -0xc0(%rbp), %rcx
movl 0xc(%rcx), %ecx
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xc0(%rbp), %rcx
movl (%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
movq -0xc0(%rbp), %rcx
movl 0x18(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
addl %ecx, %eax
cltq
movq %rax, -0x80(%rbp)
movq -0xd0(%rbp), %rax
movq 0x58(%rax), %rax
movq -0xc0(%rbp), %rcx
movl (%rcx), %ecx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0xc0(%rbp), %rcx
movl 0x18(%rcx), %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
subl %ecx, %eax
cltq
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x28(%rbp), %rax
ja 0x253d7e
movq $0x1, -0x88(%rbp)
movq -0x80(%rbp), %rax
cmpq 0x20(%rbp), %rax
jb 0x253d95
movq -0x80(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x253da0
movq 0x20(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rax
cmpq 0x28(%rbp), %rax
ja 0x253dc5
movq -0x78(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x253dd0
movq 0x28(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x253ea8
movq 0x38(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rbp), %rcx
movq -0x68(%rbp), %rdx
imulq $0x1c, (%rcx,%rdx,8), %rcx
addq %rcx, %rax
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movl 0x4(%rax), %ecx
movq -0xc8(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0xc0(%rbp), %rax
movl 0x10(%rax), %ecx
movq -0xc8(%rbp), %rax
movl %ecx, 0x10(%rax)
movq -0xc0(%rbp), %rax
movl 0x8(%rax), %ecx
movq -0xc8(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0xc0(%rbp), %rax
movl 0x14(%rax), %ecx
movq -0xc8(%rbp), %rax
movl %ecx, 0x14(%rax)
movq -0xd0(%rbp), %rdi
movq -0x90(%rbp), %rsi
movq -0x98(%rbp), %rdx
movq -0xc0(%rbp), %rcx
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %r8
movq -0x90(%rbp), %rax
shlq $0x3, %rax
addq %rax, %r8
xorl %eax, %eax
subq 0x20(%rbp), %rax
shlq $0x3, %rax
addq %rax, %r8
callq 0x24b4a0
jmp 0x253eaa
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x253cb3
movq 0x28(%rbp), %rax
subq 0x20(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq 0x38(%rbp), %rax
movq 0x38(%rax), %rax
movq %rax, -0x90(%rbp)
movq 0x38(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x98(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0xa0(%rbp), %rax
jae 0x2543ef
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
movl 0x4(%rax,%rcx), %eax
incq %rax
movq %rax, -0xb0(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
movl (%rax,%rcx), %eax
incl %eax
movq %rax, -0xa8(%rbp)
movaps 0x12e0c6(%rip), %xmm3   # 0x382010
movq -0xb0(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x12e0c2(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0xa8(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x60(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x253fc4
movq -0xb0(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x253fc4
movq -0xa8(%rbp), %rax
cmpq 0x40(%rbp), %rax
jb 0x253fc4
cmpq $0x1, -0xb0(%rbp)
ja 0x253fd5
movq -0xa8(%rbp), %rax
cmpq 0x40(%rbp), %rax
jae 0x2542de
movq $0x1, -0xb8(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
movl 0xc(%rax,%rcx), %eax
incq %rax
movq %rax, -0xb0(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
movl 0x8(%rax,%rcx), %eax
incl %eax
movq %rax, -0xa8(%rbp)
movaps 0x12dfe6(%rip), %xmm3   # 0x382010
movq -0xb0(%rbp), %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x12dfe2(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0xa8(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x60(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2540a4
movq -0xb0(%rbp), %rax
movq -0xa8(%rbp), %rcx
subq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
jbe 0x2540a4
movq -0xa8(%rbp), %rax
cmpq 0x40(%rbp), %rax
jb 0x2540a4
cmpq $0x1, -0xb0(%rbp)
ja 0x2540b1
movq -0xa8(%rbp), %rax
cmpq 0x40(%rbp), %rax
jae 0x2540bc
movq $0x0, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
je 0x254193
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x25417d
cmpq $-0x1, -0x90(%rbp)
je 0x25417d
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %r13
movq -0x58(%rbp), %r12
movq 0x38(%rbp), %r15
movq 0x48(%rbp), %r14
movq 0x40(%rbp), %rbx
movsd -0x60(%rbp), %xmm0
movq 0x50(%rbp), %r11
movq 0x58(%rbp), %r10
movl 0x60(%rbp), %eax
movl %eax, -0xe4(%rbp)
xorl %eax, %eax
movl -0xe4(%rbp), %eax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movl %eax, 0x40(%rsp)
callq 0x253650
movq %rax, %rcx
movq 0x68(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0x90(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rax
shrq $0x20, %rax
addq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq %rcx
movq (%rax,%rcx,8), %rax
addl $0x1, %eax
movl %eax, %eax
movq %rax, -0xa8(%rbp)
movq 0x20(%rbp), %rax
addq -0x68(%rbp), %rax
cmpq -0x98(%rbp), %rax
jne 0x254204
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x2542d9
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x2542b7
cmpq $-0x1, -0x90(%rbp)
je 0x2542b7
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %r13
movq -0x58(%rbp), %r12
movq 0x38(%rbp), %r15
movq 0x48(%rbp), %r14
movq 0x40(%rbp), %rbx
movsd -0x60(%rbp), %xmm0
movq 0x50(%rbp), %r11
movq 0x58(%rbp), %r10
movl 0x60(%rbp), %eax
movl %eax, -0xe8(%rbp)
xorl %eax, %eax
movl -0xe8(%rbp), %eax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movl %eax, 0x40(%rsp)
callq 0x253650
movq %rax, %rcx
movq 0x68(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq 0x20(%rbp), %rax
addq -0x68(%rbp), %rax
movq %rax, -0x90(%rbp)
movq 0x20(%rbp), %rax
addq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x2543a7
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x254391
cmpq $-0x1, -0x90(%rbp)
je 0x254391
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x90(%rbp), %rdx
movq -0x98(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0x50(%rbp), %r13
movq -0x58(%rbp), %r12
movq 0x38(%rbp), %r15
movq 0x48(%rbp), %r14
movq 0x40(%rbp), %rbx
movsd -0x60(%rbp), %xmm0
movq 0x50(%rbp), %r11
movq 0x58(%rbp), %r10
movl 0x60(%rbp), %eax
movl %eax, -0xec(%rbp)
xorl %eax, %eax
movl -0xec(%rbp), %eax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movl %eax, 0x40(%rsp)
callq 0x253650
movq %rax, %rcx
movq 0x68(%rbp), %rax
addq (%rax), %rcx
movq %rcx, (%rax)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0x90(%rbp)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq %rcx
movq $0x0, 0x8(%rax,%rcx,8)
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x10(%rax), %rax
movq -0x68(%rbp), %rcx
shlq %rcx
movq $0x0, (%rax,%rcx,8)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x253ef0
movq 0x38(%rbp), %rax
movq $-0x1, 0x40(%rax)
movq 0x38(%rbp), %rax
movq $-0x1, 0x38(%rax)
movq -0x98(%rbp), %rax
cmpq -0x90(%rbp), %rax
jbe 0x25443f
cmpq $-0x1, -0x90(%rbp)
je 0x25443f
movq -0x90(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x38(%rax)
movq -0x98(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x88(%rbp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)