// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xd0, %rsp
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, -0x30(%rbp)
movq %rsi, -0x38(%rbp)
movq %rdx, -0x40(%rbp)
movq %rcx, -0x48(%rbp)
movq %r8, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movsd %xmm0, -0x60(%rbp)
movsd %xmm1, -0x68(%rbp)
movsd %xmm2, -0x70(%rbp)
movq -0x30(%rbp), %rax
movq 0x48(%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
subl 0x4(%rcx), %eax
movl %eax, %eax
movq %rax, -0x78(%rbp)
cmpq $0x0, 0x38(%rbp)
je 0x52eef
movq 0x38(%rbp), %rax
movabsq $0x7fffffffffffffff, %rcx # imm = 0x7FFFFFFFFFFFFFFF
movq %rcx, (%rax)
cmpq $0x0, 0x30(%rbp)
je 0x52f2a
movq -0x78(%rbp), %rdi
movq -0x30(%rbp), %rax
movl 0x28(%rax), %eax
movl %eax, %esi
movsd -0x60(%rbp), %xmm0
xorl %edx, %edx
leaq -0x68(%rbp), %rcx
callq 0xa7100
cmpl $0x0, %eax
jne 0x52f2a
movsd 0x32f378(%rip), %xmm0   # 0x382298
movsd %xmm0, -0x28(%rbp)
jmp 0x53322
movq $0x0, -0xa0(%rbp)
movq $0x0, -0x98(%rbp)
movq -0x90(%rbp), %rax
subq $0x1, %rax
movq %rax, -0x80(%rbp)
movq -0x30(%rbp), %rax
movl 0x8(%rax), %eax
movq %rax, -0x88(%rbp)
cmpq $0x0, -0x80(%rbp)
jl 0x5317a
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl $0x1, %eax
movq -0x30(%rbp), %rcx
movq 0x58(%rcx), %rcx
movq -0x80(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
subl (%rcx), %eax
cltq
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movswq 0x14(%rax), %rax
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movslq 0x4(%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x5310c
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq 0x20(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
callq 0x9e2e0
movq -0xb0(%rbp), %rax
addq $0x1, %rax
subq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r12
movq 0x18(%rbp), %r15
movq 0x20(%rbp), %r14
movsd -0x68(%rbp), %xmm0
movq 0x28(%rbp), %rbx
movq -0xa8(%rbp), %r11
movq -0xb0(%rbp), %r10
movq -0x80(%rbp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x51cf0
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movsd -0x70(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x53107
cvtsi2sdq -0xa0(%rbp), %xmm0
cvtsi2sdq -0x78(%rbp), %xmm1
mulsd -0x70(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x53107
movsd 0x32f19b(%rip), %xmm0   # 0x382298
movsd %xmm0, -0x28(%rbp)
jmp 0x53322
jmp 0x52fea
movq -0x30(%rbp), %rax
movq 0x58(%rax), %rax
movq -0x80(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl (%rax), %eax
subl $0x1, %eax
cltq
movq %rax, -0x88(%rbp)
movsd -0x70(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x53167
cvtsi2sdq -0xa0(%rbp), %xmm0
cvtsi2sdq -0x78(%rbp), %xmm1
mulsd -0x70(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x53167
movsd 0x32f13b(%rip), %xmm0   # 0x382298
movsd %xmm0, -0x28(%rbp)
jmp 0x53322
jmp 0x53169
movq -0x80(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0x52f5d
movq -0x88(%rbp), %rax
movq -0x30(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jl 0x532c4
movq -0x30(%rbp), %rax
movl 0x4(%rax), %eax
subq $0x1, %rax
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
cmpq -0x88(%rbp), %rax
jge 0x532c2
movq -0xb0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa8(%rbp)
movq -0x30(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movq 0x20(%rbp), %rdx
leaq -0xb0(%rbp), %rcx
callq 0x9e2e0
movq -0xb0(%rbp), %rax
addq $0x1, %rax
subq -0xa8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
addq -0x98(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %r8
movq -0x58(%rbp), %r9
movq 0x10(%rbp), %r12
movq 0x18(%rbp), %r15
movq 0x20(%rbp), %r14
movsd -0x68(%rbp), %xmm0
movq 0x28(%rbp), %rbx
movq -0xa8(%rbp), %r11
movq -0xb0(%rbp), %r10
movq -0x80(%rbp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movq %r10, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x51cf0
addq -0xa0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movsd -0x70(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x532bd
cvtsi2sdq -0xa0(%rbp), %xmm0
cvtsi2sdq -0x78(%rbp), %xmm1
mulsd -0x70(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x532bd
movsd 0x32efe2(%rip), %xmm0   # 0x382298
movsd %xmm0, -0x28(%rbp)
jmp 0x53322
jmp 0x531a3
jmp 0x532c4
movq -0x98(%rbp), %rax
cmpq -0x78(%rbp), %rax
jne 0x532d3
jmp 0x532f2
leaq 0x32fc66(%rip), %rdi    # 0x382f40
leaq 0x32f03a(%rip), %rsi    # 0x38231b
movl $0x3480, %edx           # imm = 0x3480
leaq 0x32fd10(%rip), %rcx    # 0x382ffd
callq 0x2130
cmpq $0x0, 0x38(%rbp)
je 0x53307
movq -0xa0(%rbp), %rcx
movq 0x38(%rbp), %rax
movq %rcx, (%rax)
cvtsi2sdq -0xa0(%rbp), %xmm0
cvtsi2sdq -0x98(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movsd -0x28(%rbp), %xmm0
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nopw (%rax,%rax)