// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq 0x60(%rbp), %rax
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
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, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq -0x30(%rbp), %rax
movq $0x0, 0x18(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x99139
jmp 0x99609
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x58(%rbp), %rsi
movq 0x30(%rbp), %rdx
movsd 0x2e8eaa(%rip), %xmm0   # 0x382000
divsd -0xb8(%rbp), %xmm0
cvttsd2si %xmm0, %eax
movslq %eax, %rcx
leaq -0xb0(%rbp), %rdi
callq 0x9f470
movq -0xa8(%rbp), %rax
addq $0x3e, %rax
addq $0x1, %rax
movq %rax, -0x70(%rbp)
movq 0x18(%rbp), %rdi
movq -0x70(%rbp), %rsi
shlq %rsi
callq 0xa62c0
cmpq $0x0, 0x28(%rbp)
jne 0x9939a
movq 0x10(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0xa62c0
movq -0x58(%rbp), %rcx
movq 0x10(%rbp), %rax
movq %rcx, 0x8(%rax)
movq 0x10(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rsi
movq -0x58(%rbp), %rdx
callq 0x2180
movq $0x0, -0x78(%rbp)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x99379
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x168(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x168(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rax
movq (%rax), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movsd -0xb8(%rbp), %xmm0
movq -0xa8(%rbp), %r14
movq -0x58(%rbp), %rbx
movq 0x50(%rbp), %r11
movq -0x68(%rbp), %r10
movq 0x58(%rbp), %rax
movsd 0x2e8fc8(%rip), %xmm1   # 0x382228
xorl %r15d, %r15d
movq %r14, (%rsp)
movq $0x3e8, 0x8(%rsp)       # imm = 0x3E8
movq $0x0, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
callq 0x97a80
cmpq $0x0, %rax
jne 0x992b8
jmp 0x99368
movq -0x78(%rbp), %rax
cmpq -0x68(%rbp), %rax
je 0x99336
movq -0x30(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x78(%rbp), %rax
addq %rax, %rsi
leaq -0x160(%rbp), %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x30(%rbp), %rax
movq (%rax), %rsi
imulq $0xa8, -0x68(%rbp), %rax
addq %rax, %rsi
movq -0x30(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x78(%rbp), %rax
addq %rax, %rdi
movl $0xa8, %edx
callq 0x2180
movq -0x30(%rbp), %rax
movq (%rax), %rdi
imulq $0xa8, -0x68(%rbp), %rax
addq %rax, %rdi
leaq -0x160(%rbp), %rsi
movl $0xa8, %edx
callq 0x2180
movq -0x30(%rbp), %rax
movq (%rax), %rax
movq -0x78(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x78(%rbp)
imulq $0xa8, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movb $0x1, 0x2c(%rax)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x991dc
movq -0x78(%rbp), %rcx
movq -0x30(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x30(%rbp), %rax
cmpq $0x0, 0x10(%rax)
ja 0x99395
jmp 0x99609
jmp 0x99609
movq 0x48(%rbp), %rax
movq $0x0, 0x8(%rax)
movq 0x40(%rbp), %rax
movq $0x0, 0x8(%rax)
movq $0x0, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq -0x30(%rbp), %rcx
cmpq 0x10(%rcx), %rax
jae 0x994b5
movq -0x30(%rbp), %rax
movq (%rax), %rax
imulq $0xa8, -0x68(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x168(%rbp)
movq -0x168(%rbp), %rax
movl 0x30(%rax), %ecx
movq -0x168(%rbp), %rax
movl %ecx, 0x24(%rax)
movq -0x168(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq 0x10(%rbp), %rax
movq (%rax), %rcx
movq 0x18(%rbp), %r8
movq 0x20(%rbp), %r9
movsd -0xb8(%rbp), %xmm0
movq -0xa8(%rbp), %rax
movq %rax, -0x178(%rbp)
movq 0x38(%rbp), %r13
movq 0x40(%rbp), %r12
movq 0x48(%rbp), %r15
movq -0x58(%rbp), %r14
movq 0x50(%rbp), %rbx
movq -0x68(%rbp), %r11
movq 0x58(%rbp), %r10
movq 0x28(%rbp), %rax
movq %rax, -0x170(%rbp)
movq -0x178(%rbp), %rax
movsd 0x2e8dc5(%rip), %xmm1   # 0x382228
movq %rax, (%rsp)
movq -0x170(%rbp), %rax
movq $0x3e8, 0x8(%rsp)       # imm = 0x3E8
movq %r13, 0x10(%rsp)
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movq %r11, 0x38(%rsp)
movq %r10, 0x40(%rsp)
movq %rax, 0x48(%rsp)
callq 0x97a80
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x993ba
movq 0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x994c9
movq 0x40(%rbp), %rdi
callq 0x989a0
movq 0x40(%rbp), %rax
cmpq $0x0, 0x8(%rax)
je 0x99607
jmp 0x994da
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x38(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x40(%rbp), %rdx
addq 0x8(%rdx), %rcx
cmpq %rcx, %rax
jae 0x995c9
movq 0x38(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x40(%rbp), %rax
addq 0x8(%rax), %rcx
movq 0x38(%rbp), %rax
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
addq $-0x1, %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
shrq %rcx
movq 0x38(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x2, %rcx
movq 0x38(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x4, %rcx
movq 0x38(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x8, %rcx
movq 0x38(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
shrq $0x10, %rcx
movq 0x38(%rbp), %rax
orq 0x10(%rax), %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq 0x10(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x10(%rax)
movq 0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x38(%rbp), %rax
imulq $0x1c, 0x10(%rax), %rsi
callq 0x2360
movq %rax, %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
jmp 0x995cb
movq 0x38(%rbp), %rax
movq (%rax), %rdi
movq 0x38(%rbp), %rax
imulq $0x1c, 0x8(%rax), %rax
addq %rax, %rdi
movq 0x40(%rbp), %rax
movq (%rax), %rsi
movq 0x40(%rbp), %rax
imulq $0x1c, 0x8(%rax), %rdx
callq 0x2180
movq 0x40(%rbp), %rax
movq 0x8(%rax), %rcx
movq 0x38(%rbp), %rax
addq 0x8(%rax), %rcx
movq %rcx, 0x8(%rax)
jmp 0x99609
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)