// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x38(%rsp)
movq %r9, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq 0xb0(%rsp), %r9
movq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
movl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %r15d
movl 0x4(%rdi), %ecx
leal 0x1(%r15), %edi
movq %rcx, %rax
xorl %edx, %edx
idivq %r9
movq %rdx, %rsi
movq %rdx, %rax
subq %rcx, %rax
addq %rdi, %rax
cqto
idivq %r9
xorl %edi, %edi
testq %rdx, %rdx
setg %dil
addq %rax, %rdi
movq %rdi, 0x60(%rsp)
jle 0x5b942
movq %rcx, %r12
subq %rsi, %r12
leaq (%r9,%r12), %rax
decq %rax
cmpq %r15, %rax
cmovlq %rax, %r15
cmpq %rcx, %r12
cmovleq %rcx, %r12
leaq 0x38(%rbx), %rbp
leaq 0x1f(%r8), %rax
movq %rax, 0x58(%rsp)
xorl %r14d, %r14d
movq %r8, 0x30(%rsp)
movq %r14, 0x70(%rsp)
leaq 0x1(%r15), %rax
movq %rax, 0x68(%rsp)
movq %rax, %rdx
subq %r12, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
movq %rdx, (%rsp)
cmpq $0x4, %rdx
movq %rcx, %rsi
movl $0x1, %edx
cmovgeq %rdx, %rsi
testq %rax, %rax
cmovneq %rcx, %rsi
movq %rsi, 0x40(%rsp)
movl 0x4(%rbx), %r13d
movl 0x14(%rbx), %eax
movq %rax, 0x8(%rsp)
movl 0x40(%rbx), %esi
decl %esi
movq %rbp, %rdi
callq 0x3d110
cltq
movl 0x40(%rbx), %r14d
cmpq %rax, %r12
je 0x5b7cd
testl %r14d, %r14d
je 0x5b995
xorl %r14d, %r14d
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3d110
cltq
cmpq %rax, %r12
jl 0x5b7b8
incq %r14
movl 0x40(%rbx), %eax
cmpq %rax, %r14
jb 0x5b799
jmp 0x5b7c4
testq %r14, %r14
je 0x5b995
movl 0x40(%rbx), %eax
cmpq %rax, %r14
je 0x5b995
movq %r12, %rax
subq %r13, %rax
movq 0x8(%rsp), %r13
addq %rax, %r13
decl %r14d
movq %rbp, %rdi
movl %r14d, %esi
callq 0x3d11b
movslq %eax, %rsi
addq %r13, %rsi
sets %al
movq 0x30(%rsp), %r8
cmpq %r8, %rsi
setge %cl
orb %al, %cl
movq 0x70(%rsp), %r14
jne 0x5b917
movq 0x40(%rsp), %rdx
leaq (%rdx,%rdx), %rax
addq 0x58(%rsp), %rax
movq (%rsp), %rcx
leaq (%rcx,%rdx,2), %rcx
subq %rsi, %rax
cmpq %rcx, %rax
jl 0x5b917
movq 0x40(%rsp), %r10
subq %r10, %rsi
subq %rsi, %r8
cmpq %rcx, %r8
cmovgeq %rcx, %r8
movq %rsi, %rax
negq %rax
movq %rsi, %r9
sarq $0x3f, %r9
andq %rax, %r9
testq %rsi, %rsi
movl $0x0, %edi
cmovgq %rsi, %rdi
movl $0x0, %eax
cmovnsq %rax, %rsi
movq %r8, 0x48(%rsp)
addl %r8d, %esi
movq 0x18(%rsp), %rax
movq %rcx, 0x8(%rsp)
leaq (%rax,%r12), %rdx
movq 0x20(%rsp), %rax
movq %rdi, 0x50(%rsp)
addq %rax, %rdi
subq $0x8, %rsp
movq 0x8(%rsp), %rcx
movl %r10d, %r8d
movq %r9, 0x8(%rsp)
movq 0x30(%rsp), %r13
pushq %r13
callq 0x8b419
movq 0x18(%rsp), %rdx
addq $0x10, %rsp
movl 0x2c(%r13), %eax
cmpl 0x28(%r13), %eax
jg 0x5b917
subq 0x48(%rsp), %rdx
movq 0x28(%rsp), %r9
movslq 0x14(%r9), %rax
movq 0x50(%rsp), %r10
addq %r10, %rax
subq $0x8, %rsp
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rcx
movq 0x38(%rsp), %r8
movsd 0x40(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
pushq 0xc0(%rsp)
pushq 0xc0(%rsp)
pushq %rdx
pushq 0x20(%rsp)
pushq %rax
pushq %r10
pushq %r15
pushq %r12
pushq $0x1f
callq 0x5a9e8
addq $0x50, %rsp
testl %eax, %eax
je 0x5b991
movq 0xb0(%rsp), %rax
addq %rax, %r15
movl 0x8(%rbx), %eax
cmpq %rax, %r15
cmovgeq %rax, %r15
incq %r14
cmpq 0x60(%rsp), %r14
movq 0x68(%rsp), %r12
jne 0x5b715
movl %eax, %r15d
cmpq $0x0, 0xb8(%rsp)
je 0x5b961
movl $0x1, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rbx), %ecx
xorl %eax, %eax
testl %ecx, %ecx
je 0x5b952
subl 0x4(%rbx), %r15d
incl %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
mulsd 0x10(%rsp), %xmm0
movl %ecx, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
ucomisd %xmm0, %xmm1
jae 0x5b94d
jmp 0x5b952
xorl %eax, %eax
jmp 0x5b952
callq 0x5e33