// File: /workspace/github_cpp_makefile_star2_O3/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 $0x118, %rsp            # imm = 0x118
movsd %xmm0, (%rsp)
movq %rdx, 0x10(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x7b985
movq %r9, %r13
movq %r8, %rbp
movq %rdi, %rbx
movq %rsi, 0x18(%rsp)
movq %rcx, 0x8(%rsp)
movq 0x1c0(%rsp), %rdx
movq 0x188(%rsp), %r14
movq 0x160(%rsp), %rdi
movq 0x158(%rsp), %r12
movq %rbx, %rsi
callq 0x58595
leaq 0x7e(,%r14,2), %r15
cmpq %r15, 0x10(%r12)
jge 0x7b41e
movq 0x158(%rsp), %r14
movq (%r14), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r15, 0x10(%r14)
movq 0x150(%rsp), %r12
cmpq $0x0, 0x190(%rsp)
je 0x7b463
cmpq $0x1, 0x10(%rbx)
ja 0x7b580
jne 0x7b985
movq (%rbx), %rbx
movq $0x0, 0x48(%rbx)
cmpq $0x0, 0x50(%rbx)
je 0x7b92e
movq 0x58(%rbx), %rax
xorl %ecx, %ecx
jmp 0x7b94c
movq (%r12), %rdi
cmpq %r13, 0x10(%r12)
jge 0x7b482
movq %r13, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%r12)
movq %r13, 0x10(%r12)
movq %r13, 0x8(%r12)
movq %rbp, %rsi
movq %r13, 0x18(%rsp)
movq %r13, %rdx
callq 0x2160
cmpq $0x0, 0x10(%rbx)
je 0x7b7bb
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 0x150(%rsp), %rax
movq (%rax), %rcx
movq 0x158(%rsp), %rax
movq (%rax), %r8
subq $0x10, %rsp
movq %rbx, %rbp
movq 0x20(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movq 0x178(%rsp), %r9
movsd 0x10(%rsp), %xmm0
movsd 0x1b6134(%rip), %xmm1   # 0x231638
pushq $0x0
pushq 0x1a0(%rsp)
callq 0x5ae21
addq $0x20, %rsp
testq %rbx, %rbx
movq %rbp, %rbx
je 0x7b564
testl %eax, %eax
je 0x7b564
incl %r13d
subl 0x8(%rsp), %r13d
je 0x7b564
movl 0x28(%r12,%r14), %eax
testl %eax, %eax
je 0x7b564
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1b60ab(%rip), %xmm0   # 0x2315f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x7b564
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x7b564
movb $0x3, 0x2c(%r12,%r14)
addq %rax, 0x18(%rbx)
incq %r15
movq 0x10(%rbx), %rax
addq $0xa8, %r14
cmpq %rax, %r15
jb 0x7b4a8
jmp 0x7b7bd
movq 0x1a8(%rsp), %r8
movq 0x1a0(%rsp), %rcx
movq 0x180(%rsp), %rax
movq 0x170(%rsp), %rbp
movq 0x248(%rbp), %rdx
leaq 0x70(%rsp), %r14
movq %rdx, 0x10(%r14)
movups 0x238(%rbp), %xmm0
movaps %xmm0, (%r14)
movq 0x178(%rsp), %rsi
movq 0x10(%rsi), %rdx
leaq 0x30(%rsp), %rdi
movq %rdx, 0x10(%rdi)
movups (%rsi), %xmm0
movaps %xmm0, (%rdi)
movq 0x10(%r8), %rsi
movq %rsi, 0x28(%rsp)
leaq 0x50(%rsp), %rdx
movq %rsi, 0x10(%rdx)
movq (%r8), %rsi
movq %rsi, 0x20(%rsp)
movq %rsi, (%rdx)
movq 0x8(%r8), %r15
movq %r15, 0x8(%rdx)
movq %r13, %xmm0
punpckldq 0x1b5c78(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
movq (%r12), %rdx
subpd 0x1b5c7c(%rip), %xmm0   # 0x231290
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1b5f58(%rip), %xmm1   # 0x231580
minsd 0x1b6040(%rip), %xmm1   # 0x231670
cvttsd2si %xmm1, %rsi
subq $0x60, %rsp
movq %rsi, 0x58(%rsp)
movq %rcx, 0x48(%rsp)
movq %r13, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movq %rdi, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movq 0x1c8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1b8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq $0x1, 0x50(%rsp)
movq $0x0, (%rsp)
movq %rbx, %rdi
movq 0x78(%rsp), %rsi
movq 0x1f0(%rsp), %rdx
movq 0x1e8(%rsp), %rcx
movq %rbx, %r12
movq 0x70(%rsp), %rbx
movq %rbx, %r8
xorl %r9d, %r9d
movsd 0x60(%rsp), %xmm0
callq 0x799bd
addq $0x60, %rsp
movq 0x10(%r14), %rax
movq %rax, 0x248(%rbp)
movaps (%r14), %xmm0
movups %xmm0, 0x238(%rbp)
leaq 0x30(%rsp), %rbp
movq 0x10(%rbp), %rax
movq 0x178(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps (%rbp), %xmm0
movups %xmm0, (%rcx)
movq 0x1a8(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, 0x10(%rax)
movq 0x20(%rsp), %rcx
movq %rcx, (%rax)
movq %r15, 0x8(%rax)
movq %rax, %r15
subq $0x30, %rsp
movq 0x1e0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x1e8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r13, 0x18(%rsp)
leaq 0x80(%rsp), %r13
movq %r13, (%rsp)
movq $0x40, 0x10(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
movq 0x38(%rsp), %rdx
movq 0x1c0(%rsp), %rcx
movq %r14, %r8
movq %rbp, %r9
movsd 0x30(%rsp), %xmm0
callq 0x78640
addq $0x30, %rsp
movq 0x10(%r14), %rax
movq 0x170(%rsp), %rcx
movq %rax, 0x248(%rcx)
movaps (%r14), %xmm0
movups %xmm0, 0x238(%rcx)
movq 0x10(%rbp), %rax
movq 0x178(%rsp), %rcx
movq %rax, 0x10(%rcx)
movaps (%rbp), %xmm0
movups %xmm0, (%rcx)
movq 0x10(%r13), %rax
movq %rax, 0x10(%r15)
movapd (%r13), %xmm0
movupd %xmm0, (%r15)
jmp 0x7b985
xorl %eax, %eax
cmpq $0x0, 0x10(%rsp)
je 0x7b7fa
cmpq $0x0, 0x18(%rbx)
movq 0x178(%rsp), %rbp
jle 0x7b802
movq %rbx, %rdi
movq 0x160(%rsp), %rsi
movq %rbp, %rdx
movq 0x18(%rsp), %rcx
movq 0x188(%rsp), %r8
callq 0x4ad19
movq 0x10(%rbx), %rax
jmp 0x7b802
movq 0x178(%rsp), %rbp
testq %rax, %rax
je 0x7b925
movsd 0x1b5d5d(%rip), %xmm0   # 0x231570
mulsd (%rsp), %xmm0
addsd 0x1b5de0(%rip), %xmm0   # 0x231600
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
testq %rbp, %rbp
je 0x7b83a
movq 0x10(%rbp), %rax
jmp 0x7b83c
xorl %eax, %eax
movq (%rbx), %r15
leaq (%r15,%r12), %rdi
movq 0x150(%rsp), %rcx
movq (%rcx), %r8
movq 0x158(%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 0x1a0(%rsp)
pushq %rax
pushq 0x190(%rsp)
callq 0x5ccc3
addq $0x30, %rsp
movb $0x0, 0x2c(%r15,%r12)
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x7b90f
cmpq %r13, %r14
je 0x7b8f5
movq (%rbx), %rbp
imulq $0xa8, %r13, %r15
addq %rbp, %r15
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %r12, %rbp
movl $0xa8, %edx
movq %r15, %rdi
movq %rbp, %rsi
movq 0x178(%rsp), %rbp
callq 0x2160
movq (%rbx), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x70(%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 0x7b82f
jmp 0x7b928
xorl %r13d, %r13d
movq %r13, 0x10(%rbx)
jmp 0x7b985
movq $0x2, 0x50(%rbx)
movq 0x58(%rbx), %rdi
movl $0x40, %esi
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)
movl $0x0, 0x30(%rbx)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq