// File: /workspace/github_cpp_makefile_star2_O2/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 %r9, 0x38(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %rbp
movq 0xe8(%rsp), %rax
movq 0xe0(%rsp), %r15
movq 0xd8(%rsp), %r14
movl 0xc8(%rsp), %ebx
andl $0x0, 0x4(%rsp)
subq 0xd0(%rsp), %r14
incq %r14
pushq $0x1
popq %r8
movq %rsi, 0x18(%rsp)
testq %rsi, %rsi
movq %rdi, 0x68(%rsp)
je 0x563b3
cmpq %rax, %r14
jge 0x563e3
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 %r8d, %edi
cmpq $0x4, %r14
cmovll %ecx, %edi
jmp 0x563e6
cmpq %rax, %r14
jge 0x563f0
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
jmp 0x563f3
pushq $0xf
popq %rdi
movl %r14d, %esi
callq 0x51fcd
jmp 0x563fb
pushq $0x1f
popq %rdi
movl %r14d, %esi
callq 0x526f4
movslq %eax, %r12
leaq (%r14,%r12,2), %r13
movl %ebx, %ebx
testq %rbp, %rbp
je 0x56422
movq (%rbp), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbx, %rcx
movl (%rax,%rcx), %edx
shrl %edx
andl $0x3fffffff, %edx       # imm = 0x3FFFFFFF
jmp 0x5644f
movq 0x20(%rsp), %rax
testq %rax, %rax
je 0x56442
movq (%rax), %rax
movq 0x10(%rax), %rax
imulq $0x28, %rbx, %rcx
movl (%rax,%rcx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
jmp 0x5644f
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rax
movq (%rax,%rbx,8), %rdx
leaq 0x70(%rsp), %rax
leaq 0xe0(%rsp), %r10
leaq 0x30(%rsp), %r8
leaq 0x10(%rsp), %r9
movq %r12, %rdi
movq %r15, %rsi
movq %r13, %rcx
pushq %rax
pushq %r10
callq 0x1c7e7
popq %rcx
popq %rdx
xorl %r15d, %r15d
testl %eax, %eax
je 0x567d8
movq 0x70(%rsp), %rdx
leaq (%rdx,%r12), %rax
cmpq %r14, %rax
jl 0x567d8
movl 0xc0(%rsp), %eax
movq 0xe0(%rsp), %rsi
movq 0x30(%rsp), %r10
movq 0x18(%rsp), %r8
testq %r8, %r8
movq %rbx, 0x60(%rsp)
movq %r10, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
je 0x564f3
movq 0x10(%rsp), %r11
movzbl %al, %ecx
movq %rbx, %r9
movq %rbp, %rbx
movq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r11, 0x40(%rsp)
pushq %r11
pushq %r10
callq 0x1b475
addq $0x10, %rsp
movq %rbp, %rax
movq %rbx, %rbp
jmp 0x5651f
movq 0x10(%rsp), %r11
subq $0x8, %rsp
movzbl %al, %ecx
movq 0x40(%rsp), %rdi
movq %rbp, %r8
movq 0x28(%rsp), %r9
movq %r11, 0x48(%rsp)
pushq %r11
pushq %r10
pushq %rbx
callq 0x56ba5
addq $0x20, %rsp
movq 0xb8(%rsp), %rcx
movq 0x28(%rsp), %rdx
addq 0xd0(%rsp), %rdx
cmpl $0x0, 0xf0(%rsp)
movq %rdx, 0x28(%rsp)
movq %rax, 0x48(%rsp)
je 0x566b8
leaq 0x7d70(%rcx), %rbx
leaq 0xff0(%rcx), %r10
leaq 0xa14(%rcx), %r11
movzwl %r12w, %r8d
leaq 0x8(%rsp), %r9
movq %rax, %rdi
movl %r13d, %esi
movl %r14d, %ecx
pushq $-0x1
pushq $-0x1
pushq %r11
pushq %r10
pushq %rbx
leaq 0x2c(%rsp), %rax
pushq %rax
callq 0x51ff9
addq $0x30, %rsp
movl %eax, 0xc(%rsp)
leaq 0x8(%rsp), %rcx
movq %rcx, %r11
movl (%rcx), %r10d
pushq $-0x1
popq %rcx
cmpl %ecx, %r10d
movq 0x60(%rsp), %rbx
je 0x567d8
leal 0x1(%rax), %ecx
movslq %ecx, %rcx
cmpq %rcx, %r13
sete %dl
movl 0x4(%rsp), %ecx
testl %ecx, %ecx
sete %sil
orb %dl, %sil
cmpb $0x1, %sil
jne 0x566df
movq 0xb0(%rsp), %rsi
movslq %ecx, %r9
cltq
subq $0x8, %rsp
movq 0x30(%rsp), %rdi
movq %r14, %rdx
movq %r12, %rcx
movq 0x58(%rsp), %r8
pushq %r11
leaq 0x20(%rsp), %r15
pushq %r15
leaq 0x48(%rsp), %r15
pushq %r15
leaq 0x2c(%rsp), %r11
pushq %r11
leaq 0x2c(%rsp), %r11
pushq %r11
leaq 0x110(%rsp), %r11
pushq %r11
pushq 0x58(%rsp)
pushq %rbp
pushq 0x60(%rsp)
pushq 0x108(%rsp)
movl 0x118(%rsp), %r11d
pushq %r11
pushq %r13
pushq %rbx
pushq %r10
pushq 0xb8(%rsp)
pushq 0xd8(%rsp)
pushq %rax
callq 0x65642
addq $0x90, %rsp
testl %eax, %eax
je 0x566df
movq 0x30(%rsp), %rax
movq 0x10(%rsp), %r10
subq %rax, %r13
subq %r10, %r13
xorl %r11d, %r11d
testq %rbp, %rbp
movq 0xe0(%rsp), %rsi
cmoveq 0xb0(%rsp), %r11
subq $0x8, %rsp
movl 0xc8(%rsp), %ecx
movzbl %cl, %ecx
movq 0x40(%rsp), %rdi
movq %r13, %rdx
movq 0x20(%rsp), %r8
movq %rbp, %r9
pushq %r11
pushq %r10
pushq %rax
pushq %rbx
pushq 0x48(%rsp)
callq 0x658d7
addq $0x30, %rsp
movq %rax, 0x48(%rsp)
jmp 0x566df
movzwl %r12w, %r8d
leaq 0x8(%rsp), %rbx
movq %rax, %rdi
movl %r13d, %esi
movl %r14d, %ecx
movq %rbx, %r9
callq 0x516e4
movl %eax, 0xc(%rsp)
cmpl $-0x1, (%rbx)
je 0x567d8
movq 0x68(%rsp), %r13
leaq 0x48(%r13), %rbx
movq 0x48(%r13), %rcx
cmpq 0x50(%r13), %rcx
jne 0x5671b
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
pushq $0x2
popq %rsi
cmovneq %rax, %rsi
movq %rsi, 0x50(%r13)
movq 0x58(%r13), %rdi
shlq $0x5, %rsi
callq 0x2370
movq %rax, 0x58(%r13)
movq 0x48(%r13), %rcx
jmp 0x5671f
movq 0x58(%r13), %rax
leaq 0x1(%rcx), %rdx
shlq $0x5, %rcx
leaq (%rax,%rcx), %r15
movq %rdx, (%rbx)
movq 0xd0(%rsp), %rdx
movl %edx, (%rax,%rcx)
movq 0xd8(%rsp), %rdx
movl %edx, 0x4(%rax,%rcx)
cmpl $0x0, 0xf0(%rsp)
je 0x5678f
movq 0xb8(%rsp), %rdi
movl 0x7d70(%rdi), %esi
addq $0xa14, %rdi            # imm = 0xA14
leaq 0x8(%rsp), %rax
leaq 0x4(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %r15, %rdx
movq %rbx, %rcx
pushq 0x48(%rsp)
pushq %r14
pushq 0x38(%rsp)
pushq %rax
callq 0x52542
addq $0x20, %rsp
jmp 0x56794
andq $0x0, 0x18(%r15)
movl 0xe0(%rsp), %eax
movl 0x4(%rsp), %ecx
addl %eax, %ecx
movl %ecx, 0x8(%r15)
addl 0xc(%rsp), %eax
movl %eax, 0xc(%r15)
movzwl 0x8(%rsp), %eax
movw %ax, 0x14(%r15)
movzwl 0x30(%rsp), %eax
movw %ax, 0x10(%r15)
movzwl 0x10(%rsp), %eax
movw %ax, 0x12(%r15)
movw %r12w, 0x16(%r15)
addl %r14d, 0x28(%r13)
pushq $0x1
popq %r15
movl %r15d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq