// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xf0, %rsp
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movq %r9, -0x50(%rbp)
movsd %xmm0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0xc0(%rbp)
movq 0x28(%rbp), %rax
addq $0x1, %rax
subq 0x20(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq 0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x68(%rbp)
movq 0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
addq $0x38, %rsi
callq 0x331a0
addq -0x68(%rbp), %rax
movq %rax, -0x68(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x78(%rbp)
movq $0x0, -0x80(%rbp)
movq $-0x1, -0x98(%rbp)
movq $-0x1, -0xa0(%rbp)
cmpq $0x0, 0x30(%rbp)
jle 0x5252e
movq 0x20(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq 0x30(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x5252e
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x30(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5250c
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x30(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0xc0(%rbp), %rax
movl %eax, %r10d
movq -0xb8(%rbp), %rax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa8430
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x30(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xa0(%rbp)
movq 0x30(%rbp), %rax
addq $0x1, %rax
cmpq -0xa8(%rbp), %rax
jge 0x525fd
movq 0x28(%rbp), %rax
addq $0x1, %rax
movq -0x28(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq 0x30(%rbp), %rdx
addq $0x1, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jne 0x525fd
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x30(%rbp), %rcx
addq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x525d4
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x30(%rbp), %rax
addq $0x1, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movq -0xc0(%rbp), %rax
movl %eax, %r10d
movq -0xb8(%rbp), %rax
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
callq 0xa8430
movq -0x28(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x30(%rbp), %rcx
addq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
subq -0xc8(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $-0x1, -0xa0(%rbp)
jne 0x52625
cmpq $-0x1, -0x98(%rbp)
jne 0x52625
movq -0x68(%rbp), %rax
movq %rax, -0x98(%rbp)
movq %rax, -0xa0(%rbp)
jmp 0x5266f
cmpq $-0x1, -0xa0(%rbp)
jne 0x52649
cmpq $-0x1, -0x98(%rbp)
je 0x52649
movq -0x98(%rbp), %rax
movq %rax, -0xa0(%rbp)
jmp 0x5266d
cmpq $-0x1, -0x98(%rbp)
jne 0x5266b
cmpq $-0x1, -0xa0(%rbp)
je 0x5266b
movq -0xa0(%rbp), %rax
movq %rax, -0x98(%rbp)
jmp 0x5266d
jmp 0x5266f
cmpq $-0x1, -0xa0(%rbp)
je 0x52722
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xc0(%rbp), %rax
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movl %eax, %r8d
movq 0x20(%rbp), %r9
movq 0x28(%rbp), %r15
movq -0xa0(%rbp), %r14
movq 0x10(%rbp), %rbx
movsd -0x58(%rbp), %xmm0
movq 0x18(%rbp), %r11
leaq -0x80(%rbp), %r10
leaq -0x90(%rbp), %rax
xorl %r12d, %r12d
xorl %r12d, %r12d
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl $0x0, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0xa93a0
cmpl $0x0, %eax
jne 0x52720
movq -0xc8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x90(%rbp)
jmp 0x52722
cmpq $-0x1, -0x98(%rbp)
je 0x527da
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xc0(%rbp), %rax
movl %eax, %ecx
movq -0xb8(%rbp), %rax
movl %eax, %r8d
movq 0x20(%rbp), %r9
movq 0x28(%rbp), %r15
movq -0x98(%rbp), %r14
movq 0x10(%rbp), %rbx
movsd -0x58(%rbp), %xmm0
movq 0x18(%rbp), %r11
leaq -0x80(%rbp), %r10
addq $0x8, %r10
leaq -0x90(%rbp), %rax
addq $0x8, %rax
xorl %r12d, %r12d
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movl $0x1, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq $0x0, 0x38(%rsp)
movq %rax, 0x40(%rsp)
callq 0xa93a0
cmpl $0x0, %eax
jne 0x527d8
movq -0xc8(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x88(%rbp)
jmp 0x527da
cmpq $0x0, -0x90(%rbp)
je 0x5288d
cmpq $0x0, -0x88(%rbp)
je 0x5288d
movq -0x90(%rbp), %rax
addq -0x88(%rbp), %rax
cmpq -0xc8(%rbp), %rax
jg 0x52837
movq -0x80(%rbp), %rax
addq -0x78(%rbp), %rax
addq -0xc8(%rbp), %rax
movq -0x90(%rbp), %rcx
addq -0x88(%rbp), %rcx
subq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x5288b
cvtsi2sdq -0xc8(%rbp), %xmm0
movq -0x90(%rbp), %rax
addq -0x88(%rbp), %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x80(%rbp), %rax
addq -0x78(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd -0xb0(%rbp), %xmm2
cvtsi2sdq -0x60(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x60(%rbp)
jmp 0x52908
cmpq $0x0, -0x90(%rbp)
jne 0x528b2
cmpq $0x0, -0x88(%rbp)
jne 0x528b2
movq -0xc8(%rbp), %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x52906
cmpq $0x0, -0x90(%rbp)
je 0x528db
movq -0x80(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq -0x90(%rbp), %rcx
addq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x52904
cmpq $0x0, -0x88(%rbp)
je 0x52902
movq -0x78(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq -0x88(%rbp), %rcx
addq %rcx, %rax
addq -0x60(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x52904
jmp 0x52906
jmp 0x52908
movq -0x60(%rbp), %rax
addq $0xf0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)