// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x28(%rsp)
movq %rdi, %r12
movq 0xc8(%rsp), %rax
movq 0xc0(%rsp), %rbp
movq 0xb8(%rsp), %r14
movl 0xa8(%rsp), %ebx
movl 0x98(%rsp), %edi
subq 0xb0(%rsp), %r14
incq %r14
pushq $0x1
popq %rdx
movq %rsi, 0x18(%rsp)
testq %rsi, %rsi
je 0x56d3d
cmpq %rax, %r14
jge 0x56d33
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
movl %ecx, %edi
cmovel %edx, %edi
cmpq $0x4, %r14
cmovll %ecx, %edi
movl %r14d, %esi
callq 0x51fcd
jmp 0x56d73
cmpq %rax, %r14
jge 0x56d6b
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rax
cmpq $0x1f, %rax
pushq $0x1f
popq %rcx
cmovlq %rax, %rcx
testq %rax, %rax
pushq $0x1
popq %rdi
cmovnel %ecx, %edi
cmpq $0x4, %r14
cmovll %ecx, %edi
movl %r14d, %esi
callq 0x526f4
movl %eax, %r15d
movslq %eax, %r13
leaq (%r14,%r13,2), %rcx
movl %ebx, %ebx
movq 0x28(%rsp), %rax
testq %rax, %rax
movq %r12, 0x8(%rsp)
je 0x56da6
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbx, %rdx
movl (%rax,%rdx), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x56dd3
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x56dc6
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbx, %rdx
movl (%rax,%rdx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x56dd3
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rbx,8), %rdx
leaq 0x50(%rsp), %rax
leaq 0xc0(%rsp), %r10
movq %rsp, %r8
leaq 0x38(%rsp), %r9
movq %r13, %rdi
movq %rbp, %rsi
pushq %rax
pushq %r10
callq 0x1c7e7
popq %rcx
popq %rdx
xorl %r12d, %r12d
testl %eax, %eax
je 0x570bd
movq 0x50(%rsp), %rbp
movq %rbp, %rax
addq %r13, %rax
cmpq %r14, %rax
jl 0x570bd
movl 0xa0(%rsp), %eax
movq 0xc0(%rsp), %rsi
movq 0x18(%rsp), %r8
testq %r8, %r8
movq %rbx, 0x40(%rsp)
movq %rsi, 0x30(%rsp)
je 0x56e55
movzbl %al, %ecx
movq %rbx, %r9
movq 0x10(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x6c9e
movq %rbx, %rdi
jmp 0x56e79
subq $0x8, %rsp
movzbl %al, %ecx
movq 0x18(%rsp), %rdi
movq %rbp, %rdx
movq 0x30(%rsp), %r8
movq 0x28(%rsp), %r9
pushq %rbx
callq 0x578d7
popq %rcx
popq %rdx
movq %rax, %rdi
movq 0x90(%rsp), %rax
movq 0x48(%rsp), %rbx
addq 0xb0(%rsp), %rbx
cmpl $0x0, 0xd0(%rsp)
je 0x56ec6
movl $0x7fffffff, 0x2c(%rax) # imm = 0x7FFFFFFF
movl (%rsp), %r9d
subq $0x8, %rsp
movl %ebp, %esi
movq %rbx, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
pushq %rax
callq 0x59275
movq 0xa0(%rsp), %rdx
addq $0x10, %rsp
jmp 0x56eef
movl (%rsp), %r9d
subq $0x8, %rsp
movl %ebp, %esi
movq %rbx, %rdx
movl %r14d, %ecx
movl %r15d, %r8d
pushq %rax
callq 0x57947
movq 0xa0(%rsp), %rdx
addq $0x10, %rsp
andl $0x0, 0x10(%rdx)
movl 0x2c(%rdx), %ecx
cmpl 0x28(%rdx), %ecx
jg 0x570bd
movq 0x8(%rsp), %r12
leaq 0x48(%r12), %r15
movq 0x48(%r12), %rdi
cmpq 0x50(%r12), %rdi
jne 0x56f49
leaq (%rdi,%rdi), %rax
testq %rdi, %rdi
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r12)
movq 0x58(%r12), %rdi
shlq $0x5, %rsi
callq 0x2370
movq 0x90(%rsp), %rdx
movq %rax, 0x58(%r12)
movq 0x48(%r12), %rdi
movl 0x2c(%rdx), %ecx
jmp 0x56f4e
movq 0x58(%r12), %rax
leaq 0x1(%rdi), %rsi
movq %rsi, (%r15)
shlq $0x5, %rdi
movq 0xb0(%rsp), %rsi
movl %esi, (%rax,%rdi)
movq 0xb8(%rsp), %rsi
movl %esi, 0x4(%rax,%rdi)
movq 0x10(%rdx), %xmm0
andq $0x0, 0x18(%rax,%rdi)
leaq (%rax,%rdi), %r12
movd 0x30(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%rax,%rdi)
movw %cx, 0x14(%rax,%rdi)
cmpl $0x0, 0xd0(%rsp)
je 0x57095
movq %r12, %rdi
movq %r15, %rsi
callq 0x1e842
movq 0x90(%rsp), %rdx
movslq 0x14(%rdx), %rax
leaq 0x1(%rax), %rcx
cmpq %rcx, %rbp
je 0x56fd1
cmpl $0x0, 0x10(%rdx)
jne 0x57095
movslq 0x2c(%rdx), %r10
testq %r10, %r10
jle 0x57095
movq 0x90(%rsp), %rcx
movslq 0x10(%rcx), %r11
movq %rbx, %rdi
movq 0x10(%rsp), %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq %r13, %r8
movq 0x30(%rsp), %r9
leaq 0x38(%rsp), %rbx
pushq %rbx
leaq 0x8(%rsp), %rbx
pushq %rbx
leaq 0xd0(%rsp), %rbx
pushq %rbx
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq 0x40(%rsp)
pushq 0xc0(%rsp)
movl 0xd8(%rsp), %ebx
pushq %rbx
pushq 0x80(%rsp)
pushq %r10
pushq %rax
pushq %r11
callq 0x65940
movq 0xf0(%rsp), %rdx
addq $0x60, %rsp
testl %eax, %eax
je 0x57095
movl 0x18(%r12), %eax
movq 0x8(%rsp), %rcx
movq %rax, 0x60(%rcx)
movq %r12, %rdi
movq %r15, %rsi
movq %rdx, %rbx
callq 0x1e842
movq 0x10(%rbx), %xmm0
movd 0xc0(%rsp), %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
paddd %xmm0, %xmm1
movq %xmm1, 0x8(%r12)
movzwl 0x2c(%rbx), %eax
movw %ax, 0x14(%r12)
movzwl (%rsp), %eax
movw %ax, 0x10(%r12)
movzwl 0x38(%rsp), %eax
movw %ax, 0x12(%r12)
movw %r13w, 0x16(%r12)
movq 0x8(%rsp), %rax
addl %r14d, 0x28(%rax)
pushq $0x1
popq %r12
movl %r12d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq