// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz_adv_hc(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_hc(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, 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_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 $0x68, %rsp
movq %rcx, %r10
movq %rdi, %r15
movq 0xd0(%rsp), %rdi
movq 0xb8(%rsp), %rbx
movq 0xb0(%rsp), %rbp
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %r12
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%rdi)
movq %rbx, %rax
andq %rbp, %rax
cmpq $-0x1, %rax
je 0x35387
movq 0xc0(%rsp), %r13
jmp 0x3538b
pushq $0x3
popq %r13
orq $-0x1, 0x48(%rsp)
orq $-0x1, 0x40(%rsp)
orq $-0x1, 0x50(%rsp)
andq $0x0, 0x58(%rsp)
movq %rcx, %rax
xorq %r12, %rax
movq %rbx, %r11
xorq %rbp, %r11
pushq $0x1
popq %r14
orq %rax, %r11
je 0x35870
movq %r13, 0x38(%rsp)
movq %rcx, %r13
subq %r12, %r13
testq %r13, %r13
setle %al
cmpq %rbp, %rbx
setle %r11b
orb %al, %r11b
jne 0x3547c
movq %r15, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %r8, 0x28(%rsp)
movq %r9, 0x30(%rsp)
movq 0xf8(%rsp), %r15
testq %r15, %r15
jns 0x35454
movq 0xc8(%rsp), %rsi
subq $0x8, %rsp
leaq 0x60(%rsp), %rax
movq 0x10(%rsp), %rdi
movq %r12, %rdx
movq %rcx, %r15
movq %rbp, %r8
movq %rbx, %r9
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
pushq %rax
callq 0x33319
movsd 0x10(%rsp), %xmm0
movq %r15, %rcx
movq 0xe0(%rsp), %rdi
addq $0x10, %rsp
movq %rax, %r15
testq %r15, %r15
je 0x35484
movq 0xe8(%rsp), %rdx
cmpq 0xe0(%rsp), %r13
setg %al
movq %r15, %rcx
sarq %rcx
cmpq %rdx, %rcx
setg %cl
orb %al, %cl
je 0x354ae
xorl %r14d, %r14d
jmp 0x35870
cmpq $0x0, 0x58(%rsp)
je 0x357d1
movq %r12, %rsi
movq %rcx, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x352b9
movq 0xd0(%rsp), %rsi
jmp 0x35863
andl $0x7fffffe0, %r15d      # imm = 0x7FFFFFE0
orl $0x1f, %r15d
cmpl %edx, %r15d
movl %edx, %eax
cmovbl %r15d, %eax
subq $0x8, %rsp
leaq 0x58(%rsp), %r10
leaq 0x48(%rsp), %r11
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x40(%rsp)
pushq 0xe8(%rsp)
pushq %r10
pushq %rax
pushq %r11
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq %rbp
pushq 0xf0(%rsp)
pushq %r12
pushq 0x88(%rsp)
movsd %xmm0, 0x60(%rsp)
callq 0x3396d
movsd 0x60(%rsp), %xmm0
movq 0x108(%rsp), %r8
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x35863
cvtsi2sd %r13, %xmm1
movsd %xmm1, 0x60(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
andl $0x7fffffe0, %ecx       # imm = 0x7FFFFFE0
orl $0x1f, %ecx
movq 0xe8(%rsp), %rax
cmpl %eax, %ecx
cmovbl %ecx, %eax
cmpl %eax, %r15d
jae 0x35605
movq %rax, %r15
movl %r15d, %eax
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rcx, 0x8(%rsp)
movq 0x28(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x40(%rsp)
pushq 0xe8(%rsp)
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq %rbp
pushq 0xf0(%rsp)
pushq %r12
pushq 0x88(%rsp)
callq 0x3396d
movq 0x60(%rsp), %rcx
movq 0x108(%rsp), %r8
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
movq %r15, %rax
jne 0x35863
andl $0x3ffffff0, %eax       # imm = 0x3FFFFFF0
leal 0x1f(,%rax,2), %r15d
movq 0xe8(%rsp), %rax
cmpl %eax, %r15d
cmovbl %r15d, %eax
cmpl %eax, %ecx
jae 0x3569f
movl %eax, %eax
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x40(%rsp)
pushq 0xe8(%rsp)
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq %rbp
pushq 0xf0(%rsp)
pushq %r12
pushq 0x88(%rsp)
callq 0x3396d
movq 0x108(%rsp), %r8
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x35863
movsd 0x60(%rsp), %xmm0
mulsd 0x1adeeb(%rip), %xmm0   # 0x1e3598
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
movq 0xe8(%rsp), %rcx
cmpl %ecx, %eax
cmovael %ecx, %eax
cmpl %eax, %r15d
jae 0x35746
movl %eax, %eax
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x40(%rsp)
pushq 0xe8(%rsp)
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq %rbp
pushq 0xf0(%rsp)
pushq %r12
pushq 0x88(%rsp)
callq 0x3396d
movq 0x108(%rsp), %r8
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x35863
movq 0xe8(%rsp), %r10
cmpq 0xf0(%rsp), %r13
jg 0x3547c
subq $0x8, %rsp
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r8, %rax
movq 0xd8(%rsp), %r8
movq 0x30(%rsp), %r9
pushq 0x40(%rsp)
pushq 0xe8(%rsp)
leaq 0x68(%rsp), %r11
pushq %r11
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
pushq %rbx
pushq %rbp
pushq %rax
pushq %r12
pushq 0x88(%rsp)
callq 0x3396d
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x35863
jmp 0x3547c
subq $0x50, %rsp
movq 0x88(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %r12, 0x8(%rsp)
movq 0x80(%rsp), %rax
movq %rax, (%rsp)
movq 0x58(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x120(%rsp), %r8
movq 0x78(%rsp), %r9
movsd %xmm0, 0x50(%rsp)
callq 0x33e7f
movsd 0x50(%rsp), %xmm0
movq 0x120(%rsp), %rsi
addq $0x50, %rsp
testq %rax, %rax
je 0x35459
movq 0x100(%rsp), %rdi
callq 0x34955
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq