// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: aln_wlst_adv_exz(overlap_region*, All_reads*, hpc_t*, ul_idx_t const*, char*, char*, bit_extz_t*, unsigned int, unsigned int, unsigned int, long, long, long, long, double, unsigned int)
inline uint32_t aln_wlst_adv_exz(overlap_region *z, All_reads *rref, hpc_t *hpc_g, 
const ul_idx_t *uref, char *qstr, char *tstr, bit_extz_t *exz, uint32_t max_err,
uint32_t rev, uint32_t id, int64_t qs, int64_t qe, int64_t t_s, int64_t block_s, 
double e_rate, uint32_t is_cigar)
{
    int64_t ql, tl, aln_l, t_tot_l; window_list *p = NULL; ///int r_ts = 0, t_end;
    int64_t aux_beg, aux_end, t_pri_l; int64_t thres; char *q_string, *t_string; 
    ql = qe + 1 - qs;
    ///there are two potiential reasons for unmatched window:
    ///1. this window has a large number of differences
    ///2. DP does not start from the right offset
    if(rref) {
        thres = double_error_threshold(get_init_err_thres(ql, e_rate, block_s, max_err), ql);
    } else {
        thres = double_ul_error_threshold(get_init_err_thres(ql, e_rate, block_s, max_err), ql);
    }
    aln_l = ql + (thres << 1);
    if(hpc_g) t_tot_l = hpc_len(*hpc_g, id);
    else if(uref) t_tot_l = uref->ug->u.a[id].len;
    else t_tot_l = Get_READ_LENGTH((*rref), id);

    if(!init_waln(thres, t_s, t_tot_l, aln_l, &aux_beg, &aux_end, &t_s, &t_pri_l)) return 0;
    if(t_pri_l + thres < ql) return 0;

    q_string = qstr + qs; 
    if(rref) {
        recover_UC_Read_sub_region(tstr, t_s, t_pri_l, rev, rref, id); t_string = tstr;
    } else {
        t_string = return_str_seq_exz(tstr, t_s, t_pri_l, rev, hpc_g, uref, id);
    }
    tl = t_pri_l;
    if(is_cigar) {
        clear_align(*exz);
        ed_band_cal_semi_64_w_absent_diag_trace(t_string, tl, q_string, ql, thres, aux_beg, exz);
    } else {
        ed_band_cal_semi_64_w_absent_diag(t_string, tl, q_string, ql, thres, aux_beg, exz); exz->ps = 0;
    }

    // if(id == 40 && qs == 79670 && qe == 79824) {
    //     fprintf(stderr, "\n[M::%s::semi] exz->ps::%d, exz->pe::%d, exz->ts::%d, exz->te::%d, exz->err::%d, exz->cigar.n::%d\n", 
    //     __func__, exz->ps, exz->pe, exz->ts, exz->te, exz->err, (int32_t)exz->cigar.n);
    // }

    if(is_align(*exz)) {
        kv_pushp(window_list, z->w_list, &p);
        p->x_start = qs; p->x_end = qe; ///must set x_start/x_end here
        p->y_start = t_s + exz->ps;///difference
        p->y_end = t_s + exz->pe;
        p->error = exz->err;
        p->cidx = p->clen = 0;
        if(is_cigar) {
            push_wcigar(p, &(z->w_list), exz);
            ///this condition is always wrong
            ///in best case, r_ts = threshold, t_end = aln_l - thres - 1
            if ((((exz->pe+1) == tl) || (exz->ps == 0)) && (exz->err > 0)) {
                if(recal_boundary_exz(q_string, tstr, ql, tl, thres, t_s, exz->ps, exz->pe,
                exz->err, id, rev, exz, rref, hpc_g, uref, &t_s, &aux_beg, &aux_end)) {
                    //update cigar
                    z->w_list.c.n = p->cidx; push_wcigar(p, &(z->w_list), exz);

                    p->y_start = t_s + exz->ps;///difference
                    p->y_end = t_s + exz->pe;
                    p->error = exz->err;
                }
            }
        } 

        p->extra_begin = aux_beg;
        p->extra_end = aux_end;
        p->error_threshold = thres;
        z->align_length += ql; 
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %r11
movq %rcx, %r10
movq %rsi, %r8
movq 0xc8(%rsp), %rcx
movq 0xb8(%rsp), %r14
movl 0x98(%rsp), %eax
subq 0xb0(%rsp), %r14
incq %r14
testq %rsi, %rsi
je 0x8a54c
cmpq %rcx, %r14
jge 0x8a521
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %eax
cmovnel %ecx, %eax
cmpq $0x4, %r14
cmovll %ecx, %eax
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x2, %eax
cmovnel %ecx, %eax
cmpl $0x4, %r14d
cmovll %ecx, %eax
cmpl $0x20, %eax
movl $0x1f, %ecx
cmovgel %eax, %ecx
cmpl $0x12c, %r14d           # imm = 0x12C
cmovll %eax, %ecx
jmp 0x8a5a8
cmpq %rcx, %r14
jge 0x8a57e
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %eax
cmovnel %ecx, %eax
cmpq $0x4, %r14
cmovll %ecx, %eax
testl %eax, %eax
movl $0x1, %ecx
cmovnel %eax, %ecx
cmpl $0x4, %r14d
cmovll %eax, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x1a6fe1(%rip), %xmm0   # 0x231580
cvttsd2si %xmm0, %eax
cmpl %eax, %ecx
cmovlel %eax, %ecx
movq 0xc0(%rsp), %rbp
movl 0xa8(%rsp), %eax
cmpl $0x1f, %ecx
movl $0x1f, %r15d
cmovll %ecx, %r15d
testq %rdx, %rdx
je 0x8a5e6
movq (%rdx), %rcx
movq 0x10(%rcx), %rcx
movl %eax, %esi
leaq (%rsi,%rsi,4), %rsi
movl (%rcx,%rsi,8), %r13d
shrl %r13d
andl $0x3fffffff, %r13d      # imm = 0x3FFFFFFF
jmp 0x8a60f
testq %r10, %r10
je 0x8a605
movq (%r10), %rcx
movq 0x10(%rcx), %rcx
movl %eax, %esi
leaq (%rsi,%rsi,4), %rsi
movl (%rcx,%rsi,8), %r13d
andl $0x7fffffff, %r13d      # imm = 0x7FFFFFFF
jmp 0x8a60f
movq 0x18(%r8), %rcx
movl %eax, %esi
movq (%rcx,%rsi,8), %r13
xorl %r12d, %r12d
testq %rbp, %rbp
js 0x8a9b8
cmpq %rbp, %r13
jle 0x8a9b8
movslq %r15d, %rbx
leaq (%rbx,%rbx), %rsi
leaq (%r14,%rbx,2), %rcx
subq %rbp, %rsi
addq %r13, %rsi
addq $0x1f, %rsi
cmpq %rcx, %rsi
jl 0x8a9b8
movq $0x0, 0x28(%rsp)
subq %rbx, %rbp
movq %rbp, 0x30(%rsp)
subq %rbp, %r13
cmpq %rcx, %r13
cmovgeq %rcx, %r13
subq %r13, %rcx
movq %rcx, 0x38(%rsp)
xorl %r12d, %r12d
testq %rbp, %rbp
js 0x8a671
xorl %esi, %esi
jmp 0x8a689
addq %rbp, %r13
negq %rbp
movq %rbp, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movl %ebp, %esi
xorl %ebp, %ebp
leaq (%rbx,%r13), %rcx
cmpq %r14, %rcx
jl 0x8a9b8
movl %esi, 0x14(%rsp)
movl 0xa0(%rsp), %esi
movl %eax, %eax
testq %r8, %r8
movq %rdi, (%rsp)
movq %r10, 0x40(%rsp)
movq %r9, 0x20(%rsp)
movq %r8, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rax, 0x18(%rsp)
je 0x8a6eb
movzbl %sil, %ecx
movq %r9, %rdi
movq %rbp, %rsi
movq %r13, %rdx
movq %rax, %r9
movq %r11, 0x8(%rsp)
callq 0x32ef8
movq 0x8(%rsp), %r11
movq 0x20(%rsp), %rdi
jmp 0x8a75f
testq %rdx, %rdx
je 0x8a719
movq (%rdx), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %rdi
testb %sil, %sil
je 0x8a757
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8a759
movq (%r10), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %sil, %ecx
xorl %edi, %edi
movq %r9, %rsi
movq %rbp, %r8
movq %r13, %r9
pushq $0x0
movq %r11, 0x18(%rsp)
callq 0x36049
movq 0x18(%rsp), %r11
addq $0x10, %rsp
jmp 0x8a6e4
xorl %eax, %eax
addq %rax, %rdi
addq %rbp, %rdi
movq 0x90(%rsp), %rax
addq 0xb0(%rsp), %r11
cmpl $0x0, 0xd0(%rsp)
movq %r11, 0x8(%rsp)
je 0x8a7b4
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
subq $0x8, %rsp
movl %r13d, %esi
movq %r11, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
movl 0x1c(%rsp), %r9d
pushq 0x98(%rsp)
callq 0x8d32c
movq 0xa0(%rsp), %rdx
addq $0x10, %rsp
jmp 0x8a7e8
subq $0x8, %rsp
movl %r13d, %esi
movq %r11, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
movl 0x1c(%rsp), %r9d
pushq 0x98(%rsp)
callq 0x8b419
movq 0xa0(%rsp), %rdx
addq $0x10, %rsp
movl $0x0, 0x10(%rdx)
movl 0x2c(%rdx), %ecx
cmpl 0x28(%rdx), %ecx
movq (%rsp), %rdi
jg 0x8a9b8
leaq 0x48(%rdi), %r15
movq 0x48(%rdi), %r8
cmpq 0x50(%rdi), %r8
jne 0x8a844
leaq (%r8,%r8), %rax
testq %r8, %r8
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x50(%rdi)
movq (%rsp), %rax
movq 0x58(%rax), %rdi
shlq $0x5, %rsi
callq 0x2370
movq 0x90(%rsp), %rdx
movq (%rsp), %rdi
movq %rax, 0x58(%rdi)
movq 0x48(%rdi), %r8
movl 0x2c(%rdx), %ecx
jmp 0x8a848
movq 0x58(%rdi), %rax
leaq 0x1(%r8), %rsi
shlq $0x5, %r8
leaq (%rax,%r8), %r12
movq %rsi, (%r15)
movq 0xb0(%rsp), %rsi
movl %esi, (%rax,%r8)
movq 0xb8(%rsp), %rsi
movl %esi, 0x4(%rax,%r8)
movq 0x10(%rdx), %xmm0
movd %ebp, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rax,%r8)
movw %cx, 0x14(%rax,%r8)
movq $0x0, 0x18(%rax,%r8)
cmpl $0x0, 0xd0(%rsp)
je 0x8a992
movq %r12, %rdi
movq %r15, %rsi
callq 0x4b41c
movq 0x90(%rsp), %rdx
movq (%rsp), %rdi
movslq 0x14(%rdx), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %r13
je 0x8a8d4
cmpl $0x0, 0x10(%rdx)
jne 0x8a992
movslq 0x2c(%rdx), %r10
testq %r10, %r10
jle 0x8a992
movslq 0x10(%rdx), %r11
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %r13, %rcx
movq %rbx, %r8
movq %rbp, %r9
leaq 0x38(%rsp), %r13
pushq %r13
leaq 0x30(%rsp), %r13
pushq %r13
leaq 0x40(%rsp), %r13
pushq %r13
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq 0xc0(%rsp)
movl 0xd8(%rsp), %r13d
pushq %r13
pushq 0x58(%rsp)
pushq %r10
pushq %rax
pushq %r11
callq 0x9c4f8
movq 0xf0(%rsp), %rdx
movq 0x60(%rsp), %rdi
addq $0x60, %rsp
testl %eax, %eax
je 0x8a992
movl 0x18(%r12), %eax
movq %rax, 0x60(%rdi)
movq %r12, %rdi
movq %r15, %rsi
movq %rdx, %r15
callq 0x4b41c
movq (%rsp), %rdi
movq 0x10(%r15), %xmm0
movd 0x30(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%r12)
movzwl 0x2c(%r15), %eax
movw %ax, 0x14(%r12)
movzwl 0x28(%rsp), %eax
movw %ax, 0x10(%r12)
movzwl 0x38(%rsp), %eax
movw %ax, 0x12(%r12)
movw %bx, 0x16(%r12)
addl %r14d, 0x28(%rdi)
movl $0x1, %r12d
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq