// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: hc_aln_exz_simi_adv(long, long, ul_idx_t const*, hpc_t*, All_reads*, char*, UC_Read*, long, long, long, long, long, long, long, long, long, bit_extz_t*, long, double, long, long, long, long, overlap_region*, long)
int64_t hc_aln_exz_simi_adv(int64_t id, int64_t rev, 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 qmin, int64_t qmax, int64_t tmin, int64_t tmax, int64_t mode, 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 *z,
int64_t gen_trace)
{
    clear_align(*exz); exz->thre = 0; ///mode cannot be 3
    int64_t thre, ql = qe - qs, thre0, pts = -1, pte = -1, pthre = -1, t_tot;
    if(estimate_err < 0) estimate_err = ql*e_rate;
    if(ql <= 0) return 0;
    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);

    // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
    //     fprintf(stderr, "-0-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
    //     __func__, mode, qs, qe, ts, te);
    // }

    if(ql <= 16) {
        if(cal_exact_simi_exz(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, q_tot,
        t_tot, rev, id, mode, z)) {
            // 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);
            return 1;
        }
    }

    // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
    //     fprintf(stderr, "-a-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), ql::%ld, maxl::%ld, estimate_err::%ld, maxe::%ld\n", 
    //     __func__, mode, qs, qe, ts, te, ql, maxl, estimate_err, maxe);
    // }

    if(ql <= maxl && (estimate_err>>1) <= maxe) {
        thre = scale_ed_thre(estimate_err, maxe); 
        // if(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "-1-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre);
        // }
        if(cal_exz_infi_simi_adv(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, 
                qmin, qmax, tmin, tmax, rev, id, mode, z, gen_trace)) {
            // 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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "-2-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre);
        // }
        if(thre > thre0) {
            if(cal_exz_infi_simi_adv(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, 
                qmin, qmax, tmin, tmax, rev, id, mode, z, gen_trace)) {
                // 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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "-3-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre);
        // }
        if(thre > thre0) {
            if(cal_exz_infi_simi_adv(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, 
                qmin, qmax, tmin, tmax, rev, id, mode, z, gen_trace)) {
                // 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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
        //     fprintf(stderr, "-4-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", 
        //     __func__, mode, qs, qe, ts, te, thre);
        // }
        if(thre > thre0) {
            if(cal_exz_infi_simi_adv(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, 
                qmin, qmax, tmin, tmax, rev, id, mode, z, gen_trace)) {
                // 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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
            //     fprintf(stderr, "-5-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld), thre::%ld\n", 
            //     __func__, mode, qs, qe, ts, te, thre);
            // }
            if(cal_exz_infi_simi_adv(uref, hpc_g, rref, exz, qstr, tu, qs, qe, ts, te, &pts, &pte, thre, &pthre, 
                qmin, qmax, tmin, tmax, rev, id, mode, z, gen_trace)) {
                // 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(ts == 18327 && te == 18601 && qs == 145990 && qe == 146191) {
    //     fprintf(stderr, "-b-[M::%s::] mode::%ld, q::[%ld, %ld), t::[%ld, %ld)\n", 
    //     __func__, mode, qs, qe, ts, te);
    // }
    // 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 %r8, 0x18(%rsp)
movq %rcx, %r11
movq %rdx, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, 0x8(%rsp)
movq 0xf0(%rsp), %rcx
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%rcx)
orq $-0x1, 0x30(%rsp)
movq 0xb0(%rsp), %r10
orq $-0x1, 0x28(%rsp)
movq 0xa8(%rsp), %r12
orq $-0x1, 0x38(%rsp)
movq %r10, %rsi
subq %r12, %rsi
cvtsi2sd %rsi, %xmm1
movq 0x118(%rsp), %rbp
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
testq %rbp, %rbp
cmovsq %rax, %rbp
testq %rsi, %rsi
jle 0x4aa23
testq %r11, %r11
movq %rax, 0x60(%rsp)
je 0x4a49a
movq (%r11), %rax
movq 0x10(%rax), %rax
imulq $0x28, 0x8(%rsp), %rdx
movl (%rax,%rdx), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x4a4d0
cmpq $0x0, 0x10(%rsp)
je 0x4a4be
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, 0x8(%rsp), %rdx
movl (%rax,%rdx), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x4a4d0
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq 0x8(%rsp), %rdx
movq (%rax,%rdx,8), %rax
movq 0x120(%rsp), %rbx
movq 0xe8(%rsp), %r8
movq 0xc0(%rsp), %r13
movq 0xb8(%rsp), %rdx
movq 0xa0(%rsp), %r14
cmpq $0x10, %rsi
ja 0x4a5d7
subq $0x60, %rsp
movq %rbx, 0x58(%rsp)
movq %r8, 0x50(%rsp)
movq 0x68(%rsp), %r8
movl %r8d, 0x48(%rsp)
movq 0x80(%rsp), %r8
movl %r8d, 0x40(%rsp)
movq %rax, 0x38(%rsp)
leaq 0x88(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r13, 0x18(%rsp)
movq %rdx, 0x10(%rsp)
movq %r10, 0x8(%rsp)
movq %r12, (%rsp)
movq 0x70(%rsp), %rdi
movq %rsi, 0xb0(%rsp)
movq %r11, %rsi
movq 0x78(%rsp), %rdx
movq %r9, %r8
movq %r9, %r12
movq %r14, %r9
movq %r11, %r15
movsd %xmm1, 0xb8(%rsp)
callq 0x345f3
movsd 0xb8(%rsp), %xmm1
movq 0x148(%rsp), %r8
movq 0x118(%rsp), %rdx
movq 0xb0(%rsp), %rsi
pushq $0x1
popq %rcx
movq %r15, %r11
movq %r12, %r9
movq 0x108(%rsp), %r12
movq 0x180(%rsp), %rbx
movq 0x110(%rsp), %r10
addq $0x60, %rsp
testq %rax, %rax
jne 0x4aa25
cmpq 0x100(%rsp), %rsi
jg 0x4aa23
movq 0x108(%rsp), %rcx
movq %rbp, %rax
sarq %rax
cmpq %rcx, %rax
jg 0x4aa23
movsd %xmm1, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
andl $0x7fffffe0, %ebp       # imm = 0x7FFFFFE0
orl $0x1f, %ebp
cmpl %ecx, %ebp
movl %ecx, %eax
cmovbl %ebp, %eax
subq $0x8, %rsp
movq %r8, %r15
movq 0x18(%rsp), %rdi
movq %r11, %rsi
movq %rdx, %r14
movq 0x20(%rsp), %rdx
movq 0xf8(%rsp), %rcx
movq %r9, %r8
movq %r9, 0x50(%rsp)
movq 0xa8(%rsp), %r9
pushq 0x130(%rsp)
pushq %rbx
pushq %r15
pushq 0x28(%rsp)
pushq 0x48(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
leaq 0x88(%rsp), %rbx
pushq %rbx
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
pushq %r13
pushq %r14
pushq %r10
pushq %r12
movq %r11, %r14
callq 0x34247
addq $0x90, %rsp
testq %rax, %rax
je 0x4a6b4
pushq $0x1
popq %rcx
jmp 0x4aa25
movq %r14, 0x40(%rsp)
movq 0x60(%rsp), %rdx
andl $0x7fffffe0, %edx       # imm = 0x7FFFFFE0
orl $0x1f, %edx
movq 0x108(%rsp), %rcx
cmpl %ecx, %edx
movl %ecx, %r14d
cmovbl %edx, %r14d
cmpl %r14d, %ebp
movq 0xe8(%rsp), %rbx
movq 0x120(%rsp), %r12
jae 0x4a7a6
movl %r14d, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq %rdx, %rbp
movq 0x20(%rsp), %rdx
movq 0xf8(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0xa8(%rsp), %r9
pushq 0x130(%rsp)
pushq %r12
pushq %rbx
pushq 0x28(%rsp)
pushq 0x48(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
leaq 0x88(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq 0x130(%rsp)
callq 0x34247
movq %rbp, %rdx
movq 0x198(%rsp), %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x4a6ac
andl $0x3ffffff0, %r14d      # imm = 0x3FFFFFF0
leal 0x1f(,%r14,2), %ebp
cmpl %ecx, %ebp
movl %ecx, %eax
cmovbl %ebp, %eax
cmpl %eax, %edx
movq 0xa8(%rsp), %r11
jae 0x4a87b
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0xf8(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0xa8(%rsp), %r9
pushq 0x130(%rsp)
pushq %r12
pushq %rbx
pushq 0x28(%rsp)
pushq 0x48(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
leaq 0x88(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq %r11
movq %r11, %r14
callq 0x34247
movq %r14, %r11
movq 0x198(%rsp), %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x4a6ac
movsd 0x58(%rsp), %xmm0
mulsd 0x198d0f(%rip), %xmm0   # 0x1e3598
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
cmpl %ecx, %eax
cmovael %ecx, %eax
cmpl %eax, %ebp
movq 0xc8(%rsp), %r10
jae 0x4a967
movl %eax, %eax
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rcx, %r14
movq 0xf8(%rsp), %rcx
movq 0x50(%rsp), %r8
movq 0xa8(%rsp), %r9
pushq 0x130(%rsp)
pushq %r12
pushq %rbx
pushq 0x28(%rsp)
pushq 0x48(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq %r10
leaq 0x88(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x88(%rsp), %rax
pushq %rax
leaq 0x98(%rsp), %rax
pushq %rax
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq 0x130(%rsp)
pushq %r11
movq %r12, %rbp
movq %rbx, %r12
movq %r11, %rbx
callq 0x34247
movq 0x158(%rsp), %r10
movq %rbx, %r11
movq %r12, %rbx
movq %rbp, %r12
movq %r14, %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x4a6ac
movq %r10, %r14
movq %r11, %rbp
movq 0x50(%rsp), %rax
cmpq 0x110(%rsp), %rax
movq 0xb0(%rsp), %rax
movq 0x48(%rsp), %r8
movq 0x40(%rsp), %rsi
movq 0xb8(%rsp), %r10
movq 0xc0(%rsp), %r11
jg 0x4aa23
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rdx
movq %rcx, %r15
movq 0xf8(%rsp), %rcx
movq 0xa8(%rsp), %r9
pushq 0x130(%rsp)
pushq %r12
pushq %rbx
pushq 0x28(%rsp)
pushq 0x48(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq %r14
leaq 0x88(%rsp), %rbx
pushq %rbx
pushq %r15
leaq 0x88(%rsp), %rbx
pushq %rbx
leaq 0x98(%rsp), %rbx
pushq %rbx
pushq %r11
pushq %r10
pushq %rax
pushq %rbp
callq 0x34247
pushq $0x1
popq %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x4aa25
xorl %ecx, %ecx
movq %rcx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq