// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %xmm0, 0x80(%rsp)
movq %rdx, %rsi
movq 0x110(%rsp), %r10
testq %r10, %r10
js 0x672ae
movq %r10, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rsi), %r8
addq $0x8, %r8
movl (%r8), %edx
cmpq %r9, %rdx
jb 0x672ae
leaq -0x1(%r10), %rdx
addq $-0x10, %r8
testq %r10, %r10
movq %rdx, %r10
jg 0x6728f
movq $-0x1, %r10
xorl %eax, %eax
testq %r10, %r10
cmovgq %r10, %rax
cmpq %rcx, %rax
jge 0x67622
movq 0xc8(%rsp), %r15
movq %rax, %rdx
shlq $0x4, %rdx
leaq (%rdx,%rsi), %r11
addq $0x8, %r11
movq $-0x1, %rbp
movq $-0x1, %r14
movl (%r11), %r8d
cmpq 0xc0(%rsp), %r8
jae 0x67316
testq %r14, %r14
movq %r14, %rdx
cmovsq %rax, %rdx
incq %rax
cmpq %r9, %r8
cmovaeq %rdx, %r14
cmovaeq %rax, %rbp
addq $0x10, %r11
cmpq %rax, %rcx
jne 0x672e5
movq %rcx, %rax
testq %r14, %r14
js 0x67622
testq %rbp, %rbp
js 0x67622
cmpq %r14, %rbp
jle 0x67622
movq %rax, 0x58(%rsp)
movq %rcx, 0x68(%rsp)
movq %r9, 0x70(%rsp)
movq %r14, %r12
shlq $0x4, %r12
addq %rsi, %r12
movq %r14, %rcx
movq %r14, 0x60(%rsp)
movq %rsi, 0x78(%rsp)
movq %rdi, 0x48(%rsp)
cmpq %r14, %rbp
je 0x6737b
movl (%r12), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cmpl %edx, %eax
je 0x6737b
cmpl %eax, 0xc(%r12)
jb 0x67609
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x30(%rsp)
movdqa %xmm0, 0x20(%rsp)
movq $-0x1, %rax
movq %rax, 0x8(%rsp)
movq %rax, 0x10(%rsp)
cmpq %r14, %rcx
jge 0x673c2
movq %rcx, %rdx
shlq $0x4, %rdx
leaq (%rsi,%rdx), %rax
movl (%rsi,%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
cmpl %r8d, %edx
je 0x673d8
cmpl %edx, 0xc(%rax)
jae 0x673d8
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq $-0x1, %r10
movq %rax, %r8
jmp 0x673ef
movl 0x8(%rax), %r8d
movq %r8, 0x20(%rsp)
movl 0x4(%rax), %r10d
movq %r10, 0x30(%rsp)
movq %rcx, 0x10(%rsp)
cmpq %rbp, %r14
jge 0x6740a
movl (%r12), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cmpl %edx, %eax
je 0x6744d
cmpl %eax, 0xc(%r12)
jae 0x6744d
movq 0xc0(%rsp), %rax
movq %rax, 0x28(%rsp)
movl $0x1, %eax
cmpq $-0x1, %r10
jne 0x674db
movq $0x3, 0x18(%rsp)
movl $0x3, %r13d
movq $-0x1, %r11
movq $-0x1, %rdx
movq 0xc0(%rsp), %r9
jmp 0x674ab
movl 0x8(%r12), %r9d
movq %r9, 0x28(%rsp)
movl 0x4(%r12), %r11d
movq %r11, 0x38(%rsp)
movq %r14, 0x8(%rsp)
cmpq $-0x1, %r10
je 0x674d6
movq $0x0, 0x18(%rsp)
shlq $0x4, %rcx
movl (%rsi,%rcx), %eax
movl %eax, %ecx
notl %ecx
testl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jne 0x674a5
testl %eax, %eax
js 0x674a5
cmpl $-0x1, (%r12)
movq %r14, %rdx
movl $0x0, %r13d
movq %r14, %rcx
je 0x67609
jmp 0x674ab
movq %r14, %rdx
xorl %r13d, %r13d
movq %r10, %rcx
movq %r11, %r10
testq %rdx, %rdx
movq %r8, 0x50(%rsp)
js 0x674c3
incq %rdx
movq %rdx, 0x8(%rsp)
movq 0x100(%rsp), %rbx
movq %r9, %rax
movq %rcx, %r11
jmp 0x6755f
movl $0x2, %eax
movq %rax, 0x18(%rsp)
subq $0x8, %rsp
movq 0x70(%rsp), %rdx
movq 0x108(%rsp), %rbx
movq %rbx, %rcx
movq 0x110(%rsp), %r8
movq %r15, %r9
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
leaq 0x48(%rsp), %rax
pushq %rax
pushq $0x4
pushq $0x2710                 # imm = 0x2710
callq 0x631bc
movq 0x98(%rsp), %rdi
addq $0x50, %rsp
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %r11
movq 0x38(%rsp), %r10
movq 0x18(%rsp), %r13
subq $0x8, %rsp
movq 0xd8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0xf0(%rsp), %r8
movq 0xf8(%rsp), %r9
movq 0x88(%rsp), %xmm0
pushq $0x0
pushq $-0x1
pushq $0x100                  # imm = 0x100
pushq $0x3ff                  # imm = 0x3FF
pushq $0xc00                  # imm = 0xC00
pushq %rbx
pushq 0x130(%rsp)
pushq %r15
pushq %r13
pushq %r10
pushq %r11
pushq %rax
pushq 0xb8(%rsp)
callq 0x65f06
movq 0xb8(%rsp), %rdi
addq $0x70, %rsp
movq %r14, %rcx
testq %rax, %rax
movq 0x78(%rsp), %rsi
jne 0x67609
movq 0x10(%rsp), %rax
testq %rax, %rax
cmovsq 0x60(%rsp), %rax
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
cmovsq %rbp, %rdx
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %r14, %rcx
cmpq %rax, %rdx
jle 0x67634
leaq 0x1(%r14), %rax
addq $0x10, %r12
cmpq %rbp, %r14
movq %rax, %r14
jl 0x6735c
movq 0x58(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1cb79d(%rip), %rdi    # 0x232dd8
leaq 0x1ca111(%rip), %rsi    # 0x231753
leaq 0x1cb79b(%rip), %rcx    # 0x232de4
movl $0x407e, %edx           # imm = 0x407E
callq 0x2110