// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Correct.cpp
// Function: ff_tend(overlap_region*, long, long, double, double, long)
uint32_t inline ff_tend(overlap_region *z, int64_t wn, int64_t dn, double dr, double er, int64_t min_err)
{
    int64_t k, zwn = z->w_list.n, err, mm, qi, ci, cn, ql, ws, we, zs, ze, s, e, os, oe, ovlp; bit_extz_t ez; uint32_t cl; uint16_t c;
    zs = z->x_pos_s; ze = z->x_pos_e + 1; ql = ze - zs; 
    if(ql < wn) return 0; if(dn > (ql*dr)) dn = ql*dr; if(dn < wn) return 0; if(ql < dn) return 0;
    
    s = zs; e = zs + dn;
    // if(z->y_id == 27) fprintf(stderr, "-a-[M::%s] tid::%u\t%.*s\tz::[%ld,%ld)\ti::[%ld,%ld)\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), zs, ze, s, e);
    for (k = err = mm = 0, qi = zs; (k < zwn) && (z->w_list.a[k].x_start < e); k++) {
        // if(z->y_id == 27) fprintf(stderr, "-a-[M::%s] tid::%u\t%.*s\tw::[%d,%d)\terr::%d\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), z->w_list.a[k].x_start, z->w_list.a[k].x_end + 1, z->w_list.a[k].error);
        if(!(is_ualn_win(z->w_list.a[k]))) {
            set_bit_extz_t(ez, (*z), k); 
            ci = 0; cn = ez.cigar.n; qi = ez.ts; //ti = ez.ps;
            while (ci < cn && qi < e) {
                ws = qi;
                ci = pop_trace(&(ez.cigar), ci, &c, &cl);
                if(c!=2) qi += cl;
                // if(c!=3) ti += cl; 
                we = qi; 

                if(c == 0) {
                    os = MAX(s, ws); oe = MIN(e, we);
                    ovlp = ((oe>os)? (oe-os):0); mm += ovlp;
                } else {
                    err += cl;
                }
                // assert(is_ovlp_debug(s, e, ws, we, c));

                if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                    // fprintf(stderr, "-0-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                    return 1;
                }
            }
        } else {
            ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end + 1;
            err += we - ws;

            // assert(is_ovlp_debug(s, e, ws, we, -1));

            if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                // fprintf(stderr, "-1-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                return 1;
            }
        } 
    }

    s = ze - dn; e = ze;
    for (k = zwn - 1, err = mm = 0, qi = ze; (k >= 0) && ((z->w_list.a[k].x_end + 1) > s); k--) {
        if(!(is_ualn_win(z->w_list.a[k]))) {
            set_bit_extz_t(ez, (*z), k); 
            ci = ((int64_t)ez.cigar.n) - 1; qi = ez.te + 1; //ti = ez.pe + 1;
            while (ci >= 0 && qi > s) {
                we = qi;
                ci = pop_trace_back(&(ez.cigar), ci, &c, &cl);
                if(c!=2) qi -= cl;
                // if(c!=3) ti += cl; 
                ws = qi; 

                if(c == 0) {
                    os = MAX(s, ws); oe = MIN(e, we);
                    ovlp = ((oe>os)? (oe-os):0); mm += ovlp;
                } else {
                    err += cl;
                }

                // assert(is_ovlp_debug(s, e, ws, we, c));

                if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                    // fprintf(stderr, "-2-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                    return 1;
                }
            }
        } else {
            ws = z->w_list.a[k].x_start; we = z->w_list.a[k].x_end + 1;
            err += we - ws;

            // assert(is_ovlp_debug(s, e, ws, we, -1));

            if((err > min_err) && ((mm + err) > wn) && (err > ((mm + err)*er))) {
                // fprintf(stderr, "-3-[M::%s] tid::%u\t%.*s\tmm::%ld\terr::%ld\tdn::%ld\twn::%ld\tdif::%f\n", __func__, z->y_id, (int)Get_NAME_LENGTH(R_INF, z->y_id), Get_NAME(R_INF, z->y_id), mm, err, dn, wn, er);
                return 1;
            }
        } 
    }
    
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rcx, 0x20(%rsp)
movsd %xmm1, 0x18(%rsp)
movl 0x4(%rdi), %r15d
movq %rdi, 0x8(%rsp)
movl 0x8(%rdi), %ecx
incl %ecx
movq %rcx, 0x38(%rsp)
subq %r15, %rcx
xorl %eax, %eax
cmpq %rsi, %rcx
jl 0x63545
cvtsi2sd %rdx, %xmm1
cvtsi2sd %rcx, %xmm2
mulsd %xmm0, %xmm2
ucomisd %xmm2, %xmm1
cvttsd2si %xmm2, %rdi
cmovbeq %rdx, %rdi
cmpq %rsi, %rdi
setl %dl
cmpq %rdi, %rcx
setl %cl
orb %dl, %cl
jne 0x63545
movq %rsi, 0x30(%rsp)
movq 0x8(%rsp), %rax
movq 0x48(%rax), %rax
movq %rdi, 0x40(%rsp)
leaq (%rdi,%r15), %r12
testq %rax, %rax
movl $0x0, %ecx
movq %rax, 0x10(%rsp)
cmovgq %rax, %rcx
movq %rcx, 0x48(%rsp)
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %esi, %esi
cmpq 0x48(%rsp), %rsi
je 0x6338b
movq 0x8(%rsp), %rax
movq 0x58(%rax), %rax
movq %rsi, %rcx
shlq $0x5, %rcx
movl (%rax,%rcx), %edx
movslq %edx, %rbx
cmpq %rbx, %r12
jle 0x6338b
movq %rsi, 0x28(%rsp)
leaq (%rax,%rcx), %rdi
movswl 0x14(%rdi), %esi
cmpl $0x7fff, %esi           # imm = 0x7FFF
jne 0x63289
movl 0x1c(%rdi), %ebp
testl %ebp, %ebp
jne 0x6328d
xorl %ebp, %ebp
cmpw $0x0, 0x12(%rdi)
jns 0x6328d
movslq 0x4(%rdi), %rax
subq %rbx, %rax
addq %rax, %r13
incq %r13
cmpq 0x20(%rsp), %r13
movq 0x28(%rsp), %rsi
jle 0x63383
leaq (%r14,%r13), %rax
cmpq 0x30(%rsp), %rax
jle 0x63383
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x63383
jmp 0x63542
movl 0x1c(%rax,%rcx), %ebp
movl 0x18(%rax,%rcx), %edi
addq %rdi, %rdi
movq 0x8(%rsp), %r8
addq 0x70(%r8), %rdi
movq %rdi, 0x108(%rsp)
movl %ebp, %edi
movq %rdi, 0x100(%rsp)
movq %rdi, 0xf8(%rsp)
movl %edx, 0x6c(%rsp)
movl 0x4(%rax,%rcx), %edx
movl %edx, 0x70(%rsp)
movq 0x8(%rax,%rcx), %rax
movq %rax, 0x60(%rsp)
movl %esi, 0x7c(%rsp)
movl %esi, 0x78(%rsp)
xorl %eax, %eax
cmpl %ebp, %eax
jae 0x6337e
cmpq %r12, %rbx
jge 0x6337e
leaq 0xf8(%rsp), %rdi
movl %eax, %esi
leaq 0x2(%rsp), %rdx
leaq 0x4(%rsp), %rcx
callq 0x53a7d
movzwl 0x2(%rsp), %edi
movl 0x4(%rsp), %esi
leaq (%rbx,%rsi), %rdx
cmpw $0x2, %di
movq %rdx, %rcx
cmoveq %rbx, %rcx
testw %di, %di
je 0x63323
addq %rsi, %r13
jmp 0x63340
cmpq %r15, %rbx
cmovleq %r15, %rbx
cmpq %rdx, %r12
cmovlq %r12, %rdx
subq %rbx, %rdx
movl $0x0, %esi
cmovleq %rsi, %rdx
addq %rdx, %r14
movq %rcx, %rbx
cmpq 0x20(%rsp), %r13
jle 0x632d7
leaq (%r14,%r13), %rdx
movq %rcx, %rbx
cmpq 0x30(%rsp), %rdx
jle 0x632d7
cvtsi2sd %r13, %xmm0
cvtsi2sd %rdx, %xmm1
mulsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
movq %rcx, %rbx
jbe 0x632d7
jmp 0x63542
movq 0x28(%rsp), %rsi
incq %rsi
jmp 0x631ef
movq 0x38(%rsp), %r15
subq 0x40(%rsp), %r15
leaq 0x4(%rsp), %rbp
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x30(%rsp), %rbx
movq 0x10(%rsp), %rdx
movq %rdx, %rcx
shlq $0x5, %rcx
addq $-0x4, %rcx
xorl %eax, %eax
testq %rdx, %rdx
jle 0x63545
movq %rdx, 0x10(%rsp)
movq 0x8(%rsp), %rdx
movq 0x58(%rdx), %rdx
movslq -0x18(%rdx,%rcx), %rdi
leaq 0x1(%rdi), %r14
cmpq %r14, %r15
jge 0x63545
movswl -0x8(%rdx,%rcx), %eax
cmpl $0x7fff, %eax           # imm = 0x7FFF
jne 0x63441
movl (%rdx,%rcx), %esi
testl %esi, %esi
jne 0x63448
cmpw $0x0, -0xa(%rdx,%rcx)
jns 0x63446
movslq -0x1c(%rdx,%rcx), %rax
subq %rax, %r14
addq %r14, %r13
cmpq 0x20(%rsp), %r13
jle 0x63430
leaq (%r12,%r13), %rax
cmpq %rbx, %rax
jle 0x63430
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x63542
addq $-0x20, %rcx
movq 0x10(%rsp), %rdx
decq %rdx
jmp 0x633b5
movl (%rdx,%rcx), %esi
jmp 0x63448
xorl %esi, %esi
movl -0x4(%rdx,%rcx), %r8d
addq %r8, %r8
movq 0x8(%rsp), %r9
addq 0x70(%r9), %r8
movq %r8, 0x108(%rsp)
movl %esi, %esi
movq %rsi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movl -0x1c(%rdx,%rcx), %r8d
movl %r8d, 0x6c(%rsp)
movl %edi, 0x70(%rsp)
movq -0x14(%rdx,%rcx), %rcx
movq %rcx, 0x60(%rsp)
movl %eax, 0x7c(%rsp)
movl %eax, 0x78(%rsp)
decq %rsi
decq 0x10(%rsp)
testq %rsi, %rsi
js 0x633a5
cmpq %r15, %r14
jle 0x633a5
leaq 0xf8(%rsp), %rdi
leaq 0x2(%rsp), %rdx
movq %rbp, %rcx
callq 0x62f1e
movzwl 0x2(%rsp), %edi
movl 0x4(%rsp), %esi
movq %r14, %rdx
subq %rsi, %rdx
cmpw $0x2, %di
movq %rdx, %rcx
cmoveq %r14, %rcx
testw %di, %di
je 0x634e6
addq %rsi, %r13
jmp 0x63508
cmpq %rdx, %r15
cmovgq %r15, %rdx
movq 0x38(%rsp), %rsi
cmpq %rsi, %r14
cmovgeq %rsi, %r14
subq %rdx, %r14
movl $0x0, %edx
cmovleq %rdx, %r14
addq %r14, %r12
movslq %eax, %rsi
movq %rcx, %r14
cmpq 0x20(%rsp), %r13
jle 0x6349b
leaq (%r12,%r13), %rax
movq %rcx, %r14
cmpq %rbx, %rax
jle 0x6349b
cvtsi2sd %r13, %xmm0
cvtsi2sd %rax, %xmm1
mulsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
movq %rcx, %r14
jbe 0x6349b
pushq $0x1
popq %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq