// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ul_rid_lalign_adv(overlap_region_alloc*, Candidates_list*, ul_idx_t const*, ug_opt_t const*, char*, unsigned long, UC_Read*, UC_Read*, bit_extz_t*, overlap_region*, double, long, kv_ul_ov_t*, kv_ul_ov_t*, kv_rtrace_t*, long, unsigned long, void*)
void ul_rid_lalign_adv(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, bit_extz_t *exz, overlap_region *aux_o, double e_rate, 
        int64_t wl, kv_ul_ov_t *aln, kv_ul_ov_t *cln, kv_rtrace_t *trace, int64_t sid, uint64_t khit, void *km)
{
    uint64_t i, bs, k; Window_Pool w; double err; 
    overlap_region t; overlap_region *z; //asg64_v iidx, buf, buf1;
    ol->mapped_overlaps_length = 0;
    if(ol->length <= 0) return;

    ///base alignment
    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) {
        resize_UC_Read(qu, ql); qu->length = ql; memcpy(qu->seq, qstr, ql); 
        for (i = k = 0; i < ol->length; i++) {
            z = &(ol->list[i]); z->shared_seed = z->non_homopolymer_errors;///for index
            if(!ul_local_aln(z, cl, uref, qu->seq, tu, exz, err, w.window_length, 
            1000, OVERLAP_THRESHOLD_NOSI_FILTER, NULL, NULL, NULL, ql, sid, i, khit, NULL)) {
                continue;
            }
            if(k != i) {
                t = ol->list[k];
                ol->list[k] = ol->list[i];
                ol->list[i] = t;
            }
            z = &(ol->list[k++]); z->is_match = 1; 
        }
        ol->length = k;
        if(ol->length <= 0) return;
    } else {
        for (i = cln->n = trace->n = 0; i < ol->length; i++) {
            z = &(ol->list[i]); z->shared_seed = z->non_homopolymer_errors;///for index
            // fprintf(stderr, "\n[M::%s::utg%.6dl(%c)] i::%lu, aln_l::%u, q::[%u, %u), ql::%u\n", __func__, 
            // (int32_t)z->y_id + 1, "+-"[z->y_pos_strand], i, z->align_length, 
            // z->x_pos_s, z->x_pos_e+1, z->x_pos_e+1-z->x_pos_s);
            ul_local_aln(z, cl, uref, qu->seq, tu, exz, err, w.window_length, 1000, 
            OVERLAP_THRESHOLD_NOSI_FILTER, aln, cln, trace, ql, sid, i, khit, aux_o);
        }

        ///contained reads
        if(cln->n) dedup_ul_ov_t(cln);
        if(cln->n) {
            kv_resize(ul_ov_t, *aln, aln->n+cln->n);
            memcpy(aln->a+aln->n, cln->a, cln->n*sizeof(*(aln->a)));
            aln->n += cln->n;
        }

    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movsd %xmm0, 0x68(%rsp)
movq %r9, 0x50(%rsp)
andq $0x0, 0x18(%rdi)
movq %rdx, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
cmpq $0x0, 0x10(%rdi)
je 0x4fcbf
movq %r8, %r15
movq %rdi, %r14
movq 0x170(%rsp), %rax
movq 0x158(%rsp), %rdi
movq 0x150(%rsp), %r13
leaq 0x7e(,%rax,2), %rsi
callq 0x562fa
cmpq $0x0, 0x168(%rsp)
je 0x4fb85
movq 0x188(%rsp), %r15
andq $0x0, 0x8(%r15)
movq 0x180(%rsp), %rax
movq %rax, %rbx
andq $0x0, 0x8(%rax)
movq 0x178(%rsp), %r13
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpq 0x10(%r14), %rbp
jae 0x4fae0
movq (%r14), %rax
leaq (%rax,%r12), %rdi
movl 0x30(%rax,%r12), %ecx
movl %ecx, 0x24(%rax,%r12)
movq 0x150(%rsp), %rax
movq (%rax), %rcx
movq 0x168(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbp, 0x38(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r15, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq %r13, 0x10(%rsp)
movq 0x170(%rsp), %rax
movq %rax, (%rsp)
movq $0x3e8, 0x8(%rsp)       # imm = 0x3E8
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x158(%rsp), %r8
movq 0x160(%rsp), %r9
movsd 0x68(%rsp), %xmm0
movsd 0x193a24(%rip), %xmm1   # 0x1e34f0
callq 0x4ef55
incq %rbp
addq $0xa8, %r12
jmp 0x4fa31
cmpq $0x0, 0x8(%rbx)
je 0x4fcbf
movq %rbx, %rdi
callq 0x4f74d
movq 0x8(%rbx), %rcx
testq %rcx, %rcx
je 0x4fcbf
movq (%r13), %rax
movq 0x8(%r13), %rdx
leaq (%rdx,%rcx), %rsi
cmpq %rsi, 0x10(%r13)
jae 0x4fb65
decq %rsi
movq %rsi, %rcx
shrq %rcx
orq %rsi, %rcx
movq %rcx, %rdx
shrq $0x2, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x4, %rcx
orq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
orq %rcx, %rdx
movq %rdx, %rcx
shrq $0x10, %rcx
orq %rdx, %rcx
incq %rcx
movq %rcx, 0x10(%r13)
imulq $0x1c, %rcx, %rsi
movq %rax, %rdi
callq 0x2370
movq %rax, (%r13)
movq 0x8(%r13), %rdx
movq 0x8(%rbx), %rcx
imulq $0x1c, %rdx, %rdi
addq %rax, %rdi
movq (%rbx), %rsi
imulq $0x1c, %rcx, %rdx
callq 0x2160
movq 0x8(%rbx), %rax
addq %rax, 0x8(%r13)
jmp 0x4fcbf
movq %r13, %rdi
movq 0x50(%rsp), %rbx
movq %rbx, %rsi
callq 0x562fa
movq %rbx, 0x8(%r13)
movq (%r13), %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2160
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %ebx, %ebx
cmpq 0x10(%r14), %rbx
jae 0x4fcbb
movq (%r14), %rax
leaq (%rax,%r15), %rdi
movl 0x30(%rax,%r15), %ecx
movl %ecx, 0x24(%rax,%r15)
movq (%r13), %rcx
movq 0x198(%rsp), %rax
movq %rax, 0x40(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rsp)
movq 0x170(%rsp), %rax
movq %rax, (%rsp)
andq $0x0, 0x48(%rsp)
andq $0x0, 0x20(%rsp)
movq $0x3e8, 0x8(%rsp)       # imm = 0x3E8
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x158(%rsp), %r8
movq 0x160(%rsp), %r9
movsd 0x68(%rsp), %xmm0
movsd 0x1938b4(%rip), %xmm1   # 0x1e34f0
callq 0x4ef55
testq %rax, %rax
je 0x4fcac
cmpq %r12, %rbx
je 0x4fc9a
movq (%r14), %rbp
imulq $0xa8, %r12, %r13
addq %rbp, %r13
movl $0xa8, %edx
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x2160
addq %r15, %rbp
movl $0xa8, %edx
movq %r13, %rdi
movq 0x150(%rsp), %r13
movq %rbp, %rsi
callq 0x2160
movq (%r14), %rdi
addq %r15, %rdi
movl $0xa8, %edx
leaq 0x70(%rsp), %rsi
callq 0x2160
movq (%r14), %rax
imulq $0xa8, %r12, %rcx
incq %r12
movb $0x1, 0x2c(%rax,%rcx)
incq %rbx
addq $0xa8, %r15
jmp 0x4fbb0
movq %r12, 0x10(%r14)
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq