// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_extend_err_0_non_retrieve_exz(overlap_region*, char*, char*, long, bit_extz_t*, unsigned long*, long, double, long, long, long, long)
int64_t gen_extend_err_0_non_retrieve_exz(overlap_region *z, char* qstr, char *tstr, int64_t tl, bit_extz_t *exz, uint64_t *v_idx, int64_t block_s, double e_rate, int64_t max_err, int64_t qs, int64_t qe, int64_t pk)
{
    int64_t tot_e = 0, ts, di[2], al[2], tb[2], an = z->w_list.n; double rr;
    int64_t id = z->y_id, rev = z->y_pos_strand, ql = qe + 1 - qs;
    ///check if there are some windows that cannot be algined by any overlaps/unitigs
    ///if no, it is likely that the UL read itself has issues
    
    ts = (qs - z->x_pos_s) + z->y_pos_s; ts += y_start_offset(qs, &(z->f_cigar));

    di[0] = di[1] = al[0] = al[1] = 0; tb[0] = tb[1] = -1;
    if((pk > 0) && (qs == (z->w_list.a[pk].x_end + 1))) {
        if(z->w_list.a[pk].clen == 0) {///do not have cigar
            gen_backtrace_non_retrieve_adv_exz(&(z->w_list.a[pk]), z, qstr, tstr, tl, exz, rev, id);
        }
        tb[0] = z->w_list.a[pk].y_end + 1;
    }

    if(((pk+1) < an) && ((qe+1) == (z->w_list.a[pk+1].x_start))) {
        if(z->w_list.a[pk+1].clen == 0) {///do not have cigar
            gen_backtrace_non_retrieve_adv_exz(&(z->w_list.a[pk+1]), z, qstr, tstr, tl, exz, rev, id);
        }
        tb[1] = z->w_list.a[pk+1].y_start-ql;
    }

    if(tb[0] == -1 && tb[1] == -1) tb[0] = tb[1] = ts;
    else if(tb[0] == -1 && tb[1] != -1) tb[0] = tb[1];
    else if(tb[1] == -1 && tb[0] != -1) tb[1] = tb[0];

    if(tb[0] != -1) {
        if(!ed_non_retrieve_cut(qstr, tstr, tl, rev, id, qs, qe, tb[0], block_s, e_rate, max_err,
                                                                    0, &(di[0]), NULL, NULL, &(al[0]))) {
            di[0] = ql; al[0] = 0;
        }
    }

    if(tb[1] != -1) {
        if(!ed_non_retrieve_cut(qstr, tstr, tl, rev, id, qs, qe, tb[1], block_s, e_rate, max_err,
                                                                    1, &(di[1]), NULL, NULL, &(al[1]))) {
            di[1] = ql; al[1] = 0;
        }
    }

    if(al[0] && al[1]) {///matched in both sides
        if((al[0] + al[1]) <= ql) {
            tot_e += di[0] + di[1] + ql - (al[0] + al[1]);
        } else {
            rr = ((double)ql)/((double)(al[0] + al[1]));
            tot_e += (di[0] + di[1])*rr;
        }
    } else if((!al[0]) && (!al[1])) {//failed
        tot_e += ql;
    } else if(al[0]) {
        tot_e += di[0] + (ql - al[0]);
    }else if(al[1]) {
        tot_e += di[1] + (ql - al[1]);
    }
    // if(z->y_id == 6) {
    //     fprintf(stderr, "-[M::%s] qs->%ld, ts->%ld, tb[0]->%ld, tb[1]->%ld, di[0]->%ld, di[1]->%ld, al[0]->%ld, al[1]->%ld, block_s->%ld, e_rate->%f\n", __func__, 
    //     qs, ts, tb[0], tb[1], di[0], di[1], al[0], al[1], block_s, e_rate);
    // }
    return tot_e;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x50(%rsp)
movq %r8, 0x48(%rsp)
movq %rcx, %r12
movq %rdx, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
movq 0xb0(%rsp), %r15
movq 0x48(%rdi), %rax
movq %rax, 0x20(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0x58(%rsp)
movl 0x10(%rdi), %eax
movq %rax, 0x38(%rsp)
movl 0x1c(%rdi), %eax
movq %rax, 0x40(%rsp)
movl 0x14(%rdi), %ebp
leaq 0x38(%rdi), %rbx
movl 0x40(%rdi), %esi
decl %esi
movq %rbx, %rdi
callq 0x3d110
cltq
movl 0x40(%r13), %r14d
cmpq %r15, %rax
movq %r12, 0x28(%rsp)
movq %rbp, 0x60(%rsp)
je 0x5c48d
testl %r14d, %r14d
je 0x5c8fc
xorl %r14d, %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3d110
cltq
cmpq %r15, %rax
jg 0x5c477
incq %r14
movl 0x40(%r13), %eax
cmpq %rax, %r14
jb 0x5c457
jmp 0x5c484
testq %r14, %r14
je 0x5c8fc
movl 0x40(%r13), %eax
cmpq %rax, %r14
je 0x5c8fc
movq 0xc0(%rsp), %rbp
movq 0xb8(%rsp), %rax
leaq 0x1(%rax), %r12
decl %r14d
movq %rbx, %rdi
movl %r14d, %esi
callq 0x3d11b
movq $-0x1, %r14
movq $-0x1, %r15
testq %rbp, %rbp
jle 0x5c523
movq 0x58(%r13), %rdx
movq %rbp, %rbx
shlq $0x5, %rbx
movslq 0x4(%rdx,%rbx), %rcx
incq %rcx
cmpq 0xb0(%rsp), %rcx
jne 0x5c523
leaq (%rdx,%rbx), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5c51b
movq %r13, %rsi
movl %eax, %r15d
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r9
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x8b162
movl %r15d, %eax
addq $0x10, %rsp
movq 0x58(%r13), %rdx
movslq 0xc(%rdx,%rbx), %r15
incq %r15
movq %r12, %r8
subq 0xb0(%rsp), %r8
incq %rbp
cmpq 0x20(%rsp), %rbp
jge 0x5c5a5
movq 0x58(%r13), %rdx
shlq $0x5, %rbp
movslq (%rdx,%rbp), %rcx
cmpq %rcx, %r12
movq 0xb8(%rsp), %r10
jne 0x5c5ad
leaq (%rdx,%rbp), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5c59b
movq %r13, %rsi
movl %eax, %r14d
movq 0x18(%rsp), %rdx
movq 0x10(%rsp), %rcx
movq %r8, %rbx
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %r9
pushq 0x38(%rsp)
pushq 0x48(%rsp)
callq 0x8b162
movl %r14d, %eax
movq %rbx, %r8
movq 0xc8(%rsp), %r10
addq $0x10, %rsp
movq 0x58(%r13), %rdx
movslq 0x8(%rdx,%rbp), %r14
subq %r8, %r14
jmp 0x5c5ad
movq 0xb8(%rsp), %r10
movq 0xa8(%rsp), %r13
movq 0xa0(%rsp), %r9
cmpq $-0x1, %r15
jne 0x5c5f1
cmpq $-0x1, %r14
jne 0x5c5f1
movq 0xb0(%rsp), %r12
movq %r12, %rdx
subq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
addq %rdx, %rcx
movslq %eax, %r15
addq %rcx, %r15
movq %r15, %r14
movq 0x28(%rsp), %rbp
jmp 0x5c622
cmpq $-0x1, %r15
movq 0xb0(%rsp), %r12
movq 0x28(%rsp), %rbp
jne 0x5c60f
cmpq $-0x1, %r14
je 0x5c60f
movq %r14, %r15
jmp 0x5c62c
cmpq $-0x1, %r15
movq %r14, %rax
cmovneq %r15, %rax
cmpq $-0x1, %r14
cmoveq %rax, %r14
cmpq $-0x1, %r15
je 0x5c6d9
movq %r10, %rcx
subq %r12, %rcx
incq %rcx
movq %r13, %rax
cmpq %r9, %rcx
jge 0x5c66e
cvtsi2sd %rcx, %xmm0
mulsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq $0x1f, %rax
movl $0x1f, %edx
cmovlq %rax, %rdx
testq %rax, %rax
movl $0x1, %eax
cmovneq %rdx, %rax
cmpq $0x4, %rcx
cmovlq %rdx, %rax
leal (%rax,%rax), %edx
testl %eax, %eax
movl $0x2, %esi
cmovnel %edx, %esi
cmpl $0x4, %ecx
cmovll %edx, %esi
cmpl $0x20, %esi
movl $0x1f, %eax
cmovgel %esi, %eax
movl $0x1f, %edx
cmpl $0x12c, %ecx            # imm = 0x12C
cmovll %esi, %eax
cmpl $0x1f, %eax
cmovgel %edx, %eax
testq %r15, %r15
sets %dl
cmpq %rbp, %r15
setge %sil
orb %dl, %sil
jne 0x5c6ce
cltq
leaq (%rax,%rax), %rdx
leaq (%rcx,%rax,2), %rsi
movq %rbp, %rdi
subq %r15, %rdi
addq %rdi, %rdx
addq $0x1f, %rdx
cmpq %rsi, %rdx
jge 0x5c6e3
xorl %r15d, %r15d
movq %r8, %rbx
jmp 0x5c767
xorl %ebx, %ebx
xorl %r15d, %r15d
jmp 0x5c767
movq %r8, 0x20(%rsp)
subq %rax, %r15
xorl %edi, %edi
testq %r15, %r15
cmovgq %r15, %rdi
movq 0x18(%rsp), %rdx
addq %r12, %rdx
addq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rbx
leaq 0x34(%rsp), %r15
movzwl %ax, %r8d
leaq 0xc(%rsp), %r9
pushq %rbx
pushq %r15
callq 0x9b415
addq $0x10, %rsp
cmpl $-0x1, (%r15)
setne %cl
movl (%rbx), %eax
cmpl $-0x1, %eax
setne %dl
testb %dl, %cl
je 0x5c74c
movslq %eax, %r15
incq %r15
movl 0xc(%rsp), %ebx
movq 0xb8(%rsp), %r10
movq 0x20(%rsp), %r8
jmp 0x5c75f
xorl %r15d, %r15d
movq 0x20(%rsp), %r8
movq %r8, %rbx
movq 0xb8(%rsp), %r10
movq 0xa0(%rsp), %r9
cmpq $-0x1, %r14
je 0x5c8c3
subq %r12, %r10
incq %r10
cmpq %r9, %r10
jge 0x5c7b1
xorps %xmm0, %xmm0
cvtsi2sd %r10, %xmm0
mulsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %r13d
cmovneq %rcx, %r13
cmpq $0x4, %r10
cmovlq %rcx, %r13
leal (,%r13,2), %eax
testl %r13d, %r13d
movl $0x2, %ecx
cmovnel %eax, %ecx
cmpl $0x4, %r10d
cmovll %eax, %ecx
cmpl $0x20, %ecx
movl $0x1f, %eax
cmovgel %ecx, %eax
movl $0x1f, %edx
cmpl $0x12c, %r10d           # imm = 0x12C
cmovll %ecx, %eax
cmpl $0x1f, %eax
cmovgel %edx, %eax
testq %r14, %r14
sets %cl
cmpq %rbp, %r14
setge %dl
orb %cl, %dl
jne 0x5c8c3
cltq
leaq (%rax,%rax), %rcx
leaq (%r10,%rax,2), %rsi
subq %r14, %rbp
addq %rbp, %rcx
addq $0x1f, %rcx
cmpq %rsi, %rcx
jl 0x5c8c3
movq %r8, %rbp
subq %rax, %r14
xorl %ecx, %ecx
testq %r14, %r14
cmovgq %r14, %rcx
movq 0x18(%rsp), %rdx
addq %r12, %rdx
movq 0x10(%rsp), %rdi
addq %rcx, %rdi
leaq 0x30(%rsp), %r14
leaq 0x34(%rsp), %r12
movzwl %ax, %r8d
leaq 0xc(%rsp), %r9
movl %r10d, %ecx
pushq %r14
pushq %r12
movq %r10, %r13
callq 0x9c751
addq $0x10, %rsp
cmpl $-0x1, (%r12)
setne %cl
movl (%r14), %eax
cmpl $-0x1, %eax
setne %dl
testb %dl, %cl
je 0x5c8c0
cltq
subq %rax, %r13
movq %rbp, %r8
je 0x5c8c3
movq %r13, %rcx
movl 0xc(%rsp), %eax
testq %r15, %r15
je 0x5c8e6
addq %r15, %rcx
cmpq %r8, %rcx
jle 0x5c8f1
cvtsi2sd %r8, %xmm0
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
addq %rax, %rbx
xorps %xmm1, %xmm1
cvtsi2sd %rbx, %xmm1
mulsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
jmp 0x5c8d7
movq %rbp, %r8
xorl %eax, %eax
testq %r15, %r15
cmoveq %r8, %rax
je 0x5c8d7
addq %r8, %rbx
subq %r15, %rbx
movq %rbx, %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq %rcx, %r8
addq %rax, %r8
movq %r8, %rax
jmp 0x5c8d7
addq %r8, %rbx
subq %rcx, %rbx
addq %rax, %rbx
jmp 0x5c8d4
callq 0x5eb7