// File: /workspace/github_cpp_makefile_star2_O1/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, 0x50(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r9, 0x70(%rsp)
movq %r8, 0x10(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, %r8
movq %rdi, %r13
movq 0xe8(%rsp), %rdi
movq %rsi, 0x38(%rsp)
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
sete %cl
andb %al, %cl
movzbl %cl, %eax
movq %rax, 0x58(%rsp)
movq $0x0, 0x48(%r13)
movb $0x0, 0x2c(%r13)
movl $0x0, 0x28(%r13)
movl 0x8(%r13), %ebx
movl 0x4(%r13), %ecx
leal 0x1(%rbx), %esi
movq %rcx, %rax
xorl %edx, %edx
idivq %rdi
subq %rcx, %rdx
addq %rdx, %rsi
movq %rsi, %rax
cqto
idivq %rdi
xorl %edi, %edi
testq %rdx, %rdx
setg %dil
addq %rax, %rdi
jle 0x5aacc
movq %rcx, %rax
xorl %edx, %edx
movq 0xe8(%rsp), %rsi
idivq %rsi
movq %rcx, %r9
subq %rdx, %r9
leaq (%rsi,%r9), %rax
decq %rax
cmpq %rbx, %rax
cmovlq %rax, %rbx
cmpq %rcx, %r9
cmovleq %rcx, %r9
leaq 0x38(%r13), %rax
movq %rax, 0x88(%rsp)
movq %r13, 0x78(%rsp)
movq %r8, 0x68(%rsp)
movq %rdi, 0xa0(%rsp)
movq %r9, %rdi
leaq 0x1(%rbx), %rbp
movq %rbp, 0x98(%rsp)
subq %r9, %rbp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rcx
cmpq $0x1f, %rax
jl 0x5a7a3
movl $0x1f, %ecx
cmpq $0x4, %rbp
movq %rcx, %r12
movl $0x1, %edx
cmovgeq %rdx, %r12
testq %rax, %rax
cmovneq %rcx, %r12
movl 0x4(%r13), %r14d
movl 0x14(%r13), %r15d
movq %rdi, 0x28(%rsp)
movq 0x88(%rsp), %rsi
callq 0x48df6
movq 0x68(%rsp), %r8
testq %r8, %r8
je 0x5a7fa
movq (%r8), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%r13), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x5a818
movq 0x38(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%r13), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
movq 0x28(%rsp), %rsi
movq %rsi, %rdx
subq %r14, %rdx
addq %rax, %r15
addq %rdx, %r15
leaq (,%r12,2), %r10
addq %rbp, %r10
movl %ecx, %edi
movb $0x1, %al
movq $-0x1, %rdx
cmpq %rdi, %r15
jae 0x5a892
leaq (%r12,%r12), %rcx
subq %r15, %rcx
addq %rdi, %rcx
addq $0x1f, %rcx
movq $-0x1, %r9
movq $-0x1, 0x18(%rsp)
movq $-0x1, %r14
cmpq %r10, %rcx
jl 0x5a8bf
subq %r12, %r15
movq %rdi, %r14
subq %r15, %r14
cmpq %r10, %r14
cmovgeq %r10, %r14
movq %r10, %r9
subq %r14, %r9
testq %r15, %r15
js 0x5a8ab
xorl %edx, %edx
movq %r15, 0x18(%rsp)
jmp 0x5a8bd
movq $-0x1, %r9
movq $-0x1, 0x18(%rsp)
movq $-0x1, %r14
jmp 0x5a8bf
addq %r15, %r14
negq %r15
movq $0x0, 0x18(%rsp)
movq %r15, %rdx
xorl %eax, %eax
movq %r9, 0x20(%rsp)
movq %rdi, 0x90(%rsp)
testb %al, %al
jne 0x5aaa0
movb 0x1c(%r13), %dil
movl 0x10(%r13), %r13d
testq %r8, %r8
movq %rdx, 0x80(%rsp)
je 0x5a913
movq (%r8), %rax
movq 0x10(%rax), %rax
leaq (,%r13,4), %rcx
addq %r13, %rcx
testb %dil, %dil
je 0x5a975
leaq (%rax,%rcx,8), %rdi
movl (%rdi), %r13d
shrl %r13d
andl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
jmp 0x5a978
movb %dil, 0xf(%rsp)
movq 0x10(%rsp), %rdi
movl $0x4e, %esi
movq %rdx, %r15
movq %r10, 0x30(%rsp)
callq 0x20e0
movq 0x10(%rsp), %rax
addq %rax, %r15
movq 0x38(%rsp), %rax
movq (%rax), %rax
leaq (,%r13,4), %rdx
addq %r13, %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl 0x17(%rsp), %ecx
xorl %edi, %edi
movq %r15, %rsi
movq 0x20(%rsp), %r8
movq %r14, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
jmp 0x5a9b9
xorl %r13d, %r13d
movq 0x20(%rsp), %rdi
addq 0x20(%rax,%rcx,8), %r13
orq %rdx, %rdi
je 0x5a9df
movq 0x10(%rsp), %rdi
movl $0x4e, %esi
movq %rdx, %r15
movq %r10, 0x30(%rsp)
callq 0x20e0
movq 0x10(%rsp), %rax
addq %rax, %r15
addq 0x18(%rsp), %r13
movq %r15, %rdi
movq %r13, %rsi
movq %r14, %rdx
callq 0x2160
addq %r14, %r15
movq %r15, %rdi
movl $0x4e, %esi
movq 0x20(%rsp), %rdx
callq 0x20e0
movq 0x30(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x10(%rsp), %r13
jmp 0x5a9e4
addq 0x18(%rsp), %r13
movq 0x60(%rsp), %rax
leaq (%rax,%rsi), %rdx
movzwl %r12w, %r8d
movq %r13, %rdi
movl %r10d, %esi
movl %ebp, %ecx
leaq 0x4c(%rsp), %r9
callq 0x8578f
movq 0x28(%rsp), %r11
movl 0x4c(%rsp), %r10d
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
cmpq %rcx, %r10
movq 0x78(%rsp), %r13
movq 0x68(%rsp), %r8
movq 0x80(%rsp), %r9
je 0x5aaa0
cltq
movq 0x18(%rsp), %rbp
addq %rbp, %rax
subq $0x8, %rsp
xorl %r14d, %r14d
movq 0x40(%rsp), %rdi
movq %r8, %rsi
xorl %edx, %edx
movq %r13, %rcx
movq 0x68(%rsp), %r8
movq %r9, %r15
movq 0x18(%rsp), %r9
movsd 0x48(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
pushq 0x60(%rsp)
pushq 0xf8(%rsp)
pushq %r12
pushq 0x40(%rsp)
pushq %r15
pushq %r10
pushq 0xc8(%rsp)
pushq %rax
pushq %rbp
pushq %rbx
pushq %r11
pushq 0x140(%rsp)
pushq 0xd8(%rsp)
callq 0x5a1fb
addq $0x70, %rsp
testl %eax, %eax
je 0x5ab62
addq 0xe8(%rsp), %rbx
movl 0x8(%r13), %eax
cmpq %rax, %rbx
cmovgeq %rax, %rbx
movq 0xa0(%rsp), %rdi
decq %rdi
movq 0x98(%rsp), %r9
jne 0x5a768
movl 0x8(%r13), %edx
movl 0x28(%r13), %eax
incl %edx
testl %eax, %eax
sete %sil
subl 0x4(%r13), %edx
sete %cl
orb %sil, %cl
je 0x5aaed
xorl %r14d, %r14d
jmp 0x5ab62
movl %edx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
movsd 0x1ca771(%rip), %xmm1   # 0x225270
subsd 0x40(%rsp), %xmm1
mulsd %xmm0, %xmm1
movl %eax, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorl %r14d, %r14d
ucomisd %xmm1, %xmm0
setae %dil
orb %dil, %cl
je 0x5ab28
movb %dil, %r14b
jmp 0x5ab62
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
cmpl $0x0, 0x58(%rsp)
je 0x5ab50
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x5aae8
mulsd 0x1ca9a2(%rip), %xmm0   # 0x2254f0
jmp 0x5ab56
mulsd 0x50(%rsp), %xmm0
movl $0x1, %r14d
ucomisd %xmm0, %xmm1
jb 0x5aae8
movl %r14d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq