// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: append_cov_line_ug_rid_cov_t(unsigned long, unsigned long*, u_trans_t*, ug_rid_cov_t*, unsigned long, double)
uint32_t append_cov_line_ug_rid_cov_t(uint64_t uid, uint64_t *qcc, u_trans_t *p, ug_rid_cov_t *idx, uint64_t hom_cut, double cut_rate)
{
    uint32_t k, rid; uint64_t qs, qe, oqs, oqe, ovq, ots, ote, no, tot, ava; 
    ma_utg_t *qu, *tu; uint64_t s_shift, e_shift; int64_t itk, itl, l;
    qu = &(idx->ug->u.a[p->qn]); tu = &(idx->ug->u.a[p->tn]);
    if(qu->n <= 0 || tu->n <= 0) return 0;
    if(p->rev) {
        itk = tu->n-1; itl = ((int64_t)(tu->len))-((int64_t)(idx->rg->seq[itk].len));
    } else {
        itk = 0; itl = 0;
    }
    // if(uid == 7929) {
    //     fprintf(stderr, "*****[M::%s]\tutg%.6lu%c\tqu->n::%u\ttu->n::%u\n", __func__, 
    //             uid+1, "lc"[idx->ug->u.a[uid].circ], (uint32_t)qu->n, (uint32_t)tu->n);
    // }
    for (k = l = 0, tot = ava = 0; k < qu->n; k++) {
        rid = qu->a[k]>>33;
        qs = l; qe = l + idx->rg->seq[rid].len;
        l += (uint32_t)qu->a[k];
        if(qe <= p->qs) continue;
        if(qs >= p->qe) break;
        ///qe > p->qs && qs < p->qe && qe > qs

        oqs = MAX(qs, p->qs); oqe = MIN(qe, p->qe);
        ovq = ((oqe > oqs)? (oqe - oqs):0);
        
        // if(!(ovq > 0)) {
        //     fprintf(stderr, "[M::%s::] qn::%u, tn::%u, q::[%lu, %lu), p->q::[%u, %u)\n", 
        //     __func__, p->qn, p->tn, qs, qe, p->qs, p->qe);
        // }
        if(ovq <= 0) continue;
        assert(ovq > 0);
        if((hom_cut != ((uint64_t)-1)) && (cut_rate >= 0) && (ovq <= (idx->rg->seq[rid].len*0.55))) {
            continue;
        }

        s_shift = get_offset_adjust(oqs-p->qs, p->qe-p->qs, p->te-p->ts);
        e_shift = get_offset_adjust(p->qe-oqe, p->qe-p->qs, p->te-p->ts);
        if(p->rev) {
            ovq = s_shift; s_shift = e_shift; e_shift = ovq;
        }
        ots = p->ts + s_shift; ote = p->te-e_shift;
        if(ote <= ots) continue;
        no = get_ovlp_cov_1(tu, idx->cov.a + idx->idx[p->tn], idx->rg, ots, ote, &itk, &itl, idx->rg->seq[rid].len);
        // if(uid == 7929) {
        //     fprintf(stderr, "[%u]\trid::%u\tno::%lu\tqcc[k]::%lu\trlen::%u\n", 
        //     k, rid, no, qcc[k], idx->rg->seq[rid].len);
        // }
        // to = qcc[k] + no;
        // qcc[k] = to;
        if((hom_cut == ((uint64_t)-1)) || (cut_rate < 0)) {
            qcc[k] += no;
        } else {
            tot++; 
            if((qcc[k] + no) <= (hom_cut*((uint64_t)idx->rg->seq[rid].len))) ava++;
        }        
    }

    // if(uid == 7929) {
    //     fprintf(stderr, "[M::%s]\tutg%.6u%c\t%u\t%u\t%u\t%c\tutg%.6u%c\t%u\t%u\t%u\tis_reliable::%u\tw::%f\n", __func__, 
    //         p->qn+1, "lc"[idx->ug->u.a[p->qn].circ], idx->ug->u.a[p->qn].len, p->qs, p->qe, "+-"[p->rev],
    //         p->tn+1, "lc"[idx->ug->u.a[p->tn].circ], idx->ug->u.a[p->tn].len, p->ts, p->te, ((p->f == RC_0) || (p->f == RC_1))?1:0, p->nw);
    //     fprintf(stderr, "[M::%s]\tutg%.6lu%c\thom_cut::%lu\tcut_rate::%f\tava::%lu\ttot::%lu\n", __func__, 
    //         uid+1, "lc"[idx->ug->u.a[uid].circ], hom_cut, cut_rate, ava, tot);
    // }

    if((hom_cut != ((uint64_t)-1)) && (cut_rate >= 0) && (ava >= (tot*cut_rate))) {
        return 1;
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, 0x20(%rsp)
movq (%rcx), %rax
movq 0x10(%rax), %r10
movl 0x8(%rdx), %eax
leaq (%rax,%rax,4), %rdi
movl 0x10(%r10,%rdi,8), %r9d
xorl %eax, %eax
testq %r9, %r9
je 0xc7809
movl 0x14(%rdx), %ecx
movq %rcx, 0x60(%rsp)
leaq (%rcx,%rcx,4), %rcx
leaq (%r10,%rcx,8), %rcx
movq %rcx, 0x50(%rsp)
movl 0x10(%rcx), %ecx
testl %ecx, %ecx
je 0xc7809
leaq (%r10,%rdi,8), %rax
testb $0x40, 0x28(%rdx)
movq %rdx, 0x8(%rsp)
jne 0xc7572
movq 0x20(%rsp), %rcx
movq 0x8(%rcx), %rcx
movq %rcx, 0x38(%rsp)
movq 0x20(%rcx), %rcx
movq %rcx, 0x10(%rsp)
xorl %ecx, %ecx
xorl %r10d, %r10d
jmp 0xc75b0
decl %ecx
movq 0x50(%rsp), %rdx
movl (%rdx), %r10d
movq %r9, %r11
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andq %r9, %r10
movq 0x20(%rsp), %rdx
movq 0x8(%rdx), %rdi
movq %rdi, 0x38(%rsp)
movq 0x20(%rdi), %rdi
movq %rdi, 0x10(%rsp)
movl (%rdi,%rcx,8), %edi
andq %r9, %rdi
movq %r11, %r9
movq 0x8(%rsp), %rdx
subq %rdi, %r10
leaq 0x70(%rsp), %rdi
movq %rcx, (%rdi)
leaq 0x68(%rsp), %rcx
movq %r10, (%rcx)
movq 0x18(%rax), %r15
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movl (%rdx), %r14d
setae %al
movq %r8, 0x58(%rsp)
cmpq $-0x1, %r8
setne %dil
sete %cl
andb %al, %dil
movb %dil, 0x6(%rsp)
ucomisd %xmm0, %xmm1
seta %al
orb %cl, %al
movb %al, 0x7(%rsp)
xorl %r13d, %r13d
xorl %r12d, %r12d
movq $0x0, 0x28(%rsp)
movq $0x0, 0x30(%rsp)
movq 0x10(%rsp), %r8
movsd %xmm0, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movq %r9, 0x18(%rsp)
movq (%r15,%r13,8), %rbx
movq %rbx, %rax
shrq $0x21, %rax
movl (%r8,%rax,8), %ebp
andl $0x7fffffff, %ebp       # imm = 0x7FFFFFFF
leaq (%r12,%rbp), %rax
cmpq %r14, %rax
jbe 0xc779e
movl 0x4(%rdx), %ecx
cmpq %rcx, %r12
jae 0xc77af
cmpq %r14, %r12
movq %r14, %r11
cmovaq %r12, %r11
cmpq %rcx, %rax
cmovaeq %rcx, %rax
movq %rax, %rdi
movl $0x0, %edx
subq %r11, %rdi
cmovaeq %rdi, %rdx
jbe 0xc7799
cmpb $0x0, 0x6(%rsp)
je 0xc76a0
xorps %xmm2, %xmm2
cvtsi2sd %rdx, %xmm2
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
mulsd 0x169ee2(%rip), %xmm1   # 0x231578
ucomisd %xmm2, %xmm1
jae 0xc7799
subl %r14d, %r11d
movl %ecx, %edi
subl %r14d, %edi
movq 0x8(%rsp), %rdx
movl 0xc(%rdx), %r10d
movl 0x10(%rdx), %r8d
movl %r8d, %r9d
subl %r10d, %r9d
xorps %xmm3, %xmm3
cvtsi2sd %r11, %xmm3
xorps %xmm1, %xmm1
cvtsi2sd %rdi, %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %r9, %xmm2
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rdi
movl %edi, %edi
subl %eax, %ecx
xorps %xmm3, %xmm3
cvtsi2sd %rcx, %xmm3
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rax
movl %eax, %eax
testb $0x40, 0x28(%rdx)
movq %rax, %rcx
cmoveq %rdi, %rcx
cmoveq %rax, %rdi
addq %r10, %rcx
subq %rdi, %r8
cmpq %rcx, %r8
jbe 0xc7760
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %rax
movq 0x60(%rsp), %rdx
movq (%rax,%rdx,8), %rsi
shlq $0x3, %rsi
addq 0x28(%rdi), %rsi
movq 0x50(%rsp), %rdi
movq 0x38(%rsp), %rdx
leaq 0x70(%rsp), %r9
pushq %rbp
leaq 0x70(%rsp), %rax
pushq %rax
callq 0xc7320
addq $0x10, %rsp
cmpb $0x0, 0x7(%rsp)
je 0xc776c
movq 0x40(%rsp), %rsi
addq %rax, (%rsi,%r13,8)
jmp 0xc7789
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
jmp 0xc779e
incq 0x30(%rsp)
movq 0x40(%rsp), %rsi
addq (%rsi,%r13,8), %rax
imulq 0x58(%rsp), %rbp
cmpq %rax, %rbp
sbbq $-0x1, 0x28(%rsp)
movsd 0x48(%rsp), %xmm0
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r8
movq 0x8(%rsp), %rdx
movl %ebx, %eax
addq %rax, %r12
incq %r13
cmpq %r13, %r9
jne 0xc7624
cmpb $0x0, 0x6(%rsp)
je 0xc7807
movsd 0x28(%rsp), %xmm4
movsd 0x169abc(%rip), %xmm1   # 0x231280
unpcklps %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
movapd 0x169ac1(%rip), %xmm2   # 0x231290
subpd %xmm2, %xmm4
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
addsd %xmm4, %xmm3
movsd 0x30(%rsp), %xmm4
unpcklps %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
subpd %xmm2, %xmm4
movapd %xmm4, %xmm1
unpckhpd %xmm4, %xmm1            # xmm1 = xmm1[1],xmm4[1]
addsd %xmm4, %xmm1
mulsd %xmm0, %xmm1
movl $0x1, %eax
ucomisd %xmm1, %xmm3
jae 0xc7809
xorl %eax, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq