// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: align_hc_ed_post_non_retrieve_extz(overlap_region*, char*, long, char*, long, bit_extz_t*, double, long, double, long, void*)
uint32_t align_hc_ed_post_non_retrieve_extz(overlap_region *z, char* qstr, int64_t ql, char *tstr, int64_t tl, bit_extz_t *exz, double e_rate, int64_t w_l, double ovlp_cut, int64_t force_aln, void *km)
{
    int64_t q_s, q_e, nw, k, q_l, t_l, aux_beg, aux_end, t_s, thre, aln_l, t_pri_l;
    char *q_string, *t_string; 
    z->w_list.n = 0; z->is_match = 0; z->align_length = 0;
    nw = get_num_wins(z->x_pos_s, z->x_pos_e+1, w_l);
    get_win_se_by_normalize_xs(z, (z->x_pos_s/w_l)*w_l, w_l, &q_s, &q_e);
    for (k = 0; k < nw; k++) {
        aux_beg = aux_end = 0; q_l = 1 + q_e - q_s;
        thre = q_l*e_rate; thre = Adjust_Threshold(thre, q_l);
        if(thre > THRESHOLD_MAX_SIZE) thre = THRESHOLD_MAX_SIZE;
        ///offset of y
        t_s = (q_s - z->x_pos_s) + z->y_pos_s;
        t_s += y_start_offset(q_s, &(z->f_cigar));

        aln_l = q_l + (thre<<1); 
        if(init_waln(thre, t_s, tl, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) {
            q_string = qstr+q_s; t_string = tstr+t_s; t_l = t_pri_l;

            ed_band_cal_semi_64_w_absent_diag(t_string, t_l, q_string, q_l, thre, aux_beg, exz);

            // if(z->x_id == 5569 && z->y_id == 5557 && q_s == 10075 && q_e == 10849) {
            //     fprintf(stderr, "\n[M::%s::semi::t_s->%ld::t_pri_l->%ld::aux_beg->%ld::aux_end->%ld::thre->%ld] exz->ps::%d, exz->pe::%d, exz->ts::%d, exz->te::%d, exz->err::%d, exz->cigar.n::%d, thre::%ld\n", 
            //     __func__, t_s, t_pri_l, aux_beg, aux_end, thre, exz->ps, exz->pe, exz->ts, exz->te, exz->err, (int32_t)exz->cigar.n, thre);
            //     fprintf(stderr, "[tstr::len->%ld] %.*s\n", t_l, (int32_t)t_l, t_string);
            //     fprintf(stderr, "[qstr::len->%ld] %.*s\n", q_l, (int32_t)q_l, q_string);
            // } 
            if (is_align(*exz)) {
                // ed_band_cal_semi_64_w(t_string, aln_l, q_string, q_l, thre, exz);
                // assert(exz->err <= exz->thre);
                // fprintf(stderr, "[M::%s] exz->err::%d\n", __func__, exz->err);
                ///t_s do not have aux_beg, while t_s + t_end (aka, te) has 
                if(!push_hc_wlst_non_retrieve_exz(z, qstr, ql, tstr, tl, exz, THRESHOLD_MAX_SIZE, q_s, q_e, t_s, t_s + exz->pe, 
                                        aux_beg, aux_end, e_rate, w_l, ovlp_cut, force_aln, km)) {
                    return 0;
                }
                // append_window_list(z, q_s, q_e, t_s, t_s + t_end, error, aux_beg, aux_end, thre, w_l, km);
            }
        }
        q_s = q_e + 1; q_e = q_s + w_l - 1; 
        if(q_e >= (int64_t)z->x_pos_e) q_e = z->x_pos_e;
    }

    // if((!force_aln) && (!simi_pass(z->x_pos_e+1-z->x_pos_s, z->align_length, 0, ovlp_cut, &e_rate)) &&
    //     (!simi_pass(z->x_pos_e+1-z->x_pos_s, z->align_length, sec_check, ovlp_cut, NULL))) return 0;
    if((!force_aln) && (!pass_qovlp(z->x_pos_e+1-z->x_pos_s, z->align_length, ovlp_cut))) return 0;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, (%rsp)
movq %rcx, 0x18(%rsp)
movq %rsi, 0x10(%rsp)
movq 0x90(%rsp), %r8
movq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
movl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %r14d
movl 0x4(%rdi), %ecx
leal 0x1(%r14), %esi
movq %rcx, %rax
xorl %edx, %edx
idivq %r8
subq %rcx, %rdx
addq %rdx, %rsi
movq %rsi, %rax
cqto
idivq %r8
xorl %ebx, %ebx
testq %rdx, %rdx
setg %bl
addq %rax, %rbx
jle 0x5c258
movq %rcx, %rax
xorl %edx, %edx
movq 0x90(%rsp), %rsi
idivq %rsi
movq %rcx, %r12
subq %rdx, %r12
leaq (%rsi,%r12), %rax
decq %rax
cmpq %r14, %rax
cmovlq %rax, %r14
cmpq %rcx, %r12
cmovleq %rcx, %r12
leaq 0x38(%rdi), %rax
movq %rax, 0x48(%rsp)
movq (%rsp), %rax
addq $0x1f, %rax
movq %rax, 0x38(%rsp)
movq %rdi, 0x28(%rsp)
leaq 0x1(%r14), %r13
movq %r13, 0x50(%rsp)
subq %r12, %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x30(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
cmpq $0x1f, %rax
jl 0x5c0b4
movl $0x1f, %ecx
cmpq $0x4, %r13
movq %rcx, %rbp
movl $0x1, %edx
cmovgeq %rdx, %rbp
testq %rax, %rax
cmovneq %rcx, %rbp
movl 0x4(%rdi), %eax
movl 0x14(%rdi), %r15d
movq %r12, %rcx
subq %rax, %rcx
addq %rcx, %r15
movq %r12, %rdi
movq 0x48(%rsp), %rsi
callq 0x48df6
addq %r15, %rax
sets %cl
cmpq (%rsp), %rax
setge %sil
movb $0x1, %dl
movq $-0x1, %r9
orb %cl, %sil
jne 0x5c15d
leaq (,%rbp,2), %rdi
addq 0x38(%rsp), %rdi
leaq (,%rbp,2), %rcx
addq %r13, %rcx
subq %rax, %rdi
movq $-0x1, %r15
movq $-0x1, %r11
movq $-0x1, %rsi
cmpq %rcx, %rdi
jl 0x5c185
subq %rbp, %rax
movq (%rsp), %rsi
subq %rax, %rsi
cmpq %rcx, %rsi
cmovgeq %rcx, %rsi
subq %rsi, %rcx
testq %rax, %rax
js 0x5c174
xorl %r9d, %r9d
movq %rcx, %r15
movq %rax, %r11
jmp 0x5c183
movq $-0x1, %r15
movq $-0x1, %r11
movq $-0x1, %rsi
jmp 0x5c185
addq %rax, %rsi
negq %rax
xorl %r11d, %r11d
movq %rax, %r9
movq %rcx, %r15
xorl %edx, %edx
testb %dl, %dl
jne 0x5c233
movq 0x10(%rsp), %rax
leaq (%rax,%r12), %rdx
movq 0x18(%rsp), %rax
leaq (%rax,%r11), %rdi
subq $0x8, %rsp
movl %r13d, %ecx
movl %ebp, %r8d
movq 0x28(%rsp), %r13
pushq %r13
movq %r9, %rbp
movq %r12, 0x50(%rsp)
movq %rbx, %r12
movq %r11, %rbx
callq 0x8c179
movq %rbx, %r11
movq %r12, %rbx
movq 0x50(%rsp), %r12
addq $0x10, %rsp
movl 0x2c(%r13), %eax
cmpl 0x28(%r13), %eax
jg 0x5c233
movq 0x20(%rsp), %r9
movslq 0x14(%r9), %rax
addq %r11, %rax
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rcx
movq 0x8(%rsp), %r8
movsd 0x38(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
pushq 0xa0(%rsp)
pushq 0xa0(%rsp)
pushq %r15
pushq %rbp
pushq %rax
pushq %r11
pushq %r14
pushq %r12
pushq $0x1f
callq 0x5b43d
addq $0x50, %rsp
testl %eax, %eax
je 0x5c2a8
addq 0x90(%rsp), %r14
movq 0x28(%rsp), %rdi
movl 0x8(%rdi), %eax
cmpq %rax, %r14
cmovgeq %rax, %r14
decq %rbx
movq 0x50(%rsp), %r12
jne 0x5c087
cmpq $0x0, 0x98(%rsp)
je 0x5c277
movl $0x1, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rdi), %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x5c268
movl 0x8(%rdi), %edx
subl 0x4(%rdi), %edx
incl %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd 0x8(%rsp), %xmm0
movl %ecx, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
ucomisd %xmm0, %xmm1
jae 0x5c263
jmp 0x5c268
xorl %eax, %eax
jmp 0x5c268