// File: /workspace/github_cpp_makefile_star2_O1/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 $0x88, %rsp
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %r9, 0x50(%rsp)
movq %r8, 0x38(%rsp)
movq %rcx, 0x48(%rsp)
movq %rdx, %r8
movq %rdi, %rcx
movq 0xc0(%rsp), %r9
movq %rsi, 0x10(%rsp)
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
sete %dl
andb %al, %dl
movzbl %dl, %eax
movq %rax, 0x30(%rsp)
movq $0x0, 0x48(%rdi)
movb $0x0, 0x2c(%rdi)
movl $0x0, 0x28(%rdi)
movl 0x8(%rdi), %r13d
movl 0x4(%rdi), %edi
leal 0x1(%r13), %esi
movq %rdi, %rax
xorl %edx, %edx
idivq %r9
subq %rdi, %rdx
addq %rdx, %rsi
movq %rsi, %rax
cqto
idivq %r9
xorl %r9d, %r9d
testq %rdx, %rdx
setg %r9b
addq %rax, %r9
jle 0x5bbd0
movq %rdi, %rax
xorl %edx, %edx
movq 0xc0(%rsp), %rsi
idivq %rsi
movq %rdi, %r10
subq %rdx, %r10
leaq (%rsi,%r10), %rax
decq %rax
cmpq %r13, %rax
cmovlq %rax, %r13
cmpq %rdi, %r10
cmovleq %rdi, %r10
leaq 0x38(%rcx), %rax
movq %rax, 0x68(%rsp)
movq %rcx, 0x40(%rsp)
movq %r8, 0x58(%rsp)
movq %r9, 0x80(%rsp)
movq %r10, %rdi
leaq 0x1(%r13), %rbp
movq %rbp, 0x78(%rsp)
subq %r10, %rbp
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x18(%rsp), %xmm0
cvttsd2si %xmm0, %rax
movq %rax, %rdx
cmpq $0x1f, %rax
jl 0x5b938
movl $0x1f, %edx
cmpq $0x4, %rbp
movq %rdx, %r15
movl $0x1, %esi
cmovgeq %rsi, %r15
testq %rax, %rax
cmovneq %rdx, %r15
movl 0x4(%rcx), %r14d
movl 0x14(%rcx), %r12d
movq %rdi, 0x20(%rsp)
movq 0x68(%rsp), %rsi
movq %rcx, %rbx
callq 0x48df6
movq 0x58(%rsp), %r8
testq %r8, %r8
je 0x5b98e
movq (%r8), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rbx), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %esi
shrl %esi
andl $0x3fffffff, %esi       # imm = 0x3FFFFFFF
jmp 0x5b9ab
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq 0x10(%rcx), %rcx
movl 0x10(%rbx), %edx
leaq (%rdx,%rdx,4), %rdx
movl (%rcx,%rdx,8), %esi
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %esi
movq %rbx, %rcx
movq 0x20(%rsp), %r10
movq %r10, %rdx
subq %r14, %rdx
addq %rax, %r12
addq %rdx, %r12
movl %esi, %edi
movb $0x1, %sil
movq $-0x1, %r9
cmpq %rdi, %r12
jae 0x5ba2a
leaq (%r15,%r15), %rdx
leaq (,%r15,2), %rax
addq %rbp, %rax
subq %r12, %rdx
addq %rdi, %rdx
addq $0x1f, %rdx
movq $-0x1, %r11
movq $-0x1, 0x8(%rsp)
movq $-0x1, %r14
cmpq %rax, %rdx
jl 0x5ba5a
subq %r15, %r12
movq %rdi, %r14
subq %r12, %r14
cmpq %rax, %r14
cmovgeq %rax, %r14
subq %r14, %rax
testq %r12, %r12
js 0x5ba43
xorl %r9d, %r9d
movq %rax, %r11
movq %r12, 0x8(%rsp)
jmp 0x5ba58
movq $-0x1, %r11
movq $-0x1, 0x8(%rsp)
movq $-0x1, %r14
jmp 0x5ba5a
addq %r12, %r14
negq %r12
movq $0x0, 0x8(%rsp)
movq %r12, %r9
movq %rax, %r11
xorl %esi, %esi
movq %r11, 0x70(%rsp)
testb %sil, %sil
jne 0x5bba8
movb 0x1c(%rcx), %sil
movl 0x10(%rcx), %eax
leaq (%rax,%rax,4), %rdx
testq %r8, %r8
je 0x5ba97
movq (%r8), %rax
movq 0x10(%rax), %rax
movq %rdi, %r12
testb %sil, %sil
je 0x5bae3
leaq (%rax,%rdx,8), %rcx
movl (%rcx), %edi
shrl %edi
andl $0x3fffffff, %edi       # imm = 0x3FFFFFFF
jmp 0x5bae5
movq %rdi, %r12
movq 0x10(%rsp), %rax
movq (%rax), %rax
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %sil, %ecx
xorl %edi, %edi
movq %r9, 0x68(%rsp)
movq 0x40(%rsp), %rbx
movq %rbx, %rsi
movq 0x10(%rsp), %r8
movq %r14, %r9
pushq $0x0
callq 0x36b0d
movq 0x30(%rsp), %r10
addq $0x10, %rsp
movq %rbx, %rdi
movq 0x60(%rsp), %r9
jmp 0x5baef
xorl %edi, %edi
addq 0x8(%rsp), %rdi
addq 0x20(%rax,%rdx,8), %rdi
movq 0x48(%rsp), %rax
leaq (%rax,%r10), %rdx
subq $0x8, %rsp
movl %r14d, %esi
movl %ebp, %ecx
movl %r15d, %r8d
movq %r9, %r14
movq 0x58(%rsp), %rbx
pushq %rbx
callq 0x8c179
movq 0x30(%rsp), %r11
addq $0x10, %rsp
movl 0x2c(%rbx), %eax
cmpl 0x28(%rbx), %eax
movq 0x40(%rsp), %rcx
movq 0x58(%rsp), %r8
jg 0x5bba8
movq %r14, %rbx
movq 0x50(%rsp), %r10
movslq 0x14(%r10), %rax
movq 0x8(%rsp), %r15
addq %r15, %rax
subq $0x10, %rsp
xorl %r14d, %r14d
movq 0x20(%rsp), %rdi
movq %r8, %rsi
xorl %edx, %edx
movq 0x58(%rsp), %r8
movq 0x48(%rsp), %r9
movsd 0x28(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
pushq 0xd8(%rsp)
pushq 0x48(%rsp)
pushq 0xe0(%rsp)
pushq 0x98(%rsp)
pushq %rbx
pushq %r12
pushq %rax
pushq %r15
pushq %r13
pushq %r11
pushq $0x1f
pushq %r10
callq 0x5aba0
movq 0xb0(%rsp), %rcx
addq $0x70, %rsp
testl %eax, %eax
je 0x5bc74
addq 0xc0(%rsp), %r13
movl 0x8(%rcx), %eax
cmpq %rax, %r13
cmovgeq %rax, %r13
movq 0x80(%rsp), %r9
decq %r9
movq 0x78(%rsp), %r10
jne 0x5b900
movl $0x1, %r14d
cmpq $0x0, 0xc8(%rsp)
jne 0x5bc74
movl 0x8(%rcx), %edx
movl 0x28(%rcx), %eax
incl %edx
testl %eax, %eax
sete %sil
subl 0x4(%rcx), %edx
sete %cl
orb %sil, %cl
je 0x5bc03
xorl %r14d, %r14d
jmp 0x5bc74
movl %edx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
movsd 0x1c965b(%rip), %xmm1   # 0x225270
subsd 0x18(%rsp), %xmm1
mulsd %xmm0, %xmm1
movl %eax, %esi
xorps %xmm0, %xmm0
cvtsi2sd %rsi, %xmm0
xorl %edi, %edi
ucomisd %xmm1, %xmm0
setae %r8b
orb %r8b, %cl
je 0x5bc40
movb %r8b, %dil
movl %edi, %r14d
jmp 0x5bc74
xorps %xmm0, %xmm0
cvtsi2sd %rdx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
cmpl $0x0, 0x30(%rsp)
je 0x5bc68
cmpl $0x1c2, %eax            # imm = 0x1C2
jb 0x5bbfe
mulsd 0x1c988a(%rip), %xmm0   # 0x2254f0
jmp 0x5bc6e
mulsd 0x28(%rsp), %xmm0
ucomisd %xmm0, %xmm1
jb 0x5bbfe
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq