// File: /workspace/github_cpp_makefile_star2_O2/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 $0xb8, %rsp
movsd %xmm0, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq %rcx, 0x60(%rsp)
movq %rdx, 0x70(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, %r13
movq 0x110(%rsp), %r12
movq 0x108(%rsp), %rcx
movq 0x100(%rsp), %rbx
movq 0x48(%rdi), %rax
movq %rax, 0xb0(%rsp)
movl 0x4(%rdi), %eax
movq %rax, 0xa8(%rsp)
movl 0x10(%rdi), %eax
movl %eax, 0x4c(%rsp)
movl 0x1c(%rdi), %eax
movl %eax, 0x48(%rsp)
leaq 0x1(%rcx), %rax
movq %rax, 0x78(%rsp)
movl 0x14(%rdi), %r15d
leaq 0x38(%rdi), %rsi
movq %rbx, %rdi
callq 0x1bebf
movq %rbx, %r9
movq %rax, %rdx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x50(%rsp)
pushq $-0x1
popq %r14
movq %r14, %rbp
testq %r12, %r12
jle 0x2c99b
movq 0x58(%r13), %rax
movq %r12, %rbx
shlq $0x5, %rbx
movslq 0x4(%rax,%rbx), %rcx
incq %rcx
movq %r14, %rbp
cmpq %r9, %rcx
jne 0x2c99b
leaq (%rax,%rbx), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x2c993
movl 0x4c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x48(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rsi
movq %rdx, %rbp
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x60(%rsp), %r8
movq 0x98(%rsp), %r9
callq 0x576c5
movq %rbp, %rdx
movq 0x100(%rsp), %r9
movq 0x58(%r13), %rax
movslq 0xc(%rax,%rbx), %rbp
incq %rbp
movq 0x78(%rsp), %rsi
movq %rsi, %rbx
subq %r9, %rbx
incq %r12
cmpq 0xb0(%rsp), %r12
jge 0x2ca18
movq 0x58(%r13), %rax
shlq $0x5, %r12
movslq (%rax,%r12), %rcx
cmpq %rcx, %rsi
jne 0x2ca18
leaq (%rax,%r12), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x2ca10
movl 0x4c(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x48(%rsp), %eax
movl %eax, (%rsp)
movq %r13, %rsi
movq %rdx, 0x78(%rsp)
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq 0x60(%rsp), %r8
movq %r9, %r14
movq 0x98(%rsp), %r9
callq 0x576c5
movq 0x78(%rsp), %rdx
movq %r14, %r9
movq 0x58(%r13), %rax
movslq 0x8(%rax,%r12), %r14
subq %rbx, %r14
movq 0xf8(%rsp), %r13
movq 0xf0(%rsp), %r12
cmpq $-0x1, %rbp
jne 0x2ca52
cmpq $-0x1, %r14
jne 0x2ca52
movq %r9, %rax
subq 0xa8(%rsp), %rax
addq %rax, %r15
addq %rdx, %r15
movq %r15, %rbp
movq %r15, %r14
movq 0x60(%rsp), %r15
jmp 0x2ca7b
cmpq $-0x1, %rbp
movq 0x60(%rsp), %r15
jne 0x2ca68
cmpq $-0x1, %r14
je 0x2ca68
movq %r14, %rbp
jmp 0x2ca81
cmpq $-0x1, %rbp
movq %r14, %rax
cmovneq %rbp, %rax
cmpq $-0x1, %r14
cmoveq %rax, %r14
cmpq $-0x1, %rbp
je 0x2cafe
leaq 0x80(%rsp), %rax
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %r13, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq %rbp, 0x8(%rsp)
andl $0x0, 0x20(%rsp)
movq 0x108(%rsp), %rax
movq %rax, (%rsp)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movl 0x48(%rsp), %ecx
movl 0x4c(%rsp), %r8d
movq %r9, %rbp
movsd 0xa0(%rsp), %xmm0
callq 0x57ef8
testl %eax, %eax
jne 0x2cafb
movq %rbx, 0x50(%rsp)
andq $0x0, 0x80(%rsp)
movq %rbp, %r9
cmpq $-0x1, %r14
je 0x2cb70
leaq 0x88(%rsp), %rax
leaq 0x58(%rsp), %rcx
movq %rax, 0x40(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x30(%rsp)
movq %rcx, 0x28(%rsp)
movq %r13, 0x18(%rsp)
movq %r12, 0x10(%rsp)
movq %r14, 0x8(%rsp)
movq 0x108(%rsp), %rax
movq %rax, (%rsp)
movl $0x1, 0x20(%rsp)
movq 0x68(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq %r15, %rdx
movl 0x48(%rsp), %ecx
movl 0x4c(%rsp), %r8d
movsd 0xa0(%rsp), %xmm0
callq 0x57ef8
testl %eax, %eax
je 0x2cb7a
movq 0x88(%rsp), %rax
jmp 0x2cb81
movq %rbx, 0x58(%rsp)
xorl %eax, %eax
movq 0x80(%rsp), %rcx
testq %rcx, %rcx
setne %dl
testq %rax, %rax
setne %sil
andb %dl, %sil
cmpb $0x1, %sil
jne 0x2cbda
addq %rax, %rcx
movq %rbx, %rax
subq %rcx, %rax
jge 0x2cbf1
cvtsi2sd %rbx, %xmm0
cvtsi2sd %rcx, %xmm1
movq 0x58(%rsp), %rax
addq 0x50(%rsp), %rax
divsd %xmm1, %xmm0
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
jmp 0x2cbfe
movq %rcx, %rdx
orq %rax, %rdx
je 0x2cbfe
testq %rcx, %rcx
je 0x2cc13
subq %rcx, %rbx
addq 0x50(%rsp), %rbx
jmp 0x2cbfe
addq 0x50(%rsp), %rax
addq 0x58(%rsp), %rax
movq %rax, %rbx
movq %rbx, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rax, %rax
je 0x2cc22
subq %rax, %rbx
addq 0x58(%rsp), %rbx
jmp 0x2cbfe
xorl %ebx, %ebx
jmp 0x2cbfe