// File: chhylp123[P]hifiasm/Correct.cpp
// Function: gen_extend_err_0(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, char*, char*, char*, Correct_dumy*, unsigned long*, long, double, long, long, long)
int64_t gen_extend_err_0(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, 
char *tstr, char *tstr_1, Correct_dumy* dumy, uint64_t *v_idx, int64_t block_s, double e_rate, 
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
    if(uref && v_idx && z->is_match == 4) {
        if(check_coverage_gap(v_idx, qs, qe, block_s)) {
            tot_e += THRESHOLD_MAX_SIZE; return tot_e;
        } 
    }
    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_adv(&(z->w_list.a[pk]), z, rref, hpc_g, uref, qstr, tstr, tstr_1, dumy, 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_adv(&(z->w_list.a[pk+1]), z, rref, hpc_g, uref, qstr, tstr, tstr_1, dumy, 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_cut(uref, hpc_g, rref, qstr, tstr, rev, id, qs, qe, tb[0], block_s, e_rate, ((rref)?THRESHOLD:THRESHOLD_MAX_SIZE),
                                                                    0, &(di[0]), NULL, NULL, &(al[0]))) {
            di[0] = ql; al[0] = 0;
        }
    }

    if(tb[1] != -1) {
        if(!ed_cut(uref, hpc_g, rref, qstr, tstr, rev, id, qs, qe, tb[1], block_s, e_rate,  ((rref)?THRESHOLD:THRESHOLD_MAX_SIZE),
                                                                    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 %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x38(%rbp)
movq %rsi, -0x40(%rbp)
movq %rdx, -0x48(%rbp)
movq %rcx, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %r9, -0x60(%rbp)
movsd %xmm0, -0x68(%rbp)
movq $0x0, -0x70(%rbp)
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0xb8(%rbp)
movq -0x38(%rbp), %rax
movl 0x10(%rax), %eax
movq %rax, -0xc8(%rbp)
movq -0x38(%rbp), %rax
movl 0x1c(%rax), %eax
movq %rax, -0xd0(%rbp)
movq 0x38(%rbp), %rax
addq $0x1, %rax
subq 0x30(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x0, -0x40(%rbp)
je 0x51704
cmpq $0x0, 0x20(%rbp)
je 0x51704
movq -0x38(%rbp), %rax
movzbl 0x2c(%rax), %eax
cmpl $0x4, %eax
jne 0x51704
movq 0x20(%rbp), %rdi
movq 0x30(%rbp), %rsi
movq 0x38(%rbp), %rdx
movq 0x28(%rbp), %rcx
callq 0x349f0
cmpq $0x0, %rax
je 0x51702
movq -0x70(%rbp), %rax
addq $0x1f, %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x51cd3
jmp 0x51704
movq 0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movl 0x4(%rcx), %ecx
subq %rcx, %rax
movq -0x38(%rbp), %rcx
movl 0x14(%rcx), %ecx
addq %rcx, %rax
movq %rax, -0x78(%rbp)
movq 0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x38, %rsi
callq 0x331a0
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x88(%rbp)
movq $0x0, -0x90(%rbp)
movq $-0x1, -0xa8(%rbp)
movq $-0x1, -0xb0(%rbp)
cmpq $0x0, 0x40(%rbp)
jle 0x5184a
movq 0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq 0x40(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movl 0x4(%rcx), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
cmpq %rcx, %rax
jne 0x5184a
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x51828
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x40(%rbp), %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x58(%rbp), %r9
movq -0x60(%rbp), %r14
movq 0x10(%rbp), %rbx
movq 0x18(%rbp), %r11
movq -0xd0(%rbp), %rax
movl %eax, %r10d
movq -0xc8(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movl %eax, 0x20(%rsp)
callq 0xa6b10
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
addl $0x1, %eax
cltq
movq %rax, -0xb0(%rbp)
movq 0x40(%rbp), %rax
addq $0x1, %rax
cmpq -0xb8(%rbp), %rax
jge 0x51934
movq 0x38(%rbp), %rax
addq $0x1, %rax
movq -0x38(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq 0x40(%rbp), %rdx
addq $0x1, %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jne 0x51934
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rbp), %rcx
addq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x1c(%rax)
jne 0x5190b
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rdi
movq 0x40(%rbp), %rax
addq $0x1, %rax
shlq $0x5, %rax
addq %rax, %rdi
movq -0x38(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x40(%rbp), %r8
movq -0x58(%rbp), %r9
movq -0x60(%rbp), %r14
movq 0x10(%rbp), %rbx
movq 0x18(%rbp), %r11
movq -0xd0(%rbp), %rax
movl %eax, %r10d
movq -0xc8(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movl %r10d, 0x18(%rsp)
movl %eax, 0x20(%rsp)
callq 0xa6b10
movq -0x38(%rbp), %rax
movq 0x58(%rax), %rax
movq 0x40(%rbp), %rcx
addq $0x1, %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x8(%rax), %rax
subq -0xd8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpq $-0x1, -0xb0(%rbp)
jne 0x5195c
cmpq $-0x1, -0xa8(%rbp)
jne 0x5195c
movq -0x78(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq %rax, -0xb0(%rbp)
jmp 0x519a6
cmpq $-0x1, -0xb0(%rbp)
jne 0x51980
cmpq $-0x1, -0xa8(%rbp)
je 0x51980
movq -0xa8(%rbp), %rax
movq %rax, -0xb0(%rbp)
jmp 0x519a4
cmpq $-0x1, -0xa8(%rbp)
jne 0x519a2
cmpq $-0x1, -0xb0(%rbp)
je 0x519a2
movq -0xb0(%rbp), %rax
movq %rax, -0xa8(%rbp)
jmp 0x519a4
jmp 0x519a6
cmpq $-0x1, -0xb0(%rbp)
je 0x51a94
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0xd0(%rbp), %rax
movl %eax, %r9d
movq -0xc8(%rbp), %rax
movl %eax, %r13d
movq 0x30(%rbp), %r12
movq 0x38(%rbp), %r15
movq -0xb0(%rbp), %r14
movq 0x28(%rbp), %rbx
movsd -0x68(%rbp), %xmm0
movq -0x50(%rbp), %r11
movl $0x1f, %eax
movl $0xf, %r10d
cmpq $0x0, %r11
cmovnel %r10d, %eax
movslq %eax, %r11
leaq -0x90(%rbp), %r10
leaq -0xa0(%rbp), %rax
movq %rax, -0xe0(%rbp)
xorl %eax, %eax
xorl %eax, %eax
movq -0xe0(%rbp), %rax
movl %r13d, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %rax, 0x50(%rsp)
callq 0xa9000
cmpl $0x0, %eax
jne 0x51a92
movq -0xd8(%rbp), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0xa0(%rbp)
jmp 0x51a94
cmpq $-0x1, -0xa8(%rbp)
je 0x51b88
movq -0x40(%rbp), %rdi
movq -0x48(%rbp), %rsi
movq -0x50(%rbp), %rdx
movq -0x58(%rbp), %rcx
movq -0x60(%rbp), %r8
movq -0xd0(%rbp), %rax
movl %eax, %r9d
movq -0xc8(%rbp), %rax
movl %eax, %r13d
movq 0x30(%rbp), %r12
movq 0x38(%rbp), %r15
movq -0xa8(%rbp), %r14
movq 0x28(%rbp), %rbx
movsd -0x68(%rbp), %xmm0
movq -0x50(%rbp), %r11
movl $0x1f, %eax
movl $0xf, %r10d
cmpq $0x0, %r11
cmovnel %r10d, %eax
movslq %eax, %r11
leaq -0x90(%rbp), %r10
addq $0x8, %r10
leaq -0xa0(%rbp), %rax
addq $0x8, %rax
movq %rax, -0xe8(%rbp)
xorl %eax, %eax
movq -0xe8(%rbp), %rax
movl %r13d, (%rsp)
movq %r12, 0x8(%rsp)
movq %r15, 0x10(%rsp)
movq %r14, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movq %r11, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movq %r10, 0x38(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
movq %rax, 0x50(%rsp)
callq 0xa9000
cmpl $0x0, %eax
jne 0x51b86
movq -0xd8(%rbp), %rax
movq %rax, -0x88(%rbp)
movq $0x0, -0x98(%rbp)
jmp 0x51b88
cmpq $0x0, -0xa0(%rbp)
je 0x51c4a
cmpq $0x0, -0x98(%rbp)
je 0x51c4a
movq -0xa0(%rbp), %rax
addq -0x98(%rbp), %rax
cmpq -0xd8(%rbp), %rax
jg 0x51beb
movq -0x90(%rbp), %rax
addq -0x88(%rbp), %rax
addq -0xd8(%rbp), %rax
movq -0xa0(%rbp), %rcx
addq -0x98(%rbp), %rcx
subq %rcx, %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x51c45
cvtsi2sdq -0xd8(%rbp), %xmm0
movq -0xa0(%rbp), %rax
addq -0x98(%rbp), %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x90(%rbp), %rax
addq -0x88(%rbp), %rax
cvtsi2sd %rax, %xmm0
movsd -0xc0(%rbp), %xmm2
cvtsi2sdq -0x70(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %rax
movq %rax, -0x70(%rbp)
jmp 0x51ccb
cmpq $0x0, -0xa0(%rbp)
jne 0x51c6f
cmpq $0x0, -0x98(%rbp)
jne 0x51c6f
movq -0xd8(%rbp), %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x51cc9
cmpq $0x0, -0xa0(%rbp)
je 0x51c9b
movq -0x90(%rbp), %rax
movq -0xd8(%rbp), %rcx
subq -0xa0(%rbp), %rcx
addq %rcx, %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x51cc7
cmpq $0x0, -0x98(%rbp)
je 0x51cc5
movq -0x88(%rbp), %rax
movq -0xd8(%rbp), %rcx
subq -0x98(%rbp), %rcx
addq %rcx, %rax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
jmp 0x51cc7
jmp 0x51cc9
jmp 0x51ccb
movq -0x70(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax)