// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz_non_retrieve_adv_hc(overlap_region*, char*, char*, long, long, long, long, long, long, bit_extz_t*, long, long, double, long, long, long, long, overlap_region*)
int64_t hc_aln_exz_non_retrieve_adv_hc(overlap_region *z, char* qstr, char* tstr, 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, int64_t t_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, pthre = -1, full = 0;
    if(ql == 0 && (te-ts) == 0) return 1;
    if((ql <= 0) || (te-ts) <= 0) return 0;
    if(estimate_err < 0) estimate_err = cal_estimate_err_hc(z, wl, qs, qe, ts, te, e_rate, &full);
    

    
    // if(ql <= 16) {
    if(estimate_err == 0) {
        if(full) {
            // if(!cal_exact_exz(z, uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, q_tot, mode)) {
            //     fprintf(stderr, "[M::%s::ql::%ld::%c] xid::%d, yid::%d, qs::[%ld, %ld), ts::[%ld, %ld), mode::%ld, est_err::%ld, e_rate::%f, maxe::%ld\n", 
            //                                             __func__, ql, "+-"[z->y_pos_strand], z->x_id, z->y_id, qs, qe, ts, te, mode, estimate_err, e_rate, maxe);
            //     exit(1);
            // }
            set_exact_exz(exz, qs, qe, ts, te); push_alnw(aux_o, exz);
            return 1;
        } else if(cal_exact_non_retrieve_exz(z, exz, qstr, tstr, qs, qe, ts, te, q_tot, t_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_non_retrieve_adv(z, exz, qstr, tstr, qs, qe, ts, te, thre, &pthre, q_tot, t_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_non_retrieve_adv(z, exz, qstr, tstr, qs, qe, ts, te, thre, &pthre, q_tot, t_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_non_retrieve_adv(z, exz, qstr, tstr, qs, qe, ts, te, thre, &pthre, q_tot, t_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_non_retrieve_adv(z, exz, qstr, tstr, qs, qe, ts, te, thre, &pthre, q_tot, t_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_non_retrieve_adv(z, exz, qstr, tstr, qs, qe, ts, te, thre, &pthre, q_tot, t_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 $0x48, %rsp
movq %r9, %r15
movq %r8, %r13
movq %rcx, %rbp
movq %rdi, %r8
movq 0x98(%rsp), %rdi
movq 0x80(%rsp), %r9
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%rdi)
movq %r9, %rax
andq %r15, %rax
cmpq $-0x1, %rax
je 0x358d0
movq 0x88(%rsp), %r10
jmp 0x358d4
pushq $0x3
popq %r10
orq $-0x1, 0x30(%rsp)
andq $0x0, 0x38(%rsp)
movq %r13, %rax
xorq %rbp, %rax
movq %r9, %rcx
xorq %r15, %rcx
pushq $0x1
popq %rbx
orq %rax, %rcx
je 0x35d19
movq %r13, %r12
subq %rbp, %r12
testq %r12, %r12
setle %al
cmpq %r15, %r9
setle %cl
orb %al, %cl
jne 0x359af
movq %r10, 0x18(%rsp)
movq %r8, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq 0xc8(%rsp), %r14
testq %r14, %r14
jns 0x3597f
movq 0x90(%rsp), %rsi
subq $0x8, %rsp
leaq 0x40(%rsp), %rax
movq 0x18(%rsp), %rdi
movq %rbp, %rdx
movq %r13, %rcx
movq %r15, %r8
movq %r9, %r14
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
pushq %rax
callq 0x33319
movsd 0x18(%rsp), %xmm0
movq %r14, %r9
movq 0xa8(%rsp), %rdi
addq $0x10, %rsp
movq %rax, %r14
movq 0xa8(%rsp), %r10
testq %r14, %r14
je 0x359b6
movq 0xb8(%rsp), %rdx
cmpq 0xb0(%rsp), %r12
setg %al
movq %r14, %rcx
sarq %rcx
cmpq %rdx, %rcx
setg %cl
orb %al, %cl
je 0x359e0
xorl %ebx, %ebx
jmp 0x35d19
cmpq $0x0, 0x38(%rsp)
je 0x35ca5
movq %rbp, %rsi
movq %r13, %rdx
movq %r15, %rcx
movq %r9, %r8
callq 0x352b9
movq 0x98(%rsp), %rsi
jmp 0x35d0c
andl $0x7fffffe0, %r14d      # imm = 0x7FFFFFE0
orl $0x1f, %r14d
cmpl %edx, %r14d
movl %edx, %eax
cmovbl %r14d, %eax
subq $0x8, %rsp
leaq 0x38(%rsp), %r11
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
pushq 0x20(%rsp)
pushq %r10
pushq 0xb8(%rsp)
pushq %r11
pushq %rax
pushq 0xb0(%rsp)
pushq %r15
movsd %xmm0, 0x48(%rsp)
callq 0x33ca4
movsd 0x48(%rsp), %xmm0
movq 0xe8(%rsp), %r10
movq 0xc0(%rsp), %r9
movq 0xd8(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x35d0c
cvtsi2sd %r12, %xmm1
movsd %xmm1, 0x40(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andl $0x7fffffe0, %ecx       # imm = 0x7FFFFFE0
orl $0x1f, %ecx
movq 0xb8(%rsp), %rax
cmpl %eax, %ecx
cmovbl %ecx, %eax
cmpl %eax, %r14d
jae 0x35b14
movq %rax, %r14
movl %r14d, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq %rcx, 0x10(%rsp)
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
pushq 0x20(%rsp)
pushq %r10
pushq 0xb8(%rsp)
leaq 0x50(%rsp), %r10
pushq %r10
pushq %rax
pushq 0xb0(%rsp)
pushq %r15
callq 0x33ca4
movq 0x48(%rsp), %rcx
movq 0xe8(%rsp), %r10
movq 0xc0(%rsp), %r9
movq 0xd8(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
movq %r14, %rax
jne 0x35d0c
andl $0x3ffffff0, %eax       # imm = 0x3FFFFFF0
leal 0x1f(,%rax,2), %r14d
movq 0xb8(%rsp), %rax
cmpl %eax, %r14d
cmovbl %r14d, %eax
cmpl %eax, %ecx
jae 0x35b9f
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
pushq 0x20(%rsp)
pushq %r10
pushq 0xb8(%rsp)
leaq 0x50(%rsp), %r10
pushq %r10
pushq %rax
pushq 0xb0(%rsp)
pushq %r15
callq 0x33ca4
movq 0xe8(%rsp), %r10
movq 0xc0(%rsp), %r9
movq 0xd8(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x35d0c
movsd 0x40(%rsp), %xmm0
mulsd 0x1ad9eb(%rip), %xmm0   # 0x1e3598
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
movq 0xb8(%rsp), %rcx
cmpl %ecx, %eax
cmovael %ecx, %eax
cmpl %eax, %r14d
jae 0x35c37
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
pushq 0x20(%rsp)
pushq %r10
pushq 0xb8(%rsp)
leaq 0x50(%rsp), %r10
pushq %r10
pushq %rax
pushq 0xb0(%rsp)
pushq %r15
callq 0x33ca4
movq 0xe8(%rsp), %r10
movq 0xc0(%rsp), %r9
movq 0xd8(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x35d0c
movq 0xb8(%rsp), %r11
cmpq 0xc0(%rsp), %r12
jg 0x359af
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %rbp, %r8
movq %r9, %rax
movq %r13, %r9
pushq 0x20(%rsp)
pushq %r10
pushq 0xb8(%rsp)
leaq 0x50(%rsp), %r10
pushq %r10
pushq %r11
pushq %rax
pushq %r15
callq 0x33ca4
movq 0xd8(%rsp), %rsi
addq $0x40, %rsp
testq %rax, %rax
jne 0x35d0c
jmp 0x359af
subq $0x30, %rsp
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r10, 0x18(%rsp)
movq %r9, 0x8(%rsp)
movq %r15, (%rsp)
movq 0x40(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq %rbp, %r8
movq %r13, %r9
movsd %xmm0, 0x38(%rsp)
callq 0x3407b
movsd 0x38(%rsp), %xmm0
movq 0xd8(%rsp), %r10
movq 0xc8(%rsp), %rsi
addq $0x30, %rsp
testq %rax, %rax
je 0x3598c
movq 0xd0(%rsp), %rdi
callq 0x34955
movq %rbx, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq