// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: align_hc_ed_post_extz(overlap_region*, All_reads*, char*, char*, bit_extz_t*, double, long, double, long, void*)
uint32_t align_hc_ed_post_extz(overlap_region *z, All_reads *rref, char* qstr, char *tstr, 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, t_tot_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);
    // if(z->x_id == 19350 && z->y_id == 19324) {
    //     fprintf(stderr, "-z-[M::%s] tid::%u(%c)\tq::[%u,%u)\tt::[%u,%u)\n", __func__, z->y_id, "+-"[z->y_pos_strand], z->x_pos_s, z->x_pos_e+1, z->y_pos_s, z->y_pos_e+1);
    // }
    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); t_tot_l = Get_READ_LENGTH((*rref), z->y_id); 
        if(init_waln(thre, t_s, t_tot_l, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) {
            q_string = qstr+q_s; 
            recover_UC_Read_sub_region(tstr, t_s, t_pri_l, z->y_pos_strand, rref, z->y_id); t_string = tstr;
            // t_string = return_str_seq_exz(tstr, t_s, t_pri_l, z->y_pos_strand, hpc_g, uref, z->y_id);
            
            t_l = t_pri_l;
            // t_end = Reserve_Banded_BPM(t_string, aln_l, q_string, q_l, thre, &error);
            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);
                // if(z->x_id == 19350 && z->y_id == 19324) {
                //     fprintf(stderr, "+[M::%s]\tq::[%ld,%ld)\tt::[%ld,%ld)\texz->err::%d\n", __func__, q_s, q_e + 1, t_s, t_s + exz->pe + 1, exz->err);
                // }
                ///t_s do not have aux_beg, while t_s + t_end (aka, te) has 
                if(!push_hc_wlst_exz(NULL, NULL, rref, z, qstr, tstr, exz, THRESHOLD_MAX_SIZE, q_s, q_e, t_s, t_s + exz->pe, 
                                        t_tot_l, 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);
            } 
            // else {
                // if(z->x_id == 19350 && z->y_id == 19324) {
                //     fprintf(stderr, "-[M::%s]\tq::[%ld,%ld)\tt::[%ld,%ld)\texz->err::%d\n", __func__, q_s, q_e + 1, t_s, t_s + exz->pe + 1, exz->err);
                // }
            // }
        }
        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 $0x88, %rsp
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %rcx
movq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
movl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %r15d
movl 0x4(%rdi), %edi
leal 0x1(%r15), %esi
movq %rdi, %rax
xorl %edx, %edx
idivq %r9
subq %rdi, %rdx
addq %rdx, %rsi
movq %rsi, %rax
cqto
movq %r9, 0x10(%rsp)
idivq %r9
xorl %r12d, %r12d
testq %rdx, %rdx
setg %r12b
addq %rax, %r12
jle 0x5bf6f
movq %rdi, %rax
xorl %edx, %edx
movq 0x10(%rsp), %rsi
idivq %rsi
movq %rdi, %r8
subq %rdx, %r8
leaq (%rsi,%r8), %rax
decq %rax
cmpq %r15, %rax
cmovlq %rax, %r15
cmpq %rdi, %r8
cmovleq %rdi, %r8
leaq 0x38(%rcx), %rax
movq %rax, 0x70(%rsp)
movq %rcx, 0x18(%rsp)
leaq 0x1(%r15), %r13
movq %r13, 0x80(%rsp)
subq %r8, %r13
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
mulsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rdx
cmpq $0x1f, %rax
jl 0x5bd6f
movl $0x1f, %edx
cmpq $0x4, %r13
movq %rdx, %r14
movl $0x1, %esi
cmovgeq %rsi, %r14
testq %rax, %rax
cmovneq %rdx, %r14
movl 0x4(%rcx), %eax
movl 0x14(%rcx), %ebx
movq %r8, %rdx
subq %rax, %rdx
addq %rdx, %rbx
movq %r8, 0x50(%rsp)
movq %r8, %rdi
movq 0x70(%rsp), %rsi
callq 0x48df6
movq 0x18(%rsp), %rcx
addq %rbx, %rax
movq 0x20(%rsp), %rdx
movq 0x18(%rdx), %rdx
movl 0x10(%rcx), %r9d
movq (%rdx,%r9,8), %r10
movb $0x1, %dil
movq $-0x1, 0x8(%rsp)
js 0x5be30
cmpq %r10, %rax
jge 0x5be30
leaq (%r14,%r14), %rsi
leaq (,%r14,2), %rdx
addq %r13, %rdx
subq %rax, %rsi
leaq (%rsi,%r10), %r8
addq $0x1f, %r8
movq $-0x1, %r11
movq $-0x1, %rsi
movq $-0x1, %rbp
cmpq %rdx, %r8
jl 0x5be59
subq %r14, %rax
movq %r10, %rbp
subq %rax, %rbp
cmpq %rdx, %rbp
cmovgeq %rdx, %rbp
subq %rbp, %rdx
testq %rax, %rax
js 0x5be47
movq $0x0, 0x8(%rsp)
movq %rdx, %r11
movq %rax, %rsi
jmp 0x5be57
movq $-0x1, %r11
movq $-0x1, %rsi
movq $-0x1, %rbp
jmp 0x5be59
addq %rax, %rbp
negq %rax
xorl %esi, %esi
movq %rax, 0x8(%rsp)
movq %rdx, %r11
xorl %edi, %edi
movq %r11, 0x78(%rsp)
testb %dil, %dil
jne 0x5bf4f
movq 0x30(%rsp), %rax
movq 0x50(%rsp), %rdx
addq %rdx, %rax
movq %rax, 0x58(%rsp)
movzbl 0x1c(%rcx), %ecx
movq %r12, %rbx
movq 0x38(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rdx
movq %rsi, 0x60(%rsp)
movq 0x20(%rsp), %r8
movq %r10, 0x68(%rsp)
callq 0x337f7
subq $0x8, %rsp
movq %r12, %rdi
movq %rbx, %r12
movl %ebp, %esi
movq 0x60(%rsp), %rdx
movl %r13d, %ecx
movl %r14d, %r8d
movq 0x10(%rsp), %r9
movq 0x48(%rsp), %rbx
pushq %rbx
callq 0x8c179
movq 0x70(%rsp), %r8
movq 0x78(%rsp), %r11
movq 0x28(%rsp), %rcx
addq $0x10, %rsp
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
jg 0x5bf4f
movq 0x40(%rsp), %r10
movslq 0x14(%r10), %rax
addq %r8, %rax
subq $0x8, %rsp
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movq 0x28(%rsp), %rdx
movq %r8, %rbx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
movsd 0x50(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
pushq 0xc8(%rsp)
pushq 0x20(%rsp)
pushq 0x90(%rsp)
pushq 0x28(%rsp)
pushq %r11
pushq %rax
pushq %rbx
pushq %r15
pushq 0x98(%rsp)
pushq $0x1f
pushq %r10
callq 0x5b01f
movq 0x78(%rsp), %rcx
addq $0x60, %rsp
testl %eax, %eax
je 0x5bf80
addq 0x10(%rsp), %r15
movl 0x8(%rcx), %eax
cmpq %rax, %r15
cmovgeq %rax, %r15
decq %r12
movq 0x80(%rsp), %r8
jne 0x5bd3f
cmpq $0x0, 0xc0(%rsp)
je 0x5bf95
movl $0x1, %r14d
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rcx), %eax
xorl %r14d, %r14d
testl %eax, %eax
je 0x5bf80
movl 0x8(%rcx), %edx
subl 0x4(%rcx), %edx
incl %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd 0x28(%rsp), %xmm0
movl %eax, %eax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
jae 0x5bf7a
jmp 0x5bf80