// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movq %r8, (%rsp)
movq %rdx, %r12
movq 0xe0(%rsp), %rdx
movq 0xa0(%rsp), %r10
movq 0x98(%rsp), %r8
movq 0x108(%rsp), %rbp
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
movq %rax, 0x28(%rdx)
movq %r10, %r15
subq %r8, %r15
movq $-0x1, %rax
movq %rax, 0x10(%rsp)
movq %rax, 0x8(%rsp)
cvtsi2sd %r15, %xmm1
movq %rax, 0x18(%rsp)
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
testq %rbp, %rbp
cmovsq %rax, %rbp
testq %r15, %r15
jle 0x7e171
movq %rsi, %r11
testq %rcx, %rcx
movq %rcx, 0x38(%rsp)
movsd %xmm1, 0x48(%rsp)
movq %rax, 0x50(%rsp)
je 0x7dc45
movq (%rcx), %rax
movq 0x10(%rax), %rax
leaq (%rdi,%rdi,4), %rcx
movl (%rax,%rcx,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x7dc6c
testq %r12, %r12
je 0x7dc60
movq (%r12), %rax
movq 0x10(%rax), %rax
leaq (%rdi,%rdi,4), %rcx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x7dc6c
movq (%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rdi,8), %rax
movq 0xb0(%rsp), %rdx
movq 0xa8(%rsp), %rcx
cmpq $0x10, %r15
jg 0x7dd43
subq $0x60, %rsp
movq 0x170(%rsp), %rsi
movq %rsi, 0x58(%rsp)
movq 0x138(%rsp), %rsi
movq %rsi, 0x50(%rsp)
movl %edi, 0x48(%rsp)
movl %r11d, 0x40(%rsp)
movq %rax, 0x38(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x70(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rdx, 0x18(%rsp)
movq %rcx, 0x10(%rsp)
movq %r10, 0x8(%rsp)
movq %r8, (%rsp)
movq %rdi, %r13
movq %r12, %rdi
movq 0x98(%rsp), %rsi
movq 0x60(%rsp), %rdx
movq 0x140(%rsp), %rcx
movq %r9, %r8
movq %r12, %rbx
movq %r9, %r12
movq 0xf0(%rsp), %r9
movq %r11, %r14
callq 0x65745
movq 0xf8(%rsp), %r8
movq %r12, %r9
movq %rbx, %r12
movq %r13, %rdi
movq 0x110(%rsp), %rdx
movq %r14, %r11
movq 0x100(%rsp), %r10
addq $0x60, %rsp
movl $0x1, %ebx
testq %rax, %rax
jne 0x7e173
cmpq 0xf0(%rsp), %r15
jg 0x7e171
movq 0xf8(%rsp), %rsi
movq %rbp, %rax
sarq %rax
cmpq %rsi, %rax
jg 0x7e171
andl $0x7fffffe0, %ebp       # imm = 0x7FFFFFE0
orl $0x1f, %ebp
cmpl %esi, %ebp
movl %esi, %eax
cmovbl %ebp, %eax
subq $0x8, %rsp
movq %rdx, %r14
movq %rdi, %r13
movq %r12, %rdi
movq %r12, 0x48(%rsp)
movq 0x40(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq %r8, %r12
movq %r9, %r8
movq %r9, 0x38(%rsp)
movq 0x98(%rsp), %r9
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0xf0(%rsp)
pushq %r13
pushq %r11
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
leaq 0x68(%rsp), %rbx
pushq %rbx
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq %r14
pushq 0x120(%rsp)
pushq %r10
pushq %r12
movq %r11, %r14
callq 0x652ae
addq $0x90, %rsp
movl $0x1, %ebx
testq %rax, %rax
jne 0x7e173
movq %r13, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq 0x50(%rsp), %rdx
andl $0x7fffffe0, %edx       # imm = 0x7FFFFFE0
orl $0x1f, %edx
movq 0xf8(%rsp), %rcx
cmpl %ecx, %edx
movl %ecx, %r14d
cmovbl %edx, %r14d
cmpl %r14d, %ebp
movq 0x38(%rsp), %r13
movq 0x40(%rsp), %r12
jae 0x7df10
movl %r14d, %eax
subq $0x8, %rsp
movq %r12, %rdi
movq %r13, %rsi
movq %rdx, %rbp
movq 0x8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x98(%rsp), %r9
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0xf0(%rsp)
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0x120(%rsp)
callq 0x652ae
movq %rbp, %rdx
movq 0x188(%rsp), %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x7e173
andl $0x3ffffff0, %r14d      # imm = 0x3FFFFFF0
leal 0x1f(,%r14,2), %ebp
cmpl %ecx, %ebp
movl %ecx, %eax
cmovbl %ebp, %eax
cmpl %eax, %edx
movq 0x98(%rsp), %r11
jae 0x7dfe3
movl %eax, %eax
subq $0x8, %rsp
movq %r12, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x98(%rsp), %r9
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0xf0(%rsp)
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq %r11
movq %r11, %r14
callq 0x652ae
movq %r14, %r11
movq 0x188(%rsp), %rcx
addq $0x90, %rsp
testq %rax, %rax
jne 0x7e173
movsd 0x48(%rsp), %xmm0
mulsd 0x1a7557(%rip), %xmm0   # 0x225548
cvttsd2si %xmm0, %rax
andl $0x7fffffe0, %eax       # imm = 0x7FFFFFE0
orl $0x1f, %eax
cmpl %ecx, %eax
movq %rcx, %r14
cmovael %ecx, %eax
cmpl %eax, %ebp
movq 0xb8(%rsp), %rbp
jae 0x7e0b7
movl %eax, %eax
subq $0x8, %rsp
movq %r12, %rdi
movq %r13, %rsi
movq 0x8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x98(%rsp), %r9
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0xf0(%rsp)
pushq 0x40(%rsp)
pushq 0x50(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq %rbp
leaq 0x68(%rsp), %r10
pushq %r10
pushq %rax
leaq 0x68(%rsp), %rax
pushq %rax
leaq 0x78(%rsp), %rax
pushq %rax
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq %r11
callq 0x652ae
movq 0x148(%rsp), %rbp
addq $0x90, %rsp
testq %rax, %rax
jne 0x7e173
movq %r13, %rsi
cmpq 0x100(%rsp), %r15
movq 0xa0(%rsp), %rax
movq 0x28(%rsp), %r10
movq 0x20(%rsp), %rcx
movq 0x30(%rsp), %r8
movq 0xa8(%rsp), %r15
movq 0xb0(%rsp), %r13
jg 0x7e171
subq $0x8, %rsp
movq %rcx, %r11
movq %r12, %rdi
movq 0x8(%rsp), %rdx
movq 0xe8(%rsp), %rcx
movq 0x98(%rsp), %r9
pushq 0x120(%rsp)
pushq 0x120(%rsp)
pushq 0xf0(%rsp)
pushq %r11
pushq %r10
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq %rbp
leaq 0x68(%rsp), %r10
pushq %r10
pushq %r14
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
pushq %r13
pushq %r15
pushq %rax
pushq 0x120(%rsp)
callq 0x652ae
addq $0x90, %rsp
testq %rax, %rax
jne 0x7e173
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq