// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movq %r9, -0x68(%rbp)
movsd %xmm1, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq $0x0, 0x48(%rax)
movq -0x38(%rbp), %rax
movb $0x0, 0x2c(%rax)
movq -0x38(%rbp), %rax
movl $0x0, 0x28(%rax)
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, %edi
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
movq -0x68(%rbp), %rdx
callq 0x340c0
movq %rax, -0x88(%rbp)
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rax
movl 0x4(%rax), %eax
cqto
idivq -0x68(%rbp)
movq %rax, %rsi
imulq -0x68(%rbp), %rsi
movq -0x68(%rbp), %rdx
leaq -0x78(%rbp), %rcx
leaq -0x80(%rbp), %r8
callq 0x9e0f0
movq $0x0, -0x90(%rbp)
movq -0x90(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x511a8
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xb0(%rbp)
movq -0x80(%rbp), %rax
addq $0x1, %rax
subq -0x78(%rbp), %rax
movq %rax, -0x98(%rbp)
cvtsi2sdq -0x98(%rbp), %xmm0
mulsd -0x60(%rbp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x50ed2
cmpq $0x4, -0x98(%rbp)
jl 0x50ed2
movl $0x1, %eax
movq %rax, -0xf0(%rbp)
jmp 0x50ee0
movq -0xc8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x1f, -0xc8(%rbp)
jle 0x50f03
movq $0x1f, -0xc8(%rbp)
movq -0x78(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x38(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0xc0(%rbp)
movq -0x78(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x38, %rsi
callq 0x331a0
addq -0xc0(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x98(%rbp), %rax
movq -0xc8(%rbp), %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xa8(%rbp)
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
movq -0xd0(%rbp), %rcx
leaq -0xb0(%rbp), %r8
leaq -0xb8(%rbp), %r9
leaq -0xc0(%rbp), %r10
leaq -0xd8(%rbp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x33f90
cmpl $0x0, %eax
je 0x51158
movq -0x48(%rbp), %rax
addq -0x78(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0x50(%rbp), %rdi
movq -0xc0(%rbp), %rsi
movq -0xd8(%rbp), %rdx
movq -0x38(%rbp), %rax
movl 0x1c(%rax), %eax
movq -0x40(%rbp), %r8
movq -0x38(%rbp), %rcx
movl 0x10(%rcx), %ecx
movl %ecx, %r9d
movzbl %al, %ecx
callq 0x9a30
movq -0x50(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xe8(%rbp), %rdi
movq -0xa0(%rbp), %rax
movl %eax, %esi
movq -0xe0(%rbp), %rdx
movq -0x98(%rbp), %rax
movl %eax, %ecx
movq -0xc8(%rbp), %rax
movl %eax, %r8d
movq -0xb0(%rbp), %rax
movl %eax, %r9d
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
callq 0xa8960
movq -0x58(%rbp), %rax
movl 0x2c(%rax), %eax
movq -0x58(%rbp), %rcx
cmpl 0x28(%rcx), %eax
jg 0x51156
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %r8
movq -0x50(%rbp), %r9
movq -0x58(%rbp), %rax
movq -0x78(%rbp), %rsi
movq %rsi, -0x108(%rbp)
movq -0x80(%rbp), %rsi
movq %rsi, -0x100(%rbp)
movq -0xc0(%rbp), %r13
movq -0xc0(%rbp), %r12
movq -0x58(%rbp), %rsi
movslq 0x14(%rsi), %rsi
addq %rsi, %r12
movq -0xa8(%rbp), %r15
movq -0xb0(%rbp), %r14
movq -0xb8(%rbp), %rbx
movsd -0x60(%rbp), %xmm0
movq -0x68(%rbp), %r11
movsd -0x70(%rbp), %xmm1
movq 0x10(%rbp), %r10
movq 0x18(%rbp), %rsi
movq %rsi, -0xf8(%rbp)
xorl %esi, %esi
movq %rsi, %rdi
movq %rax, (%rsp)
movq -0x108(%rbp), %rax
movl $0x1f, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq -0x100(%rbp), %rax
movq %rax, 0x18(%rsp)
movq -0xf8(%rbp), %rax
movq %r13, 0x20(%rsp)
movq %r12, 0x28(%rsp)
movq %r15, 0x30(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
movq %r11, 0x48(%rsp)
movq %r10, 0x50(%rsp)
movq %rax, 0x58(%rsp)
callq 0x4f880
cmpl $0x0, %eax
jne 0x51154
movl $0x0, -0x2c(%rbp)
jmp 0x511f3
jmp 0x51156
jmp 0x51158
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
addq -0x68(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x8(%rcx), %ecx
cmpq %rcx, %rax
jl 0x5118f
movq -0x38(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x80(%rbp)
jmp 0x51191
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x50e59
cmpq $0x0, 0x10(%rbp)
jne 0x511ec
movq -0x38(%rbp), %rax
cmpl $0x0, 0x28(%rax)
jbe 0x511e3
movq -0x38(%rbp), %rax
movl 0x4(%rax), %edx
movl 0x8(%rax), %ecx
subl %edx, %ecx
incl %ecx
cvtsi2sd %rcx, %xmm1
movsd -0x70(%rbp), %xmm0
mulsd %xmm0, %xmm1
movl 0x28(%rax), %eax
cvtsi2sd %rax, %xmm0
ucomisd %xmm1, %xmm0
jae 0x511ec
movl $0x0, -0x2c(%rbp)
jmp 0x511f3
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)