// File: 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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x1c0, %rsp            # imm = 0x1C0
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x18(%rbp)
movq %rsi, -0x20(%rbp)
movq %rdx, -0x28(%rbp)
movq %rcx, -0x30(%rbp)
movq %r8, -0x38(%rbp)
movq %r9, -0x40(%rbp)
movsd %xmm0, -0x48(%rbp)
movq -0x18(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x578e3
jmp 0x57f2f
movq 0x18(%rbp), %rdi
movq -0x18(%rbp), %rsi
movq 0x40(%rbp), %rdx
callq 0x4c460
movsd -0x48(%rbp), %xmm0
movsd %xmm0, -0xa8(%rbp)
movq -0x38(%rbp), %rsi
movq 0x30(%rbp), %rdx
movsd 0x32a6ef(%rip), %xmm0   # 0x382000
divsd -0xa8(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0xa0(%rbp), %rdi
callq 0x9f470
movq -0x98(%rbp), %rax
addq $0x3e, %rax
addq $0x1, %rax
movq %rax, -0x58(%rbp)
movq 0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
shlq %rsi
callq 0xa62c0
cmpq $0x0, 0x38(%rbp)
je 0x57d68
movq -0x40(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0xa62c0
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x40(%rbp), %rax
movq (%rax), %rdi
movq -0x30(%rbp), %rsi
movq -0x38(%rbp), %rdx
callq 0x2180
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x57aab
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x158(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x158(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rax
movq (%rax), %rcx
movq 0x10(%rbp), %rax
movq (%rax), %r8
movq 0x10(%rbp), %rax
movq (%rax), %r9
addq -0x58(%rbp), %r9
movq 0x18(%rbp), %r11
movsd -0xa8(%rbp), %xmm0
movq -0x98(%rbp), %r10
movq 0x40(%rbp), %rax
xorl %edx, %edx
movsd 0x32a85c(%rip), %xmm1   # 0x382278
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x4ea30
cmpl $0x0, %eax
jne 0x57a36
jmp 0x57a9a
cmpq $0x0, -0x28(%rbp)
je 0x57a98
movq -0x68(%rbp), %rdi
movq -0x158(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %esi
movq -0x28(%rbp), %rcx
xorl %edx, %edx
movl $0x1, %eax
cmpq $0x0, %rcx
cmovnel %eax, %edx
movsd 0x32a811(%rip), %xmm0   # 0x382278
xorl %eax, %eax
movl %eax, %ecx
callq 0xa7100
cmpl $0x0, %eax
je 0x57a98
movq -0x158(%rbp), %rax
movb $0x3, 0x2c(%rax)
movq -0x158(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %ecx
movq -0x18(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
jmp 0x57a9a
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x5798f
cmpq $0x0, -0x28(%rbp)
je 0x57ae1
movq -0x18(%rbp), %rax
cmpq $0x0, 0x18(%rax)
jle 0x57ae1
movq -0x18(%rbp), %rdi
movq 0x18(%rbp), %rsi
movq 0x28(%rbp), %rdx
movsd -0xa8(%rbp), %xmm0
movq -0x38(%rbp), %rcx
movq -0x98(%rbp), %r8
callq 0x37650
movsd 0x32a717(%rip), %xmm0   # 0x382200
mulsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0x160(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x57d47
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x158(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x158(%rbp), %rax
movq %rax, -0x1a0(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x198(%rbp)
movq -0x40(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x190(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x188(%rbp)
movq 0x10(%rbp), %rax
movq (%rax), %rax
addq -0x58(%rbp), %rax
movq %rax, -0x180(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x178(%rbp)
cmpq $0x0, 0x28(%rbp)
je 0x57bbb
movq 0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x1a8(%rbp)
jmp 0x57bc6
xorl %eax, %eax
movq %rax, -0x1a8(%rbp)
jmp 0x57bc6
movq -0x178(%rbp), %rbx
movq -0x180(%rbp), %r14
movq -0x188(%rbp), %r9
movq -0x190(%rbp), %r8
movq -0x198(%rbp), %rsi
movq -0x1a0(%rbp), %rdi
movq -0x1a8(%rbp), %r11
movq -0x98(%rbp), %r10
movsd -0xa8(%rbp), %xmm1
movsd 0x32a672(%rip), %xmm2   # 0x382280
addsd -0x160(%rbp), %xmm2
xorl %eax, %eax
movl %eax, %ecx
movsd 0x32a656(%rip), %xmm0   # 0x382278
leaq -0x170(%rbp), %rax
movq %rcx, %rdx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
callq 0x52920
movsd %xmm0, -0x168(%rbp)
movq -0x158(%rbp), %rax
movb $0x0, 0x2c(%rax)
movsd -0x168(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x57d34
movq -0x60(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0x57cf4
movq -0x18(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rsi
leaq -0x150(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rsi
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x60(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x50(%rbp), %rax
addq %rax, %rdi
leaq -0x150(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x60(%rbp), %rcx
addq %rcx, %rax
movb $0x1, 0x2c(%rax)
movq -0x170(%rbp), %rax
movl %eax, %ecx
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x60(%rbp), %rdx
addq %rdx, %rax
movl %ecx, 0x30(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x57d36
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x57b09
movq -0x60(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x18(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x57d63
jmp 0x57f2f
jmp 0x57f2f
movq -0x18(%rbp), %rax
cmpq $0x1, 0x10(%rax)
ja 0x57d78
jmp 0x57f2f
movq $0x0, -0x50(%rbp)
movq -0x50(%rbp), %rax
movq -0x18(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x57eee
movq -0x18(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x50(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x158(%rbp)
movq -0x158(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x158(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq -0x158(%rbp), %rax
movb $0x1, 0x2c(%rax)
movq $0x0, -0x60(%rbp)
movq -0x60(%rbp), %rax
movq -0x158(%rbp), %rcx
cmpq 0x48(%rcx), %rax
jae 0x57e93
movq -0x158(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x60(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
je 0x57e0e
jmp 0x57e82
movq -0x158(%rbp), %rax
movq 0x58(%rax), %rdi
movq -0x60(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x158(%rbp), %rsi
movq -0x28(%rbp), %r8
movq -0x40(%rbp), %rax
movq (%rax), %r9
movq 0x10(%rbp), %rax
movq (%rax), %r14
movq 0x10(%rbp), %rax
movq (%rax), %rbx
addq -0x58(%rbp), %rbx
movq 0x18(%rbp), %r11
movq -0x158(%rbp), %rax
movl 0x1c(%rax), %r10d
movq -0x158(%rbp), %rax
movl 0x10(%rax), %eax
xorl %ecx, %ecx
movq %rcx, %rdx
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movl %eax, 0x20(%rsp)
callq 0xa6b10
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0x57ddb
movq -0x68(%rbp), %rcx
movq -0x18(%rbp), %rax
addq 0x18(%rax), %rcx
movq %rcx, 0x18(%rax)
movq -0x158(%rbp), %rdi
movq -0x98(%rbp), %rsi
callq 0x34ab0
movq -0x158(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq -0x40(%rbp), %rcx
movsd -0xa8(%rbp), %xmm0
movq -0x98(%rbp), %r8
callq 0x36730
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0x57d80
movq -0x18(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movsd -0xa8(%rbp), %xmm0
movq -0x98(%rbp), %r10
movq 0x40(%rbp), %rax
movl $0x1, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
callq 0x4b540
addq $0x1c0, %rsp            # imm = 0x1C0
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax,%rax)