// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm0, 0x68(%rsp)
movq %r9, %rbx
movq %r8, %r15
movq %rcx, 0x48(%rsp)
movq %rdx, 0x40(%rsp)
movq %rdi, %r12
movq 0xe0(%rsp), %r13
movq 0xd0(%rsp), %rcx
movq 0x48(%rdi), %r8
movl 0x10(%rdi), %eax
movq %rax, 0x28(%rsp)
movl 0x1c(%rdi), %eax
movq %rax, 0x30(%rsp)
movq %rsi, 0x38(%rsp)
testq %rsi, %rsi
sete %al
testq %rcx, %rcx
sete %dl
orb %al, %dl
jne 0x5ba50
cmpb $0x4, 0x2c(%r12)
jne 0x5ba50
movq %r13, %rax
xorl %edx, %edx
divq 0xd8(%rsp)
movq (%rcx,%rax,8), %rdx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rdx, %rax
je 0x5ba50
shrq $0x20, %rdx
leaq (%rcx,%rdx,8), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
movl %esi, %edi
shrq $0x20, %rsi
xorq %r13, %rsi
xorq 0xe8(%rsp), %rdi
orq %rsi, %rdi
je 0x5bea8
incq %rdx
cmpq %rdx, %rax
jne 0x5ba2a
movq %r8, 0x80(%rsp)
movl 0x4(%r12), %eax
movq %rax, 0x70(%rsp)
movl 0x14(%r12), %eax
movq %rax, 0x78(%rsp)
leaq 0x38(%r12), %r14
movl 0x40(%r12), %esi
decl %esi
movq %r14, %rdi
callq 0x3d110
cltq
movl 0x40(%r12), %ebp
cmpq %r13, %rax
movq %rbx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
je 0x5bad7
testl %ebp, %ebp
je 0x5bec8
xorl %ebp, %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x3d110
cltq
cmpq %r13, %rax
jg 0x5bac0
incq %rbp
movl 0x40(%r12), %eax
cmpq %rax, %rbp
jb 0x5baa0
jmp 0x5bace
testq %rbp, %rbp
je 0x5bec8
movl 0x40(%r12), %eax
cmpq %rax, %rbp
je 0x5bec8
movq 0xf0(%rsp), %r13
movq 0xe8(%rsp), %rax
leaq 0x1(%rax), %rbx
decl %ebp
movq %r14, %rdi
movl %ebp, %esi
callq 0x3d11b
movl %eax, %edx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, (%rsp)
movq $-0x1, %rbp
movq $-0x1, %r15
testq %r13, %r13
jle 0x5bb94
movq 0x58(%r12), %rax
movq %r13, %r14
shlq $0x5, %r14
movslq 0x4(%rax,%r14), %rcx
incq %rcx
cmpq 0xe0(%rsp), %rcx
jne 0x5bb94
leaq (%rax,%r14), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5bb8c
subq $0x8, %rsp
movq %r12, %rsi
movl %edx, %r15d
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x20(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq 0x48(%rsp)
callq 0x89ef9
movl %r15d, %edx
addq $0x30, %rsp
movq 0x58(%r12), %rax
movslq 0xc(%rax,%r14), %r15
incq %r15
movq %rbx, %r14
subq 0xe0(%rsp), %r14
incq %r13
cmpq 0x80(%rsp), %r13
jge 0x5bc17
movq 0x58(%r12), %rax
shlq $0x5, %r13
movslq (%rax,%r13), %rcx
cmpq %rcx, %rbx
jne 0x5bc17
leaq (%rax,%r13), %rdi
cmpl $0x0, 0x1c(%rdi)
jne 0x5bc0f
subq $0x8, %rsp
movq %r12, %rsi
movl %edx, %ebx
movq 0x50(%rsp), %rdx
movq 0x48(%rsp), %rcx
movq 0x40(%rsp), %r8
movq 0x20(%rsp), %r9
pushq 0x30(%rsp)
pushq 0x40(%rsp)
pushq 0xe0(%rsp)
pushq 0xe0(%rsp)
pushq 0x48(%rsp)
callq 0x89ef9
movl %ebx, %edx
addq $0x30, %rsp
movq 0x58(%r12), %rax
movslq 0x8(%rax,%r13), %rbp
subq %r14, %rbp
cmpq $-0x1, %r15
jne 0x5bc4d
cmpq $-0x1, %rbp
jne 0x5bc4d
movq 0xe0(%rsp), %rax
subq 0x70(%rsp), %rax
movq 0x78(%rsp), %rcx
addq %rax, %rcx
movslq %edx, %r15
addq %rcx, %r15
movq %r15, %rbp
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jmp 0x5bc7b
cmpq $-0x1, %r15
movq 0x20(%rsp), %r12
movq 0x18(%rsp), %r13
jne 0x5bc68
cmpq $-0x1, %rbp
je 0x5bc68
movq %rbp, %r15
jmp 0x5bc85
cmpq $-0x1, %r15
movq %rbp, %rax
cmovneq %r15, %rax
cmpq $-0x1, %rbp
cmoveq %rax, %rbp
cmpq $-0x1, %r15
je 0x5bd43
xorl %eax, %eax
movq 0x48(%rsp), %rdx
testq %rdx, %rdx
sete %al
shll $0x4, %eax
orq $0xf, %rax
subq $0x60, %rsp
leaq 0xb0(%rsp), %rcx
movq %rcx, 0x50(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rsp)
leaq 0x60(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x138(%rsp), %rcx
movq %rcx, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq 0x148(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq 0x140(%rsp), %rcx
movq %rcx, 0x8(%rsp)
movq 0x88(%rsp), %rcx
movl %ecx, (%rsp)
movq %rax, 0x28(%rsp)
movl $0x0, 0x30(%rsp)
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %r13, %rcx
movq %r12, %r8
movq 0x90(%rsp), %r9
movsd 0xc8(%rsp), %xmm0
callq 0x8b761
addq $0x60, %rsp
testl %eax, %eax
jne 0x5bd43
movq %r14, (%rsp)
movq $0x0, 0x50(%rsp)
cmpq $-0x1, %rbp
je 0x5be11
leaq 0x58(%rsp), %rbx
leaq 0x8(%rsp), %rax
xorl %ecx, %ecx
movq 0x48(%rsp), %rdx
testq %rdx, %rdx
sete %cl
shll $0x4, %ecx
orq $0xf, %rcx
subq $0x60, %rsp
movq %rbx, 0x50(%rsp)
xorpd %xmm0, %xmm0
movupd %xmm0, 0x40(%rsp)
movq %rax, 0x38(%rsp)
movq 0x138(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rbp, 0x18(%rsp)
movq 0x148(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x88(%rsp), %rax
movl %eax, (%rsp)
movq %rcx, 0x28(%rsp)
movl $0x1, 0x30(%rsp)
movq 0x98(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq %r13, %rcx
movq %r12, %r8
movq 0x90(%rsp), %r9
movsd 0xc8(%rsp), %xmm0
callq 0x8b761
addq $0x60, %rsp
movq -0x8(%rbx), %rcx
testl %eax, %eax
jne 0x5be16
movq %r14, 0x8(%rsp)
testq %rcx, %rcx
setne %sil
xorl %edx, %edx
xorl %eax, %eax
jmp 0x5be73
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rax
testq %rcx, %rcx
setne %sil
testq %rax, %rax
setne %dl
movl %esi, %edi
andb %dl, %dil
cmpb $0x1, %dil
jne 0x5be73
addq %rax, %rcx
movq %r14, %rax
subq %rcx, %rax
jge 0x5be8c
xorps %xmm0, %xmm0
cvtsi2sd %r14, %xmm0
cvtsi2sd %rcx, %xmm1
movq 0x8(%rsp), %rax
addq (%rsp), %rax
divsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %rax, %xmm1
mulsd %xmm0, %xmm1
xorpd %xmm0, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r14
jmp 0x5beb3
movl %esi, %edi
orb %dl, %dil
testb $0x1, %dil
je 0x5beb3
testb %sil, %sil
je 0x5be9a
subq %rcx, %r14
addq (%rsp), %r14
jmp 0x5beb3
addq (%rsp), %rax
addq 0x8(%rsp), %rax
movq %rax, %r14
jmp 0x5beb3
testb %dl, %dl
je 0x5beb0
subq %rax, %r14
addq 0x8(%rsp), %r14
jmp 0x5beb3
movl $0x1f, %r14d
jmp 0x5beb3
xorl %r14d, %r14d
movq %r14, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5e5f