// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz(overlap_region*, Candidates_list*, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, long, long, long, long, long, long, long, bit_extz_t*, long, double, long, long)
void hc_aln_exz(overlap_region *z, Candidates_list *cl, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, 
char* qstr, UC_Read *tu, int64_t qs, int64_t qe, int64_t ts, int64_t te, int64_t estimate_err,
int64_t mode, int64_t wl, bit_extz_t *exz, int64_t q_tot, double e_rate, int64_t maxl, int64_t maxe)
{
    clear_align(*exz);
    int64_t thre, ql = qe - qs, thre0, t_tot, id = z->y_id; 
    if(((ts == -1) && (te == -1))) mode = 3;///set to semi-global
    
    if(mode == 1 || mode == 2) {
        if(hpc_g) t_tot = hpc_len(*hpc_g, id);
        else if(uref) t_tot = uref->ug->u.a[id].len;
        else t_tot = Get_READ_LENGTH((*rref), id);
        // fprintf(stderr, "\n+[M::%s::ql->%ld::tl->%ld::mode->%ld] q::[%ld, %ld), t::[%ld, %ld), zx[%d, %d), zy[%d, %d)\n", 
        // __func__, q_tot, t_tot, mode, qs, qe, ts, te, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
        ///find a aligned window >= qe to convert extension to global; or do extension directly
        adjust_specific_ext_offset(z, uref, hpc_g, rref, exz, qstr, tu, &qs, &qe, &ts, &te, q_tot, t_tot, wl, &mode);
        // fprintf(stderr, "-[M::%s::ql->%ld::tl->%ld::mode->%ld] q::[%ld, %ld), t::[%ld, %ld), zx[%d, %d), zy[%d, %d)\n", 
        // __func__, q_tot, t_tot, mode, qs, qe, ts, te, z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
    }

    // fprintf(stderr, "[M::%s::ql::%ld] qs::%ld, qe::%ld, ts::%ld, te::%ld, mode::%ld, estimate_err::%ld, e_rate::%f", 
    //                                                     __func__, ql, qs, qe, ts, te, mode, estimate_err, e_rate);

    if(ql <= maxl && (estimate_err*1.2) <= maxe) {
        thre = scale_ed_thre(estimate_err, maxe); if(thre > ql) thre = ql;
        if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
            // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(+)\n", exz->err, exz->thre, thre);
            return;
        }

        thre0 = thre; thre = ql*e_rate;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                return;
            }
        }

        thre0 = thre; thre <<= 1;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                return;
            }
        }

        thre0 = thre; thre = ql*0.51;
        thre = scale_ed_thre(thre, maxe); if(thre > ql) thre = ql;
        if(thre > thre0) {
            if(cal_exz_infi(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, thre, q_tot, mode)) {
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(*)\n", exz->err, exz->thre, thre);
                return;
            }
        }
    }
    // fprintf(stderr, ", err::%d, thre::%d\n", INT32_MAX, exz->thre);
    // anchor_aln(z, cl, uref, hpc_g, rref, qstr, tu, qs, qe, ts, te, thre, mode, wl, exz, q_tot, A_L);
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movq %rdi, %rbp
movq 0xd0(%rsp), %rcx
movq 0xb0(%rsp), %rax
movq 0xa0(%rsp), %r12
subq 0x98(%rsp), %r12
andq 0xa8(%rsp), %rax
movl $0x7fffffff, 0x2c(%rcx) # imm = 0x7FFFFFFF
cmpq $-0x1, %rax
jne 0x6156a
movq $0x3, 0xc0(%rsp)
movq 0xe0(%rsp), %rbx
movq 0xd8(%rsp), %r10
movq 0xc0(%rsp), %rax
decq %rax
cmpq $0x1, %rax
ja 0x61651
movl 0x10(%rbp), %ecx
movq 0xc8(%rsp), %rax
testq %r15, %r15
je 0x615ba
movq %rdx, %r14
movq (%r15), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rcx
movl (%rdx,%rcx,8), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x615e8
testq %rdx, %rdx
je 0x615d8
movq %rdx, %r14
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
leaq (%rcx,%rcx,4), %rcx
movl (%rdx,%rcx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0x615e8
movq %rdx, %r14
movq 0x8(%rsp), %rdx
movq 0x18(%rdx), %rdx
movq (%rdx,%rcx,8), %rcx
subq $0x50, %rsp
leaq 0x110(%rsp), %rdx
movq %rdx, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq %r10, 0x28(%rsp)
leaq 0x100(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0xf8(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xf0(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xe8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rbp, %rdi
movq %r10, %r13
callq 0x60c17
movq %r13, %r10
addq $0x50, %rsp
movq %r14, %rdx
cmpq %rbx, %r12
jg 0x6174a
movq 0xe8(%rsp), %rax
movq 0xb8(%rsp), %r14
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
mulsd 0x1c3ec6(%rip), %xmm0   # 0x225540
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x6174a
andl $0x7fffffe0, %r14d      # imm = 0x7FFFFFE0
orl $0x1f, %r14d
cmpl %eax, %r14d
cmovael %eax, %r14d
cmpq %r14, %r12
movq %r14, %rax
cmovlq %r12, %rax
movq 0x98(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0xa0(%rsp), %rbx
movq 0xa8(%rsp), %r13
movq %rbp, %rdi
movq %r15, %rbp
movq 0xb0(%rsp), %r15
movq 0xc0(%rsp), %r11
movq %rdx, %rsi
movq %rdx, 0x40(%rsp)
movq %rbp, %rdx
movq 0x8(%rsp), %rcx
movq 0xd0(%rsp), %r8
movq 0x18(%rsp), %r9
movq %r11, 0x20(%rsp)
pushq %r11
pushq %r10
pushq %rax
movq %r15, 0x40(%rsp)
pushq %r15
movq %rbp, %r15
movq %rdi, %rbp
movq %r13, 0x50(%rsp)
pushq %r13
movq %rbx, 0x60(%rsp)
pushq %rbx
pushq 0x40(%rsp)
pushq 0xc8(%rsp)
callq 0x610b5
movq 0x128(%rsp), %rcx
movq 0x118(%rsp), %r10
movq 0x80(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
je 0x61759
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm1, %xmm1
cvtsi2sd %r12, %xmm1
movsd 0x50(%rsp), %xmm0
movsd %xmm1, 0x48(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
andl $0x7fffffe0, %ebx       # imm = 0x7FFFFFE0
orl $0x1f, %ebx
cmpl %ecx, %ebx
cmovael %ecx, %ebx
cmpq %rbx, %r12
movq %rbx, %r13
cmovlq %r12, %r13
cmpq %r14, %r13
jle 0x617f4
movq %rbp, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0xd0(%rsp), %r8
movq 0x18(%rsp), %r9
pushq 0x20(%rsp)
pushq %r10
pushq %r13
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x40(%rsp)
pushq 0xc8(%rsp)
callq 0x610b5
movq 0x128(%rsp), %rcx
movq 0x118(%rsp), %r10
movq 0x80(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x6174a
andl $0x3ffffff0, %r13d      # imm = 0x3FFFFFF0
leal 0x1f(,%r13,2), %r14d
cmpl %ecx, %r14d
cmovael %ecx, %r14d
cmpq %r14, %r12
movq %r14, %rax
cmovlq %r12, %rax
cmpq %rbx, %rax
jle 0x61879
movq %rbp, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0xd0(%rsp), %r8
movq 0x18(%rsp), %r9
pushq 0x20(%rsp)
pushq %r10
pushq %rax
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x40(%rsp)
pushq 0xc8(%rsp)
callq 0x610b5
movq 0x128(%rsp), %rcx
movq 0x118(%rsp), %r10
movq 0x80(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x6174a
movsd 0x48(%rsp), %xmm0
mulsd 0x1c3cc1(%rip), %xmm0   # 0x225548
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
cmpl %ecx, %eax
cmovael %ecx, %eax
cmpq %rax, %r12
cmovlq %r12, %rax
cmpq %r14, %rax
jle 0x6174a
movq %rbp, %rdi
movq %r15, %rdx
movq 0x8(%rsp), %rcx
movq 0xd0(%rsp), %r8
movq 0x18(%rsp), %r9
pushq 0x20(%rsp)
pushq %r10
pushq %rax
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x40(%rsp)
pushq 0xc8(%rsp)
callq 0x610b5
addq $0x98, %rsp
jmp 0x6174e