// File: /workspace/github_cpp_makefile_star2_O2/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 $0x128, %rsp            # imm = 0x128
movsd %xmm0, 0x8(%rsp)
movq %rcx, 0x10(%rsp)
andq $0x0, 0x18(%rdi)
movq %rdx, 0x18(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x488f1
movq %rdi, %rbx
movq %r8, 0x20(%rsp)
movq %rsi, 0x38(%rsp)
movq %r9, 0x28(%rsp)
movq 0x1d0(%rsp), %rdx
movq 0x1a0(%rsp), %r13
movq 0x198(%rsp), %r14
movq 0x170(%rsp), %r12
movq 0x168(%rsp), %r15
movq 0x160(%rsp), %rbp
movq %r12, %rdi
movq %rbx, %rsi
callq 0x29361
leaq 0x7e(,%r14,2), %rsi
movq %r15, %rdi
callq 0x562fa
testq %r13, %r13
je 0x483eb
cmpq $0x1, 0x10(%rbx)
ja 0x48656
jne 0x488f1
movq (%rbx), %rbx
andq $0x0, 0x48(%rbx)
cmpq $0x0, 0x50(%rbx)
je 0x4889f
movq 0x58(%rbx), %rax
xorl %ecx, %ecx
jmp 0x488bb
movq %rbp, %rdi
movq 0x28(%rsp), %r14
movq %r14, %rsi
callq 0x562fa
movq %r14, 0x8(%rbp)
movq (%rbp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x2160
xorl %r14d, %r14d
xorl %r15d, %r15d
cmpq 0x10(%rbx), %r15
jae 0x484ee
movq (%rbx), %r12
leaq (%r12,%r14), %rdi
movl 0x4(%r12,%r14), %eax
movl %eax, 0x10(%rsp)
movl 0x8(%r12,%r14), %r13d
movl 0x30(%r12,%r14), %eax
movl %eax, 0x24(%r12,%r14)
movq (%rbp), %rcx
movq 0x168(%rsp), %rax
movq (%rax), %r8
subq $0x10, %rsp
movq %rbx, %rbp
movq 0x28(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movq 0x188(%rsp), %r9
movsd 0x18(%rsp), %xmm0
movsd 0x19b10b(%rip), %xmm1   # 0x1e3580
pushq $0x0
pushq 0x1b0(%rsp)
callq 0x2b6ed
addq $0x20, %rsp
testq %rbx, %rbx
movq %rbp, %rbx
je 0x484cf
testl %eax, %eax
je 0x484cf
incl %r13d
subl 0x10(%rsp), %r13d
je 0x484cf
movl 0x28(%r12,%r14), %eax
testl %eax, %eax
je 0x484cf
cvtsi2sd %r13, %xmm0
mulsd 0x19b095(%rip), %xmm0   # 0x1e3548
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x484cf
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x484cf
movb $0x3, 0x2c(%r12,%r14)
addq %rax, 0x18(%rbx)
incq %r15
addq $0xa8, %r14
movq 0x160(%rsp), %rbp
movq 0x170(%rsp), %r12
jmp 0x48416
cmpq $0x0, 0x18(%rsp)
je 0x48523
cmpq $0x0, 0x18(%rbx)
jle 0x48523
movq %rbx, %rdi
movq %r12, %rsi
movq 0x188(%rsp), %rdx
movsd 0x8(%rsp), %xmm0
movq 0x28(%rsp), %rcx
movq 0x198(%rsp), %r8
callq 0x1e2db
movsd 0x19af95(%rip), %xmm0   # 0x1e34c0
mulsd 0x8(%rsp), %xmm0
addsd 0x19b017(%rip), %xmm0   # 0x1e3550
movsd %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
leaq 0x40(%rsp), %r13
xorl %r14d, %r14d
xorl %r15d, %r15d
movq 0x188(%rsp), %rax
cmpq 0x10(%rbx), %r15
jae 0x4864d
testq %rax, %rax
je 0x48569
movq 0x10(%rax), %rax
jmp 0x4856b
xorl %eax, %eax
movq (%rbx), %r12
leaq (%r12,%rbp), %rdi
movq 0x160(%rsp), %rcx
movq (%rcx), %r8
movq 0x168(%rsp), %rcx
movq (%rcx), %r9
movq 0x18(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
pushq %r13
pushq $0x1
pushq $0x1f
pushq 0x1b0(%rsp)
pushq %rax
pushq 0x1a0(%rsp)
callq 0x2cfcc
addq $0x30, %rsp
movb $0x0, 0x2c(%r12,%rbp)
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x4863e
cmpq %r14, %r15
je 0x48624
movq (%rbx), %r13
imulq $0xa8, %r14, %r12
addq %r13, %r12
movl $0xa8, %edx
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
addq %rbp, %r13
movl $0xa8, %edx
movq %r12, %rdi
movq %r13, %rsi
leaq 0x40(%rsp), %r13
callq 0x2160
movq (%rbx), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0x80(%rsp), %rsi
callq 0x2160
movq (%rbx), %rax
imulq $0xa8, %r14, %rcx
movb $0x1, 0x2c(%rax,%rcx)
movl 0x40(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
incq %r14
incq %r15
addq $0xa8, %rbp
jmp 0x4854c
movq %r14, 0x10(%rbx)
jmp 0x488f1
movq %r13, %r8
movq 0x1b8(%rsp), %r9
movq 0x1b0(%rsp), %rcx
movq 0x190(%rsp), %rax
movq 0x180(%rsp), %rdi
movq 0x248(%rdi), %rdx
leaq 0x80(%rsp), %r13
movq %rdx, 0x10(%r13)
movups 0x238(%rdi), %xmm0
movaps %xmm0, (%r13)
movq 0x188(%rsp), %rsi
movq 0x10(%rsi), %rdx
leaq 0x40(%rsp), %rdi
movq %rdx, 0x10(%rdi)
movups (%rsi), %xmm0
movaps %xmm0, (%rdi)
movq %rdi, %r10
movq 0x10(%r9), %rdx
movq %rdx, 0x20(%rsp)
leaq 0x60(%rsp), %rsi
movq %rdx, 0x10(%rsi)
movq (%r9), %rdi
movq %rdi, 0x30(%rsp)
movq %rdi, (%rsi)
movq 0x8(%r9), %r15
movq %r15, 0x8(%rsi)
movq 0x28(%rsp), %r14
movq %r14, %xmm0
punpckldq 0x19ab83(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq (%rbp), %rdx
subpd 0x19ab87(%rip), %xmm0   # 0x1e3280
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x19adc3(%rip), %xmm1   # 0x1e34d0
minsd 0x19aeab(%rip), %xmm1   # 0x1e35c0
cvttsd2si %xmm1, %rsi
subq $0x60, %rsp
movq %rsi, 0x58(%rsp)
movq %rcx, 0x48(%rsp)
movq %r14, 0x38(%rsp)
movq %r13, 0x30(%rsp)
movq %r10, %rbp
movq %r10, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
andq $0x0, (%rsp)
movq $0x1, 0x50(%rsp)
movq %rbx, %rdi
movq 0x98(%rsp), %rsi
movq %r8, %rdx
movq 0x1f8(%rsp), %rcx
movq %rbx, %r12
movq 0x78(%rsp), %rbx
movq %rbx, %r8
xorl %r9d, %r9d
movsd 0x68(%rsp), %xmm0
callq 0x4718d
addq $0x60, %rsp
movq 0x10(%r13), %rax
movq 0x180(%rsp), %rcx
movq %rax, 0x248(%rcx)
movaps (%r13), %xmm0
movups %xmm0, 0x238(%rcx)
movq 0x10(%rbp), %rax
movq 0x188(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps (%rbp), %xmm0
movups %xmm0, (%rcx)
movq 0x1b8(%rsp), %rax
movq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x30(%rsp), %rcx
movq %rcx, (%rax)
movq %r15, 0x8(%rax)
movq %rax, %r15
subq $0x30, %rsp
movq 0x1f0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1f8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x18(%rsp)
leaq 0x90(%rsp), %r14
movq %r14, (%rsp)
movq $0x40, 0x10(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
movq 0x40(%rsp), %rdx
movq 0x1d0(%rsp), %rcx
movq %r13, %r8
movq %rbp, %r9
movsd 0x38(%rsp), %xmm0
callq 0x45fbe
addq $0x30, %rsp
movq 0x10(%r13), %rax
movq 0x180(%rsp), %rcx
movq %rax, 0x248(%rcx)
movaps (%r13), %xmm0
movups %xmm0, 0x238(%rcx)
movq 0x10(%rbp), %rax
movq 0x188(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps (%rbp), %xmm0
movups %xmm0, (%rcx)
movq 0x10(%r14), %rax
movq %rax, 0x10(%r15)
movapd (%r14), %xmm0
movupd %xmm0, (%r15)
jmp 0x488f1
movq $0x2, 0x50(%rbx)
movq 0x58(%rbx), %rdi
pushq $0x40
popq %rsi
callq 0x2370
movq %rax, 0x58(%rbx)
movq 0x48(%rbx), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x48(%rbx)
shlq $0x5, %rcx
xorpd %xmm0, %xmm0
movupd %xmm0, 0x10(%rax,%rcx)
movupd %xmm0, (%rax,%rcx)
movl 0x4(%rbx), %edx
movl %edx, (%rax,%rcx)
movl 0x8(%rbx), %esi
incl %esi
movl %esi, 0x4(%rax,%rcx)
subl %edx, %esi
movl %esi, 0x20(%rbx)
movl %esi, 0x28(%rbx)
andl $0x0, 0x30(%rbx)
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq