// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Correct.cpp
// Function: aln_wlst_adv(overlap_region*, All_reads*, hpc_t*, ul_idx_t const*, char*, char*, char*, Correct_dumy*, unsigned int, unsigned int, long, long, long, long, double, unsigned int)
inline uint32_t aln_wlst_adv(overlap_region *z, All_reads *rref, hpc_t *hpc_g, 
const ul_idx_t *uref, char *qstr, char *tstr, char *tstr1, Correct_dumy* dumy,
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, 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; unsigned int error;
    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, THRESHOLD), ql);
    } else {
        thres = double_ul_error_threshold(get_init_err_thres(ql, e_rate, block_s, THRESHOLD_MAX_SIZE), 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) {
        fill_subregion(tstr, t_s, t_pri_l, rev, rref, id, aux_beg, aux_end); t_string = tstr;
    } else {
        t_string = return_str_seq(tstr, t_s, t_pri_l, rev, hpc_g, uref, id, aux_beg, aux_end);
    }

    if(is_cigar) {
        ///note!!! need notification
        t_end = Reserve_Banded_BPM_PATH(t_string, aln_l, q_string, ql, thres, &error, &r_ts,
        &(dumy->path_length), dumy->matrix_bit, dumy->path, -1, -1);
    } else {
        ///note!!! need notification
        t_end = Reserve_Banded_BPM(t_string, aln_l, q_string, ql, thres, &error);    
    }
    if(error!=(unsigned int)-1) {
        if(is_cigar) {
            ///this condition is always wrong
            ///in best case, r_ts = threshold, t_end = aln_l - thres - 1
            if (((t_end+1) == aln_l) || (r_ts == 0)) {
                if(recal_boundary(q_string, tstr1, ql, thres, t_s, r_ts, t_end,
                aux_beg, aux_end, error, id, aln_l, rev, dumy, rref, hpc_g, uref, 
                &t_s, &r_ts, &t_end, &aux_beg, &aux_end, &error)) {
                    t_string = update_des_str(tstr, t_s, aln_l-aux_beg-aux_end, rev, rref, hpc_g, uref,
                                                    id, aux_beg, aux_end, hpc_g?NULL:tstr1);
                }
            }
        }

        kv_pushp(window_list, z->w_list, &p);
        p->x_start = qs; p->x_end = qe; ///must set x_start/x_end here
        if(is_cigar) {
            generate_cigar(dumy->path, dumy->path_length, p, &(z->w_list), &r_ts, &t_end, &error, q_string, ql, t_string);  
        } else {
            p->cidx = p->clen = 0;
        }
        p->y_start = t_s + r_ts;///difference
        p->y_end = t_s + t_end;
        p->error = error;
        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 $0x78, %rsp
movq %r8, %r15
movq %rdx, %rbx
movq %rsi, %r11
movq 0xe8(%rsp), %rdx
movq 0xe0(%rsp), %rax
movq 0xd8(%rsp), %r14
movq 0xd0(%rsp), %r12
movl 0xc8(%rsp), %r10d
movl $0x0, 0xc(%rsp)
subq %r12, %r14
incq %r14
testq %rsi, %rsi
je 0x8a3c8
movl $0xf, %esi
cmpq %rdx, %r14
jge 0x8a39d
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdx
cmpq $0x1f, %rdx
movl $0x1f, %r8d
cmovlq %rdx, %r8
testq %rdx, %rdx
movl $0x1, %esi
cmovnel %r8d, %esi
cmpq $0x4, %r14
cmovll %r8d, %esi
leal (%rsi,%rsi), %edx
testl %esi, %esi
movl $0x2, %esi
cmovnel %edx, %esi
cmpl $0x4, %r14d
cmovll %edx, %esi
cmpl $0x20, %esi
movl $0x1f, %edx
cmovgel %esi, %edx
cmpl $0x12c, %r14d           # imm = 0x12C
cmovll %esi, %edx
jmp 0x8a42c
movl $0x1f, %esi
cmpq %rdx, %r14
jge 0x8a402
cvtsi2sd %r14, %xmm1
mulsd %xmm0, %xmm1
cvttsd2si %xmm1, %rdx
cmpq $0x1f, %rdx
movl $0x1f, %r8d
cmovlq %rdx, %r8
testq %rdx, %rdx
movl $0x1, %esi
cmovnel %r8d, %esi
cmpq $0x4, %r14
cmovll %r8d, %esi
testl %esi, %esi
movl $0x1, %edx
cmovnel %esi, %edx
cmpl $0x4, %r14d
cmovll %esi, %edx
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
mulsd 0x19b05d(%rip), %xmm0   # 0x225480
cvttsd2si %xmm0, %esi
cmpl %esi, %edx
cmovlel %esi, %edx
cmpl $0x1f, %edx
movl $0x1f, %esi
cmovll %edx, %esi
movslq %esi, %r8
movl %r10d, %r10d
testq %rbx, %rbx
movq %r10, 0x20(%rsp)
je 0x8a462
movq (%rbx), %rdx
movq 0x10(%rdx), %rsi
leaq (%r10,%r10,4), %rdx
movl (%rsi,%rdx,8), %r10d
shrl %r10d
andl $0x3fffffff, %r10d      # imm = 0x3FFFFFFF
jmp 0x8a487
testq %rcx, %rcx
je 0x8a47f
movq (%rcx), %rdx
movq 0x10(%rdx), %rsi
leaq (%r10,%r10,4), %rdx
movl (%rsi,%rdx,8), %r10d
andl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
jmp 0x8a487
movq 0x18(%r11), %rdx
movq (%rdx,%r10,8), %r10
leaq (%r14,%r8,2), %r13
movq $-0x1, %rbp
movq %rbp, 0xe0(%rsp)
movq %rbp, 0x48(%rsp)
movq %rbp, 0x30(%rsp)
movb $0x1, %dl
testq %rax, %rax
js 0x8a50d
cmpq %rax, %r10
jle 0x8a50d
leaq (%r8,%r8), %rsi
subq %rax, %rsi
addq %r10, %rsi
addq $0x1f, %rsi
cmpq %r13, %rsi
jl 0x8a50d
movq $0x0, 0x30(%rsp)
subq %r8, %rax
movq %rax, 0xe0(%rsp)
subq %rax, %r10
cmpq %r13, %r10
movq %r10, %rbp
cmovgeq %r13, %rbp
movq %r13, %rdx
subq %rbp, %rdx
movq %rdx, 0x48(%rsp)
testq %rax, %rax
jns 0x8a50b
addq %rax, %rbp
negq %rax
movq %rax, 0x30(%rsp)
movq $0x0, 0xe0(%rsp)
xorl %edx, %edx
xorl %esi, %esi
testb %dl, %dl
jne 0x8a9d2
leaq (%r8,%rbp), %rax
cmpq %r14, %rax
jl 0x8a9d2
movq %r13, 0x28(%rsp)
movq %rbx, 0x60(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdi, 0x68(%rsp)
movl 0xc0(%rsp), %edx
movq 0xe0(%rsp), %r10
movq 0x30(%rsp), %rbx
movq 0x48(%rsp), %r13
testq %r11, %r11
movq %r9, 0x18(%rsp)
movq %r8, 0x50(%rsp)
movq %r11, 0x70(%rsp)
je 0x8a5b8
movslq %ebx, %rbx
movq %r15, %r12
leaq (%r9,%rbx), %r15
movzbl %dl, %ecx
movq %r15, %rdi
movq %r10, %rsi
movq %rbp, %rdx
movq %r11, %r8
movq 0x20(%rsp), %r9
callq 0x337f7
movq 0x18(%rsp), %rdi
movl $0x4e, %esi
movq %rbx, %rdx
callq 0x20e0
addq %rbp, %r15
movslq %r13d, %rdx
movq %r15, %rdi
movq %r12, %r15
movq 0xd0(%rsp), %r12
movl $0x4e, %esi
jmp 0x8a6a3
movq 0x60(%rsp), %rax
testq %rax, %rax
je 0x8a5ea
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %rsi
testb %dl, %dl
je 0x8a646
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8a648
movq %r13, 0x40(%rsp)
movq %r9, %rdi
movl $0x4e, %esi
movq %rbx, %rdx
movq %r10, %r13
callq 0x20e0
addq 0x18(%rsp), %rbx
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movl 0xc8(%rsp), %eax
movzbl %al, %ecx
xorl %edi, %edi
movq %rbx, %rsi
movq %r13, %r8
movq %rbp, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
jmp 0x8a693
xorl %eax, %eax
movq %rsi, %rcx
addq %rax, %rcx
movq %r13, %rax
orq %rbx, %rax
je 0x8a9e3
movq %r13, 0x40(%rsp)
movq %r9, %rdi
movl $0x4e, %esi
movq %rbx, %rdx
movq %rcx, %r12
movq %r10, %r13
callq 0x20e0
addq 0x18(%rsp), %rbx
addq %r13, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq 0xd0(%rsp), %r12
movq %rbp, %rdx
callq 0x2160
addq %rbp, %rbx
movq %rbx, %rdi
movl $0x4e, %esi
movq 0x40(%rsp), %rdx
callq 0x20e0
movq 0x50(%rsp), %r8
movq 0x18(%rsp), %rcx
movq 0xb8(%rsp), %rbp
addq %r12, %r15
cmpl $0x0, 0xf0(%rsp)
movq 0x68(%rsp), %r13
movq %r15, 0x40(%rsp)
movq %rcx, 0x10(%rsp)
je 0x8a720
leaq 0x7d70(%rbp), %rax
leaq 0xff0(%rbp), %r10
leaq 0xa14(%rbp), %r11
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
movq %r15, %rdx
leaq 0xc(%rsp), %r15
movzwl %r8w, %r8d
leaq 0x8(%rsp), %r9
movq %rcx, %rdi
movq 0x28(%rsp), %rsi
movl %r14d, %ecx
pushq %rbx
pushq %rbx
pushq %r11
pushq %r10
pushq %rax
pushq %r15
callq 0x8607d
addq $0x30, %rsp
jmp 0x8a73c
movzwl %r8w, %r8d
leaq 0x8(%rsp), %r9
movq %rcx, %rdi
movq 0x28(%rsp), %rsi
movq %r15, %rdx
movl %r14d, %ecx
callq 0x8578f
movl %eax, 0x3c(%rsp)
movl 0x8(%rsp), %r10d
cmpl $-0x1, %r10d
movl $0x0, %esi
je 0x8a9d2
cmpl $0x0, 0xf0(%rsp)
je 0x8a8e6
leal 0x1(%rax), %ecx
movslq %ecx, %rcx
movq 0x28(%rsp), %r11
cmpq %rcx, %r11
sete %dl
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x8a8e6
movq 0xb0(%rsp), %rsi
movslq %ecx, %r9
leaq 0xe0(%rsp), %r12
movq (%r12), %r8
cltq
leaq 0x30(%rsp), %r15
leaq 0x48(%rsp), %rbx
subq $0x8, %rsp
leaq 0x10(%rsp), %r13
movq 0x48(%rsp), %rdi
movq %r14, %rdx
movq 0x58(%rsp), %rcx
pushq %r13
movq 0x78(%rsp), %r13
pushq %rbx
pushq %r15
leaq 0x5c(%rsp), %rbx
pushq %rbx
leaq 0x34(%rsp), %rbx
pushq %rbx
pushq %r12
movq 0x108(%rsp), %r12
pushq 0x90(%rsp)
movq 0xa0(%rsp), %r15
pushq %r15
pushq 0xb8(%rsp)
pushq %rbp
movl 0x118(%rsp), %ebx
pushq %rbx
pushq %r11
pushq 0x88(%rsp)
pushq %r10
leaq 0xc0(%rsp), %r10
pushq (%r10)
leaq 0xb0(%rsp), %r10
pushq (%r10)
pushq %rax
callq 0x9b9c1
addq $0x90, %rsp
testl %eax, %eax
je 0x8a8e6
xorl %eax, %eax
testq %r15, %r15
cmoveq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
jne 0x8a8e6
movq 0x30(%rsp), %r15
movq 0x48(%rsp), %r9
movq 0x28(%rsp), %r12
subq %r15, %r12
subq %r9, %r12
movq 0xe0(%rsp), %r10
movq 0x70(%rsp), %r8
testq %r8, %r8
movq %r9, 0x10(%rsp)
je 0x8a9eb
movslq %r15d, %r15
movq 0x18(%rsp), %rbx
leaq (%rbx,%r15), %r13
movl 0xc0(%rsp), %eax
movzbl %al, %ecx
movq %r13, %rdi
movq %r10, %rsi
movq %r12, %rdx
movq 0x20(%rsp), %r9
callq 0x337f7
movq %rbx, %rdi
movl $0x4e, %esi
movq %r15, %rdx
callq 0x20e0
addq %r12, %r13
movslq 0x10(%rsp), %rdx
movq %r13, %rdi
movq 0x68(%rsp), %r13
movl $0x4e, %esi
callq 0x20e0
movq %rbx, 0x10(%rsp)
movq 0xd0(%rsp), %r12
movq 0x50(%r13), %rax
cmpq %rax, 0x48(%r13)
jne 0x8a915
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x48(%r13), %rax
movq 0x58(%r13), %rcx
leaq 0x1(%rax), %rdx
shlq $0x5, %rax
leaq (%rcx,%rax), %rbx
movq %rdx, 0x48(%r13)
movl %r12d, (%rcx,%rax)
movq 0xd8(%rsp), %rdx
movl %edx, 0x4(%rcx,%rax)
cmpl $0x0, 0xf0(%rsp)
movq 0x40(%rsp), %r10
je 0x8a986
leaq 0x48(%r13), %rcx
movl 0x7d70(%rbp), %esi
addq $0xa14, %rbp            # imm = 0xA14
leaq 0x8(%rsp), %rax
leaq 0xc(%rsp), %r8
leaq 0x3c(%rsp), %r9
movq %rbp, %rdi
movq %rbx, %rdx
pushq 0x10(%rsp)
pushq %r14
pushq %r10
pushq %rax
callq 0x8663c
addq $0x20, %rsp
jmp 0x8a98e
movq $0x0, 0x18(%rbx)
movl 0xe0(%rsp), %eax
movl 0xc(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%rbx)
addl 0x3c(%rsp), %eax
movl %eax, 0xc(%rbx)
movzwl 0x8(%rsp), %eax
movw %ax, 0x14(%rbx)
movzwl 0x30(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x48(%rsp), %eax
movw %ax, 0x12(%rbx)
movq 0x50(%rsp), %rax
movw %ax, 0x16(%rbx)
addl %r14d, 0x28(%r13)
movl $0x1, %esi
movl %esi, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
addq %r10, %rcx
jmp 0x8a6b2
movq %r12, 0x28(%rsp)
movq 0x60(%rsp), %rax
testq %rax, %rax
movq 0x18(%rsp), %r12
je 0x8aa32
movq (%rax), %rax
movq 0x10(%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %rbx
movl 0xc0(%rsp), %edx
testb %dl, %dl
je 0x8aaa9
leaq (%rax,%rcx,8), %rax
movl (%rax), %eax
shrl %eax
andl $0x3fffffff, %eax       # imm = 0x3FFFFFFF
jmp 0x8aaab
movq %r12, %rdi
movl $0x4e, %esi
movq %r15, %rdx
movq %r10, %rbx
callq 0x20e0
addq %r12, %r15
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq 0x20(%rsp), %rcx
leaq (%rcx,%rcx,4), %rdx
shlq $0x3, %rdx
addq 0x10(%rax), %rdx
subq $0x8, %rsp
movl 0xc8(%rsp), %eax
movzbl %al, %ecx
xorl %edi, %edi
movq %r15, %rsi
movq %rbx, %r8
movq 0x30(%rsp), %rbx
movq %rbx, %r9
pushq $0x0
callq 0x36b0d
addq $0x10, %rsp
addq %rbx, %r15
movq %r15, %rdi
movl $0x4e, %esi
movq 0x10(%rsp), %rdx
callq 0x20e0
movq %r12, 0x10(%rsp)
jmp 0x8a8de
xorl %eax, %eax
addq %rax, %rbx
movq %r9, %rax
orq %r15, %rax
je 0x8ab0a
movq %r12, %rdi
movl $0x4e, %esi
movq %r15, %rdx
movq %r10, 0x20(%rsp)
callq 0x20e0
addq %r12, %r15
addq 0x20(%rsp), %rbx
movq %r15, %rdi
movq %rbx, %rsi
movq 0x28(%rsp), %r12
movq %r12, %rdx
callq 0x2160
addq %r12, %r15
movq %r15, %rdi
movl $0x4e, %esi
movq 0x10(%rsp), %rdx
callq 0x20e0
movq 0x18(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x8a8de
addq %r10, %rbx
jmp 0x8a8d9