// File: /workspace/github_cpp_makefile_star2_O3/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 0x82547
movl 0x28(%rbx), %eax
cmpq %r14, %rax
jbe 0x8288f
movq %rsi, 0xa0(%rsp)
movq %rcx, 0x88(%rsp)
movsd %xmm0, 0x68(%rsp)
movq 0x140(%rsp), %rcx
movq %rcx, 0x98(%rsp)
movq 0x138(%rsp), %rcx
movq %rcx, 0xc0(%rsp)
movq 0x128(%rsp), %rcx
movq %rcx, 0x70(%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), %r12
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, 0x60(%rsp)
movq %rdx, 0x80(%rsp)
movq %r8, 0x78(%rsp)
movq %rsi, 0xa8(%rsp)
jae 0x825dd
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, %r14
xorl %edx, %edx
xorl %ecx, %ecx
movq %rbp, %r8
callq 0x7cf05
movq %r14, %rdi
movq %rbx, %rsi
movsd 0x50(%rsp), %xmm0
movq %r15, %rdx
movq 0x90(%rsp), %rcx
movq %rbp, %r8
callq 0x7cf05
jmp 0x82673
movq (%r8), %r8
movq 0x58(%rsp), %rax
movq %rax, (%rsp)
movq $0x1, 0x8(%rsp)
movsd 0x1af0d4(%rip), %xmm1   # 0x231638
movq %rbx, %rdi
movq %rdx, %rsi
xorl %edx, %edx
movsd %xmm2, 0x50(%rsp)
callq 0x5ae21
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 0x82891
cmpq %r14, %rcx
jbe 0x8288f
movq 0x48(%rbx), %rsi
movq 0x58(%rbx), %rdi
mulsd 0x1af02c(%rip), %xmm0   # 0x2315f0
movq %r14, %rdx
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x82198
movq (%rbp), %rax
cmpq 0x10(%rbp), %r12
jne 0x82618
leaq (%r12,%r12), %rcx
testq %r12, %r12
movl $0x2, %edx
cmovneq %rcx, %rdx
movq %rdx, 0x10(%rbp)
leaq (%rdx,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rsi
addq %rdx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%rbp)
movq 0x8(%rbp), %rcx
jmp 0x8261b
movq %r12, %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 %ecx, 0x18(%rax,%rdx)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movl %ecx, (%rax,%rdx)
movl %ecx, 0xc(%rax,%rdx)
movq 0x8(%rbp), %rax
cmpq %r12, %rax
jbe 0x8288f
movq (%rbp), %rsi
leaq (%r12,%r12,8), %rcx
leaq (%rcx,%rcx,2), %r14
leaq (%rsi,%r12), %rdi
addq %r14, %rdi
leaq (%rax,%rax,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rax, %rsi
addq %rcx, %rsi
callq 0x46b25
movq 0x8(%rbp), %rcx
movq %r12, %rsi
cmpq %rcx, %r12
jae 0x82722
leaq (%r12,%r14), %rax
addq $0x8, %rax
xorl %edi, %edi
movq %r12, %rsi
movq %r12, %rdx
movq (%rbp), %r8
testq %rdi, %rdi
je 0x826e5
movl 0x8(%rdi), %r9d
cmpl %r9d, -0x4(%r8,%rax)
jae 0x826e5
movl (%r8,%rax), %r8d
cmpl %r9d, %r8d
jbe 0x82716
movl %r8d, 0x8(%rdi)
jmp 0x82716
leaq (%rsi,%rsi,8), %rcx
leaq (%rcx,%rcx,2), %rcx
addq %rsi, %rcx
incq %rsi
leaq (%r8,%rcx), %rdi
movupd -0x8(%r8,%rax), %xmm0
movupd 0x4(%r8,%rax), %xmm1
movupd %xmm1, 0xc(%r8,%rcx)
movupd %xmm0, (%r8,%rcx)
movq 0x8(%rbp), %rcx
incq %rdx
addq $0x1c, %rax
cmpq %rcx, %rdx
jb 0x826c2
movq %rsi, 0x8(%rbp)
movq %rbx, %rdi
movq 0xa0(%rsp), %r15
movq %r15, %rsi
callq 0x7d1f5
addq %r12, %r14
addq (%rbp), %r14
movq 0x8(%rbp), %rcx
subq %r12, %rcx
movq 0x98(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %r13, 0x28(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x88(%rsp), %r13
movq %r13, 0x10(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
movq 0x58(%rsp), %r8
movq 0x80(%rsp), %r14
movq %r14, %r9
movsd 0x68(%rsp), %xmm0
callq 0x6a497
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), %r12
movq %r12, 0x20(%rsp)
movq 0xc0(%rsp), %rbp
movq %rbp, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r13, %rdx
movq 0x78(%rsp), %r14
movq %r14, %rcx
movq 0x60(%rsp), %r8
movq 0x70(%rsp), %r13
movq %r13, %r9
movsd 0x50(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
callq 0x81bea
movq 0xb0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r12, 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 0x88(%rsp), %rdx
movq %r14, %rcx
movq 0x60(%rsp), %r8
movq %r13, %r9
movsd 0x50(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
callq 0x81bea
movl $0x1, %eax
jmp 0x82891
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq