// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz_adv(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, long, long, long, long, long, long, bit_extz_t*, long, double, long, long, long, long, overlap_region*)
int64_t hc_aln_exz_adv(overlap_region *z, 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 mode, int64_t wl, 
bit_extz_t *exz, int64_t q_tot, double e_rate, int64_t maxl, int64_t maxe, int64_t force_l, 
int64_t estimate_err, overlap_region *aux_o)
{
    clear_align(*exz); exz->thre = 0; 
    if(((ts == -1) && (te == -1))) mode = 3;///set to semi-global
    int64_t thre, ql = qe - qs, thre0, pts = -1, pte = -1, pthre = -1;
    if(ql == 0 && (te-ts) == 0) return 1;
    if((ql <= 0) || (te-ts) <= 0) return 0;
    if(estimate_err < 0) {
        if(ql > wl) estimate_err = cal_estimate_err(z, wl, qs, qe, e_rate);
        else estimate_err = ql*e_rate;
    }
    

    // fprintf(stderr, "[M::%s::ql::%ld] qs::[%ld, %ld), ts::[%ld, %ld), mode::%ld, est_err::%ld, e_rate::%f, maxe::%ld", 
    //                                                     __func__, ql, qs, qe, ts, te, mode, estimate_err, e_rate, maxe);
    if(ql <= 16) {
        if(cal_exact_exz(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, q_tot, mode)) {
            // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
            // fprintf(stderr, ", err::%d, thre::%d, scale::0(+)\n", exz->err, exz->thre);
            push_alnw(aux_o, exz);
            return 1;
        }
    }

    if(ql <= maxl && (estimate_err>>1) <= maxe) {
        thre = scale_ed_thre(estimate_err, maxe); 
        if(cal_exz_infi_adv(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, q_tot, mode)) {
            // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
            // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(+)\n", exz->err, exz->thre, thre);
            push_alnw(aux_o, exz);
            return 1;
        }

        thre0 = thre; thre = ql*e_rate; thre = scale_ed_thre(thre, maxe); 
        if(thre > thre0) {
            if(cal_exz_infi_adv(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, q_tot, mode)) {
                // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                push_alnw(aux_o, exz);
                return 1;
            }
        }

        thre0 = thre; thre <<= 1; thre = scale_ed_thre(thre, maxe); 
        if(thre > thre0) {
            if(cal_exz_infi_adv(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, q_tot, mode)) {
                // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(-)\n", exz->err, exz->thre, thre);
                push_alnw(aux_o, exz);
                return 1;
            }
        }

        thre0 = thre; thre = ql*0.51; thre = scale_ed_thre(thre, maxe); 
        if(thre > thre0) {
            if(cal_exz_infi_adv(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, q_tot, mode)) {
                // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(*)\n", exz->err, exz->thre, thre);
                push_alnw(aux_o, exz);
                return 1;
            }
        }

        if(ql <= force_l) {
            thre = maxe; 
            if(cal_exz_infi_adv(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, q_tot, mode)) {
                // ref_cigar_check(qstr, tu, uref, hpc_g, rref, z->y_id, z->y_pos_strand, exz);
                // fprintf(stderr, ", err::%d, thre::%d, scale::%ld(*)\n", exz->err, exz->thre, thre);
                push_alnw(aux_o, exz);
                return 1;
            }
        }
    }
    // fprintf(stderr, ", err::%d, thre::%d\n", INT32_MAX, exz->thre);
    // if(mode == 0) {
    //     fprintf(stderr, "[M::%s::] pstr::%.*s\n", __func__, (int32_t)tu->length, tu->seq);
    //     fprintf(stderr, "[M::%s::] tstr::%.*s\n", __func__, (int32_t)(qe-qs), qstr+qs);
    // }
    return 0;
    
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, %r15
movq 0xc0(%rsp), %r9
movq 0xa8(%rsp), %r11
movq 0xa0(%rsp), %r14
movq 0x98(%rsp), %rbx
movq 0x90(%rsp), %r13
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%r9)
movq %r11, %r10
andq %r14, %r10
movq $-0x1, %rax
movl $0x3, %ebp
cmpq %rax, %r10
je 0x6624c
movq 0xb0(%rsp), %rbp
movq %rax, 0x38(%rsp)
movq %rax, 0x30(%rsp)
movq %rax, 0x48(%rsp)
movq %rbx, %r12
xorq %r13, %r12
movq %r11, %r10
xorq %r14, %r10
movl $0x1, %eax
orq %r12, %r10
je 0x666cb
movq %rbx, %r12
subq %r13, %r12
testq %r12, %r12
setle %al
cmpq %r14, %r11
setle %r10b
orb %al, %r10b
jne 0x666c9
movsd %xmm0, 0x40(%rsp)
movq %rbp, 0x8(%rsp)
movq %rdi, (%rsp)
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq 0xe8(%rsp), %rbp
testq %rbp, %rbp
jns 0x66316
movq 0xb8(%rsp), %rsi
cmpq %rsi, %r12
jle 0x66306
movq (%rsp), %rdi
movq %r13, %rdx
movq %rbx, %rcx
movsd 0x40(%rsp), %xmm0
movq %r14, %rbp
movq %rbx, %r14
movq %r13, %rbx
movq %r15, %r13
movq %r9, %r15
callq 0x63deb
movq %r15, %r9
movq %r13, %r15
movq %rbx, %r13
movq %r14, %rbx
movq %rbp, %r14
movq %rax, %rbp
jmp 0x66316
cvtsi2sd %r12, %xmm0
mulsd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %rbp
cmpq $0x10, %r12
jg 0x663b6
subq $0x50, %rsp
movq 0x58(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x80(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x10(%rsp)
movq %r13, 0x8(%rsp)
movq %r15, (%rsp)
movq 0x50(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq %rbx, %r14
movq %r13, %rbx
movq %r15, %r13
movq %r9, %r15
movq %r9, %r8
movq 0x78(%rsp), %r9
callq 0x64c45
movq %r15, %rsi
movq %r13, %r15
movq %rbx, %r13
movq %r14, %rbx
movq 0xf0(%rsp), %r14
addq $0x50, %rsp
testq %rax, %rax
jne 0x66456
xorl %eax, %eax
cmpq 0xd0(%rsp), %r12
jg 0x666cb
movq 0xd8(%rsp), %rdx
movq %rbp, %rcx
sarq %rcx
cmpq %rdx, %rcx
jg 0x666cb
andl $0x7fffffe0, %ebp       # imm = 0x7FFFFFE0
orl $0x1f, %ebp
cmpl %edx, %ebp
movl %edx, %eax
cmovbl %ebp, %eax
subq $0x8, %rsp
leaq 0x50(%rsp), %r10
leaq 0x38(%rsp), %r11
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x10(%rsp)
pushq 0xd8(%rsp)
pushq %r10
pushq %rax
pushq %r11
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0xe0(%rsp)
pushq %r14
pushq %rbx
pushq %r13
pushq %r15
callq 0x64685
movq 0x120(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
je 0x6646d
movq 0xf0(%rsp), %rdi
callq 0x65d35
movl $0x1, %eax
jmp 0x666cb
cvtsi2sd %r12, %xmm1
movsd 0x40(%rsp), %xmm0
movsd %xmm1, 0x50(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andl $0x7fffffe0, %ecx       # imm = 0x7FFFFFE0
orl $0x1f, %ecx
movq 0xd8(%rsp), %rax
cmpl %eax, %ecx
cmovbl %ecx, %eax
cmpl %eax, %ebp
jae 0x66526
movq %rax, %rbp
movl %ebp, %eax
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rcx, 0x48(%rsp)
movq 0x28(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x10(%rsp)
pushq 0xd8(%rsp)
leaq 0x60(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0xe0(%rsp)
pushq %r14
pushq %rbx
pushq %r13
pushq %r15
callq 0x64685
movq 0xa0(%rsp), %rcx
movq 0x120(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
movq %rbp, %rax
jne 0x66456
andl $0x3ffffff0, %eax       # imm = 0x3FFFFFF0
leal 0x1f(,%rax,2), %ebp
movq 0xd8(%rsp), %rax
cmpl %eax, %ebp
cmovbl %ebp, %eax
cmpl %eax, %ecx
jae 0x665b1
movl %eax, %eax
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x10(%rsp)
pushq 0xd8(%rsp)
leaq 0x60(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0xe0(%rsp)
pushq %r14
pushq %rbx
pushq %r13
pushq %r15
callq 0x64685
movq 0x120(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66456
movsd 0x50(%rsp), %xmm0
mulsd 0x1bef89(%rip), %xmm0   # 0x225548
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
movq 0xd8(%rsp), %rcx
cmpl %ecx, %eax
cmovael %ecx, %eax
cmpl %eax, %ebp
jae 0x6664b
movl %eax, %eax
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x10(%rsp)
pushq 0xd8(%rsp)
leaq 0x60(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0xe0(%rsp)
pushq %r14
pushq %rbx
pushq %r13
pushq %r15
callq 0x64685
movq 0x120(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66456
movq 0xd8(%rsp), %rax
cmpq 0xe0(%rsp), %r12
jg 0x666c9
subq $0x8, %rsp
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xc8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x10(%rsp)
pushq 0xd8(%rsp)
leaq 0x60(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0xe0(%rsp)
pushq %r14
pushq %rbx
pushq %r13
pushq %r15
callq 0x64685
movq 0x120(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66456
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq