// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: align_ul_ed_post(overlap_region*, ul_idx_t const*, hpc_t*, char*, char*, char*, Correct_dumy*, double, long, double, void*)
uint32_t align_ul_ed_post(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, char* qstr, char *tstr, char *tstr_1, 
Correct_dumy* dumy, double e_rate, int64_t w_l, double ovlp_cut, void *km)
{
    int64_t q_s, q_e, nw, k, q_l, t_tot_l, sec_check = (uref&&(!hpc_g))?1:0;
    int64_t aux_beg, aux_end, t_s, thre, aln_l, t_pri_l, t_end;
    char *q_string, *t_string; unsigned int error;
    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); t_tot_l = hpc_g?hpc_len(*hpc_g, z->y_id):uref->ug->u.a[z->y_id].len;
        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; 
            t_string = return_str_seq(tstr, t_s, t_pri_l, z->y_pos_strand, hpc_g, uref, z->y_id, aux_beg, aux_end);

            t_end = Reserve_Banded_BPM(t_string, aln_l, q_string, q_l, thre, &error);
            // int32_t debug_t_end, debug_error;
            // debug_t_end = ed_band_cal_semi(t_string, aln_l, q_string, q_l, thre, &debug_error);
            // if((t_end != debug_t_end) || (t_end >= 0 && debug_t_end >= 0 && debug_error != (int32_t)error)) {
            //     fprintf(stderr, "[M::%s] debug_error->%d, error->%d\n", __func__, debug_error, error);
            // }
            

            if (error!=((unsigned int)-1)) {
                ///t_s do not have aux_beg, while t_s + t_end (aka, te) has 
                if(!push_wlst(uref, hpc_g, NULL, z, qstr, tstr, tstr_1, dumy, q_s, q_e, t_s, t_s + t_end, 
                                        t_tot_l, error, aux_beg, aux_end, thre, e_rate, w_l, sec_check, ovlp_cut, 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((!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;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r9, 0x68(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %rdx, %rbp
movq %rdi, %r15
movq 0xe8(%rsp), %r9
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
sete %cl
andb %al, %cl
movzbl %cl, %eax
movq %rax, 0x30(%rsp)
andq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
andl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %edi
movl 0x4(%r15), %esi
incl %edi
xorl %ebx, %ebx
movq %rsi, %rax
xorl %edx, %edx
idivq %r9
movq %rdx, %rcx
movq %rdx, %rax
subq %rsi, %rax
addq %rdi, %rax
cqto
idivq %r9
xorl %r14d, %r14d
testq %rdx, %rdx
setg %r14b
addq %rax, %r14
subq %rcx, %rsi
leaq 0x8(%rsp), %rcx
movq %rsp, %r8
movq %r15, %rdi
movq %r9, %rdx
callq 0x51f30
leaq 0x38(%r15), %rax
movq %rax, 0x80(%rsp)
testq %r14, %r14
cmovleq %rbx, %r14
movq %rbp, 0x20(%rsp)
subq $0x1, %r14
jb 0x2a991
movq %r14, 0x98(%rsp)
andq $0x0, 0x58(%rsp)
andq $0x0, 0x60(%rsp)
movq (%rsp), %rbx
movq 0x8(%rsp), %rdi
subq %rdi, %rbx
leaq 0x1(%rbx), %rdx
cvtsi2sd %rdx, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
cmpq $0x1f, %rax
jl 0x2a792
pushq $0x1f
popq %rcx
movq %rdx, 0x90(%rsp)
cmpq $0x4, %rdx
movq %rcx, %r12
pushq $0x1
popq %rdx
cmovgeq %rdx, %r12
testq %rax, %rax
cmovneq %rcx, %r12
movl 0x4(%r15), %eax
movl 0x14(%r15), %r14d
movq %rdi, %rcx
subq %rax, %rcx
addq %rcx, %r14
movq 0x80(%rsp), %rsi
callq 0x1bebf
addq %r14, %rax
movq %rax, 0x50(%rsp)
movq %r15, %r14
movl 0x10(%r15), %r13d
testq %rbp, %rbp
je 0x2a7fa
movq (%rbp), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %r13, %rdx
movl (%rcx,%rdx), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x2a814
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
imulq $0x28, %r13, %rdx
movl (%rcx,%rdx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
leaq (%rbx,%r12,2), %rbp
incq %rbp
movl %ecx, %edx
movq %r12, %rdi
movq %rax, %rsi
movq %rdx, 0x88(%rsp)
movq %rbp, %rcx
leaq 0x60(%rsp), %r8
leaq 0x58(%rsp), %r9
leaq 0xa0(%rsp), %rax
pushq %rax
leaq 0x58(%rsp), %rax
pushq %rax
callq 0x1c7e7
popq %rcx
popq %rdx
testl %eax, %eax
movq %r14, %r15
je 0x2a95b
movq 0x8(%rsp), %r14
addq 0x40(%rsp), %r14
movq 0x50(%rsp), %rbx
movq 0xa0(%rsp), %rdx
movzbl 0x1c(%r15), %ecx
movq 0x60(%rsp), %rax
movq 0x58(%rsp), %r10
subq $0x8, %rsp
movq 0x50(%rsp), %rdi
movq %rbx, %rsi
movq 0x28(%rsp), %r8
movq 0x18(%rsp), %r9
movq %r10, 0x78(%rsp)
pushq %r10
movq %rax, 0x88(%rsp)
pushq %rax
pushq %r13
callq 0x56ba5
addq $0x20, %rsp
movzwl %r12w, %r8d
movq %rax, %rdi
movl %ebp, %esi
movq %r14, %rdx
movq 0x90(%rsp), %rcx
leaq 0x2c(%rsp), %r9
callq 0x516e4
movl 0x2c(%rsp), %r10d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r10
je 0x2a95b
cltq
addq %rbx, %rax
subq $0x8, %rsp
xorl %r13d, %r13d
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rsi
xorl %edx, %edx
movq %r15, %rcx
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %r9
movsd 0x20(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
pushq 0x38(%rsp)
pushq 0xf8(%rsp)
pushq %r12
pushq 0x90(%rsp)
pushq 0xa0(%rsp)
pushq %r10
pushq 0xc0(%rsp)
pushq %rax
pushq %rbx
pushq 0x50(%rsp)
pushq 0x60(%rsp)
pushq 0x140(%rsp)
pushq 0xd0(%rsp)
callq 0x2a20c
addq $0x70, %rsp
testl %eax, %eax
je 0x2a9eb
movq (%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
addq 0xe8(%rsp), %rax
movl 0x8(%r15), %ecx
cmpq %rcx, %rax
cmovgeq %rcx, %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rbp
movq 0x98(%rsp), %r14
jmp 0x2a748
movl 0x8(%r15), %edi
movl 0x28(%r15), %esi
incl %edi
testq %rsi, %rsi
sete %al
subl 0x4(%r15), %edi
sete %cl
orb %al, %cl
jne 0x2a9ce
cvtsi2sd %rdi, %xmm0
movsd 0x1b88e7(%rip), %xmm1   # 0x1e32a0
subsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
cvtsi2sd %rsi, %xmm0
ucomisd %xmm1, %xmm0
jae 0x2a9e7
xorl %r13d, %r13d
movq 0x30(%rsp), %rdx
movsd 0x38(%rsp), %xmm0
xorl %ecx, %ecx
callq 0x56b28
testl %eax, %eax
je 0x2a9eb
pushq $0x1
popq %r13
movl %r13d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq