// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x68(%rbp), %rax
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 0x60(%rbp), %rax
movq %rax, -0x68(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x68(%rbp)
movb %al, -0xc1(%rbp)
jl 0x62fa7
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x58(%rbp), %rax
setae %al
movb %al, -0xc1(%rbp)
movb -0xc1(%rbp), %al
testb $0x1, %al
jne 0x62fb3
jmp 0x62fc3
jmp 0x62fb5
movq -0x68(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x62f79
cmpq $0x0, -0x68(%rbp)
jge 0x62fd2
movq $0x0, -0x68(%rbp)
movq $-0x1, -0x80(%rbp)
movq $-0x1, -0x78(%rbp)
movq -0x68(%rbp), %rcx
xorl %eax, %eax
cmpq -0x48(%rbp), %rcx
movb %al, -0xc2(%rbp)
jge 0x63013
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq 0x10(%rbp), %rax
setb %al
movb %al, -0xc2(%rbp)
movb -0xc2(%rbp), %al
testb $0x1, %al
jne 0x6301f
jmp 0x6307d
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq -0x58(%rbp), %rax
jb 0x6306a
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
cmpq 0x10(%rbp), %rax
jae 0x6306a
cmpq $0x0, -0x78(%rbp)
jge 0x6305e
movq -0x68(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x6306c
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x62fe2
movq -0x68(%rbp), %rax
movq %rax, 0x60(%rbp)
cmpq $0x0, -0x78(%rbp)
jl 0x635a0
cmpq $0x0, -0x80(%rbp)
jl 0x635a0
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
jle 0x635a0
movq -0x78(%rbp), %rax
movq %rax, -0x70(%rbp)
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x80(%rbp), %rax
jg 0x6359e
movq -0x68(%rbp), %rax
cmpq -0x80(%rbp), %rax
je 0x6311b
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x6311b
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x40(%rbp), %rcx
movq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jb 0x6358b
movq $-0x1, -0xa8(%rbp)
movq $-0x1, -0xb0(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0xa0(%rbp)
movq $-0x1, -0xc0(%rbp)
movq $-0x1, -0xb8(%rbp)
movq $-0x1, -0x88(%rbp)
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jge 0x6320d
movq -0x70(%rbp), %rax
cmpq -0x80(%rbp), %rax
jge 0x6320d
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x631ce
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x40(%rbp), %rcx
movq -0x70(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jb 0x6320d
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0xa0(%rbp)
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
jmp 0x63218
movq -0x58(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x80(%rbp), %rax
jge 0x632af
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
je 0x63270
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movq -0x40(%rbp), %rcx
movq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movl (%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jb 0x632af
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xc0(%rbp)
jmp 0x632ba
movq 0x10(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $-0x1, -0xb0(%rbp)
je 0x632db
cmpq $-0x1, -0xa8(%rbp)
je 0x632db
movq $0x0, -0x88(%rbp)
jmp 0x6332c
cmpq $-0x1, -0xb0(%rbp)
je 0x632fc
cmpq $-0x1, -0xa8(%rbp)
jne 0x632fc
movq $0x1, -0x88(%rbp)
jmp 0x6332a
cmpq $-0x1, -0xb0(%rbp)
jne 0x6331d
cmpq $-0x1, -0xa8(%rbp)
je 0x6331d
movq $0x2, -0x88(%rbp)
jmp 0x63328
movq $0x3, -0x88(%rbp)
jmp 0x6332a
jmp 0x6332c
cmpq $0x0, -0x88(%rbp)
jne 0x633a7
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x633a7
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jne 0x633a7
movq -0x40(%rbp), %rax
movq -0x70(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x633a7
movq -0x40(%rbp), %rax
movq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl (%rax), %eax
shrl $0x1f, %eax
cmpl $0x1, %eax
jne 0x633a7
jmp 0x63583
cmpq $0x1, -0x88(%rbp)
je 0x633bf
cmpq $0x2, -0x88(%rbp)
jne 0x6344b
movq -0x30(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
movq 0x50(%rbp), %rcx
movq 0x58(%rbp), %r8
movq 0x18(%rbp), %r9
leaq -0xa0(%rbp), %r14
leaq -0xa0(%rbp), %rbx
addq $0x8, %rbx
leaq -0xb0(%rbp), %r11
leaq -0xb0(%rbp), %r10
addq $0x8, %r10
leaq -0xb8(%rbp), %r12
leaq -0xc0(%rbp), %r15
leaq -0x88(%rbp), %rax
movq $0x2710, (%rsp)         # imm = 0x2710
movq $0x4, 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 %rax, 0x40(%rsp)
callq 0x5ccc0
jmp 0x63469
cmpq $0x0, -0xc0(%rbp)
jl 0x63467
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x63469
movq -0x30(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq 0x28(%rbp), %rdx
movq 0x30(%rbp), %rcx
movq 0x38(%rbp), %r8
movq 0x40(%rbp), %r9
movq -0xa0(%rbp), %r13
movq -0x98(%rbp), %r12
movq -0xb0(%rbp), %r15
movq -0xa8(%rbp), %r14
movq -0x88(%rbp), %rbx
movq 0x18(%rbp), %r11
movq 0x48(%rbp), %r10
movq 0x50(%rbp), %rax
movq %rax, -0xd0(%rbp)
movsd -0x60(%rbp), %xmm0
xorl %eax, %eax
movq -0xd0(%rbp), %rax
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movq %r10, 0x30(%rsp)
movq %rax, 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 $0x0, 0x60(%rsp)
callq 0x613a0
cmpq $0x0, %rax
jne 0x63581
cmpq $0x0, -0xb8(%rbp)
jge 0x63539
movq -0x78(%rbp), %rax
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xc0(%rbp)
jge 0x6354e
movq -0x80(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
cmpq -0xb8(%rbp), %rax
jle 0x63560
jmp 0x6357f
leaq 0x320439(%rip), %rdi    # 0x3839a0
leaq 0x31edad(%rip), %rsi    # 0x38231b
movl $0x407e, %edx           # imm = 0x407E
leaq 0x320432(%rip), %rcx    # 0x3839ac
callq 0x2130
jmp 0x63581
jmp 0x63583
movq -0x68(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x6358d
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0x630b5
jmp 0x635a2
jmp 0x635a2
movq 0x60(%rbp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)