// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/ecovlp.cpp
// Function: cns_gen0(overlap_region*, All_reads*, unsigned long, unsigned long, unsigned long, UC_Read*, cc_idx_t*, unsigned long, double, asg32_v*, unsigned int*)
uint64_t cns_gen0(overlap_region* ol, All_reads *rref, uint64_t s, uint64_t e, uint64_t ql, UC_Read* tu, cc_idx_t *idx, uint64_t occ_tot, double occ_max, asg32_v* b32, uint32_t *rc)
{
    if(e > s + simp_vote_len) return 0;///too long

    uint64_t *id_a = NULL, id_n, an = 0, oc[2]; b32->n = 0; uint32_t m, *a = NULL;
    id_n = iter_cc_idx_t(ol, idx, s, e, idx->rr, ((s==e)?1:0), &id_a);
    // debug_inter0(ol, idx->c_idx, idx->idx->a + idx->i0, idx->srt_n - idx->i0, id_a, id_n, s, e, ((s==e)?1:0), 0, "-1-");
    uint64_t k, l, q[2], os, oe; ul_ov_t *p; overlap_region *z; idx->rr = 0;
    // fprintf(stderr, "[M::%s] [%lu, %lu) id_n::%lu\n", __func__, s, e, id_n);
    for (k = 0; k < id_n; k++) {
        p = &(idx->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(s == 11851 && e == 11853) {
        //     fprintf(stderr, "[M::%s] tid::%u\t%.*s\twid::%u\tq::[%u, %u)\terr::%d\toerr::%u\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, ol[ovlp_id(*p)].w_list.a[ovlp_cur_wid(*p)].error, ol[ovlp_id(*p)].non_homopolymer_errors);
        // }
        
        if(q[1] <= e) idx->rr = 1;
        os = MAX(q[0], s); oe = MIN(q[1], e);
        // if((oe > os) || ((s == e) && (s >= q[0]) && (s <= q[1]))) {
        if((oe > os) || ((s == e) && (s > q[0]) && (s < q[1]))) {
        // if(oe >= os) {
            ///[-4-][-12-][-4-][-12-]
            ///[cigar_len][cigar][base_len][base]
            m = extract_sub_cigar_ii(z, ql, rref, os, oe, s, e, tu, p); an++;
            if(m != ((uint32_t)-1)) {///no gap in both sides
                kv_push(uint32_t, *b32, m);
            }
        }
    }

    oc[0] = b32->n; oc[1] = an + 1; //+1 for the reference read
    // if(s == 11851 && e == 11853) {
    //     fprintf(stderr, "-0-[M::%s] oc[0]::%lu, oc[1]::%lu\n", __func__, oc[0], oc[1]);
    // }
    if(((oc[0] > (oc[1]*occ_max)) && (oc[0] > (oc[1]-oc[0])) && (oc[1] >= occ_tot) && (oc[0] > 1))) {
        radix_sort_ec32(b32->a, b32->a+b32->n); an = 0;
        for (k = 1, l = 0; k <= b32->n; ++k) {
            if (k == b32->n || b32->a[k] != b32->a[l]) {
                if(k - l > an) {
                    an = k - l; a = b32->a + l;
                }
                l = k;
            }
        }      
        oc[0] = an;
        // fprintf(stderr, "-1-[M::%s] oc[0]::%lu, oc[1]::%lu\n", __func__, oc[0], oc[1]);
        if(((oc[0] > (oc[1]*occ_max)) && (oc[0] > (oc[1]-oc[0])) && (oc[1] >= occ_tot) && (oc[0] > 1))) {
            (*rc) = a[0];
            // prt_cigar0((a[0]<<4)>>20, a[0]>>28);
            // prt_bp0((a[0]<<20)>>20, (a[0]<<16)>>28);
            return 1;
        }
    }

    idx->ru = 1;
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x30(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
leaq 0x6(%rdx), %rax
cmpq %rcx, %rax
jb 0x13f218
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, %r14
movsd %xmm0, 0x18(%rsp)
movq 0x90(%rsp), %rax
leaq 0x40(%rsp), %r15
andq $0x0, (%r15)
movq 0x80(%rsp), %rbx
andq $0x0, (%rax)
movq 0x28(%rbx), %r8
xorl %r9d, %r9d
cmpq %rcx, %rdx
sete %r9b
subq $0x8, %rsp
movq %rbx, %rsi
pushq %r15
callq 0x13ea8a
movq %rbx, %r10
addq $0x10, %rsp
movq %rax, (%rsp)
andq $0x0, 0x28(%rbx)
xorl %ebp, %ebp
movq (%r15), %rax
movq %rax, 0x38(%rsp)
xorl %ebx, %ebx
cmpq %rbp, (%rsp)
je 0x13f0cb
movq (%r10), %rax
movq 0x38(%rsp), %rcx
imulq $0x1c, (%rcx,%rbp,8), %rdx
movl (%rax,%rdx), %r8d
movl 0xc(%rax,%rdx), %ecx
imulq $0xa8, %rcx, %rdi
movq 0x58(%r14,%rdi), %r9
shlq $0x5, %r8
movslq (%r9,%r8), %rcx
movl 0x18(%rax,%rdx), %esi
andl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
movl 0x4(%r9,%r8), %r8d
subl %esi, %r8d
incl %r8d
movslq %r8d, %r9
cmpq %r12, %r9
ja 0x13eff9
movq $0x1, 0x28(%r10)
addq %rcx, %rsi
movq %rsi, %rcx
cmpq %r13, %rsi
ja 0x13f007
movq %r13, %rcx
movq %r9, %r8
cmpq %r12, %r9
jb 0x13f012
movq %r12, %r8
cmpq %rcx, %r8
ja 0x13f032
cmpq %r12, %r13
jne 0x13f0c3
cmpq %r13, %rsi
jae 0x13f0c3
cmpq %r13, %r9
jbe 0x13f0c3
addq %rdx, %rax
addq %r14, %rdi
subq $0x8, %rsp
movq 0x30(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %r13, %r9
pushq %rax
pushq 0x40(%rsp)
pushq %r12
callq 0x13d753
addq $0x20, %rsp
incq %rbx
cmpl $-0x1, %eax
je 0x13f0bb
movl %eax, %r15d
movq 0x90(%rsp), %rdi
movq (%rdi), %rcx
movq 0x10(%rdi), %rax
cmpq 0x8(%rdi), %rcx
jne 0x13f0a6
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%rdi)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0x90(%rsp), %rdi
movq %rax, 0x10(%rdi)
movq (%rdi), %rcx
movq 0x80(%rsp), %r10
leaq 0x1(%rcx), %rdx
movq %rdx, (%rdi)
movl %r15d, (%rax,%rcx,4)
jmp 0x13f0c3
movq 0x80(%rsp), %r10
incq %rbp
jmp 0x13efa1
movq 0x90(%rsp), %rsi
movq (%rsi), %rax
incq %rbx
movq %rax, %xmm0
movq 0xa418a(%rip), %xmm1    # 0x1e3270
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xa418e(%rip), %xmm2    # 0x1e3280
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movq %rbx, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm3
setbe %cl
movq %rbx, %rdx
subq %rax, %rdx
cmpq %rdx, %rax
setbe %dl
orb %cl, %dl
jne 0x13f210
cmpq 0x88(%rsp), %rbx
setb %cl
cmpq $0x2, %rax
setb %dl
orb %cl, %dl
jne 0x13f210
movapd %xmm1, (%rsp)
movq 0x10(%rsi), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0x13ce5b
movq 0x90(%rsp), %r10
movq (%r10), %rdx
pushq $0x1
popq %rsi
xorl %edi, %edi
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rdx, %rsi
ja 0x13f1bd
je 0x13f196
movq 0x10(%r10), %r8
movl (%r8,%rsi,4), %r9d
cmpl (%r8,%rdi,4), %r9d
jne 0x13f196
movq %rdi, %r8
jmp 0x13f1b5
movq %rsi, %r9
subq %rdi, %r9
movq %rsi, %r8
cmpq %rcx, %r9
jbe 0x13f1b5
shlq $0x2, %rdi
addq 0x10(%r10), %rdi
movq %rsi, %r8
movq %rdi, %rax
movq %r9, %rcx
incq %rsi
movq %r8, %rdi
jmp 0x13f17c
cmpq $0x2, %rcx
movq 0x80(%rsp), %r10
movapd (%rsp), %xmm2
jb 0x13f210
movq %rcx, %xmm0
punpckldq 0xa4093(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xa409b(%rip), %xmm0    # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x13f210
subq %rcx, %rbx
cmpq %rbx, %rcx
jbe 0x13f210
movl (%rax), %eax
movq 0x98(%rsp), %rcx
movl %eax, (%rcx)
pushq $0x1
popq %rax
jmp 0x13f21a
movq $0x1, 0x30(%r10)
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq