// File: /workspace/github_cpp_makefile_star2_O1/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 $0xd8, %rsp
movsd %xmm0, 0x18(%rsp)
movq %r8, (%rsp)
movq %rdx, 0x20(%rsp)
movq $0x0, 0x18(%rdi)
cmpq $0x0, 0x10(%rdi)
je 0x8577d
movq %rsi, %rbp
movq %rdi, %rbx
movq %r9, 0x8(%rsp)
movq 0x158(%rsp), %rdx
movq 0x138(%rsp), %r14
movq 0x120(%rsp), %rdi
movq 0x118(%rsp), %r15
movq %rbx, %rsi
callq 0x59139
leaq 0x7e(,%r14,2), %r12
cmpq %r12, 0x10(%r15)
jge 0x85368
movq 0x118(%rsp), %r14
movq (%r14), %rdi
movq %r12, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r12, 0x10(%r14)
movq 0x110(%rsp), %r14
cmpq $0x0, 0x130(%rsp)
je 0x854cb
cmpq $0x0, 0x10(%rbx)
movq 0x8(%rsp), %r12
je 0x8577d
testq %r14, %r14
je 0x853c6
cmpq %r12, 0x10(%r14)
jge 0x853ac
movq (%r14), %rdi
movq %r12, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r12, 0x10(%r14)
movq %r12, 0x8(%r14)
movq (%r14), %rdi
movq (%rsp), %rsi
movq %r12, %rdx
callq 0x2160
movq (%r14), %rax
movq %rax, (%rsp)
cmpq $0x0, 0x10(%rbx)
je 0x8576d
movq %rbp, 0x10(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
xorl %r14d, %r14d
movq (%rbx), %r15
leaq (%r15,%rbp), %rdi
subq $0x30, %rsp
movq 0x160(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x178(%rsp), %rax
movq %rax, 0x18(%rsp)
movq %r12, 0x8(%rsp)
movq 0x168(%rsp), %rax
movq %rax, (%rsp)
movq 0x40(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0x148(%rsp), %r8
movq 0x158(%rsp), %r9
movsd 0x48(%rsp), %xmm0
callq 0x830a5
addq $0x30, %rsp
testq %rax, %rax
je 0x854ac
cmpq %r14, %r13
je 0x85498
movq (%rbx), %r12
imulq $0xa8, %r14, %r15
addq %r12, %r15
movl $0xa8, %edx
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x2160
addq %rbp, %r12
movl $0xa8, %edx
movq %r15, %rdi
movq %r12, %rsi
movq 0x8(%rsp), %r12
callq 0x2160
movq (%rbx), %rdi
addq %rbp, %rdi
movl $0xa8, %edx
leaq 0x30(%rsp), %rsi
callq 0x2160
movq (%rbx), %rax
imulq $0xa8, %r14, %rcx
movb $0x1, 0x2c(%rax,%rcx)
incq %r14
jmp 0x854b2
movb $0x0, 0x2c(%r15,%rbp)
incq %r13
addq $0xa8, %rbp
cmpq 0x10(%rbx), %r13
jb 0x853de
jmp 0x85770
testq %r14, %r14
movq 0x8(%rsp), %r15
je 0x85507
cmpq %r15, 0x10(%r14)
jge 0x854ed
movq (%r14), %rdi
movq %r15, %rsi
callq 0x2370
movq %rax, (%r14)
movq %r15, 0x10(%r14)
movq %r15, 0x8(%r14)
movq (%r14), %rdi
movq (%rsp), %rsi
movq %r15, %rdx
callq 0x2160
movq (%r14), %rax
movq %rax, (%rsp)
cmpq $0x0, 0x10(%rbx)
je 0x8565c
movq 0x150(%rsp), %r11
xorl %r12d, %r12d
movq %rbp, 0x10(%rsp)
movq (%rbx), %r13
imulq $0xa8, %r12, %rax
movl 0x4(%r13,%rax), %r15d
movl 0x8(%r13,%rax), %r9d
movslq 0x30(%r13,%rax), %rcx
movl %ecx, 0x24(%r13,%rax)
movq 0x8(%rbp), %rdx
subq %rcx, %rdx
jbe 0x8557e
movq (%rbp), %rsi
shlq $0x4, %rcx
movl (%rsi,%rcx), %edi
addq %rsi, %rcx
xorl %esi, %esi
movl (%rcx), %r8d
xorl %edi, %r8d
testl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
jne 0x85580
cmpq %r11, %rsi
jae 0x85580
incq %rsi
addq $0x10, %rcx
cmpq %rsi, %rdx
jne 0x85559
movq %rdx, %rsi
jmp 0x85580
xorl %esi, %esi
cmpq %r11, %rsi
jae 0x85593
incq %r12
cmpq 0x10(%rbx), %r12
jb 0x85522
jmp 0x8565c
addq %rax, %r13
movq 0x118(%rsp), %rax
movq (%rax), %r8
subq $0x10, %rsp
movq %r13, %rdi
movq 0x30(%rsp), %rbp
movq %rbp, %rsi
xorl %edx, %edx
movq 0x10(%rsp), %rcx
movq %rbx, %r14
movl %r9d, 0x18(%rsp)
movq 0x138(%rsp), %r9
movsd 0x28(%rsp), %xmm0
movsd 0x19ff63(%rip), %xmm1   # 0x225538
pushq $0x0
pushq 0x150(%rsp)
movq %r11, %rbx
callq 0x5b826
movq %rbx, %r11
movl 0x28(%rsp), %esi
movq %r14, %rbx
addq $0x20, %rsp
testq %rbp, %rbp
movq 0x10(%rsp), %rbp
je 0x85585
testl %eax, %eax
je 0x85585
incl %esi
movl 0x28(%r13), %eax
testq %rax, %rax
sete %cl
cmpl %r15d, %esi
sete %dl
orb %cl, %dl
jne 0x85585
subl %r15d, %esi
movl %esi, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
mulsd 0x19febf(%rip), %xmm0   # 0x2254f0
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x85585
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x85585
movb $0x3, 0x2c(%r13)
addq %rax, 0x18(%rbx)
jmp 0x85585
cmpq $0x0, 0x10(%rbx)
je 0x85776
movsd 0x19fe01(%rip), %xmm0   # 0x225470
mulsd 0x18(%rsp), %xmm0
addsd 0x19fe83(%rip), %xmm0   # 0x225500
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbp
xorl %r14d, %r14d
xorl %r13d, %r13d
movq (%rbx), %r12
leaq (%r12,%r15), %rdi
movq 0x118(%rsp), %rax
movq (%rax), %r9
movq 0x20(%rsp), %rsi
xorl %edx, %edx
xorl %ecx, %ecx
movq (%rsp), %r8
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
pushq %rbp
pushq $0x1
pushq $0x1f
pushq 0x150(%rsp)
pushq $0x0
pushq 0x150(%rsp)
callq 0x5d4ae
addq $0x30, %rsp
movb $0x0, 0x2c(%r12,%r15)
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x85757
cmpq %r13, %r14
je 0x8573d
movq (%rbx), %r12
imulq $0xa8, %r13, %rbp
addq %r12, %rbp
movl $0xa8, %edx
leaq 0x30(%rsp), %rdi
movq %rbp, %rsi
callq 0x2160
addq %r15, %r12
movl $0xa8, %edx
movq %rbp, %rdi
leaq 0x28(%rsp), %rbp
movq %r12, %rsi
callq 0x2160
movq (%rbx), %rdi
addq %r15, %rdi
movl $0xa8, %edx
leaq 0x30(%rsp), %rsi
callq 0x2160
movq (%rbx), %rax
imulq $0xa8, %r13, %rcx
movb $0x1, 0x2c(%rax,%rcx)
movl 0x28(%rsp), %edx
movl %edx, 0x30(%rax,%rcx)
incq %r13
incq %r14
addq $0xa8, %r15
cmpq 0x10(%rbx), %r14
jb 0x85691
jmp 0x85779
xorl %r14d, %r14d
movq %r14, 0x10(%rbx)
jmp 0x8577d
xorl %r13d, %r13d
movq %r13, 0x10(%rbx)
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq