// File: /workspace/github_cpp_makefile_star2_O1/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 0x609ba
movq %r8, %r12
movq %rcx, %r13
movq %rdi, %rsi
movq %r9, 0x10(%rsp)
movq 0x150(%rsp), %rdx
movq 0x148(%rsp), %r14
movq 0x140(%rsp), %r15
movq 0x128(%rsp), %rdi
movq 0x120(%rsp), %rbx
movq %rsi, 0x8(%rsp)
callq 0x59139
leaq 0x7e(,%r15,2), %rbp
cmpq %rbp, 0x10(%rbx)
jge 0x605ba
movq 0x120(%rsp), %rbx
movq (%rbx), %rdi
movq %rbp, %rsi
callq 0x2370
movq %rax, (%rbx)
movq %rbp, 0x10(%rbx)
movq 0x140(%rsp), %rax
addq $0x3f, %rax
movq %rax, 0x30(%rsp)
testq %r14, %r14
je 0x6085f
movq 0x10(%rsp), %r14
cmpq %r12, 0x10(%r14)
movq 0x8(%rsp), %rbx
jge 0x605f6
movq (%r14), %rdi
movq %r12, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r12, 0x10(%r14)
movq %r12, 0x8(%r14)
movq (%r14), %rdi
movq %r13, %rsi
movq %r12, 0x28(%rsp)
movq %r12, %rdx
callq 0x2160
cmpq $0x0, 0x10(%rbx)
je 0x606e5
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rbx), %r12
leaq (%r12,%r14), %rdi
movl 0x4(%r12,%r14), %ebp
movl 0x8(%r12,%r14), %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), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movsd 0x30(%rsp), %xmm0
movsd 0x1c4ed1(%rip), %xmm1   # 0x225538
pushq 0x150(%rsp)
pushq 0x140(%rsp)
callq 0x5a68a
addq $0x20, %rsp
testq %rbx, %rbx
je 0x606cc
testl %eax, %eax
je 0x606cc
incl %r13d
subl %ebp, %r13d
je 0x606cc
movl 0x28(%r12,%r14), %eax
testl %eax, %eax
je 0x606cc
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x1c4e48(%rip), %xmm0   # 0x2254f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x606cc
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x606cc
movb $0x3, 0x2c(%r12,%r14)
movq 0x8(%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %r15
addq $0xa8, %r14
movq 0x8(%rsp), %rbx
cmpq 0x10(%rbx), %r15
jb 0x6061e
movq 0x138(%rsp), %r13
cmpq $0x0, 0x18(%rsp)
je 0x6071c
cmpq $0x0, 0x18(%rbx)
jle 0x6071c
movq %rbx, %rdi
movq 0x128(%rsp), %rsi
movq %r13, %rdx
movq 0x28(%rsp), %rcx
movq 0x140(%rsp), %r8
callq 0x4b691
cmpq $0x0, 0x10(%rbx)
movq 0x10(%rsp), %rbp
je 0x609b3
movsd 0x1c4d3c(%rip), %xmm0   # 0x225470
mulsd 0x20(%rsp), %xmm0
addsd 0x1c4dbe(%rip), %xmm0   # 0x225500
movsd %xmm0, 0x28(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r14d, %r14d
testq %r13, %r13
je 0x6075c
movq 0x10(%r13), %rax
jmp 0x6075e
xorl %eax, %eax
movq (%rbx), %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 0x5d0fb
addq $0x30, %rsp
movb $0x0, 0x2c(%r13,%r12)
movsd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x6083e
cmpq %r14, %r15
je 0x60824
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), %rbx
callq 0x2160
movq (%rbx), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
movq (%rbx), %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 %r15
addq $0xa8, %r12
cmpq 0x10(%rbx), %r15
jb 0x60751
jmp 0x609b6
movq 0x8(%rsp), %rbx
cmpq $0x2, 0x10(%rbx)
movq 0x10(%rsp), %r15
jb 0x609ba
cmpq $0x0, 0x10(%rbx)
je 0x60964
xorl %r14d, %r14d
movq (%rbx), %rax
imulq $0xa8, %r14, %rcx
leaq (%rax,%rcx), %rbp
movl 0x8(%rax,%rcx), %r15d
subl 0x4(%rax,%rcx), %r15d
movb $0x1, 0x2c(%rax,%rcx)
cmpq $0x0, 0x48(%rax,%rcx)
je 0x60912
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x58(%rbp), %rdi
cmpl $0x0, 0x1c(%rdi,%r12)
jne 0x60905
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), %ebx
subq $0x8, %rsp
movq %rbp, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x20(%rsp), %r8
pushq %r11
pushq %rbx
pushq 0x140(%rsp)
pushq %r10
pushq %rax
callq 0x8ab12
addq $0x30, %rsp
incq %r13
addq $0x20, %r12
cmpq 0x48(%rbp), %r13
jb 0x608ad
incl %r15d
movq 0x8(%rsp), %rbx
addq %r15, 0x18(%rbx)
movq %rbp, %rdi
movq 0x140(%rsp), %r12
movq %r12, %rsi
callq 0x4a082
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq 0x10(%rsp), %r15
movq %r15, %rcx
movsd 0x20(%rsp), %xmm0
movq %r12, %r8
callq 0x4aed4
incq %r14
cmpq 0x10(%rbx), %r14
jb 0x60882
subq $0x20, %rsp
movq 0x170(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x160(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq 0x38(%rsp), %rsi
movq %r15, %rdx
movq 0x140(%rsp), %rcx
movq 0x148(%rsp), %r8
movq 0x150(%rsp), %r9
callq 0x584ad
addq $0x108, %rsp            # imm = 0x108
jmp 0x609c1
xorl %r14d, %r14d
movq %r14, 0x10(%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq