// File: /workspace/github_cpp_makefile_star2_O1/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 %r9, %r15
movq %r8, 0x20(%rsp)
movq %rsi, %r9
movq 0xc8(%rsp), %rsi
movq 0xb8(%rsp), %r14
movl 0xa8(%rsp), %eax
movl 0x98(%rsp), %r8d
subq 0xb0(%rsp), %r14
incq %r14
testq %r9, %r9
je 0x8b15a
cmpq %rsi, %r14
jge 0x8b127
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8
cmpq $0x1f, %r8
movl $0x1f, %esi
cmovlq %r8, %rsi
testq %r8, %r8
movl $0x1, %r8d
cmovnel %esi, %r8d
cmpq $0x4, %r14
cmovll %esi, %r8d
leal (%r8,%r8), %esi
testl %r8d, %r8d
movl $0x2, %r8d
cmovnel %esi, %r8d
cmpl $0x4, %r14d
cmovll %esi, %r8d
cmpl $0x20, %r8d
movl $0x1f, %esi
cmovgel %r8d, %esi
cmpl $0x12c, %r14d           # imm = 0x12C
cmovll %r8d, %esi
jmp 0x8b1bf
cmpq %rsi, %r14
jge 0x8b18f
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %r8
cmpq $0x1f, %r8
movl $0x1f, %esi
cmovlq %r8, %rsi
testq %r8, %r8
movl $0x1, %r8d
cmovnel %esi, %r8d
cmpq $0x4, %r14
cmovll %esi, %r8d
testl %r8d, %r8d
movl $0x1, %esi
cmovnel %r8d, %esi
cmpl $0x4, %r14d
cmovll %r8d, %esi
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x19a2cd(%rip), %xmm0   # 0x225480
cvttsd2si %xmm0, %r8d
cmpl %r8d, %esi
cmovlel %r8d, %esi
movq 0xc0(%rsp), %r11
cmpl $0x1f, %esi
movl $0x1f, %r12d
cmovll %esi, %r12d
movl %eax, %esi
testq %rdx, %rdx
movq %rsi, 0x18(%rsp)
je 0x8b1f7
movq (%rdx), %rax
movq 0x10(%rax), %rax
leaq (%rsi,%rsi,4), %rsi
movl (%rax,%rsi,8), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8b219
testq %rcx, %rcx
je 0x8b211
movq (%rcx), %rax
movq 0x10(%rax), %rax
leaq (%rsi,%rsi,4), %rsi
movl (%rax,%rsi,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
jmp 0x8b219
movq 0x18(%r9), %rax
movq (%rax,%rsi,8), %rax
movslq %r12d, %r13
movq $-0x1, %rbx
movq %rbx, 0xc0(%rsp)
movq %rbx, 0x28(%rsp)
movq %rbx, 0x8(%rsp)
movb $0x1, %r10b
testq %r11, %r11
js 0x8b2a5
cmpq %r11, %rax
jle 0x8b2a5
leaq (,%r13,2), %r8
leaq (%r14,%r13,2), %rsi
subq %r11, %r8
addq %rax, %r8
addq $0x1f, %r8
cmpq %rsi, %r8
jl 0x8b2a5
movq $0x0, 0x8(%rsp)
subq %r13, %r11
subq %r11, %rax
cmpq %rsi, %rax
movq %rax, %rbx
cmovgeq %rsi, %rbx
movq %r11, 0xc0(%rsp)
subq %rbx, %rsi
movq %rsi, 0x28(%rsp)
testq %r11, %r11
jns 0x8b2a2
addq %r11, %rbx
negq %r11
movq %r11, 0x8(%rsp)
movq $0x0, 0xc0(%rsp)
xorl %r10d, %r10d
xorl %ebp, %ebp
testb %r10b, %r10b
jne 0x8b59f
leaq (%rbx,%r13), %rax
cmpq %r14, %rax
jl 0x8b59f
movq %rdi, 0x10(%rsp)
movl 0xa0(%rsp), %r10d
movq 0xc0(%rsp), %r8
testq %r9, %r9
movq %r9, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rcx, 0x40(%rsp)
movq %r15, 0x38(%rsp)
je 0x8b30a
movzbl %r10b, %ecx
movq %r15, %rdi
movq %r8, %rsi
movq %rbx, %rdx
movq %r9, %r8
movq 0x18(%rsp), %r9
callq 0x337f7
movq %r15, %rdi
jmp 0x8b371
testq %rdx, %rdx
je 0x8b338
movq (%rdx), %rax
movq 0x10(%rax), %rax
movq 0x18(%rsp), %rsi
leaq (%rsi,%rsi,4), %rsi
movq 0x20(%rax,%rsi,8), %rdi
testb %r10b, %r10b
je 0x8b369
leaq (%rax,%rsi,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8b36b
movq (%rcx), %rax
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movzbl %r10b, %ecx
xorl %edi, %edi
movq %r15, %rsi
movq %rbx, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
jmp 0x8b305
xorl %eax, %eax
addq %r8, %rax
addq %rax, %rdi
movq 0x90(%rsp), %r15
movq 0x20(%rsp), %rdx
addq 0xb0(%rsp), %rdx
cmpl $0x0, 0xd0(%rsp)
movq %rdx, 0x20(%rsp)
je 0x8b3bb
movl $0x7fffffff, 0x2c(%r15) # imm = 0x7FFFFFFF
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movl %ebx, %esi
movl %r14d, %ecx
movl %r12d, %r8d
pushq %r15
callq 0x8dd8a
addq $0x10, %rsp
jmp 0x8b3df
movl 0x8(%rsp), %r9d
subq $0x8, %rsp
movl %ebx, %esi
movl %r14d, %ecx
movl %r12d, %r8d
pushq %r15
callq 0x8c179
addq $0x10, %rsp
movl $0x0, 0x10(%r15)
movl 0x2c(%r15), %eax
cmpl 0x28(%r15), %eax
movq 0x10(%rsp), %rdi
jg 0x8b59f
movq 0x50(%rdi), %rax
cmpq %rax, 0x48(%rdi)
jne 0x8b426
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x50(%rdi)
movq 0x58(%rdi), %rdi
shlq $0x5, %rsi
callq 0x2370
movq 0x10(%rsp), %rdi
movq %rax, 0x58(%rdi)
movq 0x48(%rdi), %rax
movq 0x58(%rdi), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x48(%rdi)
shlq $0x5, %rax
leaq (%rcx,%rax), %r12
movq 0xb0(%rsp), %rdx
movl %edx, (%rcx,%rax)
movq 0xb8(%rsp), %rdx
movl %edx, 0x4(%rcx,%rax)
movl 0xc0(%rsp), %edx
movl 0x10(%r15), %esi
addl %edx, %esi
movl %esi, 0x8(%rcx,%rax)
addl 0x14(%r15), %edx
movl %edx, 0xc(%rcx,%rax)
movzwl 0x2c(%r15), %edx
movw %dx, 0x14(%rcx,%rax)
movq $0x0, 0x18(%rcx,%rax)
cmpl $0x0, 0xd0(%rsp)
je 0x8b57a
leaq 0x48(%rdi), %rbp
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4bdb5
movq 0x10(%rsp), %rdi
movslq 0x14(%r15), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %rbx
je 0x8b4be
cmpl $0x0, 0x10(%r15)
jne 0x8b57a
movslq 0x2c(%r15), %r10
testq %r10, %r10
jle 0x8b57a
leaq 0xc0(%rsp), %r11
movq (%r11), %r9
movslq 0x10(%r15), %rcx
movq %rcx, 0x30(%rsp)
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r13, %r8
leaq 0x28(%rsp), %rbx
pushq %rbx
leaq 0x10(%rsp), %rbx
pushq %rbx
pushq %r11
pushq 0x58(%rsp)
pushq 0x68(%rsp)
pushq 0x78(%rsp)
pushq %r15
movl 0xd8(%rsp), %r11d
pushq %r11
pushq 0x58(%rsp)
pushq %r10
pushq %rax
pushq 0x88(%rsp)
callq 0x9bdcb
movq 0x70(%rsp), %rdi
addq $0x60, %rsp
testl %eax, %eax
je 0x8b57a
movl 0x18(%r12), %eax
movq %rax, 0x60(%rdi)
movq %r12, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x4bdb5
movq 0x10(%rsp), %rdi
movl 0xc0(%rsp), %eax
movl 0x10(%r15), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%r12)
addl 0x14(%r15), %eax
movl %eax, 0xc(%r12)
movzwl 0x2c(%r15), %eax
movw %ax, 0x14(%r12)
movzwl 0x8(%rsp), %eax
movw %ax, 0x10(%r12)
movzwl 0x28(%rsp), %eax
movw %ax, 0x12(%r12)
movw %r13w, 0x16(%r12)
addl %r14d, 0x28(%rdi)
movl $0x1, %ebp
movl %ebp, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq