// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_lalign(overlap_region_alloc*, Candidates_list*, ul_idx_t const*, ug_opt_t const*, char*, unsigned long, UC_Read*, UC_Read*, Correct_dumy*, bit_extz_t*, haplotype_evdience_alloc*, kvec_t_u64_warp*, overlap_region*, double, long, kv_ul_ov_t*, long, unsigned long, st_mt_t*, idx_emask_t*, mask_ul_ov_t*, void*)
void ul_lalign(overlap_region_alloc* ol, Candidates_list *cl, const ul_idx_t *uref, const ug_opt_t *uopt, 
        char *qstr, uint64_t ql, UC_Read* qu, UC_Read* tu, Correct_dumy* dumy, bit_extz_t *exz, haplotype_evdience_alloc* hap, 
        kvec_t_u64_warp* v_idx, overlap_region *aux_o, double e_rate, int64_t wl, kv_ul_ov_t *aln, int64_t sid, uint64_t khit, 
        st_mt_t *stb, idx_emask_t *mm, mask_ul_ov_t *mk, void *km)
{
    uint64_t i, bs, k, ovl/**, on**/; Window_Pool w; double err; 
    /**int64_t sc;**/ overlap_region t; overlap_region *z; asg64_v iidx, buf, buf1;
    ol->mapped_overlaps_length = 0;
    if(ol->length <= 0) return;

    ///base alignment
    clear_Correct_dumy(dumy, ol, km); err = e_rate; 
    init_Window_Pool(&w, ql, wl, (int)(1.0/err));
    bs = (w.window_length)+(THRESHOLD_MAX_SIZE<<1)+1;
    resize_UC_Read(tu, bs<<1);

    if(!aln) {    
        resize_UC_Read(qu, ql); qu->length = ql; memcpy(qu->seq, qstr, ql);
        for (i = 0; i < ol->length; i++) {
            z = &(ol->list[i]); ovl = z->x_pos_e+1-z->x_pos_s; 
            z->shared_seed = z->non_homopolymer_errors;///for index
            if(!align_ul_ed_post_extz(z, uref, NULL, qu->seq, tu->seq, exz, err, w.window_length, -1, 0, km)) {
                continue;
            }
            if(uref && simi_pass(ovl, z->align_length, uref?1:0, -1, NULL)) {
                z->is_match = 3; ol->mapped_overlaps_length += z->align_length;
            }
        }

        if(uref && ol->mapped_overlaps_length > 0) {
            set_herror_win(ol, dumy, v_idx, err, ql, w.window_length);
        }

        double e_max = err*1.5, rr; int64_t re;
        for (i = k = 0; i < ol->length; i++) {
            z = &(ol->list[i]); ovl = z->x_pos_e + 1 - z->x_pos_s;
            rr = gen_extend_err_exz(z, uref, NULL, NULL, qu->seq, tu->seq, exz, v_idx?v_idx->a.a:NULL, w.window_length, -1, err, (e_max+0.000001), THRESHOLD_MAX_SIZE, 1, &re);
            z->is_match = 0;///must be here;


            // fprintf(stderr, "[M::%s::utg%.6dl::%c] sid::%ld, q::[%d, %d), t::[%d, %d), err::%ld\n", 
            //     __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], sid, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1,
            //     re);

            if (rr <= err) {
                if(k != i) {
                    t = ol->list[k];
                    ol->list[k] = ol->list[i];
                    ol->list[i] = t;
                }
                ol->list[k].is_match = 1; ol->list[k].non_homopolymer_errors = re;
                // fprintf(stderr, "+[M::%s] on::%lu\n", __func__, ol->length);
                /****for debug****/
                // z = &(ol->list[k]);
                // fprintf(stderr, "[M::%s::utg%.6dl::%c] sid::%ld, q::[%d, %d), t::[%d, %d), err::%u\n", 
                // __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], sid, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1,
                // z->non_homopolymer_errors);
                /****for debug****/
                k++;
            } 
        }

        ol->length = k;
        // fprintf(stderr, "+[M::%s] on::%lu\n", __func__, ol->length);
        if(ol->length <= 0) return;
    } else {
        // fprintf(stderr, "-[M::%s] on::%lu\n", __func__, ol->length);
        if(ol->length <= 1) {
            if(ol->length == 1) {
                window_list *p;
                z = &(ol->list[0]); z->w_list.n = 0;
                kv_pushp(window_list, z->w_list, &p);
                memset(p, 0, sizeof((*p)));
                p->x_start = z->x_pos_s; p->x_end = z->x_pos_e+1; p->clen = 0;
                z->align_length = z->overlapLen = z->x_pos_e+1-z->x_pos_s;
                z->non_homopolymer_errors = 0;
            }
            return;
        }
        ///coordinates for all intervals with cov > 1
        copy_asg_arr(iidx, hap->snp_srt); copy_asg_arr(buf, v_idx->a); copy_asg_arr(buf1, (*stb));
        // fprintf(stderr, "\n[M::%s] iidx_n::%ld\n", __func__, (int64_t)iidx.n);
        ul_gap_filling_adv(ol, cl, aln, wl, uref, NULL, NULL, qu->seq, tu, exz, aux_o, &buf, &iidx, err, ql, sid, khit, 1, MAX_LGAP(ql));
        copy_asg_arr(hap->snp_srt, iidx); copy_asg_arr(v_idx->a, buf); copy_asg_arr((*stb), buf1);

        copy_asg_arr(iidx, hap->snp_srt); copy_asg_arr(buf, v_idx->a); copy_asg_arr(buf1, (*stb));
        // region_phase(ol, uref, uopt, aln, &iidx, &buf, &buf1, sid);
        rphase_hl(ol, uref, uopt, aln, &iidx, &buf, &buf1, sid, 64, ql, mk, mm, err);
        copy_asg_arr(hap->snp_srt, iidx); copy_asg_arr(v_idx->a, buf); copy_asg_arr((*stb), buf1);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movsd %xmm0, (%rsp)
movq %rdx, 0x10(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x7be17
movq %r8, %rbp
movq %rdi, %rbx
movq %rsi, 0x28(%rsp)
movq %rcx, 0x8(%rsp)
movq %r9, 0x18(%rsp)
movq 0x1b0(%rsp), %rdx
movq 0x178(%rsp), %r14
movq 0x150(%rsp), %rdi
movq 0x148(%rsp), %r12
movq %rbx, %rsi
callq 0x59139
leaq 0x7e(,%r14,2), %r15
cmpq %r15, 0x10(%r12)
jge 0x7b894
movq 0x148(%rsp), %r14
movq (%r14), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r15, 0x10(%r14)
movq 0x140(%rsp), %r13
cmpq $0x0, 0x180(%rsp)
je 0x7b930
cmpq $0x1, 0x10(%rbx)
ja 0x7bba2
jne 0x7be17
movq (%rbx), %rbx
movq $0x0, 0x48(%rbx)
cmpq $0x0, 0x50(%rbx)
jne 0x7b8e8
movq $0x2, 0x50(%rbx)
movq 0x58(%rbx), %rdi
movl $0x40, %esi
callq 0x2370
movq %rax, 0x58(%rbx)
movq 0x48(%rbx), %rax
movq 0x58(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%rbx)
shlq $0x5, %rax
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rcx,%rax)
movupd %xmm0, (%rcx,%rax)
movl 0x4(%rbx), %edx
movl %edx, (%rcx,%rax)
movl 0x8(%rbx), %esi
incl %esi
movl %esi, 0x4(%rcx,%rax)
xorl %edi, %edi
movl %edi, 0x1c(%rcx,%rax)
subl %edx, %esi
movl %esi, 0x20(%rbx)
movl %esi, 0x28(%rbx)
movl %edi, 0x30(%rbx)
jmp 0x7be17
movq 0x18(%rsp), %r14
cmpq %r14, 0x10(%r13)
jge 0x7b94f
movq (%r13), %rdi
movq %r14, %rsi
callq 0x2370
movq %rax, (%r13)
movq %r14, 0x10(%r13)
movq %r14, 0x8(%r13)
movq (%r13), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2160
cmpq $0x0, 0x10(%rbx)
je 0x7ba43
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rbx), %r12
leaq (%r12,%r14), %rdi
movl 0x4(%r12,%r14), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%r12,%r14), %r13d
movl 0x30(%r12,%r14), %eax
movl %eax, 0x24(%r12,%r14)
movq 0x140(%rsp), %rax
movq (%rax), %rcx
movq 0x148(%rsp), %rax
movq (%rax), %r8
subq $0x10, %rsp
movq %rbx, %rbp
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movq 0x168(%rsp), %r9
movsd 0x10(%rsp), %xmm0
movsd 0x1a9b69(%rip), %xmm1   # 0x225538
pushq $0x0
pushq 0x190(%rsp)
callq 0x5b826
addq $0x20, %rsp
testq %rbx, %rbx
movq %rbp, %rbx
je 0x7ba2f
testl %eax, %eax
je 0x7ba2f
incl %r13d
subl 0x8(%rsp), %r13d
je 0x7ba2f
movl 0x28(%r12,%r14), %eax
testl %eax, %eax
je 0x7ba2f
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1a9ae0(%rip), %xmm0   # 0x2254f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x7ba2f
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x7ba2f
movb $0x3, 0x2c(%r12,%r14)
addq %rax, 0x18(%rbx)
incq %r15
addq $0xa8, %r14
cmpq 0x10(%rbx), %r15
jb 0x7b973
cmpq $0x0, 0x10(%rsp)
movq 0x168(%rsp), %rbp
je 0x7ba7a
cmpq $0x0, 0x18(%rbx)
jle 0x7ba7a
movq %rbx, %rdi
movq 0x150(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq 0x178(%rsp), %r8
callq 0x4b691
cmpq $0x0, 0x10(%rbx)
je 0x7be10
movsd 0x1a99e3(%rip), %xmm0   # 0x225470
mulsd (%rsp), %xmm0
addsd 0x1a9a66(%rip), %xmm0   # 0x225500
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
testq %rbp, %rbp
je 0x7bab4
movq 0x10(%rbp), %rax
jmp 0x7bab6
xorl %eax, %eax
movq (%rbx), %r15
leaq (%r15,%r12), %rdi
movq 0x140(%rsp), %rcx
movq (%rcx), %r8
movq 0x148(%rsp), %rcx
movq (%rcx), %r9
movq 0x10(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd (%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
leaq 0x30(%rsp), %r10
pushq %r10
pushq $0x1
pushq $0x1f
pushq 0x190(%rsp)
pushq %rax
pushq 0x180(%rsp)
callq 0x5d4ae
addq $0x30, %rsp
movb $0x0, 0x2c(%r15,%r12)
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x7bb89
cmpq %r13, %r14
je 0x7bb6f
movq (%rbx), %rbp
imulq $0xa8, %r13, %r15
addq %rbp, %r15
movl $0xa8, %edx
leaq 0x60(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %r12, %rbp
movl $0xa8, %edx
movq %r15, %rdi
movq %rbp, %rsi
movq 0x168(%rsp), %rbp
callq 0x2160
movq (%rbx), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x60(%rsp), %rsi
callq 0x2160
movq (%rbx), %rax
imulq $0xa8, %r13, %rcx
movb $0x1, 0x2c(%rax,%rcx)
movl 0x30(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
incq %r13
incq %r14
addq $0xa8, %r12
cmpq 0x10(%rbx), %r14
jb 0x7baa9
jmp 0x7be13
movq %rbx, %rdi
movq %rbx, 0x20(%rsp)
movq 0x198(%rsp), %r8
movq 0x190(%rsp), %rcx
movq 0x170(%rsp), %rax
movq 0x160(%rsp), %r9
movq 0x248(%r9), %rdx
leaq 0x60(%rsp), %r14
movq %rdx, 0x10(%r14)
movq 0x238(%r9), %rdx
movq %rdx, (%r14)
movq 0x240(%r9), %rdx
movq %rdx, 0x8(%r14)
movq 0x168(%rsp), %rbx
movq 0x10(%rbx), %rdx
leaq 0x30(%rsp), %rbp
movq %rdx, 0x10(%rbp)
movq (%rbx), %rdx
movq %rdx, (%rbp)
movq 0x8(%rbx), %rdx
movq %rdx, 0x8(%rbp)
movq 0x10(%r8), %rdx
leaq 0x48(%rsp), %r12
movq %rdx, 0x10(%r12)
movq (%r8), %rdx
movq %rdx, (%r12)
movq 0x8(%r8), %rdx
movq %rdx, 0x8(%r12)
movq 0x18(%rsp), %r15
movq %r15, %xmm0
punpckldq 0x1a95fd(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq (%r13), %rdx
subpd 0x1a9601(%rip), %xmm0   # 0x225250
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1a981d(%rip), %xmm1   # 0x225480
minsd 0x1a9905(%rip), %xmm1   # 0x225570
cvttsd2si %xmm1, %rsi
subq $0x60, %rsp
movq %rsi, 0x58(%rsp)
movq %rcx, 0x48(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq %rbp, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1a8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq $0x1, 0x50(%rsp)
movq $0x0, (%rsp)
movq 0x88(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movq 0x70(%rsp), %r13
movq %r13, %r8
xorl %r9d, %r9d
movsd 0x60(%rsp), %xmm0
callq 0x79f08
addq $0x60, %rsp
movq 0x10(%r14), %rax
movq 0x160(%rsp), %rcx
movq %rax, 0x248(%rcx)
movq (%r14), %rax
movq %rax, 0x238(%rcx)
movq 0x8(%r14), %rax
movq %rax, 0x240(%rcx)
movq 0x10(%rbp), %rax
movq %rax, 0x10(%rbx)
movq (%rbp), %rax
movq %rax, (%rbx)
movq 0x8(%rbp), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%r12), %rax
movq 0x198(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq (%r12), %rax
movq %rax, (%rcx)
movq 0x8(%r12), %rax
movq %rax, 0x8(%rcx)
subq $0x30, %rsp
movq 0x1d0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq %r12, (%rsp)
movq $0x40, 0x10(%rsp)
movq 0x50(%rsp), %rdi
movq %r13, %rsi
movq 0x38(%rsp), %rdx
movq 0x1b0(%rsp), %rcx
movq %r14, %r8
movq %rbp, %r9
movsd 0x30(%rsp), %xmm0
callq 0x78b60
addq $0x30, %rsp
movq 0x10(%r14), %rax
movq 0x160(%rsp), %rcx
movq %rax, 0x248(%rcx)
movq (%r14), %rax
movq %rax, 0x238(%rcx)
movq 0x8(%r14), %rax
movq %rax, 0x240(%rcx)
movq 0x10(%rbp), %rax
movq %rax, 0x10(%rbx)
movq (%rbp), %rax
movq %rax, (%rbx)
movq 0x8(%rbp), %rax
movq %rax, 0x8(%rbx)
movq 0x10(%r12), %rax
movq 0x198(%rsp), %rcx
movq %rax, 0x10(%rcx)
movq (%r12), %rax
movq %rax, (%rcx)
movq 0x8(%r12), %rax
movq %rax, 0x8(%rcx)
jmp 0x7be17
xorl %r13d, %r13d
movq %r13, 0x10(%rbx)
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq