// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: append_cov_line(unsigned long, unsigned long*, unsigned long*, u_trans_t*, ma_ug_t*, asg_t*, unsigned long, double)
uint32_t append_cov_line(uint64_t uid, uint64_t *qcc, uint64_t *cc, u_trans_t *p, ma_ug_t *ug, asg_t *sg, 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 = &(ug->u.a[p->qn]); tu = &(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)(sg->seq[itk].len));
    } else {
        itk = 0; itl = 0;
    }
    // if(uid == 315 || uid == 1055) {
    //     fprintf(stderr, "*****[M::%s]\tutg%.6lu%c\tqu->n::%u\ttu->n::%u\n", __func__, 
    //             uid+1, "lc"[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 + sg->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 <= (sg->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(tu, cc, sg, ots, ote, &itk, &itl, sg->seq[rid].len);
        // if(uid == 315 || uid == 1055) {
        //     fprintf(stderr, "[%u]\trid::%u\tno::%lu\tqcc[k]::%lu\trlen::%u\n", k, rid, no, qcc[k], sg->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)sg->seq[rid].len))) ava++;
        }        
    }

    // if(uid == 315 || uid == 1055) {
    //     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"[ug->u.a[p->qn].circ], ug->u.a[p->qn].len, p->qs, p->qe, "+-"[p->rev],
    //         p->tn+1, "lc"[ug->u.a[p->tn].circ], 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"[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 $0x58, %rsp
movq %rcx, %r14
movq %rsi, 0x40(%rsp)
movq 0x10(%r8), %rbp
movl 0x8(%rcx), %eax
imulq $0x28, %rax, %rcx
movl 0x10(%rbp,%rcx), %r11d
xorl %eax, %eax
testq %r11, %r11
je 0x8b512
movl 0x14(%r14), %esi
imulq $0x28, %rsi, %rsi
addq %rbp, %rsi
movq %rsi, 0x38(%rsp)
movl 0x10(%rsi), %r8d
testl %r8d, %r8d
je 0x8b512
testb $0x40, 0x28(%r14)
jne 0x8b2c1
xorl %r8d, %r8d
xorl %eax, %eax
jmp 0x8b2e1
decl %r8d
movq 0x38(%rsp), %rax
movl (%rax), %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andq %rsi, %rax
movq 0x20(%r9), %rdi
movl (%rdi,%r8,8), %edi
andq %rsi, %rdi
subq %rdi, %rax
addq %rcx, %rbp
leaq 0x50(%rsp), %rcx
movq %r8, (%rcx)
leaq 0x48(%rsp), %rcx
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
movq %rax, (%rcx)
setae %al
cmpq $-0x1, 0x90(%rsp)
setne %sil
sete %cl
andb %al, %sil
movb %sil, 0x6(%rsp)
ucomisd %xmm0, %xmm1
seta %al
orb %cl, %al
movb %al, 0x7(%rsp)
xorl %r13d, %r13d
movq $0x0, 0x18(%rsp)
movq $0x0, 0x10(%rsp)
xorl %r12d, %r12d
movsd %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movq %r11, 0x8(%rsp)
cmpq %r13, %r11
je 0x8b4b8
movq 0x18(%rbp), %rax
movq (%rax,%r13,8), %rbx
movq 0x20(%r9), %rax
movq %rbx, %rcx
shrq $0x21, %rcx
movl (%rax,%rcx,8), %r15d
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
leaq (%r12,%r15), %rax
movl (%r14), %r10d
cmpq %r10, %rax
jbe 0x8b4ab
movl 0x4(%r14), %ecx
cmpq %rcx, %r12
jae 0x8b4b8
cmpq %r10, %r12
movq %r10, %rsi
cmovaq %r12, %rsi
cmpq %rcx, %rax
cmovaeq %rcx, %rax
movq %rax, %r8
movl $0x0, %edi
subq %rsi, %r8
cmovaeq %r8, %rdi
jbe 0x8b4ab
cmpb $0x0, 0x6(%rsp)
je 0x8b3de
cvtsi2sd %rdi, %xmm2
cvtsi2sd %r15d, %xmm1
mulsd 0x1580f4(%rip), %xmm1   # 0x1e34c8
ucomisd %xmm2, %xmm1
jae 0x8b4ab
subl %r10d, %esi
movl %ecx, %edi
subl %r10d, %edi
movl 0xc(%r14), %r11d
movl 0x10(%r14), %r8d
movl %r8d, %r10d
subl %r11d, %r10d
cvtsi2sd %rsi, %xmm3
cvtsi2sd %rdi, %xmm1
cvtsi2sd %r10, %xmm2
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rsi
movl %esi, %esi
subl %eax, %ecx
cvtsi2sd %rcx, %xmm3
divsd %xmm1, %xmm3
mulsd %xmm2, %xmm3
cvttsd2si %xmm3, %rax
movl %eax, %eax
testb $0x40, 0x28(%r14)
movq %rax, %rcx
cmoveq %rsi, %rcx
cmoveq %rax, %rsi
addq %r11, %rcx
subq %rsi, %r8
cmpq %rcx, %r8
jbe 0x8b4a6
movq 0x38(%rsp), %rdi
movq %rdx, %rsi
movq %r9, %rdx
leaq 0x50(%rsp), %r9
pushq %r15
leaq 0x50(%rsp), %rax
pushq %rax
callq 0x8ac17
addq $0x10, %rsp
cmpb $0x0, 0x7(%rsp)
je 0x8b476
movq 0x40(%rsp), %rcx
addq %rax, (%rcx,%r13,8)
jmp 0x8b496
incq 0x18(%rsp)
movq 0x40(%rsp), %rcx
addq (%rcx,%r13,8), %rax
imulq 0x90(%rsp), %r15
cmpq %rax, %r15
sbbq $-0x1, 0x10(%rsp)
movsd 0x30(%rsp), %xmm0
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %rdx
movq 0x8(%rsp), %r11
movl %ebx, %eax
addq %rax, %r12
incq %r13
jmp 0x8b351
cmpb $0x0, 0x6(%rsp)
je 0x8b50b
movsd 0x10(%rsp), %xmm4
movsd 0x157da3(%rip), %xmm1   # 0x1e3270
unpcklps %xmm1, %xmm4            # xmm4 = xmm4[0],xmm1[0],xmm4[1],xmm1[1]
movapd 0x157da8(%rip), %xmm2   # 0x1e3280
subpd %xmm2, %xmm4
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
addsd %xmm4, %xmm3
movsd 0x18(%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
ucomisd %xmm1, %xmm3
jae 0x8b50f
xorl %eax, %eax
jmp 0x8b512
pushq $0x1
popq %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq