// File: /workspace/github_cpp_makefile_star2_O1/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 $0x58, %rsp
movsd %xmm0, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, (%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r12
movq 0xb0(%rsp), %r13
movq 0xa8(%rsp), %rax
movq 0xa0(%rsp), %rbx
movq 0x48(%rdi), %rcx
movq %rcx, 0x20(%rsp)
movl 0x4(%rdi), %ecx
movq %rcx, 0x50(%rsp)
movl 0x10(%rdi), %ecx
movq %rcx, 0x30(%rsp)
movl 0x1c(%rdi), %ecx
movq %rcx, 0x38(%rsp)
leaq 0x1(%rax), %r15
movl 0x14(%rdi), %r14d
leaq 0x38(%rdi), %rsi
movq %rbx, %rdi
callq 0x48df6
movq %rbx, %r11
movq $-0x1, %rbp
testq %r13, %r13
jle 0x5cccd
movq 0x58(%r12), %rdi
movq %r13, %rbx
shlq $0x5, %rbx
movslq 0x4(%rdi,%rbx), %rcx
incq %rcx
cmpq %r11, %rcx
jne 0x5cccd
addq %rbx, %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5ccc0
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %rax, %rbp
movq 0x10(%rsp), %rcx
movq (%rsp), %r8
movq 0x40(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
callq 0x8be12
movq %rbp, %rax
movq 0xb0(%rsp), %r11
addq $0x10, %rsp
movq 0x58(%r12), %rcx
movslq 0xc(%rcx,%rbx), %rbp
incq %rbp
movq %r15, %rbx
subq %r11, %rbx
incq %r13
cmpq 0x20(%rsp), %r13
jge 0x5cd4a
movq 0x58(%r12), %rdi
shlq $0x5, %r13
movslq (%rdi,%r13), %rcx
cmpq %rcx, %r15
movq (%rsp), %r8
movq $-0x1, %r15
jne 0x5cd55
addq %r13, %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5cd3b
movq %r12, %rsi
movq 0x18(%rsp), %rdx
movq %rax, 0x20(%rsp)
movq 0x10(%rsp), %rcx
movq 0x40(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
movq %r11, %r15
callq 0x8be12
movq 0x30(%rsp), %rax
movq %r15, %r11
movq 0x10(%rsp), %r8
addq $0x10, %rsp
movq 0x58(%r12), %rcx
movslq 0x8(%rcx,%r13), %r15
subq %rbx, %r15
jmp 0x5cd55
movq (%rsp), %r8
movq $-0x1, %r15
cmpq $-0x1, %rbp
jne 0x5cd77
cmpq $-0x1, %r15
jne 0x5cd77
movq %r11, %rcx
subq 0x50(%rsp), %rcx
addq %rcx, %r14
addq %rax, %r14
movq %r14, %rbp
movq %r14, %r15
jmp 0x5cd9b
cmpq $-0x1, %rbp
jne 0x5cd88
cmpq $-0x1, %r15
je 0x5cd88
movq %r15, %rbp
jmp 0x5cd9b
cmpq $-0x1, %rbp
movq %r15, %rax
cmovneq %rbp, %rax
cmpq $-0x1, %r15
cmoveq %rax, %r15
movq 0x98(%rsp), %r14
movq 0x90(%rsp), %rdx
cmpq $-0x1, %rbp
je 0x5ce81
movq 0xa8(%rsp), %rcx
subq %r11, %rcx
incq %rcx
movq %r14, %rax
cmpq %rdx, %rcx
jge 0x5cdfc
cvtsi2sd %rcx, %xmm0
mulsd 0x48(%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, %eax
cmovnel %edx, %eax
cmpl $0x4, %ecx
cmovll %edx, %eax
cmpl $0x20, %eax
movl $0x1f, %r12d
cmovgel %eax, %r12d
movl $0x1f, %edx
cmpl $0x12c, %ecx            # imm = 0x12C
cmovll %eax, %r12d
cmpl $0x1f, %r12d
cmovgel %edx, %r12d
movslq %r12d, %rdx
leaq (%rcx,%rdx,2), %rsi
testq %rbp, %rbp
sets %r9b
cmpq %r8, %rbp
setge %r10b
movb $0x1, %al
movq $-0x1, %rdi
orb %r9b, %r10b
jne 0x5ce94
leaq (%rdx,%rdx), %r9
movq %r8, %r10
subq %rbp, %r10
addq %r9, %r10
addq $0x1f, %r10
movb $0x1, %r9b
cmpq %rsi, %r10
jl 0x5ce97
subq %rdx, %rbp
xorl %r9d, %r9d
testq %rbp, %rbp
cmovleq %r9, %rbp
movq %rbp, %rdi
jmp 0x5ce97
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0xa8(%rsp), %rbp
jmp 0x5cf26
movb $0x1, %r9b
testb %r9b, %r9b
je 0x5cea6
movl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
xorl %ecx, %ecx
jmp 0x5cf09
movq 0x18(%rsp), %rax
leaq (%rax,%r11), %rdx
addq 0x10(%rsp), %rdi
movq %rbx, %r13
leaq 0x2c(%rsp), %rax
leaq 0x8(%rsp), %rbx
movzwl %r12w, %r8d
leaq 0xc(%rsp), %r9
pushq %rax
movq %rax, %r12
pushq %rbx
movq %r11, %rbp
callq 0x9aced
movq %rbp, %r11
addq $0x10, %rsp
cmpl $-0x1, (%rbx)
movq (%rsp), %r8
sete %cl
movl (%r12), %edx
movq %r13, %rbx
incl %edx
sete %al
orb %cl, %al
movslq %edx, %rdx
xorl %ecx, %ecx
testb $0x1, %al
cmoveq %rdx, %rcx
leaq 0xc(%rsp), %rdx
movl (%rdx), %r12d
movq 0xa8(%rsp), %rbp
movq 0x90(%rsp), %rdx
xorl %r13d, %r13d
testb $0x1, %al
cmovneq %rbx, %r12
cmoveq %rcx, %r13
cmpq $-0x1, %r15
je 0x5cff6
subq %r11, %rbp
incq %rbp
cmpq %rdx, %rbp
jge 0x5cf70
xorps %xmm0, %xmm0
cvtsi2sd %rbp, %xmm0
mulsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %rax
cmpq $0x1f, %rax
movl $0x1f, %ecx
cmovlq %rax, %rcx
testq %rax, %rax
movl $0x1, %r14d
cmovneq %rcx, %r14
cmpq $0x4, %rbp
cmovlq %rcx, %r14
leal (%r14,%r14), %eax
testl %r14d, %r14d
movl $0x2, %ecx
cmovnel %eax, %ecx
cmpl $0x4, %ebp
cmovll %eax, %ecx
cmpl $0x20, %ecx
movl $0x1f, %eax
cmovgel %ecx, %eax
movl $0x1f, %edx
cmpl $0x12c, %ebp            # imm = 0x12C
cmovll %ecx, %eax
cmpl $0x1f, %eax
cmovgel %edx, %eax
movslq %eax, %rdi
leaq (,%rdi,2), %rsi
addq %rbp, %rsi
testq %r15, %r15
sets %dl
cmpq %r8, %r15
setge %r9b
movb $0x1, %cl
movq $-0x1, %r10
orb %dl, %r9b
jne 0x5cffc
leaq (%rdi,%rdi), %rdx
subq %r15, %r8
leaq (%r8,%rdx), %r9
addq $0x1f, %r9
movb $0x1, %r8b
cmpq %rsi, %r9
jl 0x5cfff
subq %rdi, %r15
xorl %r8d, %r8d
testq %r15, %r15
cmovleq %r8, %r15
movq %r15, %r10
jmp 0x5cfff
xorl %eax, %eax
xorl %edx, %edx
jmp 0x5d073
movb $0x1, %r8b
testb %r8b, %r8b
je 0x5d00d
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %ebp, %ebp
jmp 0x5d067
movq 0x18(%rsp), %rdx
addq %r11, %rdx
movq 0x10(%rsp), %rdi
addq %r10, %rdi
leaq 0x2c(%rsp), %r14
leaq 0x8(%rsp), %r10
movzwl %ax, %r8d
leaq 0xc(%rsp), %r15
movl %ebp, %ecx
movq %r15, %r9
pushq %r14
pushq %r10
callq 0x9c033
addq $0x10, %rsp
movslq (%r14), %rax
subq %rax, %rbp
xorl %ecx, %ecx
cmpq $-0x1, %rax
cmoveq %rcx, %rbp
leaq 0x8(%rsp), %rax
cmpl $-0x1, (%rax)
cmoveq %rcx, %rbp
movl (%r15), %eax
testq %rbp, %rbp
sete %cl
xorl %edx, %edx
testb %cl, %cl
cmovneq %rbx, %rax
cmoveq %rbp, %rdx
testq %r13, %r13
je 0x5d0b4
testq %rdx, %rdx
je 0x5d0b4
addq %r13, %rdx
cmpq %rbx, %rdx
jle 0x5d0c9
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
cvtsi2sd %rdx, %xmm1
divsd %xmm1, %xmm0
addq %r12, %rax
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rbx
jmp 0x5d0d5
movq %r13, %rcx
orq %rdx, %rcx
je 0x5d0d5
testq %r13, %r13
je 0x5d0e7
addq %rbx, %r12
subq %r13, %r12
jmp 0x5d0d2
addq %rbx, %r12
addq %rax, %r12
subq %rdx, %r12
movq %r12, %rbx
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rdx, %rdx
je 0x5d0f7
addq %rbx, %rax
subq %rdx, %rax
movq %rax, %rbx
jmp 0x5d0d5
xorl %ebx, %ebx
jmp 0x5d0d5