// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: align_ul_ed_post_extz(overlap_region*, ul_idx_t const*, hpc_t*, char*, char*, bit_extz_t*, double, long, double, long, void*)
uint32_t align_ul_ed_post_extz(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, 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, sec_check = (uref&&(!hpc_g))?1:0;
    int64_t 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); 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_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->y_id == 178 && q_s == 86800 && q_e == 86807) {
            //     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\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);
            //     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_wlst_exz(uref, hpc_g, NULL, 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, sec_check, 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;
    return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movsd %xmm1, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r9, 0x18(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, %r10
movq %rdi, %rbx
movq 0xd0(%rsp), %r9
movq %rsi, 0x20(%rsp)
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
sete %cl
andb %al, %cl
movzbl %cl, %eax
movq %rax, 0x38(%rsp)
movq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
movl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %r13d
movl 0x4(%rdi), %ecx
leal 0x1(%r13), %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, 0x88(%rsp)
jle 0x5b1f1
movq %rcx, %r12
subq %rsi, %r12
leaq (%r9,%r12), %rax
decq %rax
cmpq %r13, %rax
cmovlq %rax, %r13
cmpq %rcx, %r12
cmovleq %rcx, %r12
leaq 0x38(%rbx), %r15
xorl %ebp, %ebp
movq %r10, 0x50(%rsp)
movq %rbp, 0x60(%rsp)
leaq 0x1(%r13), %rax
movq %rax, 0x90(%rsp)
movq %rax, %rdx
subq %r12, %rdx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
mulsd 0x28(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
movq %rdx, 0x58(%rsp)
cmpq $0x4, %rdx
movq %rcx, %rsi
movl $0x1, %edx
cmovgeq %rdx, %rsi
testq %rax, %rax
cmovneq %rcx, %rsi
movq %rsi, 0x8(%rsp)
movl 0x4(%rbx), %eax
movq %rax, 0x10(%rsp)
movl 0x14(%rbx), %ebp
movl 0x40(%rbx), %esi
decl %esi
movq %r15, %rdi
callq 0x3d110
cltq
movl 0x40(%rbx), %r14d
cmpq %rax, %r12
je 0x5afa8
testl %r14d, %r14d
je 0x5b2af
xorl %r14d, %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x3d110
cltq
cmpq %rax, %r12
jl 0x5af93
incq %r14
movl 0x40(%rbx), %eax
cmpq %rax, %r14
jb 0x5af74
jmp 0x5af9f
testq %r14, %r14
je 0x5b2af
movl 0x40(%rbx), %eax
cmpq %rax, %r14
je 0x5b2af
decl %r14d
movq %r15, %rdi
movl %r14d, %esi
callq 0x3d11b
movl 0x10(%rbx), %ecx
leaq (%rcx,%rcx,4), %rdx
movq 0x50(%rsp), %r10
testq %r10, %r10
je 0x5afdb
movq (%r10), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx,%rdx,8), %ecx
shrl %ecx
andl $0x3fffffff, %ecx       # imm = 0x3FFFFFFF
jmp 0x5aff1
movq 0x20(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx,%rdx,8), %ecx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %ecx
movq %r12, %rsi
subq 0x10(%rsp), %rsi
addq %rsi, %rbp
movslq %eax, %r14
addq %rbp, %r14
movl %ecx, %edi
cmpq %rdi, %r14
movq 0x60(%rsp), %rbp
jae 0x5b1c0
movq 0x8(%rsp), %rsi
leaq (%rsi,%rsi), %rax
movq 0x58(%rsp), %rcx
leaq (%rcx,%rsi,2), %rsi
subq %r14, %rax
addq %rdi, %rax
addq $0x1f, %rax
cmpq %rsi, %rax
jl 0x5b1c0
subq 0x8(%rsp), %r14
movq %rdi, %r9
subq %r14, %r9
cmpq %rsi, %r9
cmovgeq %rsi, %r9
testq %r14, %r14
movl $0x0, %r8d
cmovgq %r14, %r8
movl $0x0, %r11d
cmovsq %r14, %r11
addq %r9, %r11
movb 0x1c(%rbx), %cl
testq %r10, %r10
movq %rdi, 0x80(%rsp)
movq %rsi, 0x10(%rsp)
movq %r8, 0x78(%rsp)
movq %r9, 0x70(%rsp)
je 0x5b09d
movq (%r10), %rax
movq 0x10(%rax), %rax
testb %cl, %cl
je 0x5b0db
leaq (%rax,%rdx,8), %rcx
movl (%rcx), %edi
shrl %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
jmp 0x5b0dd
movq 0x20(%rsp), %rax
movq (%rax), %rax
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %cl, %ecx
xorl %edi, %edi
movq 0x48(%rsp), %rbp
movq %rbp, %rsi
movq %r11, %r9
pushq $0x0
movq %r11, 0x78(%rsp)
callq 0x36049
movq 0x78(%rsp), %r11
addq $0x10, %rsp
movq %rbp, %rdi
jmp 0x5b0e5
xorl %edi, %edi
addq 0x20(%rax,%rdx,8), %rdi
addq %r8, %rdi
movq %r14, %rax
negq %rax
sarq $0x3f, %r14
andq %rax, %r14
movq 0x48(%rsp), %rax
leaq (%rax,%r12), %rdx
subq $0x8, %rsp
movl %r11d, %esi
movq 0x60(%rsp), %rcx
movq 0x10(%rsp), %r8
movl %r14d, %r9d
movq 0x20(%rsp), %rbp
pushq %rbp
callq 0x8b419
addq $0x10, %rsp
movl 0x2c(%rbp), %eax
cmpl 0x28(%rbp), %eax
movq 0x50(%rsp), %r10
movq 0x60(%rsp), %rbp
movq 0x10(%rsp), %rcx
jg 0x5b1c0
subq 0x70(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rsp), %rax
movslq 0x14(%rax), %rax
movq 0x78(%rsp), %r11
addq %r11, %rax
subq $0x10, %rsp
movq $0x0, 0x18(%rsp)
movq 0x30(%rsp), %rdi
movq %r10, %rsi
xorl %edx, %edx
movq %rbx, %rcx
movq 0x58(%rsp), %r8
movq 0x50(%rsp), %r9
movsd 0x38(%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
pushq 0xe8(%rsp)
pushq 0x50(%rsp)
pushq 0xf0(%rsp)
pushq 0x38(%rsp)
pushq %r14
pushq 0xb8(%rsp)
pushq %rax
pushq %r11
pushq %r13
pushq %r12
pushq $0x1f
pushq 0x80(%rsp)
callq 0x5a0e7
addq $0x70, %rsp
testl %eax, %eax
je 0x5b227
movq 0xd0(%rsp), %rax
addq %rax, %r13
movl 0x8(%rbx), %eax
cmpq %rax, %r13
cmovgeq %rax, %r13
incq %rbp
cmpq 0x88(%rsp), %rbp
movq 0x90(%rsp), %r12
jne 0x5aeed
movl %eax, %r13d
cmpq $0x0, 0xd8(%rsp)
je 0x5b208
movl $0x1, %eax
movq %rax, 0x8(%rsp)
jmp 0x5b227
incl %r13d
movl 0x28(%rbx), %eax
testl %eax, %eax
sete %cl
subl 0x4(%rbx), %r13d
sete %dl
orb %cl, %dl
je 0x5b23e
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
movsd 0x1d60c2(%rip), %xmm2   # 0x231310
subsd 0x28(%rsp), %xmm2
mulsd %xmm0, %xmm2
movl %eax, %ecx
xorps %xmm1, %xmm1
cvtsi2sd %rcx, %xmm1
ucomisd %xmm2, %xmm1
jae 0x5b1fc
cmpl $0x0, 0x38(%rsp)
je 0x5b290
cmpl $0x1c2, %eax            # imm = 0x1C2
movl $0x1, %eax
movq %rax, 0x8(%rsp)
jb 0x5b21e
mulsd 0x1d6368(%rip), %xmm0   # 0x2315f0
ucomisd %xmm0, %xmm1
jae 0x5b227
jmp 0x5b21e
mulsd 0x30(%rsp), %xmm0
ucomisd %xmm0, %xmm1
movl $0x1, %eax
movq %rax, 0x8(%rsp)
jb 0x5b21e
jmp 0x5b227
callq 0x5ddb