// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_lalign_old_ed(overlap_region_alloc*, Candidates_list*, ul_idx_t const*, char*, unsigned long, UC_Read*, UC_Read*, Correct_dumy*, haplotype_evdience_alloc*, kvec_t_u64_warp*, double, long, unsigned long, void*)
void ul_lalign_old_ed(overlap_region_alloc* ol, Candidates_list *cl, const ul_idx_t *uref, char *qstr, 
                        uint64_t ql, UC_Read* qu, UC_Read* tu, Correct_dumy* dumy, 
                        haplotype_evdience_alloc* hap, kvec_t_u64_warp* v_idx,   
                        double e_rate, int64_t wl, uint64_t is_base, void *km)
{
    uint64_t i, bs, k, ovl/**, on**/; Window_Pool w; double err; 
    /**int64_t sc;**/ overlap_region t; overlap_region *z; 
    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(is_base) {    
        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;
            if(!align_ul_ed_post(z, uref, NULL, qu->seq, tu->seq, tu->seq+bs, dumy, err, w.window_length, -1, 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(z, uref, NULL, NULL, qu->seq, tu->seq, tu->seq+bs,
                        dumy, v_idx?v_idx->a.a:NULL, w.window_length, -1, err, (e_max+0.000001), &re);
            z->is_match = 0;///must be here;
            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;
                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) return;
        // for (i = 0; (i < ol->length) && (ol->list[i].is_match == 1); i++); on = i;
        // if(on <= 1) return;
        // kv_resize(uint64_t, v_idx->a, (on<<1)); v_idx->a.n = on;
        // for (i = 0; i < on; i++) {
        //     sc = ol->list[i].x_pos_e+1-ol->list[i].x_pos_s;
        //     sc -= ((int64_t)(ol->list[i].non_homopolymer_errors*ERROR_RATE));
        //     if(sc < 0) sc = 0;
        //     v_idx->a.a[i] = sc; v_idx->a.a[i] <<= 32; v_idx->a.a[i] += i;
        // }
        // radix_sort_bc64(v_idx->a.a, v_idx->a.a+on);
        // for (i = 0; i < on; i++) {
        //     ol->list[i].non_homopolymer_errors = 0;
        //     k = ((uint32_t)v_idx->a.a[i]);
        //     v_idx->a.a[k]<<=32; v_idx->a.a[k]>>=32; v_idx->a.a[k]|=i;
        // }
        // ul_phase(ol->list, on, v_idx->a.a, v_idx->a.a+on);


        for (i = 0; i < ol->length; i++) {
            z = &(ol->list[i]); ovl = z->x_pos_e+1-z->x_pos_s; z->is_match = 1;
            for (k = 0; k < z->w_list.n; k++) {
                if(z->w_list.a[k].clen) continue;
                gen_backtrace_adv(&(z->w_list.a[k]), z, NULL, NULL, uref, qu->seq, tu->seq, tu->seq+bs,
                dumy, z->y_pos_strand, z->y_id);
            }
            ol->mapped_overlaps_length += ovl; 
            append_unmatched_wins(z, w.window_length);
            calculate_ul_boundary_cigars(z, uref, dumy, qu, err, w.window_length);
        }
        partition_ul_overlaps_advance(ol, uref, qu, tu, dumy, hap, 1, err, w.window_length, km);
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm0, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x6026f
movq %r8, %r12
movq %rcx, %r13
movq %rdi, %rsi
movq %r9, 0x10(%rsp)
movq 0x150(%rsp), %rdx
movq 0x148(%rsp), %rbx
movq 0x140(%rsp), %r15
movq 0x128(%rsp), %rdi
movq 0x120(%rsp), %r14
movq %rsi, 0x8(%rsp)
callq 0x58595
leaq 0x7e(,%r15,2), %rbp
cmpq %rbp, 0x10(%r14)
jge 0x5fe65
movq 0x120(%rsp), %r14
movq (%r14), %rdi
movq %rbp, %rsi
callq 0x2370
movq %rax, (%r14)
movq %rbp, 0x10(%r14)
movq 0x140(%rsp), %rax
addq $0x3f, %rax
movq %rax, 0x30(%rsp)
testq %rbx, %rbx
je 0x5ff97
movq 0x10(%rsp), %rbx
movq (%rbx), %rdi
cmpq %r12, 0x10(%rbx)
movq 0x8(%rsp), %r15
jge 0x5fea4
movq %r12, %rsi
callq 0x2370
movq %rax, %rdi
movq %rax, (%rbx)
movq %r12, 0x10(%rbx)
movq %r12, 0x8(%rbx)
movq %r13, %rsi
movq %r12, 0x28(%rsp)
movq %r12, %rdx
callq 0x2160
cmpq $0x0, 0x10(%r15)
je 0x600e7
xorl %ebx, %ebx
xorl %r14d, %r14d
movq (%r15), %r12
leaq (%r12,%rbx), %rdi
movl 0x4(%r12,%rbx), %ebp
movl 0x8(%r12,%rbx), %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x120(%rsp), %rax
movq (%rax), %r8
movq 0x30(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq 0x28(%rsp), %r15
movq %r15, %rsi
xorl %edx, %edx
movsd 0x30(%rsp), %xmm0
movsd 0x1d1727(%rip), %xmm1   # 0x231638
pushq 0x150(%rsp)
pushq 0x140(%rsp)
callq 0x59be2
addq $0x20, %rsp
testq %r15, %r15
je 0x5ff76
testl %eax, %eax
je 0x5ff76
incl %r13d
subl %ebp, %r13d
je 0x5ff76
movl 0x28(%r12,%rbx), %eax
testl %eax, %eax
je 0x5ff76
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1d169e(%rip), %xmm0   # 0x2315f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x5ff76
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x5ff76
movb $0x3, 0x2c(%r12,%rbx)
movq 0x8(%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %r14
movq 0x8(%rsp), %r15
movq 0x10(%r15), %rax
addq $0xa8, %rbx
cmpq %rax, %r14
jb 0x5fec8
jmp 0x600e9
movq 0x8(%rsp), %r14
cmpq $0x2, 0x10(%r14)
jb 0x6026f
xorl %ebx, %ebx
movq (%r14), %rax
imulq $0xa8, %rbx, %rcx
movl 0x8(%rax,%rcx), %r14d
subl 0x4(%rax,%rcx), %r14d
leaq (%rax,%rcx), %rbp
movb $0x1, 0x2c(%rax,%rcx)
movq 0x48(%rax,%rcx), %rax
testq %rax, %rax
je 0x60040
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x58(%rbp), %rdi
cmpl $0x0, 0x1c(%rdi,%r12)
jne 0x60034
addq %r12, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %r9
movq 0x120(%rsp), %rax
movq (%rax), %rax
movq 0x30(%rsp), %rcx
leaq (%rax,%rcx), %r10
movl 0x10(%rbp), %r11d
movl 0x1c(%rbp), %r15d
subq $0x8, %rsp
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x20(%rsp), %r8
pushq %r11
pushq %r15
pushq 0x140(%rsp)
pushq %r10
pushq %rax
callq 0x89ef9
addq $0x30, %rsp
movq 0x48(%rbp), %rax
incq %r13
addq $0x20, %r12
cmpq %rax, %r13
jb 0x5ffd6
incl %r14d
movq 0x8(%rsp), %r15
addq %r14, 0x18(%r15)
movq %rbp, %rdi
movq 0x140(%rsp), %r12
movq %r12, %rsi
callq 0x49760
movq %rbp, %rdi
movq %r15, %r14
movq 0x18(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq 0x10(%rsp), %r15
movq %r15, %rcx
movsd 0x20(%rsp), %xmm0
movq %r12, %r8
callq 0x4a52c
incq %rbx
cmpq 0x10(%r14), %rbx
jb 0x5ffa9
subq $0x20, %rsp
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq 0x140(%rsp), %rcx
movq 0x148(%rsp), %r8
movq 0x150(%rsp), %r9
callq 0x578f5
addq $0x108, %rsp            # imm = 0x108
jmp 0x60276
xorl %eax, %eax
movq 0x138(%rsp), %r13
cmpq $0x0, 0x18(%rsp)
je 0x6012b
cmpq $0x0, 0x18(%r15)
movq 0x10(%rsp), %rbp
jle 0x60130
movq %r15, %rdi
movq 0x128(%rsp), %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
movq 0x140(%rsp), %r8
callq 0x4ad19
movq 0x10(%r15), %rax
jmp 0x60130
movq 0x10(%rsp), %rbp
testq %rax, %rax
je 0x60268
movsd 0x1d142f(%rip), %xmm0   # 0x231570
mulsd 0x20(%rsp), %xmm0
addsd 0x1d14b1(%rip), %xmm0   # 0x231600
movsd %xmm0, 0x28(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
testq %r13, %r13
je 0x60168
movq 0x10(%r13), %rax
jmp 0x6016a
xorl %eax, %eax
movq (%r15), %r13
leaq (%r12,%r13), %rdi
movq 0x120(%rsp), %rcx
movq (%rcx), %r9
movq 0x30(%rsp), %rcx
leaq (%r9,%rcx), %r10
movq (%rbp), %r8
subq $0x8, %rsp
movq 0x20(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
leaq 0x40(%rsp), %r11
pushq %r11
pushq 0x150(%rsp)
pushq %rax
pushq 0x148(%rsp)
pushq %r10
callq 0x5c901
addq $0x30, %rsp
movb $0x0, 0x2c(%r13,%r12)
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x6024a
cmpq %r14, %rbx
je 0x60230
movq 0x8(%rsp), %rax
movq (%rax), %rbp
imulq $0xa8, %r14, %r13
addq %rbp, %r13
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
addq %r12, %rbp
movl $0xa8, %edx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x10(%rsp), %rbp
movq 0x8(%rsp), %r15
callq 0x2160
movq (%r15), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
movq (%r15), %rax
imulq $0xa8, %r14, %rcx
movb $0x1, 0x2c(%rax,%rcx)
movl 0x38(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
incq %r14
movq 0x138(%rsp), %r13
incq %rbx
addq $0xa8, %r12
cmpq 0x10(%r15), %rbx
jb 0x6015d
jmp 0x6026b
xorl %r14d, %r14d
movq %r14, 0x10(%r15)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq