// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movsd %xmm0, -0x38(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x8(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x28(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jbe 0x108abe
movq -0xa0(%rbp), %rax
cmpl $0x0, 0x10(%rax)
ja 0x108aca
movl $0x0, -0x4(%rbp)
jmp 0x108f9f
movq -0x20(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x108b2a
movq -0xa0(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0xb8(%rbp)
movq -0xa0(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movq -0xb8(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
subq %rcx, %rax
movq %rax, -0xc0(%rbp)
jmp 0x108b40
movq $0x0, -0xb8(%rbp)
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movl $0x0, -0x3c(%rbp)
movq $0x0, -0x90(%rbp)
movq $0x0, -0x88(%rbp)
movl -0x3c(%rbp), %eax
movq -0x98(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x108f2a
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x40(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0xc8(%rbp), %rax
movq -0x28(%rbp), %rcx
movq 0x8(%rcx), %rcx
movq 0x20(%rcx), %rcx
movl -0x40(%rbp), %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x98(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0xc8(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
ja 0x108bfd
jmp 0x108f1c
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jb 0x108c12
jmp 0x108f2a
movq -0x48(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jb 0x108c2e
movq -0x48(%rbp), %rax
movq %rax, -0xd0(%rbp)
jmp 0x108c3b
movq -0x20(%rbp), %rax
movl (%rax), %eax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq -0x20(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
ja 0x108c63
movq -0x50(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x108c71
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jbe 0x108c97
movq -0x60(%rbp), %rax
subq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x108ca2
xorl %eax, %eax
movq %rax, -0xe0(%rbp)
jmp 0x108ca2
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
ja 0x108cb9
jmp 0x108f1c
cmpq $0x0, -0x68(%rbp)
jbe 0x108cc2
jmp 0x108ce1
leaq 0x27ec78(%rip), %rdi    # 0x387941
leaq 0x27dbed(%rip), %rsi    # 0x3868bd
movl $0x3dfb, %edx           # imm = 0x3DFB
leaq 0x27ec6d(%rip), %rcx    # 0x387949
callq 0x2130
cmpq $-0x1, -0x30(%rbp)
je 0x108d4f
movsd -0x38(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x108d4f
movaps 0x279313(%rip), %xmm0   # 0x382010
movq -0x68(%rbp), %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x279312(%rip), %xmm0   # 0x382020
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x40(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm0
movsd 0x2794c8(%rip), %xmm2   # 0x382208
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x108d4f
jmp 0x108f1c
movq -0x58(%rbp), %rax
movq -0x20(%rbp), %rcx
movl (%rcx), %ecx
subq %rcx, %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x20(%rbp), %rax
subl (%rax), %esi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
subq -0x60(%rbp), %rax
movl %eax, %edi
movq -0x20(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x20(%rbp), %rax
subl (%rax), %esi
movq -0x20(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x20(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x108df1
movq -0xa8(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
addq -0xa8(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x20(%rbp), %rax
movl 0x10(%rax), %eax
subq -0xb0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x78(%rbp), %rax
cmpq -0x70(%rbp), %rax
ja 0x108e24
jmp 0x108f1c
movq -0xa0(%rbp), %rdi
movq -0x28(%rbp), %rax
movq 0x28(%rax), %rsi
movq -0x28(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x20(%rbp), %rcx
movl 0x14(%rcx), %ecx
movq (%rax,%rcx,8), %rax
shlq $0x3, %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x70(%rbp), %rcx
movq -0x78(%rbp), %r8
movq -0x28(%rbp), %rax
movq 0x8(%rax), %rax
movq 0x20(%rax), %rax
movl -0x40(%rbp), %r9d
movl (%rax,%r9,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
leaq -0xb8(%rbp), %r9
leaq -0xc0(%rbp), %r10
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1086c0
movq %rax, -0x80(%rbp)
cmpq $-0x1, -0x30(%rbp)
je 0x108ea9
xorps %xmm0, %xmm0
ucomisd -0x38(%rbp), %xmm0
jbe 0x108ebe
movq -0x80(%rbp), %rdx
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x108f1a
movq -0x88(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x88(%rbp)
movq -0x18(%rbp), %rax
movl -0x3c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addq -0x80(%rbp), %rax
movq -0x30(%rbp), %rcx
movq -0x28(%rbp), %rdx
movq 0x8(%rdx), %rdx
movq 0x20(%rdx), %rdx
movl -0x40(%rbp), %esi
movl (%rdx,%rsi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, %edx
imulq %rdx, %rcx
cmpq %rcx, %rax
ja 0x108f18
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0x108f1a
jmp 0x108f1c
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x108b68
cmpq $-0x1, -0x30(%rbp)
je 0x108f98
movsd -0x38(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x108f98
movq -0x90(%rbp), %xmm0
movaps 0x2790c2(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x2790c6(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x88(%rbp), %xmm1
punpckldq %xmm3, %xmm1    # xmm1 = xmm1[0],xmm3[0],xmm1[1],xmm3[1]
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd -0x38(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x108f98
movl $0x1, -0x4(%rbp)
jmp 0x108f9f
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax,%rax)