// File: /workspace/github_cpp_makefile_star2_O3/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 $0x58, %rsp
movq %r9, 0x40(%rsp)
movq %r8, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
leaq 0x6(%rdx), %rax
cmpq %rcx, %rax
jb 0x182aaf
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, %rbx
movsd %xmm0, 0x28(%rsp)
movq 0xa0(%rsp), %rcx
movq 0x90(%rsp), %r14
xorl %r15d, %r15d
leaq 0x50(%rsp), %rax
movq %r15, (%rax)
movq %r15, (%rcx)
movq 0x28(%r14), %r8
xorl %r9d, %r9d
cmpq %r12, %rdx
sete %r9b
subq $0x8, %rsp
movq %r14, %rsi
movq %r12, %rcx
pushq %rax
callq 0x182285
addq $0x10, %rsp
movq %r15, 0x28(%r14)
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x18293e
movq 0x50(%rsp), %rax
movq %rax, 0x48(%rsp)
movq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movq 0xa0(%rsp), %r10
movq (%r14), %rax
movq 0x48(%rsp), %rcx
movq (%rcx,%rbp,8), %rcx
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
movl (%rax,%rdx), %r8d
movl 0xc(%rax,%rdx), %ecx
imulq $0xa8, %rcx, %rdi
movq 0x58(%rbx,%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 0x182860
movq $0x1, 0x28(%r14)
addq %rcx, %rsi
movq %rsi, %rcx
cmpq %r13, %rsi
ja 0x18286e
movq %r13, %rcx
movq %r9, %r8
cmpq %r12, %r9
jb 0x182879
movq %r12, %r8
cmpq %rcx, %r8
ja 0x182899
cmpq %r12, %r13
jne 0x182926
cmpq %r13, %rsi
jae 0x182926
cmpq %r13, %r9
jbe 0x182926
addq %rdx, %rax
addq %rbx, %rdi
subq $0x8, %rsp
movq 0x40(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r13, %r9
pushq %rax
pushq 0x50(%rsp)
pushq %r12
callq 0x180516
addq $0x20, %rsp
incq 0x8(%rsp)
cmpl $-0x1, %eax
je 0x18291e
movl %eax, %r15d
movq 0xa0(%rsp), %r10
movq (%r10), %rcx
movq 0x10(%r10), %rax
cmpq 0x8(%r10), %rcx
jne 0x182911
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x8(%r10)
shlq $0x2, %rsi
movq %rax, %rdi
callq 0x2370
movq 0xa0(%rsp), %r10
movq %rax, 0x10(%r10)
movq (%r10), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, (%r10)
movl %r15d, (%rax,%rcx,4)
jmp 0x182926
movq 0xa0(%rsp), %r10
incq %rbp
cmpq %rbp, 0x10(%rsp)
jne 0x182808
movq 0x8(%rsp), %rbx
incq %rbx
jmp 0x18294b
movl $0x1, %ebx
movq 0xa0(%rsp), %r10
movq (%r10), %rax
movq %rax, %xmm0
movq 0xae925(%rip), %xmm1    # 0x231280
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0xae929(%rip), %xmm2    # 0x231290
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 0x28(%rsp), %xmm1
ucomisd %xmm1, %xmm3
setbe %cl
movq %rbx, %rdx
subq %rax, %rdx
cmpq %rdx, %rax
setbe %dl
orb %cl, %dl
jne 0x182aa7
cmpq 0x98(%rsp), %rbx
setb %cl
cmpq $0x2, %rax
setb %dl
orb %cl, %dl
jne 0x182aa7
movapd %xmm1, 0x10(%rsp)
movq 0x10(%r10), %rdi
leaq (%rdi,%rax,4), %rsi
callq 0x17fa25
movq 0xa0(%rsp), %r11
movq (%r11), %rdx
testq %rdx, %rdx
je 0x182a55
leaq 0x1(%rdx), %rax
cmpq $0x3, %rax
movl $0x2, %esi
cmovaeq %rax, %rsi
movl $0x1, %edi
xorl %ecx, %ecx
xorl %eax, %eax
xorl %r8d, %r8d
movapd 0x10(%rsp), %xmm2
cmpq %rdi, %rdx
je 0x182a29
movq 0x10(%r11), %r9
movl (%r9,%rdi,4), %r10d
cmpl (%r9,%r8,4), %r10d
jne 0x182a29
movq %r8, %r9
jmp 0x182a48
movq %rdi, %r10
subq %r8, %r10
movq %rdi, %r9
cmpq %rcx, %r10
jbe 0x182a48
shlq $0x2, %r8
addq 0x10(%r11), %r8
movq %rdi, %r9
movq %r8, %rax
movq %r10, %rcx
incq %rdi
movq %r9, %r8
cmpq %rdi, %rsi
jne 0x182a11
jmp 0x182a5f
xorl %eax, %eax
xorl %ecx, %ecx
movapd 0x10(%rsp), %xmm2
cmpq $0x2, %rcx
jb 0x182aa7
movq %rcx, %xmm0
punpckldq 0xae80e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0xae816(%rip), %xmm0    # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x182aa7
subq %rcx, %rbx
cmpq %rbx, %rcx
jbe 0x182aa7
movq 0xa8(%rsp), %rcx
movl (%rax), %eax
movl %eax, (%rcx)
movl $0x1, %eax
jmp 0x182ab1
movq $0x1, 0x30(%r14)
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq