// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: sub_base_aln(overlap_region*, Chain_Data*, k_mer_hit*, long, unsigned long, unsigned long, unsigned long, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, bit_extz_t*, double, long, long, long, unsigned long)
int64_t sub_base_aln(overlap_region *z, Chain_Data *dp, k_mer_hit *ch_a, int64_t ch_n, uint64_t pre_e, 
uint64_t s, uint64_t e, int64_t wl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
char* qstr, UC_Read *tu, bit_extz_t *exz, double e_rate, int64_t ql, int64_t tl, 
int64_t ch_i, uint64_t rid)
{
    int64_t i = ch_i, l, ibeg, iend, mode, q[2], t[2], ch_s, ch_e;
    for (; i >= 0 && ch_a[i].self_offset >= s; i--);
    if(i<0) i = 0; ibeg=iend=-1;
    for (; i < ch_n && ch_a[i].self_offset < e; i++) {
        if((ch_a[i].self_offset >= s) && (ch_a[i].self_offset < e)) {
            if(ibeg < 0) ibeg = i;
            iend = i+1;
        }
    }
    // debug_iter_k_mer_hit(ch_a, ch_n, s, e, ibeg, iend);
    // fprintf(stderr, "***[M::%s::rid->%lu] utg%.6dl(%c), s::%lu, e::%lu, z::[%u, %u), ibeg::%ld, iend::%ld\n", 
    // __func__, rid, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], s, e, z->x_pos_s, z->x_pos_e+1, ibeg, iend);
    ch_i = i;
    if(ibeg >= 0 && iend >= 0 && iend > ibeg) {///find some anchors[ibeg, iend)
        for (i = l = ibeg; i <= iend; i++) {
            // fprintf(stderr, "\n[M::%s::i->%ld] q::%u, t::%u, cnt::%u, readID::%u\n", __func__, 
            //                                 i, ch_a[i].self_offset, ch_a[i].offset, ch_a[i].cnt, ch_a[i].readID);
            if(i == iend || is_pri_aln(ch_a[i])) {
                q[0] = q[1] = t[0] = t[1] = -1; mode = ch_s = ch_e = -1; 
                if(l < i && l < iend && is_pri_aln(ch_a[l])) {
                    q[0] = ch_a[l].self_offset; t[0] = ch_a[l].offset; ch_s = l;
                } else {
                    q[0] = s;
                }

                if(i < iend && is_pri_aln(ch_a[i])) {
                    q[1] = ch_a[i].self_offset; t[1] = ch_a[i].offset; ch_e = i;
                } else {
                    q[1] = e;
                }

                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;//semi-global
                }

                if((mode == 0) && is_alnw(ch_a[l]) && is_alnw(ch_a[i]) 
                                            && (ch_a[l].strand == 0) && (ch_a[i].strand == 1)) {
                        ;   
                } else {
                    // 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);

                    if(mode == 1 || mode == 2) {
                        tuning_ext_offset(z, ch_a, ch_n, ql, tl, wl, MAX_SIN_L, 4, &ch_s, &ch_e, &q[0], &q[1], &t[0], &t[1], &mode);
                    } else if(ch_e >= 0) {//global
                        ch_e++;
                    }
                    if(!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, NULL)) {
                        if(ch_s < 0) ch_s = ibeg; if(ch_e < 0) ch_e = iend;
                        assert(ch_e > ch_s);
                        // chain_aln(z, dp, ch_a+ch_s, ch_e-ch_s, uref, hpc_g, rref, qstr, tu, q[0], q[1], t[0], t[1], mode, wl, exz, ql, e_rate, MAX_CNS_L, rid);
                    }
                }
                l = i;
            }
        }
    } else {//totoally no anchor; probably semi-global
        // fprintf(stderr, "\n***[M::%s::rid->%lu] utg%.6dl(%c), s::%lu, e::%lu, z::[%u, %u), ibeg::%ld, iend::%ld\n", 
        // __func__, rid, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], s, e, z->x_pos_s, z->x_pos_e+1, ibeg, iend);
    }

    return ch_i;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %xmm0, 0x60(%rsp)
movq %rdx, %rsi
movq %rdi, 0x40(%rsp)
movq 0xf0(%rsp), %rax
testq %rax, %rax
js 0x67594
movq %rax, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rsi), %rdi
addq $0x8, %rdi
movl (%rdi), %edx
cmpq %r9, %rdx
jb 0x67594
leaq -0x1(%rax), %rdx
addq $-0x10, %rdi
testq %rax, %rax
movq %rdx, %rax
jg 0x67576
movq $-0x1, %rax
xorl %r13d, %r13d
testq %rax, %rax
cmovgq %rax, %r13
movq $-0x1, %rbx
cmpq %rcx, %r13
movq %r9, 0x50(%rsp)
movq %rcx, 0x48(%rsp)
jge 0x675fb
movq %r13, %rax
shlq $0x4, %rax
addq %rsi, %rax
addq $0x8, %rax
movq $-0x1, %r14
movl (%rax), %edi
cmpq 0xa0(%rsp), %rdi
jae 0x67602
testq %rbx, %rbx
movq %rbx, %rdx
cmovsq %r13, %rdx
incq %r13
cmpq %r9, %rdi
cmovaeq %rdx, %rbx
cmovaeq %r13, %r14
addq $0x10, %rax
cmpq %r13, %rcx
jne 0x675c9
movq %rcx, %r13
jmp 0x67602
movq $-0x1, %r14
testq %rbx, %rbx
js 0x6791e
testq %r14, %r14
js 0x6791e
cmpq %rbx, %r14
jle 0x6791e
movq %rbx, %rbp
shlq $0x4, %rbp
addq %rsi, %rbp
movl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movq %rbx, %rcx
movq %rbx, %r12
movq %rsi, 0x58(%rsp)
cmpq %r12, %r14
je 0x67651
movl (%rbp), %eax
andl %r15d, %eax
cmpl %r15d, %eax
je 0x67651
cmpl %eax, 0xc(%rbp)
jb 0x67909
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x20(%rsp)
movdqa %xmm0, 0x30(%rsp)
movq $-0x1, %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x18(%rsp)
movq %rax, 0x8(%rsp)
cmpq %r12, %rcx
jge 0x6769c
cmpq %r14, %rcx
jge 0x6769c
movq %rcx, %rdx
shlq $0x4, %rdx
leaq (%rsi,%rdx), %rax
movl (%rsi,%rdx), %edx
andl %r15d, %edx
cmpl %r15d, %edx
je 0x676a8
cmpl %edx, 0xc(%rax)
jae 0x676a8
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
jmp 0x676bd
movl 0x8(%rax), %edx
movq %rdx, 0x30(%rsp)
movl 0x4(%rax), %eax
movq %rax, 0x20(%rsp)
movq %rcx, 0x18(%rsp)
cmpq %r14, %r12
jge 0x676d2
movl (%rbp), %eax
andl %r15d, %eax
cmpl %r15d, %eax
je 0x676e1
cmpl %eax, 0xc(%rbp)
jae 0x676e1
movq 0xa0(%rsp), %rax
movq %rax, 0x38(%rsp)
jmp 0x676f6
movl 0x8(%rbp), %eax
movq %rax, 0x38(%rsp)
movl 0x4(%rbp), %eax
movq %rax, 0x28(%rsp)
movq %r12, 0x10(%rsp)
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rdx
cmpq $-0x1, %rax
setne %r8b
cmpq $-0x1, %rdx
setne %dil
andb %r8b, %dil
cmpb $0x1, %dil
jne 0x67724
movq $0x0, 0x8(%rsp)
jmp 0x67770
cmpq $-0x1, %rax
setne %r8b
cmpq $-0x1, %rdx
sete %dil
andb %r8b, %dil
cmpb $0x1, %dil
jne 0x67748
movq $0x1, 0x8(%rsp)
jmp 0x67770
cmpq $-0x1, %rdx
setne %dl
cmpq $-0x1, %rax
sete %al
andb %dl, %al
cmpb $0x1, %al
jne 0x67767
movq $0x2, 0x8(%rsp)
jmp 0x67770
movq $0x3, 0x8(%rsp)
movq 0x8(%rsp), %rax
testq %rax, %rax
jne 0x6779e
shlq $0x4, %rcx
movl (%rsi,%rcx), %ecx
movl %ecx, %edx
notl %edx
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jne 0x6779e
testl %ecx, %ecx
js 0x6779e
cmpl $-0x1, (%rbp)
movq %r12, %rcx
je 0x67909
decq %rax
cmpq $0x1, %rax
ja 0x67809
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0xf0(%rsp), %r8
movq 0xb0(%rsp), %r9
leaq 0x10(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x40(%rsp), %rax
pushq %rax
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x4
pushq $0x2710                 # imm = 0x2710
callq 0x635a1
addq $0x50, %rsp
jmp 0x6781b
movq 0x10(%rsp), %rax
testq %rax, %rax
js 0x6781b
incq %rax
movq %rax, 0x10(%rsp)
movaps 0x30(%rsp), %xmm0
movaps 0x20(%rsp), %xmm1
movq 0x8(%rsp), %rax
subq $0x70, %rsp
movq 0x150(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x148(%rsp), %rcx
movq %rcx, 0x30(%rsp)
movq 0x118(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq $0x0, 0x60(%rsp)
movq $-0x1, 0x58(%rsp)
movq $0x100, 0x50(%rsp)      # imm = 0x100
movq $0x3ff, 0x48(%rsp)      # imm = 0x3FF
movq $0xc00, 0x40(%rsp)      # imm = 0xC00
movq 0xb0(%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq 0x130(%rsp), %rcx
movq 0x138(%rsp), %r8
movq 0x140(%rsp), %r9
movq 0xd0(%rsp), %xmm0
callq 0x661e6
addq $0x70, %rsp
movq %r12, %rcx
testq %rax, %rax
movq 0x58(%rsp), %rsi
jne 0x67909
movq 0x18(%rsp), %rax
testq %rax, %rax
cmovsq %rbx, %rax
movq 0x10(%rsp), %rdx
testq %rdx, %rdx
cmovsq %r14, %rdx
movq %rax, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %r12, %rcx
cmpq %rax, %rdx
jle 0x67930
leaq 0x1(%r12), %rax
addq $0x10, %rbp
cmpq %r14, %r12
movq %rax, %r12
jl 0x67638
movq %r13, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1bf391(%rip), %rdi    # 0x226cc8
leaq 0x1bdd05(%rip), %rsi    # 0x225643
leaq 0x1bf38f(%rip), %rcx    # 0x226cd4
movl $0x407e, %edx           # imm = 0x407E
callq 0x2110