// File: chhylp123[P]hifiasm/Correct.cpp
// Function: ovlp_base_aln_all(overlap_region*, k_mer_hit*, long, long, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, ul_ov_t*, long, long, long, bit_extz_t*, overlap_region*, double)
int64_t ovlp_base_aln_all(overlap_region *z, k_mer_hit *ch_a, int64_t ch_n, 
int64_t soff, int64_t eoff, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
char* qstr, UC_Read *tu, ul_ov_t *ov, int64_t ql, int64_t tl, int64_t wl, bit_extz_t *exz, 
overlap_region *aux_o, double e_rate)
{
    int64_t ibeg, iend, i, l, mode, q[2], t[2], is_done;
    ibeg = soff; iend = eoff;
    for (l = ibeg, i = ibeg + 1; i <= iend; i++) {
        l = i - 1;
        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] 
        }
        assert(mode != 3);
        if(mode == 1 || mode == 2) adjust_ext_offset(&(q[0]), &(q[1]), &(t[0]), &(t[1]), ql, tl, 0, mode);
        ///at cns chain, the base alignment fails; there is no anchor between soff and eoff
        if((eoff-soff<=1) && (((q[1]-q[0])>>1) < MAX_CNS_E)) {
            is_done = 0;
        } else {
            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_SIN_L, MAX_SIN_E, FORCE_SIN_L, -1, aux_o);
        }

        if(!is_done) {
            push_unmap_alnw(aux_o, q[0], q[1]-1, t[0], t[1]-1, mode);
            // chain_win_aln(z, dp, cl, q[0], q[1], t[0], t[1], ql, tl, wl, exz);
        }
        // if(aux_o->y_id == 109111) {
        //     fprintf(stderr, "<[M::%s::] utg%.6dl(%c), q::[%ld, %ld), t::[%ld, %ld), mode::%ld, is_done::%ld\n", 
        //             __func__, (int32_t)z->y_id+1, "+-"[z->y_pos_strand], q[0], q[1], t[0], t[1], mode, is_done);
        // }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
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 -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x70(%rbp)
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 0x64209
movq -0x78(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
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 0x63f64
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 0x63f72
movq 0x30(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xa0(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x63fb0
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 0x63fbe
movq 0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x98(%rbp)
cmpq $-0x1, -0xb0(%rbp)
je 0x63fdf
cmpq $-0x1, -0xa8(%rbp)
je 0x63fdf
movq $0x0, -0x88(%rbp)
jmp 0x64030
cmpq $-0x1, -0xb0(%rbp)
je 0x64000
cmpq $-0x1, -0xa8(%rbp)
jne 0x64000
movq $0x1, -0x88(%rbp)
jmp 0x6402e
cmpq $-0x1, -0xb0(%rbp)
jne 0x64021
cmpq $-0x1, -0xa8(%rbp)
je 0x64021
movq $0x2, -0x88(%rbp)
jmp 0x6402c
movq $0x3, -0x88(%rbp)
jmp 0x6402e
jmp 0x64030
cmpq $0x3, -0x88(%rbp)
je 0x6403c
jmp 0x6405b
leaq 0x31fb21(%rip), %rdi    # 0x383b64
leaq 0x31e2d1(%rip), %rsi    # 0x38231b
movl $0x412b, %edx           # imm = 0x412B
leaq 0x31fb18(%rip), %rcx    # 0x383b6e
callq 0x2130
cmpq $0x1, -0x88(%rbp)
je 0x6406f
cmpq $0x2, -0x88(%rbp)
jne 0x640b7
leaq -0xa0(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
addq $0x8, %rsi
leaq -0xb0(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
addq $0x8, %rcx
movq 0x38(%rbp), %r8
movq 0x40(%rbp), %r9
movq -0x88(%rbp), %rax
xorl %r10d, %r10d
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x581e0
movq -0x50(%rbp), %rax
subq -0x48(%rbp), %rax
cmpq $0x1, %rax
jg 0x640ee
movq -0x98(%rbp), %rax
subq -0xa0(%rbp), %rax
sarq %rax
cmpq $0x3ff, %rax            # imm = 0x3FF
jge 0x640ee
movq $0x0, -0xb8(%rbp)
jmp 0x641b6
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 0x48(%rbp), %rbx
movq 0x50(%rbp), %r11
movq 0x38(%rbp), %r10
movsd -0x60(%rbp), %xmm0
movq 0x58(%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 $0x2710, 0x40(%rsp)     # imm = 0x2710
movq $0x7ff, 0x48(%rsp)      # imm = 0x7FF
movq $0x200, 0x50(%rsp)      # imm = 0x200
movq $-0x1, 0x58(%rsp)
movq %rax, 0x60(%rsp)
callq 0x613a0
movq %rax, -0xb8(%rbp)
cmpq $0x0, -0xb8(%rbp)
jne 0x641f6
movq 0x58(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl %eax, %esi
movq -0x98(%rbp), %rdx
subq $0x1, %rdx
movq -0xb0(%rbp), %rcx
movq -0xa8(%rbp), %r8
subq $0x1, %r8
movq -0x88(%rbp), %r9
callq 0x60ce0
jmp 0x641f8
movq -0x78(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
jmp 0x63ecd
xorl %eax, %eax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)