// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: gen_extend_err_exz(overlap_region*, ul_idx_t const*, hpc_t*, All_reads*, char*, char*, bit_extz_t*, unsigned long*, long, double, double, double, long, long, long*)
double gen_extend_err_exz(overlap_region *z, const ul_idx_t *uref, hpc_t *hpc_g, All_reads *rref, char* qstr, 
char *tstr, bit_extz_t *exz, uint64_t *v_idx, int64_t block_s, double ovlp_cut, double e_rate, double e_max, int64_t max_err, int64_t sec_check, int64_t *r_e)
{
    int64_t ovl, k, ce, an = z->w_list.n, tot_l, tot_e, ws, we, ql; 
    ovl = z->x_pos_e+1-z->x_pos_s; if(r_e) (*r_e) = INT64_MAX;
    if((sec_check) && (!simi_pass(ovl, z->align_length, 0, ovlp_cut, &e_rate))) return DBL_MAX;
    // nw = get_num_wins(z->x_pos_s, z->x_pos_e+1, block_s);
    // for (k = 0; k < an; k++) {
    //     if(z->w_list.a[k].clen) z->w_list.a[k].y_end -= z->w_list.a[k].extra_begin;
    // }

    tot_l = tot_e = 0;
    for (k = an-1, ce = z->x_pos_e; k >= 0; k--) {
        // assert(k == 0 || z->w_list.a[k].x_end > z->w_list.a[k-1].x_start);//sorted
        tot_l += z->w_list.a[k].x_end + 1 - z->w_list.a[k].x_start;
        tot_e += z->w_list.a[k].error;///matched window

        we = z->w_list.a[k].x_end;
        while (we < ce) {
            ws = we+1;
            get_win_id_by_s(z, ws, block_s, &we);
            ql = we+1-ws; tot_l += ql; 
            tot_e += gen_extend_err_0_exz(z, uref, hpc_g, rref, qstr, tstr, exz, v_idx, block_s, e_rate, max_err, ws, we, k);
            if((e_max > 0) && (tot_e > (ovl*e_max))) return DBL_MAX;
        }
        ce = z->w_list.a[k].x_start-1;
        if((e_max > 0) && (tot_e > (ovl*e_max))) return DBL_MAX;
    }

    if(ce >= ((int64_t)z->x_pos_s)) {
        we = ((int64_t)z->x_pos_s)-1;
        while (we < ce) {
            ws = we+1;
            get_win_id_by_s(z, ws, block_s, &we);
            ql = we+1-ws; tot_l += ql; 
            tot_e += gen_extend_err_0_exz(z, uref, hpc_g, rref, qstr, tstr, exz, v_idx, block_s, e_rate, max_err, ws, we, k);
            if((e_max > 0) && (tot_e > (ovl*e_max))) return DBL_MAX;
        }
    }

    assert(tot_l == ovl); if(r_e) (*r_e) = tot_e;
    return (double)(tot_e)/(double)(tot_l);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm2, 0x18(%rsp)
movsd %xmm1, 0x38(%rsp)
movq %r9, 0x60(%rsp)
movq %r8, 0x58(%rsp)
movq %rcx, 0x50(%rsp)
movq %rdx, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq 0xd8(%rsp), %r10
movq 0xd0(%rsp), %rdx
movq 0x48(%rdi), %rbp
movl 0x4(%rdi), %eax
movq %rdi, 0x10(%rsp)
movl 0x8(%rdi), %ecx
movq %rcx, 0x20(%rsp)
incl %ecx
movl %ecx, %r8d
subl %eax, %r8d
testq %r10, %r10
je 0x2d03d
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movq %rsi, (%r10)
testq %rdx, %rdx
je 0x2d0a5
movq 0x10(%rsp), %rdx
movl 0x28(%rdx), %edx
testl %edx, %edx
sete %sil
cmpl %eax, %ecx
sete %al
orb %sil, %al
movsd 0x1b6500(%rip), %xmm0   # 0x1e3560
movsd %xmm0, 0x28(%rsp)
jne 0x2d090
cvtsi2sd %r8, %xmm2
movsd 0x1b622b(%rip), %xmm0   # 0x1e32a0
subsd 0x38(%rsp), %xmm0
mulsd %xmm2, %xmm0
movl %edx, %eax
cvtsi2sd %rax, %xmm1
ucomisd %xmm0, %xmm1
movapd %xmm2, %xmm0
jae 0x2d0aa
movsd 0x28(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cvtsi2sd %r8, %xmm0
movq %r8, 0x68(%rsp)
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %edi, %edi
movsd 0x1b649b(%rip), %xmm0   # 0x1e3560
movsd %xmm0, 0x28(%rsp)
xorl %ebx, %ebx
movq 0x10(%rsp), %r13
leaq -0x1(%rbp), %r12
testq %rbp, %rbp
jle 0x2d221
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rax
movq %r12, %r8
shlq $0x5, %r8
movslq 0x4(%rax,%r8), %rcx
movl (%rax,%r8), %edx
movq %r8, 0x70(%rsp)
movswq 0x14(%rax,%r8), %rax
movq %rcx, 0x8(%rsp)
subl %edx, %ecx
incl %ecx
movslq %ecx, %rcx
addq %rcx, %rdi
movq %rdi, (%rsp)
addq %rax, %rbx
movq %r12, %rbp
movq 0x8(%rsp), %r12
cmpq 0x20(%rsp), %r12
jge 0x2d1d9
leaq 0x1(%r12), %r14
movq %r13, %rdi
movq %r14, %rsi
movq 0xc0(%rsp), %r15
movq %r15, %rdx
leaq 0x8(%rsp), %rcx
callq 0x51f8b
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
subq %r12, %rcx
addq %rax, %rcx
movq %rcx, (%rsp)
subq $0x8, %rsp
movq %r13, %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %r9
movsd 0x40(%rsp), %xmm0
movq %rbp, %r12
pushq %rbp
pushq %rax
pushq %r14
pushq 0xe8(%rsp)
pushq %r15
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x2c426
addq $0x40, %rsp
addq %rax, %rbx
movsd 0x18(%rsp), %xmm0
ucomisd 0x1b63ab(%rip), %xmm0   # 0x1e3568
jbe 0x2d119
cvtsi2sd %rbx, %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x2d119
jmp 0x2d090
movq 0x10(%rsp), %rax
movq 0x58(%rax), %rax
movq 0x70(%rsp), %rcx
movslq (%rax,%rcx), %rax
decq %rax
movq %rax, 0x20(%rsp)
movsd 0x18(%rsp), %xmm0
ucomisd 0x1b6367(%rip), %xmm0   # 0x1e3568
movq (%rsp), %rdi
jbe 0x2d0d2
cvtsi2sd %rbx, %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x2d0d2
jmp 0x2d090
movq 0x10(%rsp), %rax
movl 0x4(%rax), %eax
cmpq %rax, 0x20(%rsp)
jl 0x2d30c
movq %rdi, (%rsp)
decq %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movq 0x10(%rsp), %r13
movq %r12, %rbp
movq 0x8(%rsp), %r12
cmpq 0x20(%rsp), %r12
jge 0x2d308
leaq 0x1(%r12), %r14
movq %r13, %rdi
movq %r14, %rsi
movq 0xc0(%rsp), %r15
movq %r15, %rdx
leaq 0x8(%rsp), %rcx
callq 0x51f8b
movq 0x8(%rsp), %rax
movq (%rsp), %rcx
subq %r12, %rcx
addq %rax, %rcx
movq %rcx, (%rsp)
subq $0x8, %rsp
movq %r13, %rdi
movq 0x48(%rsp), %rsi
movq 0x50(%rsp), %rdx
movq 0x58(%rsp), %rcx
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %r9
movsd 0x40(%rsp), %xmm0
movq %rbp, %r12
pushq %rbp
pushq %rax
pushq %r14
pushq 0xe8(%rsp)
pushq %r15
pushq 0xe8(%rsp)
pushq 0xe8(%rsp)
callq 0x2c426
addq $0x40, %rsp
addq %rax, %rbx
movsd 0x18(%rsp), %xmm0
ucomisd 0x1b627c(%rip), %xmm0   # 0x1e3568
jbe 0x2d248
cvtsi2sd %rbx, %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x2d248
jmp 0x2d090
movq (%rsp), %rdi
movq 0x68(%rsp), %rcx
cmpq %rcx, %rdi
jne 0x2d33f
movq 0xd8(%rsp), %rax
testq %rax, %rax
je 0x2d326
movq %rbx, (%rax)
cvtsi2sd %rbx, %xmm1
cvtsi2sd %rcx, %xmm0
divsd %xmm0, %xmm1
movsd %xmm1, 0x28(%rsp)
jmp 0x2d090
leaq 0x1b6f82(%rip), %rdi    # 0x1e42c8
leaq 0x1b6356(%rip), %rsi    # 0x1e36a3
leaq 0x1b7031(%rip), %rcx    # 0x1e4385
movl $0x3480, %edx           # imm = 0x3480
callq 0x2110