// File: /workspace/github_cpp_makefile_star2_O2/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 $0xd8, %rsp
movsd %xmm0, (%rsp)
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
andq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x2ff4b
movq %rdi, %r14
movq %rcx, %rbp
movq %rdx, %r15
movq 0x140(%rsp), %rdx
movq 0x138(%rsp), %rbx
movq 0x130(%rsp), %r13
movq 0x118(%rsp), %rdi
movq 0x110(%rsp), %r12
movq %r14, %rsi
callq 0x29361
leaq 0x3f(%r13), %rax
movq %rax, 0x20(%rsp)
leaq 0x7e(,%r13,2), %rsi
movq %r12, %rdi
callq 0x562fa
testq %rbx, %rbx
movq %r14, 0x18(%rsp)
je 0x2ff41
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq 0x8(%rsp), %r14
movq %r14, %rsi
callq 0x562fa
movq %r14, 0x8(%rbx)
movq (%rbx), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x2160
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x18(%rsp), %rbp
cmpq 0x10(%rbp), %r14
jae 0x2fdd5
movq (%rbp), %r12
leaq (%r12,%rbx), %rdi
movl 0x4(%r12,%rbx), %ebp
movl 0x8(%r12,%rbx), %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rcx
movq 0x110(%rsp), %rax
movq (%rax), %r8
movq 0x20(%rsp), %rax
leaq (%r8,%rax), %r9
subq $0x10, %rsp
movq %r15, %rsi
xorl %edx, %edx
movsd 0x10(%rsp), %xmm0
movsd 0x1b3819(%rip), %xmm1   # 0x1e3580
pushq 0x140(%rsp)
pushq 0x130(%rsp)
callq 0x2a687
addq $0x20, %rsp
testq %r15, %r15
je 0x2fdc6
testl %eax, %eax
je 0x2fdc6
incl %r13d
subl %ebp, %r13d
je 0x2fdc6
movl 0x28(%r12,%rbx), %eax
testl %eax, %eax
je 0x2fdc6
cvtsi2sd %r13, %xmm0
mulsd 0x1b37a3(%rip), %xmm0   # 0x1e3548
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x2fdc6
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x2fdc6
movb $0x3, 0x2c(%r12,%rbx)
movq 0x18(%rsp), %rcx
addq %rax, 0x18(%rcx)
incq %r14
addq $0xa8, %rbx
jmp 0x2fd13
testq %r15, %r15
je 0x2fe0b
cmpq $0x0, 0x18(%rbp)
jle 0x2fe0b
movq %rbp, %rdi
movq 0x118(%rsp), %rsi
movq 0x128(%rsp), %rdx
movsd (%rsp), %xmm0
movq 0x8(%rsp), %rcx
movq 0x130(%rsp), %r8
callq 0x1e2db
movsd 0x1b36ad(%rip), %xmm0   # 0x1e34c0
mulsd (%rsp), %xmm0
addsd 0x1b3730(%rip), %xmm0   # 0x1e3550
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %r14d, %r14d
movq 0x128(%rsp), %rax
cmpq 0x10(%rbp), %r14
jae 0x2ff3b
testq %rax, %rax
je 0x2fe4b
movq 0x10(%rax), %rax
jmp 0x2fe4d
xorl %eax, %eax
movq (%rbp), %r13
leaq (%r12,%r13), %rdi
movq 0x110(%rsp), %rcx
movq (%rcx), %r9
movq 0x20(%rsp), %rcx
leaq (%r9,%rcx), %r10
movq 0x10(%rsp), %rcx
movq (%rcx), %r8
subq $0x8, %rsp
movq %r15, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
leaq 0x30(%rsp), %r11
pushq %r11
pushq 0x140(%rsp)
pushq %rax
pushq 0x138(%rsp)
pushq %r10
callq 0x2cc26
addq $0x30, %rsp
movb $0x0, 0x2c(%r13,%r12)
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2ff2c
cmpq %rbx, %r14
je 0x2ff11
movq 0x18(%rsp), %rax
movq (%rax), %rbp
imulq $0xa8, %rbx, %r13
addq %rbp, %r13
movl $0xa8, %edx
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
addq %r12, %rbp
movl $0xa8, %edx
movq %r13, %rdi
movq %rbp, %rsi
movq 0x18(%rsp), %rbp
callq 0x2160
movq (%rbp), %rdi
addq %r12, %rdi
movl $0xa8, %edx
leaq 0x30(%rsp), %rsi
callq 0x2160
movq (%rbp), %rax
imulq $0xa8, %rbx, %rcx
movb $0x1, 0x2c(%rax,%rcx)
movl 0x28(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
incq %rbx
incq %r14
addq $0xa8, %r12
jmp 0x2fe2e
movq %rbx, 0x10(%rbp)
jmp 0x2ff4b
movq 0x10(%r14), %rax
cmpq $0x2, %rax
jae 0x2ff5d
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ecx, %ecx
cmpq %rax, %rcx
movq 0x140(%rsp), %rax
jae 0x30050
movq (%r14), %rax
movq %rcx, 0x8(%rsp)
imulq $0xa8, %rcx, %rcx
leaq (%rax,%rcx), %rbp
movl 0x8(%rax,%rcx), %r14d
subl 0x4(%rax,%rcx), %r14d
incl %r14d
movb $0x1, 0x2c(%rax,%rcx)
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq 0x48(%rbp), %r13
jae 0x30000
movq 0x58(%rbp), %rdi
cmpl $0x0, 0x1c(%rdi,%r12)
jne 0x2fff7
addq %r12, %rdi
movq 0x10(%rsp), %rax
movq (%rax), %r9
movq 0x110(%rsp), %rax
movq (%rax), %rax
movq 0x20(%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 %r15, %r8
pushq %r11
pushq %rbx
pushq 0x130(%rsp)
pushq %r10
pushq %rax
callq 0x567ea
addq $0x30, %rsp
incq %r13
addq $0x20, %r12
jmp 0x2ff9b
movl %r14d, %eax
movq 0x18(%rsp), %r14
addq %rax, 0x18(%r14)
movq %rbp, %rdi
movq 0x130(%rsp), %rbx
movq %rbx, %rsi
callq 0x1cf34
movq %rbp, %rdi
movq %r15, %rsi
movq 0x118(%rsp), %rdx
movq 0x10(%rsp), %rcx
movsd (%rsp), %xmm0
movq %rbx, %r8
callq 0x1dbea
movq 0x8(%rsp), %rcx
incq %rcx
movq 0x10(%r14), %rax
jmp 0x2ff5f
subq $0x20, %rsp
movq %rax, 0x10(%rsp)
movq 0x150(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq 0x138(%rsp), %r8
movq 0x140(%rsp), %r9
movsd 0x20(%rsp), %xmm0
callq 0x28a1d
addq $0xf8, %rsp
jmp 0x2ff52