// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: aln_wlst_adv_non_retrieve_exz(overlap_region*, char*, char*, long, bit_extz_t*, unsigned int, unsigned int, unsigned int, long, long, long, long, double, unsigned int)
inline uint32_t aln_wlst_adv_non_retrieve_exz(overlap_region *z, char *qstr, char *tstr, int64_t t_tot_l, bit_extz_t *exz, uint32_t max_err,
uint32_t rev, uint32_t id, int64_t qs, int64_t qe, int64_t t_s, int64_t block_s, double e_rate, uint32_t is_cigar)
{
    int64_t ql, tl, aln_l; window_list *p = NULL; ///int r_ts = 0, t_end;
    int64_t aux_beg, aux_end, t_pri_l; int64_t thres; char *q_string, *t_string; 
    ql = qe + 1 - qs;
    ///there are two potiential reasons for unmatched window:
    ///1. this window has a large number of differences
    ///2. DP does not start from the right offset
    thres = double_error_threshold(get_init_err_thres(ql, e_rate, block_s, max_err), ql);

    aln_l = ql + (thres << 1);

    if(!init_waln(thres, t_s, t_tot_l, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) return 0;
    if(t_pri_l + thres < ql) return 0;

    q_string = qstr + qs; t_string = tstr + t_s;

    tl = t_pri_l;
    if(is_cigar) {
        clear_align(*exz);
        ed_band_cal_semi_64_w_absent_diag_trace(t_string, tl, q_string, ql, thres, aux_beg, exz);
    } else {
        ed_band_cal_semi_64_w_absent_diag(t_string, tl, q_string, ql, thres, aux_beg, exz); exz->ps = 0;
    }

    // if(id == 40 && qs == 79670 && qe == 79824) {
    //     fprintf(stderr, "\n[M::%s::semi] exz->ps::%d, exz->pe::%d, exz->ts::%d, exz->te::%d, exz->err::%d, exz->cigar.n::%d\n", 
    //     __func__, exz->ps, exz->pe, exz->ts, exz->te, exz->err, (int32_t)exz->cigar.n);
    // }

    if(is_align(*exz)) {
        kv_pushp(window_list, z->w_list, &p);
        p->x_start = qs; p->x_end = qe; ///must set x_start/x_end here
        p->y_start = t_s + exz->ps;///difference
        p->y_end = t_s + exz->pe;
        p->error = exz->err;
        p->cidx = p->clen = 0;
        if(is_cigar) {
            push_wcigar(p, &(z->w_list), exz);
            ///this condition is always wrong
            ///in best case, r_ts = threshold, t_end = aln_l - thres - 1
            if ((((exz->pe+1) == tl) || (exz->ps == 0)) && (exz->err > 0)) {
                if(recal_boundary_non_retrieve_exz(q_string, tstr, t_tot_l, ql, tl, thres, t_s, exz->ps, exz->pe,
                exz->err, id, rev, exz, &t_s, &aux_beg, &aux_end)) {
                    //update cigar
                    z->w_list.c.n = p->cidx; push_wcigar(p, &(z->w_list), exz);

                    p->y_start = t_s + exz->ps;///difference
                    p->y_end = t_s + exz->pe;
                    p->error = exz->err;
                }
            }
        } 

        p->extra_begin = aux_beg;
        p->extra_end = aux_end;
        p->error_threshold = thres;
        z->align_length += ql; 
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, (%rsp)
movq %rcx, %rbp
movq %rdx, 0x18(%rsp)
movq %rsi, %r15
movq %rdi, 0x20(%rsp)
movq 0xa0(%rsp), %rbx
movq 0x98(%rsp), %r14
subq 0x90(%rsp), %r14
incq %r14
cmpq 0xa8(%rsp), %r14
jge 0x57424
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
pushq $0x1
popq %r9
cmovnel %ecx, %r9d
cmpq $0x4, %r14
cmovll %ecx, %r9d
movl %r9d, %edi
movl %r14d, %esi
callq 0x51fcd
movl %eax, %r13d
movslq %eax, %r12
leaq (%r14,%r12,2), %rcx
leaq 0x40(%rsp), %rax
leaq 0xa0(%rsp), %r10
leaq 0x8(%rsp), %r8
leaq 0x28(%rsp), %r9
movq %r12, %rdi
movq %rbx, %rsi
movq %rbp, 0x38(%rsp)
movq %rbp, %rdx
pushq %rax
pushq %r10
callq 0x1c7e7
popq %rcx
popq %rdx
xorl %ebp, %ebp
testl %eax, %eax
je 0x576b4
movq 0x40(%rsp), %rbx
leaq (%rbx,%r12), %rax
cmpq %r14, %rax
jl 0x576b4
addq 0x90(%rsp), %r15
movq 0xa0(%rsp), %rcx
movq 0x18(%rsp), %rax
movq %rcx, 0x10(%rsp)
leaq (%rax,%rcx), %rdi
cmpl $0x0, 0xb0(%rsp)
movq %r15, 0x30(%rsp)
je 0x574dd
movq (%rsp), %rax
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movl %ebx, %esi
movq %r15, %rdx
movq %rax, %r15
movl %r14d, %ecx
movl %r13d, %r8d
pushq %rax
callq 0x59275
popq %rax
popq %rcx
jmp 0x57504
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movl %ebx, %esi
movq %r15, %rdx
movl %r14d, %ecx
movl %r13d, %r8d
movq 0x8(%rsp), %r15
pushq %r15
callq 0x57947
popq %rax
popq %rcx
andl $0x0, 0x10(%r15)
movl 0x2c(%r15), %ecx
cmpl 0x28(%r15), %ecx
movq 0x20(%rsp), %r13
jg 0x576b4
leaq 0x48(%r13), %rbp
movq 0x48(%r13), %rdx
cmpq 0x50(%r13), %rdx
jne 0x57552
leaq (%rdx,%rdx), %rax
testq %rdx, %rdx
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x48(%r13), %rdx
movl 0x2c(%r15), %ecx
jmp 0x57556
movq 0x58(%r13), %rax
leaq 0x1(%rdx), %rsi
movq %rsi, (%rbp)
shlq $0x5, %rdx
movq 0x90(%rsp), %rsi
movl %esi, (%rax,%rdx)
movq 0x98(%rsp), %rsi
movl %esi, 0x4(%rax,%rdx)
movq 0x10(%r15), %xmm0
andq $0x0, 0x18(%rax,%rdx)
leaq (%rax,%rdx), %rsi
movd 0x10(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rax,%rdx)
movw %cx, 0x14(%rax,%rdx)
cmpl $0x0, 0xb0(%rsp)
je 0x57696
movq %rsi, (%rsp)
movq (%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x1e842
movq (%rsp), %rsi
movslq 0x14(%r15), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %rbx
je 0x575e0
cmpl $0x0, 0x10(%r15)
jne 0x57696
movslq 0x2c(%r15), %r10
testq %r10, %r10
jle 0x57696
movslq 0x10(%r15), %r13
movq %rbp, %r11
movl 0x88(%rsp), %ebp
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r14, %rcx
movq %rbx, %r8
movq %r12, %r9
leaq 0x28(%rsp), %rbx
pushq %rbx
leaq 0x10(%rsp), %rbx
pushq %rbx
leaq 0xb0(%rsp), %rbx
pushq %rbx
pushq %r15
movl 0xa0(%rsp), %ebx
pushq %rbx
pushq %rbp
movq %r11, %rbx
pushq %r10
pushq %rax
pushq %r13
movq 0x68(%rsp), %r13
pushq 0x58(%rsp)
callq 0x65b3f
movq 0x50(%rsp), %rsi
addq $0x50, %rsp
testl %eax, %eax
je 0x57696
movl 0x18(%rsi), %eax
movq %rax, 0x60(%r13)
movq (%rsp), %rdi
movq %rbx, %rsi
movq %r15, %rdx
callq 0x1e842
movq (%rsp), %rsi
movq 0x10(%r15), %xmm0
movd 0xa0(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rsi)
movzwl 0x2c(%r15), %eax
movw %ax, 0x14(%rsi)
pushq $0x1
popq %rbp
movzwl 0x8(%rsp), %eax
movw %ax, 0x10(%rsi)
movzwl 0x28(%rsp), %eax
movw %ax, 0x12(%rsi)
movw %r12w, 0x16(%rsi)
addl %r14d, 0x28(%r13)
movl %ebp, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq