// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_local_aln(overlap_region*, Candidates_list*, ul_idx_t const*, char*, UC_Read*, bit_extz_t*, double, long, unsigned long, double, kv_ul_ov_t*, kv_ul_ov_t*, kv_rtrace_t*, unsigned long, unsigned long, unsigned long, unsigned long, overlap_region*)
uint64_t ul_local_aln(overlap_region *z, Candidates_list *cl, const ul_idx_t *udb, char* qstr, UC_Read *tu, 
bit_extz_t *exz, double e_rate, int64_t w_l, uint64_t min_ovlp, double o_rate, kv_ul_ov_t *rln, kv_ul_ov_t *cln,
kv_rtrace_t *trace, uint64_t ql, uint64_t rid, uint64_t oid, uint64_t khit, overlap_region *aux_o)
{   
    uint64_t ol, aln_ol, cn, k, mm, rln_0; ul_contain *ct = udb->ct; utg_ct_t *ca; ul_ov_t *p;
    if(!aux_o) {
        align_ul_ed_post_extz(z, udb, NULL, qstr, tu->seq, exz, e_rate, w_l, -1, 1, NULL); 
        ol = z->x_pos_e+1-z->x_pos_s, aln_ol = z->align_length;
        if(aln_ol >= (ol*o_rate)) return 1;
        if(aln_ol <= min_ovlp) return 0;
        // fprintf(stderr, "***[M::%s::aln_ol->%lu] utg%.6dl(%c), align::%u, q::[%u, %u), t::[%u, %u)\n", __func__, aln_ol,
        //         (int32_t)z->y_id + 1, "+-"[z->y_pos_strand], z->align_length,
        //         z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
        mm = win_cluster_fliter(z->w_list.a, z->w_list.n, min_ovlp, o_rate/2);
        // print_aln_windows(z);
        // fprintf(stderr, "***[M::%s::] mm::%lu\n", __func__, mm);
        return mm;
    } else {
        ol = z->x_pos_e+1-z->x_pos_s; aln_ol = z->align_length;
        if(aln_ol <= min_ovlp) return 0;
        // if(z->y_id == 700) {
        //     fprintf(stderr, "+[M::%s::aln_ol->%lu::o_rate->%f] utg%.6dl(%c), align::%u, q::[%u, %u), t::[%u, %u), ql::%lu\n", 
        //             __func__, aln_ol, o_rate, 
        //             (int32_t)z->y_id + 1, "+-"[z->y_pos_strand], z->align_length,
        //             z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1, ql);
        //     print_aln_windows(z);
        // }
        rln_0 = rln->n;
        if((ol*o_rate) <= aln_ol) {
            kv_pushp(ul_ov_t, *rln, &p); memset(p, 0, sizeof(*p));
            ///[ts, te) -> whole interval rid at the unitig adjusted by the reverse 
            p->ts = z->y_pos_s; p->te = z->y_pos_e+1; 
            p->qs = z->x_pos_s; p->qe = z->x_pos_e+1;
            p->rev = z->y_pos_strand; p->el = 0; p->tn = p->qn = (uint32_t)-1;
        } else {
            cn = ((uint32_t)(ct->idx.a[z->y_id])); 
            ca = ct->rids.a + ((ct->idx.a[z->y_id])>>32);
            gen_sub_ov_adv(udb, z, o_rate, NULL, 0, rln);
            gen_sub_ov_adv(udb, z, o_rate, ca, cn, rln);
        }

        if(rln->n <= rln_0) return 0;
        radix_sort_ul_ov_srt_qs1(rln->a+rln_0, rln->a+rln->n);
        // if(z->y_id == 700) {
        //     for (k = 0; k < rln->n; k++) {
        //         p = &(rln->a[k]);
        //         fprintf(stderr, "+[M::%s::k->%lu] candidate_q::[%u, %u)\n", __func__, k, p->qs, p->qe);
        //     }
        // }

        for (k = mm = rln_0, p = NULL; k < rln->n; k++) {
            if((!p) || (rln->a[k].qs >= p->qe)) p = NULL;
            if(p) {
                if((rln->a[k].qs<p->qe)&&(rln->a[k].qe>p->qe)) {
                    p->qe = rln->a[k].qe; 
                }
            } else {
                p = &(rln->a[mm++]); *p = rln->a[k];
            }
        }
        rln->n = mm;
        // if(z->y_id == 700) {
        //     for (k = 0; k < rln->n; k++) {
        //         p = &(rln->a[k]);
        //         fprintf(stderr, "-[M::%s::k->%lu] candidate_q::[%u, %u)\n", __func__, k, p->qs, p->qe);
        //     }
        // }

        return_t_chain(z, cl);
        cigar_gen_by_chain_adv_local(z, cl, rln->a+rln_0, rln->n-rln_0, w_l, udb, NULL, NULL, qstr, tu, exz, aux_o, e_rate, ql, rid, khit);
        
        // fprintf(stderr, "-[M::%s::aln_ol->%lu] utg%.6dl(%c), align::%u, q::[%u, %u), t::[%u, %u)\n", __func__, aln_ol,
        //         (int32_t)z->y_id + 1, "+-"[z->y_pos_strand], z->align_length,
        //         z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
        // print_aln_windows(z);


        rln->n = rln_0;
        cn = ((uint32_t)(ct->idx.a[z->y_id])); 
        ca = ct->rids.a + ((ct->idx.a[z->y_id])>>32);
        gen_sub_ov_adv_cigar(udb, z, qstr, tu, exz, ql, o_rate, e_rate, NULL, 0, rid, oid, trace, rln);
        gen_sub_ov_adv_cigar(udb, z, qstr, tu, exz, ql, o_rate, e_rate, ca, cn, rid, oid, trace, cln);
        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movapd %xmm1, %xmm2
movq %rdi, %rbx
movq 0x148(%rsp), %r13
movq 0x108(%rsp), %r14
movq 0x100(%rsp), %rax
movq %rax, 0x58(%rsp)
testq %r13, %r13
je 0x82d31
movl 0x28(%rbx), %eax
cmpq %r14, %rax
jbe 0x83091
movq %rsi, %r15
movq %rcx, 0x68(%rsp)
movsd %xmm0, 0x70(%rsp)
movq 0x140(%rsp), %rcx
movq %rcx, 0x98(%rsp)
movq 0x138(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movq 0x128(%rsp), %rcx
movq %rcx, 0x78(%rsp)
movq 0x120(%rsp), %rcx
movq %rcx, 0xb8(%rsp)
movq 0x118(%rsp), %rcx
movq %rcx, 0xb0(%rsp)
movq 0x110(%rsp), %rbp
movq 0x20(%rdx), %rsi
movl 0x8(%rbx), %ecx
subl 0x4(%rbx), %ecx
incl %ecx
movq 0x8(%rbp), %r14
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd %xmm2, %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
movsd %xmm2, 0x50(%rsp)
movq %r9, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movq %r8, 0x60(%rsp)
movq %rsi, 0xa8(%rsp)
movq %r15, 0xa0(%rsp)
jae 0x82dc7
movq 0x10(%rsi), %rax
movl 0x10(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
movq %rcx, 0x90(%rsp)
shrq $0x20, %rax
leaq (%rax,%rax,2), %r15
shlq $0x2, %r15
addq 0x28(%rsi), %r15
movq %rdx, %rdi
movq %rbx, %rsi
movapd %xmm2, %xmm0
movq %rdx, %r12
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbp, %r8
callq 0x7d47c
movq %r12, %rdi
movq %rbx, %rsi
movsd 0x50(%rsp), %xmm0
movq %r15, %rdx
movq 0x90(%rsp), %rcx
movq %rbp, %r8
callq 0x7d47c
jmp 0x82e67
movq (%r8), %r8
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq $0x1, 0x8(%rsp)
movsd 0x1a27ea(%rip), %xmm1   # 0x225538
movq %rbx, %rdi
movq %rdx, %rsi
xorl %edx, %edx
movsd %xmm2, 0x50(%rsp)
callq 0x5b826
movsd 0x50(%rsp), %xmm0
movl 0x8(%rbx), %eax
movl 0x28(%rbx), %ecx
subl 0x4(%rbx), %eax
incl %eax
xorps %xmm2, %xmm2
cvtsi2sd %rcx, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
movl $0x1, %eax
ucomisd %xmm1, %xmm2
jae 0x83093
cmpq %r14, %rcx
jbe 0x83091
movq 0x48(%rbx), %rsi
movq 0x58(%rbx), %rdi
mulsd 0x1a2742(%rip), %xmm0   # 0x2254f0
movq %r14, %rdx
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x82954
movq 0x10(%rbp), %rax
cmpq %rax, %r14
jne 0x82dfc
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %eax
cmovneq %rcx, %rax
movq %rax, 0x10(%rbp)
movq (%rbp), %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rax, %rsi
callq 0x2370
movq %rax, (%rbp)
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x8(%rbp)
leaq (%rcx,%rcx,8), %rdx
leaq (%rdx,%rdx,2), %rdx
addq %rcx, %rdx
xorpd %xmm0, %xmm0
movupd %xmm0, 0xc(%rax,%rdx)
movupd %xmm0, (%rax,%rdx)
movl 0x14(%rbx), %ecx
movl %ecx, 0x10(%rax,%rdx)
movl 0x18(%rbx), %ecx
incl %ecx
movl %ecx, 0x14(%rax,%rdx)
movl 0x4(%rbx), %ecx
movl %ecx, 0x4(%rax,%rdx)
movl 0x8(%rbx), %ecx
incl %ecx
movl %ecx, 0x8(%rax,%rdx)
movl 0x1c(%rbx), %ecx
shll $0x1f, %ecx
movl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
andl 0x18(%rax,%rdx), %esi
orl %ecx, %esi
movl %esi, 0x18(%rax,%rdx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, (%rax,%rdx)
movl %ecx, 0xc(%rax,%rdx)
movq 0x8(%rbp), %rax
cmpq %r14, %rax
jbe 0x83091
movq (%rbp), %rsi
leaq (%r14,%r14,8), %rcx
leaq (%rcx,%rcx,2), %r15
leaq (%rsi,%r14), %rdi
addq %r15, %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x4751b
movq %r14, %rcx
cmpq 0x8(%rbp), %r14
jae 0x82f1f
leaq (%r14,%r15), %rax
addq $0x4, %rax
xorl %esi, %esi
movq %r14, %rcx
movq %r14, %rdx
testq %rsi, %rsi
je 0x82ec4
movq (%rbp), %rdi
movl (%rdi,%rax), %edi
cmpl 0x8(%rsi), %edi
jb 0x82ec6
xorl %esi, %esi
movq (%rbp), %rdi
testq %rsi, %rsi
je 0x82ee7
movl 0x8(%rsi), %r8d
cmpl %r8d, (%rdi,%rax)
jae 0x82f12
movl 0x4(%rdi,%rax), %edi
cmpl %r8d, %edi
jbe 0x82f12
movl %edi, 0x8(%rsi)
jmp 0x82f12
leaq (%rcx,%rcx,8), %rsi
leaq (%rsi,%rsi,2), %r8
addq %rcx, %r8
incq %rcx
leaq (%rdi,%r8), %rsi
movupd -0x4(%rdi,%rax), %xmm0
movupd 0x8(%rdi,%rax), %xmm1
movupd %xmm1, 0xc(%rdi,%r8)
movupd %xmm0, (%rdi,%r8)
incq %rdx
addq $0x1c, %rax
cmpq 0x8(%rbp), %rdx
jb 0x82eb3
movq %rcx, 0x8(%rbp)
movq %rbx, %rdi
movq %r14, %r12
movq 0xa0(%rsp), %r14
movq %r14, %rsi
callq 0x7d780
addq %r12, %r15
addq (%rbp), %r15
movq 0x8(%rbp), %rcx
subq %r12, %rcx
movq 0x98(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq 0x58(%rsp), %r8
movq 0x80(%rsp), %r14
movq %r14, %r9
movsd 0x70(%rsp), %xmm0
callq 0x6a8a9
movq %r12, 0x8(%rbp)
movq 0xa8(%rsp), %rdx
movq 0x10(%rdx), %rax
movl 0x10(%rbx), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %ecx
movq %rcx, 0x58(%rsp)
shrq $0x20, %rax
leaq (%rax,%rax,2), %r15
shlq $0x2, %r15
addq 0x28(%rdx), %r15
movq %rbp, 0x28(%rsp)
movq 0xb8(%rsp), %r13
movq %r13, 0x20(%rsp)
movq 0xc0(%rsp), %rbp
movq %rbp, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq 0x88(%rsp), %r14
movq %r14, %r8
movq 0x78(%rsp), %r12
movq %r12, %r9
movsd 0x50(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
callq 0x82379
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r15, (%rsp)
movq 0x80(%rsp), %rdi
movq %rbx, %rsi
movq 0x68(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
movq %r12, %r9
movsd 0x50(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
callq 0x82379
movl $0x1, %eax
jmp 0x83093
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq