// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ug_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*, overlap_region*, double, long, long, unsigned long, unsigned long, void*)
void ug_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,  
        overlap_region *aux_o, double e_rate, int64_t wl, int64_t sid, uint64_t khit, 
        uint64_t chain_cut, void *km)
{
    uint64_t i, bs, k, ovl, pid, kl, kn; Window_Pool w; double err; 
    overlap_region t; overlap_region *z; char *in = qstr;
    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(!aux_o) {    
        if(qu) {
            resize_UC_Read(qu, ql); qu->length = ql; memcpy(qu->seq, qstr, ql); in = qu->seq;
        } 
        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
            pid = cl->list[z->shared_seed].readID; kl = cl->length; kn = 0;
            for (k = z->shared_seed; k < kl && cl->list[k].readID == pid && kn < chain_cut; k++) kn++;
            if(kn < chain_cut) continue;
            
            if(!align_ul_ed_post_extz(z, uref, NULL, in/**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, in/**qu->seq**/, tu->seq, exz, NULL/**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;
            
            // if(z->x_id == 57 && z->y_id == 2175) { 
            //     fprintf(stderr, "+utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\talign_length::%u\terr::%f\trr::%f\twn::%u\tk::%lu\ti::%lu\n", 
            //         z->x_id+1, "lc"[uref->ug->u.a[z->x_id].circ], uref->ug->u.a[z->x_id].len, 
            //         z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand],
            //         z->y_id+1, "lc"[uref->ug->u.a[z->y_id].circ], uref->ug->u.a[z->y_id].len, 
            //         z->y_pos_s, z->y_pos_e+1, z->align_length, err, rr, (uint32_t)z->w_list.n/**gen_nkhits(cl, z)**/,
            //         k, i);
            // }
            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;
        // if(sid == 57) { 
        //     fprintf(stderr, "+utg%.6ld%c\tol->length::%lu\n", sid+1, "lc"[uref->ug->u.a[sid].circ], ol->length);
        // }
        if(ol->length <= 0) return;
    } else {
        // if(ol->length <= 1) return;
        if(ol->length <= 0) return;
        if(qu) {
            resize_UC_Read(qu, ql); qu->length = ql; memcpy(qu->seq, qstr, ql); in = qu->seq;
        } 
        for (i = k = 0; i < ol->length; i++) {
            z = &(ol->list[i]); 
            // if(z->x_id == 57 && z->y_id == 2175) { 
            //     fprintf(stderr, "-1-utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\talign_length::%u\terr::%f\twn::%u\tk::%lu\ti::%lu\n", 
            //         z->x_id+1, "lc"[uref->ug->u.a[z->x_id].circ], uref->ug->u.a[z->x_id].len, 
            //         z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand],
            //         z->y_id+1, "lc"[uref->ug->u.a[z->y_id].circ], uref->ug->u.a[z->y_id].len, 
            //         z->y_pos_s, z->y_pos_e+1, z->align_length, err, (uint32_t)z->w_list.n/**gen_nkhits(cl, z)**/,
            //         k, i);
            // }
            if(!ul_raw_aln(z, cl, uref, in/**qu->seq**/, tu, exz, err, wl, ql, sid, khit, aux_o)) {
                z->is_match = 0;
                continue;
            }

            // if(z->x_id == 57 && z->y_id == 2175) { 
            //     fprintf(stderr, "-2-utg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\talign_length::%u\terr::%f\twn::%u\tk::%lu\ti::%lu\n", 
            //         z->x_id+1, "lc"[uref->ug->u.a[z->x_id].circ], uref->ug->u.a[z->x_id].len, 
            //         z->x_pos_s, z->x_pos_e+1, "+-"[z->y_pos_strand],
            //         z->y_id+1, "lc"[uref->ug->u.a[z->y_id].circ], uref->ug->u.a[z->y_id].len, 
            //         z->y_pos_s, z->y_pos_e+1, z->align_length, err, (uint32_t)z->w_list.n/**gen_nkhits(cl, z)**/,
            //         k, i);
            // }
            if(k != i) {
                t = ol->list[k];
                ol->list[k] = ol->list[i];
                ol->list[i] = t;
            }
            ol->list[k].is_match = 1; 
            k++;
        }
        ol->length = k;

        // if(sid == 57) { 
        //     fprintf(stderr, "-utg%.6ld%c\tol->length::%lu\n", sid+1, "lc"[uref->ug->u.a[sid].circ], ol->length);
        // }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xe8, %rsp
movsd %xmm0, 0x10(%rsp)
movq %r8, 0x8(%rsp)
andq $0x0, 0x18(%rdi)
movq %rdx, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x516d2
movq %r9, %r13
movq %rdi, %rbx
movq 0x168(%rsp), %rdx
movq 0x148(%rsp), %r14
movq 0x130(%rsp), %rdi
movq 0x128(%rsp), %r15
movq 0x120(%rsp), %r12
movq %rbx, %rsi
callq 0x29361
leaq 0x7e(,%r14,2), %rsi
movq %r15, %rdi
callq 0x562fa
cmpq $0x0, 0x140(%rsp)
je 0x51452
cmpq $0x0, 0x10(%rbx)
je 0x516d2
testq %r12, %r12
je 0x5134f
movq %r12, %rdi
movq %r13, %rsi
callq 0x562fa
movq %r13, 0x8(%r12)
movq (%r12), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
callq 0x2160
movq (%r12), %rax
movq %rax, 0x8(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r13, 0x20(%rsp)
cmpq 0x10(%rbx), %rbp
jae 0x51449
movq (%rbx), %r15
leaq (%r15,%r14), %rdi
subq $0x30, %rsp
movq 0x170(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x188(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r13, 0x8(%rsp)
movq 0x178(%rsp), %rax
movq %rax, (%rsp)
movq 0x48(%rsp), %rsi
movq 0x58(%rsp), %rdx
movq 0x38(%rsp), %rcx
movq 0x158(%rsp), %r8
movq 0x168(%rsp), %r9
movsd 0x40(%rsp), %xmm0
callq 0x4f3c3
addq $0x30, %rsp
testq %rax, %rax
je 0x51434
cmpq %r12, %rbp
je 0x51420
movq (%rbx), %r13
imulq $0xa8, %r12, %r15
addq %r13, %r15
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %r14, %r13
movl $0xa8, %edx
movq %r15, %rdi
movq %r13, %rsi
movq 0x20(%rsp), %r13
callq 0x2160
movq (%rbx), %rdi
addq %r14, %rdi
movl $0xa8, %edx
leaq 0x40(%rsp), %rsi
callq 0x2160
movq (%rbx), %rax
imulq $0xa8, %r12, %rcx
movb $0x1, 0x2c(%rax,%rcx)
incq %r12
jmp 0x5143a
movb $0x0, 0x2c(%r15,%r14)
incq %rbp
addq $0xa8, %r14
jmp 0x5135c
movq %r12, 0x10(%rbx)
jmp 0x516d2
testq %r12, %r12
je 0x51481
movq %r12, %rdi
movq %r13, %rsi
callq 0x562fa
movq %r13, 0x8(%r12)
movq (%r12), %rdi
movq 0x8(%rsp), %rsi
movq %r13, %rdx
callq 0x2160
movq (%r12), %rax
movq %rax, 0x8(%rsp)
movq 0x160(%rsp), %r15
xorl %r12d, %r12d
movq %rbx, 0x38(%rsp)
movq 0x10(%rbx), %rax
cmpq %rax, %r12
jae 0x515c1
movq (%rbx), %rcx
imulq $0xa8, %r12, %rdx
leaq (%rcx,%rdx), %rdi
movl 0x4(%rcx,%rdx), %r13d
movl 0x8(%rcx,%rdx), %ebp
incl %ebp
movl %ebp, %r14d
subl %r13d, %r14d
movslq 0x30(%rcx,%rdx), %rax
movl %eax, 0x24(%rcx,%rdx)
movq 0x18(%rsp), %rcx
movq (%rcx), %r8
movq 0x8(%rcx), %rcx
movq %rax, %rdx
shlq $0x4, %rdx
movl (%r8,%rdx), %esi
addq %r8, %rdx
xorl %r9d, %r9d
leaq (%rax,%r9), %r8
cmpq %rcx, %r8
jae 0x51509
movl (%rdx), %r8d
xorl %esi, %r8d
testl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jne 0x51509
cmpq %r15, %r9
jae 0x51509
incq %r9
addq $0x10, %rdx
jmp 0x514e3
cmpq %r15, %r9
jae 0x51516
incq %r12
jmp 0x51491
movq 0x128(%rsp), %rax
movq (%rax), %r8
subq $0x10, %rsp
movq 0x38(%rsp), %rbx
movq %rbx, %rsi
xorl %edx, %edx
movq 0x18(%rsp), %rcx
movl %r14d, 0x30(%rsp)
movq %rdi, %r14
movq 0x148(%rsp), %r9
movsd 0x20(%rsp), %xmm0
movsd 0x19202e(%rip), %xmm1   # 0x1e3580
pushq $0x0
pushq 0x160(%rsp)
callq 0x2b6ed
movl 0x40(%rsp), %edi
addq $0x20, %rsp
testq %rbx, %rbx
movq 0x38(%rsp), %rbx
je 0x5150e
testl %eax, %eax
je 0x5150e
movl 0x28(%r14), %eax
testq %rax, %rax
sete %cl
cmpl %r13d, %ebp
sete %dl
orb %cl, %dl
jne 0x5150e
movl %edi, %ecx
cvtsi2sd %rcx, %xmm0
mulsd 0x191faf(%rip), %xmm0   # 0x1e3548
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x5150e
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x5150e
movb $0x3, 0x2c(%r14)
addq %rax, 0x18(%rbx)
jmp 0x5150e
movsd 0x191ef7(%rip), %xmm0   # 0x1e34c0
mulsd 0x10(%rsp), %xmm0
addsd 0x191f79(%rip), %xmm0   # 0x1e3550
movsd %xmm0, 0x18(%rsp)
xorl %r15d, %r15d
leaq 0x30(%rsp), %rbp
xorl %r13d, %r13d
xorl %r14d, %r14d
cmpq %rax, %r14
jae 0x516ce
movq (%rbx), %r12
leaq (%r12,%r15), %rdi
movq 0x128(%rsp), %rax
movq (%rax), %r9
movq 0x28(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq 0x8(%rsp), %r8
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
pushq %rbp
pushq $0x1
pushq $0x1f
pushq 0x160(%rsp)
pushq $0x0
pushq 0x160(%rsp)
callq 0x2cfcc
addq $0x30, %rsp
movb $0x0, 0x2c(%r12,%r15)
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x516bb
cmpq %r13, %r14
je 0x516a1
movq (%rbx), %rbp
imulq $0xa8, %r13, %r12
addq %rbp, %r12
movl $0xa8, %edx
leaq 0x40(%rsp), %rdi
movq %r12, %rsi
callq 0x2160
addq %r15, %rbp
movl $0xa8, %edx
movq %r12, %rdi
movq %rbp, %rsi
leaq 0x30(%rsp), %rbp
callq 0x2160
movq (%rbx), %rdi
addq %r15, %rdi
movl $0xa8, %edx
leaq 0x40(%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
movq 0x10(%rbx), %rax
addq $0xa8, %r15
jmp 0x515eb
movq %r13, 0x10(%rbx)
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq