// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movsd %xmm0, 0x60(%rsp)
movq %rcx, %r10
movq 0xf0(%rsp), %rbx
movq 0xe0(%rsp), %rbp
movq 0xd0(%rsp), %rax
movq 0xc0(%rsp), %rcx
movq 0xb0(%rsp), %r13
subq 0xa8(%rsp), %r13
andq 0xb8(%rsp), %rcx
movl $0x7fffffff, 0x2c(%rbp) # imm = 0x7FFFFFFF
cmpq $-0x1, %rcx
je 0x30afe
decq %rax
cmpq $0x1, %rax
ja 0x30bc5
movq 0xd8(%rsp), %rax
movl 0x10(%rdi), %ecx
testq %r10, %r10
movq %r10, 0x20(%rsp)
je 0x30b0f
movq %rdx, %rbp
movq %r8, %r15
movq %r9, %r14
movq (%r10), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x30b44
movq $0x3, 0xd0(%rsp)
jmp 0x30bc5
testq %rdx, %rdx
je 0x30b33
movq %r8, %r15
movq %r9, %r14
movq %rdx, %rbp
movq (%rdx), %rdx
movq 0x10(%rdx), %rdx
imulq $0x28, %rcx, %rcx
movl (%rdx,%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
jmp 0x30b44
movq %rdx, %rbp
movq %r9, %r14
movq %r8, %r15
movq 0x18(%r8), %rdx
movq (%rdx,%rcx,8), %rcx
subq $0x50, %rsp
leaq 0x120(%rsp), %rdx
movq %rdx, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq %rcx, 0x30(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x110(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x108(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0x100(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0xf8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq %rdi, %r12
callq 0x302d1
movq %r12, %rdi
addq $0x50, %rsp
movq %r14, %r9
movq %r15, %r8
movq 0x20(%rsp), %r10
movq %rbp, %rdx
movq 0xe0(%rsp), %rbp
cmpq %rbx, %r13
jg 0x30cd9
movq 0xf8(%rsp), %rax
movq 0xc8(%rsp), %r14
cvtsi2sd %r14, %xmm0
mulsd 0x1b29a5(%rip), %xmm0   # 0x1e3590
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jb 0x30cd9
andl $0x7fffffe0, %r14d      # imm = 0x7FFFFFE0
orl $0x1f, %r14d
cmpl %eax, %r14d
cmovael %eax, %r14d
cmpq %r14, %r13
movq %r14, %rax
cmovlq %r13, %rax
movq 0xa8(%rsp), %rcx
movq %rcx, 0x28(%rsp)
movq 0xb0(%rsp), %rbx
movq 0xb8(%rsp), %r12
movq %rbp, %r11
movq 0xc0(%rsp), %r15
movq 0xd0(%rsp), %rbp
movq %rdi, 0x10(%rsp)
movq 0x10(%rsp), %rdi
movq %rdx, 0x18(%rsp)
movq 0x18(%rsp), %rsi
movq %r10, %rdx
movq %r8, %rcx
movq %r8, 0x50(%rsp)
movq %r11, %r8
movq %r9, 0x8(%rsp)
movq 0x8(%rsp), %r9
movq %rbp, 0x30(%rsp)
pushq %rbp
pushq 0xf0(%rsp)
pushq %rax
movq %r15, 0x50(%rsp)
pushq %r15
movq %r11, %r15
movq %r12, 0x60(%rsp)
pushq %r12
movq %rbx, 0x70(%rsp)
pushq %rbx
pushq 0x58(%rsp)
pushq 0xd8(%rsp)
movq %r10, 0x60(%rsp)
callq 0x30661
movq 0x138(%rsp), %r8
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq 0x48(%rsp), %r9
addq $0x40, %rsp
testq %rax, %rax
je 0x30ce8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cvtsi2sd %r13, %xmm1
movsd 0x60(%rsp), %xmm0
movsd %xmm1, 0x58(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
andl $0x7fffffe0, %ebx       # imm = 0x7FFFFFE0
orl $0x1f, %ebx
cmpl %r8d, %ebx
cmovael %r8d, %ebx
cmpq %rbx, %r13
movq %rbx, %r12
cmovlq %r13, %r12
cmpq %r14, %r12
jle 0x30d8d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %r8
movq 0x8(%rsp), %r9
pushq 0x30(%rsp)
pushq 0xf0(%rsp)
pushq %r12
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
pushq 0x58(%rsp)
pushq 0xd8(%rsp)
callq 0x30661
movq 0x138(%rsp), %r8
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq 0x48(%rsp), %r9
addq $0x40, %rsp
testq %rax, %rax
jne 0x30cd9
andl $0x3ffffff0, %r12d      # imm = 0x3FFFFFF0
leal 0x1f(,%r12,2), %r14d
cmpl %r8d, %r14d
cmovael %r8d, %r14d
cmpq %r14, %r13
movq %r14, %rax
cmovlq %r13, %rax
cmpq %rbx, %rax
jle 0x30e1d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r15, %r8
movq 0x8(%rsp), %r9
pushq 0x30(%rsp)
pushq 0xf0(%rsp)
pushq %rax
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
pushq 0x58(%rsp)
pushq 0xd8(%rsp)
callq 0x30661
movq 0x138(%rsp), %r8
movq 0x50(%rsp), %rdi
movq 0x58(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq 0x48(%rsp), %r9
addq $0x40, %rsp
testq %rax, %rax
jne 0x30cd9
movsd 0x58(%rsp), %xmm0
mulsd 0x1b276d(%rip), %xmm0   # 0x1e3598
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
cmpl %r8d, %eax
cmovael %r8d, %eax
cmpq %rax, %r13
cmovlq %r13, %rax
cmpq %r14, %rax
jle 0x30cd9
movq %r15, %r8
pushq 0x30(%rsp)
pushq 0xf0(%rsp)
pushq %rax
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x70(%rsp)
pushq 0x58(%rsp)
pushq 0xd8(%rsp)
callq 0x30661
addq $0xa8, %rsp
jmp 0x30cdd