// File: chhylp123[P]hifiasm/Correct.cpp
// Function: ovlp_base_aln(overlap_region*, k_mer_hit*, long, ul_ov_t*, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, overlap_region*, double, long, long, unsigned long)
void ovlp_base_aln(overlap_region *z, k_mer_hit *ch_a, int64_t ch_n, 
ul_ov_t *ov, int64_t wl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, UC_Read *tu, 
bit_extz_t *exz, overlap_region *aux_o, double e_rate, int64_t ql, int64_t tl, uint64_t rid)
{
    int64_t ibeg, iend, i, l, mode, q[2], t[2], is_done;
    if(ov->qn == ((uint32_t)-1)) ibeg = -1;
    else ibeg = ov->qn;
    iend = ov->tn;
    assert(iend>=ibeg+1);
    // fprintf(stderr, "\n***[M::%s::rid->%lu] utg%.6dl(%c), s::%u, e::%u, z::[%u, %u), ibeg::%ld, iend::%ld, ch_n::%ld\n", 
    // __func__, rid, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], ov->qs, ov->qe, z->x_pos_s, z->x_pos_e+1, ibeg, iend, ch_n);
    for (l = ibeg, i = ibeg + 1; i <= iend; i++) {
        if(i == iend || is_pri_aln(ch_a[i])) {
            q[0] = q[1] = t[0] = t[1] = mode = -1; is_done = 0;
            if(l >= 0) {
                q[0] = ch_a[l].self_offset; t[0] = ch_a[l].offset;
            } else {
                q[0] = ov->qs;
            }

            if(i < ch_n) {
                q[1] = ch_a[i].self_offset; t[1] = ch_a[i].offset;
            } else {
                q[1] = ov->qe;
            }

            if((t[0] != -1) && (t[1] != -1)) {
                mode = 0;//global
            } else if((t[0] != -1) && (t[1] == -1)) {
                mode = 1;///forward extension
            } else if((t[0] == -1) && (t[1] != -1)) {
                mode = 2;///backward extension
            } else {
                mode = 3;///no primary hit within [ibeg, iend] 
            }
            
            if((mode == 0) && is_alnw(ch_a[l]) && is_alnw(ch_a[i]) 
                                            && (ch_a[l].strand == 0) && (ch_a[i].strand == 1)) {
                is_done = 1;
                // if(aux_o->x_id == 29033 && aux_o->y_id == 21307){
                //     fprintf(stderr, "*[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld\n", 
                //             __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode);
                // }
                // push_replace_alnw(aux_o, q[0], q[1]-1, t[0], t[1]-1, mode);///no need this
                push_replace_alnw_adv(z, wl, aux_o, q[0], q[1]-1, t[0], t[1]-1, mode);
            } else if(mode != 3) {
                if(mode == 1 || mode == 2) adjust_ext_offset(&(q[0]), &(q[1]), &(t[0]), &(t[1]), ql, tl, 0, mode);
                // if(aux_o->x_id == 29033 && aux_o->y_id == 21307){
                //     fprintf(stderr, "#[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld\n", 
                //             __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode);
                // }
                is_done = hc_aln_exz_adv(z, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, ql, e_rate, MAX_CNS_L, MAX_CNS_E, FORCE_CNS_L, -1, aux_o);
            }
            
            if(!is_done) {///postprocess
                // if(aux_o->x_id == 29033 && aux_o->y_id == 21307){
                //     fprintf(stderr, ">[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld\n", 
                //             __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode);
                // }
                is_done = ovlp_base_aln_all(z, ch_a, ch_n, l, i, uref, hpc_g, rref, qstr, tu, ov, ql, tl, wl, exz, aux_o, e_rate);
            }
            // if(aux_o->x_id == 29033 && aux_o->y_id == 21307){
            //     fprintf(stderr, "-is_done::%ld[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld, l::%ld, i::%ld, ch_n::%ld\n", 
            //             is_done, __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode, l, i, ch_n);
            // }
            // if(rid == (uint64_t)-1) {
            //     fprintf(stderr, "+is_done::%ld[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld, l::%ld, i::%ld, ch_n::%ld\n", 
            //             is_done, __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode, l, i, ch_n);
            // }
            l = i;
        }
    }

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
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 -0x48(%rbp), %rax
cmpl $-0x1, (%rax)
jne 0x64288
movq $-0x1, -0x68(%rbp)
jmp 0x64292
movq -0x48(%rbp), %rax
movl (%rax), %eax
movq %rax, -0x68(%rbp)
movq -0x48(%rbp), %rax
movl 0xc(%rax), %eax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq -0x68(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jl 0x642b0
jmp 0x642cf
leaq 0x31f994(%rip), %rdi    # 0x383c4b
leaq 0x31e05d(%rip), %rsi    # 0x38231b
movl $0x4148, %edx           # imm = 0x4148
leaq 0x31f98e(%rip), %rcx    # 0x383c58
callq 0x2130
movq -0x68(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
jg 0x64774
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
je 0x64349
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x64349
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x38(%rbp), %rcx
movq -0x78(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jb 0x64761
movq $-0x1, -0x88(%rbp)
movq $-0x1, -0xa8(%rbp)
movq $-0x1, -0xb0(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0xa0(%rbp)
movq $0x0, -0xb8(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x643c6
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0xa0(%rbp)
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0xb0(%rbp)
jmp 0x643d4
movq -0x48(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x64412
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0xa8(%rbp)
jmp 0x64420
movq -0x48(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
cmpq $-0x1, -0xb0(%rbp)
je 0x64441
cmpq $-0x1, -0xa8(%rbp)
je 0x64441
movq $0x0, -0x88(%rbp)
jmp 0x64492
cmpq $-0x1, -0xb0(%rbp)
je 0x64462
cmpq $-0x1, -0xa8(%rbp)
jne 0x64462
movq $0x1, -0x88(%rbp)
jmp 0x64490
cmpq $-0x1, -0xb0(%rbp)
jne 0x64483
cmpq $-0x1, -0xa8(%rbp)
je 0x64483
movq $0x2, -0x88(%rbp)
jmp 0x6448e
movq $0x3, -0x88(%rbp)
jmp 0x64490
jmp 0x64492
cmpq $0x0, -0x88(%rbp)
jne 0x6456b
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x6456b
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x6456b
movq -0x38(%rbp), %rax
movq -0x80(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x6456b
movq -0x38(%rbp), %rax
movq -0x78(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x6456b
movq $0x1, -0xb8(%rbp)
movq -0x30(%rbp), %rdi
movq -0x50(%rbp), %rsi
movq 0x38(%rbp), %rdx
movq -0xa0(%rbp), %rax
movl %eax, %ecx
movq -0x98(%rbp), %r8
subq $0x1, %r8
movq -0xb0(%rbp), %r9
movq -0xa8(%rbp), %r10
subq $0x1, %r10
movq -0x88(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x60f40
jmp 0x6469f
cmpq $0x3, -0x88(%rbp)
je 0x6469d
cmpq $0x1, -0x88(%rbp)
je 0x6458d
cmpq $0x2, -0x88(%rbp)
jne 0x645d5
leaq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
addq $0x8, %rsi
leaq -0xb0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
addq $0x8, %rcx
movq 0x40(%rbp), %r8
movq 0x48(%rbp), %r9
movq -0x88(%rbp), %rax
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
movq -0x30(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq 0x10(%rbp), %rdx
movq 0x18(%rbp), %rcx
movq 0x20(%rbp), %r8
movq 0x28(%rbp), %r9
movq -0xa0(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x98(%rbp), %r13
movq -0xb0(%rbp), %r12
movq -0xa8(%rbp), %r15
movq -0x88(%rbp), %r14
movq -0x50(%rbp), %rbx
movq 0x30(%rbp), %r11
movq 0x40(%rbp), %r10
movsd -0x60(%rbp), %xmm0
movq 0x38(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, (%rsp)
movq -0xc0(%rbp), %rax
movq %r13, 0x8(%rsp)
movq %r12, 0x10(%rsp)
movq %r15, 0x18(%rsp)
movq %r14, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %r11, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq $0xc00, 0x40(%rsp)      # imm = 0xC00
movq $0x3ff, 0x48(%rsp)      # imm = 0x3FF
movq $0x100, 0x50(%rsp)      # imm = 0x100
movq $-0x1, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x613a0
movq %rax, -0xb8(%rbp)
jmp 0x6469f
cmpq $0x0, -0xb8(%rbp)
jne 0x64759
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x80(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq 0x20(%rbp), %r13
movq 0x28(%rbp), %r12
movq -0x48(%rbp), %r15
movq 0x40(%rbp), %r14
movq 0x48(%rbp), %rbx
movq -0x50(%rbp), %r11
movq 0x30(%rbp), %r10
movq 0x38(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0xe0(%rbp), %rax
movsd -0x60(%rbp), %xmm0
movq %rax, (%rsp)
movq -0xd8(%rbp), %rax
movq %rax, 0x8(%rsp)
movq -0xd0(%rbp), %rax
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 0x63e50
movq %rax, -0xb8(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x80(%rbp)
jmp 0x64763
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x642e3
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopw %cs:(%rax,%rax)