// File: /workspace/github_cpp_makefile_star2_O2/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 0x110(%rsp), %rax
cmpq $-0x1, %rax
pushq $-0x1
popq %r10
movq %r10, %r8
cmovlq %rax, %r8
movq %rax, %rsi
shlq $0x4, %rsi
movq %rdx, (%rsp)
addq %rsi, %rdx
addq $0x8, %rdx
movq 0xc8(%rsp), %rbp
testq %rax, %rax
js 0x360c2
movl (%rdx), %esi
cmpq %r9, %rsi
jb 0x360bf
decq %rax
addq $-0x10, %rdx
jmp 0x360aa
movq %rax, %r8
xorl %eax, %eax
testq %r8, %r8
cmovgq %r8, %rax
cmpq %rcx, %rax
movq %rcx, %r11
cmovgq %rax, %r11
movq %rax, %rdx
shlq $0x4, %rdx
movq (%rsp), %rsi
leaq (%rdx,%rsi), %r8
addq $0x8, %r8
movq %r10, %r15
movq %r9, 0x70(%rsp)
movq %rcx, 0x68(%rsp)
cmpq %rcx, %rax
jge 0x36125
movl (%r8), %edx
cmpq 0xc0(%rsp), %rdx
jae 0x3612c
testq %r10, %r10
movq %r10, %rsi
cmovsq %rax, %rsi
incq %rax
cmpq %r9, %rdx
cmovaeq %rsi, %r10
cmovaeq %rax, %r15
addq $0x10, %r8
jmp 0x360f5
movq %r11, 0x58(%rsp)
jmp 0x36131
movq %rax, 0x58(%rsp)
movq %r10, 0x38(%rsp)
testq %r10, %r10
js 0x36404
testq %r15, %r15
js 0x36404
cmpq 0x38(%rsp), %r15
jle 0x36404
movq 0x38(%rsp), %rcx
movq %rcx, %r13
shlq $0x4, %r13
addq (%rsp), %r13
movq %rcx, %r12
movq %rdi, 0x60(%rsp)
movq %r15, 0x78(%rsp)
cmpq %r15, %r12
jg 0x36404
je 0x36194
movl (%r13), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cmpl %edx, %eax
je 0x36194
cmpl %eax, 0xc(%r13)
jb 0x363f8
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x40(%rsp)
movdqa %xmm0, 0x20(%rsp)
orq $-0x1, 0x8(%rsp)
orq $-0x1, 0x10(%rsp)
cmpq %r12, %rcx
jge 0x361d7
movq %rcx, %rax
shlq $0x4, %rax
movq (%rsp), %rsi
leaq (%rsi,%rax), %rdx
movl (%rsi,%rax), %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %eax
cmpl %esi, %eax
je 0x361e7
cmpl %eax, 0xc(%rdx)
jae 0x361e7
movq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
pushq $-0x1
popq %r10
jmp 0x361fd
movl 0x8(%rdx), %eax
movq %rax, 0x20(%rsp)
movl 0x4(%rdx), %r10d
movq %r10, 0x40(%rsp)
movq %rcx, 0x10(%rsp)
cmpq %r15, %r12
jge 0x36217
movl (%r13), %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %edx
cmpl %esi, %edx
je 0x36250
cmpl %edx, 0xc(%r13)
jae 0x36250
movq 0xc0(%rsp), %rcx
movq %rcx, 0x28(%rsp)
pushq $0x1
popq %rdx
cmpq $-0x1, %r10
jne 0x362b4
movq $0x3, 0x18(%rsp)
pushq $-0x1
popq %rdx
movq %rdx, %r14
movq 0xc0(%rsp), %r11
pushq $0x3
popq %rbx
jmp 0x3633f
movl 0x8(%r13), %r11d
movq %r11, 0x28(%rsp)
movl 0x4(%r13), %r14d
movq %r14, 0x48(%rsp)
movq %r12, 0x8(%rsp)
pushq $0x2
popq %rdx
cmpq $-0x1, %r10
je 0x362b4
andq $0x0, 0x18(%rsp)
shlq $0x4, %rcx
movq (%rsp), %rdx
movl (%rdx,%rcx), %ecx
movl %ecx, %edx
notl %edx
testl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jne 0x3633a
testl %ecx, %ecx
js 0x3633a
cmpl $-0x1, (%r13)
movq %r12, %rdx
movl $0x0, %ebx
movq %r12, %rcx
je 0x363f8
jmp 0x3633f
movq %rdx, 0x18(%rsp)
subq $0x8, %rsp
movq 0x8(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x108(%rsp), %r15
movq %r15, %rcx
movq 0x110(%rsp), %r8
movq %rbp, %r9
leaq 0x20(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x58(%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 0x32a78
movq 0xb0(%rsp), %rdi
addq $0x50, %rsp
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %r11
movq 0x40(%rsp), %r10
movq 0x48(%rsp), %r14
movq 0x18(%rsp), %rbx
jmp 0x36354
movq %r12, %rdx
xorl %ebx, %ebx
testq %rdx, %rdx
js 0x3634c
incq %rdx
movq %rdx, 0x8(%rsp)
movq 0x100(%rsp), %r15
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 %r15
pushq 0x130(%rsp)
pushq %rbp
pushq %rbx
pushq %r14
pushq %r10
pushq %r11
pushq %rax
callq 0x34db7
movq 0xd0(%rsp), %rdi
addq $0x70, %rsp
movq %r12, %rcx
testq %rax, %rax
movq 0x78(%rsp), %r15
jne 0x363f8
movq 0x10(%rsp), %rax
testq %rax, %rax
cmovsq 0x38(%rsp), %rax
movq 0x8(%rsp), %rdx
testq %rdx, %rdx
cmovsq %r15, %rdx
movq %rax, 0x10(%rsp)
movq %rdx, 0x8(%rsp)
movq %r12, %rcx
cmpq %rax, %rdx
jle 0x3641b
incq %r12
addq $0x10, %r13
jmp 0x36170
movq 0x58(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1ae906(%rip), %rdi    # 0x1e4d28
leaq 0x1ad27a(%rip), %rsi    # 0x1e36a3
leaq 0x1ae904(%rip), %rcx    # 0x1e4d34
movl $0x407e, %edx           # imm = 0x407E
callq 0x2110