// File: /workspace/github_cpp_makefile_star2_O1/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, %r13
movq 0xd0(%rsp), %rdi
movq 0xb8(%rsp), %rbx
movq 0xb0(%rsp), %rbp
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %r11
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%rdi)
movq %rbx, %r14
andq %rbp, %r14
movq $-0x1, %rax
movl $0x3, %r12d
cmpq %rax, %r14
je 0x6687d
movq 0xc0(%rsp), %r12
movq %rax, 0x48(%rsp)
movq %rax, 0x40(%rsp)
movq %rax, 0x50(%rsp)
movq $0x0, 0x58(%rsp)
movq %rcx, %rax
xorq %r11, %rax
movq %rbx, %r15
xorq %rbp, %r15
movl $0x1, %r14d
orq %rax, %r15
je 0x66d97
movq %r13, 0x10(%rsp)
movq %rcx, %r13
subq %r11, %r13
testq %r13, %r13
setle %al
cmpq %rbp, %rbx
setle %r15b
orb %al, %r15b
jne 0x66973
movq %rsi, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %r9, 0x38(%rsp)
movq 0xf8(%rsp), %r15
testq %r15, %r15
jns 0x6694b
movq 0xc8(%rsp), %rsi
subq $0x8, %rsp
leaq 0x60(%rsp), %rax
movq 0x18(%rsp), %rdi
movq %r11, %r15
movq %r11, %rdx
movq %rbp, %r8
movq %rbx, %r9
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
pushq %rax
callq 0x63f7f
movsd 0x18(%rsp), %xmm0
movq 0xb8(%rsp), %rcx
movq %r15, %r11
movq 0xe0(%rsp), %rdi
addq $0x10, %rsp
movq %rax, %r15
testq %r15, %r15
je 0x6697b
movq 0xe8(%rsp), %rdx
cmpq 0xe0(%rsp), %r13
setg %al
movq %r15, %rcx
sarq %rcx
cmpq %rdx, %rcx
setg %cl
orb %al, %cl
je 0x669a5
xorl %r14d, %r14d
jmp 0x66d97
cmpq $0x0, 0x58(%rsp)
je 0x66cfd
movq %r11, %rsi
movq %rcx, %rdx
movq %rbp, %rcx
movq %rbx, %r8
callq 0x666da
movq 0xd0(%rsp), %rsi
jmp 0x66d8a
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 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r12
pushq 0xe8(%rsp)
pushq %r10
pushq %rax
pushq %r11
leaq 0x78(%rsp), %rax
pushq %rax
pushq %rbx
pushq %rbp
pushq 0xf0(%rsp)
pushq 0xf0(%rsp)
pushq 0x90(%rsp)
movsd %xmm0, 0x68(%rsp)
callq 0x64685
movsd 0x68(%rsp), %xmm0
movq 0x108(%rsp), %r8
movq 0x100(%rsp), %rcx
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66d8a
cvtsi2sd %r13, %xmm1
movsd %xmm1, 0x60(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rdx
andl $0x7fffffe0, %edx       # imm = 0x7FFFFFE0
orl $0x1f, %edx
movq 0xe8(%rsp), %rax
cmpl %eax, %edx
cmovbl %edx, %eax
cmpl %eax, %r15d
jae 0x66b12
movq %rax, %r15
movl %r15d, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rdx, 0x10(%rsp)
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r12
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 0xf0(%rsp)
pushq 0x90(%rsp)
callq 0x64685
movq 0x68(%rsp), %rdx
movq 0x108(%rsp), %r8
movq 0x100(%rsp), %rcx
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
movq %r15, %rax
jne 0x66d8a
andl $0x3ffffff0, %eax       # imm = 0x3FFFFFF0
leal 0x1f(,%rax,2), %r15d
movq 0xe8(%rsp), %rax
cmpl %eax, %r15d
cmovbl %r15d, %eax
cmpl %eax, %edx
jae 0x66bbb
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r12
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 0xf0(%rsp)
pushq 0x90(%rsp)
callq 0x64685
movq 0x108(%rsp), %r8
movq 0x100(%rsp), %rcx
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66d8a
movsd 0x60(%rsp), %xmm0
mulsd 0x1be97f(%rip), %xmm0   # 0x225548
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
movq 0xe8(%rsp), %rdx
cmpl %edx, %eax
cmovael %edx, %eax
cmpl %eax, %r15d
jae 0x66c71
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r12
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 0xf0(%rsp)
pushq 0x90(%rsp)
callq 0x64685
movq 0x108(%rsp), %r8
movq 0x100(%rsp), %rcx
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66d8a
movq 0xe8(%rsp), %r11
cmpq 0xf0(%rsp), %r13
jg 0x66973
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rcx, %rax
movq 0x28(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r8, %r10
movq 0xd8(%rsp), %r8
movq 0x38(%rsp), %r9
pushq %r12
pushq 0xe8(%rsp)
leaq 0x68(%rsp), %r15
pushq %r15
pushq %r11
leaq 0x68(%rsp), %r11
pushq %r11
leaq 0x78(%rsp), %r11
pushq %r11
pushq %rbx
pushq %rbp
pushq %r10
pushq %rax
pushq 0x90(%rsp)
callq 0x64685
movq 0x130(%rsp), %rsi
addq $0x60, %rsp
testq %rax, %rax
jne 0x66d8a
jmp 0x66973
subq $0x50, %rsp
movq %r12, 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 %r11, 0x8(%rsp)
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
movq 0x60(%rsp), %rdi
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rcx
movq 0x120(%rsp), %r8
movq 0x80(%rsp), %r9
movsd %xmm0, 0x58(%rsp)
callq 0x64c45
movsd 0x58(%rsp), %xmm0
movq 0x120(%rsp), %rsi
addq $0x50, %rsp
testq %rax, %rax
je 0x66950
movq 0x100(%rsp), %rdi
callq 0x65d35
movq %r14, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq