// File: 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
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq 0x10(%rbp), %rax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movq %r9, -0x38(%rbp)
movsd %xmm0, -0x40(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x8(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movl 0x14(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jbe 0x109030
movq -0xa8(%rbp), %rax
cmpl $0x0, 0x10(%rax)
ja 0x10903c
movl $0x0, -0x4(%rbp)
jmp 0x1094e4
movq -0x28(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x109098
movq -0xa8(%rbp), %rax
movl 0x10(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movq %rax, -0xc0(%rbp)
movq -0xa8(%rbp), %rax
movl (%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
movq -0x38(%rbp), %rcx
movq 0x20(%rcx), %rcx
movq -0xc0(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
subq %rcx, %rax
movq %rax, -0xc8(%rbp)
jmp 0x1090ae
movq $0x0, -0xc0(%rbp)
movq $0x0, -0xc8(%rbp)
movq $0x0, -0xd0(%rbp)
movl $0x0, -0x44(%rbp)
movq $0x0, -0x98(%rbp)
movq $0x0, -0x90(%rbp)
movl -0x44(%rbp), %eax
movq -0xa0(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0x10946f
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movl %eax, -0x48(%rbp)
movq -0xd0(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0xd0(%rbp), %rax
movq -0x38(%rbp), %rcx
movq 0x20(%rcx), %rcx
movl -0x48(%rbp), %edx
movl (%rcx,%rdx,8), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
movq -0xa0(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, %eax
addq -0xd0(%rbp), %rax
movq %rax, -0xd0(%rbp)
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
ja 0x109167
jmp 0x109461
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
jb 0x10917c
jmp 0x10946f
movq -0x50(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
cmpq %rcx, %rax
jb 0x109198
movq -0x50(%rbp), %rax
movq %rax, -0xd8(%rbp)
jmp 0x1091a5
movq -0x28(%rbp), %rax
movl (%rax), %eax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rax
movq -0x28(%rbp), %rcx
movl 0x4(%rcx), %ecx
cmpq %rcx, %rax
ja 0x1091cd
movq -0x58(%rbp), %rax
movq %rax, -0xe0(%rbp)
jmp 0x1091db
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
cmpq -0x60(%rbp), %rax
jbe 0x109201
movq -0x68(%rbp), %rax
subq -0x60(%rbp), %rax
movq %rax, -0xe8(%rbp)
jmp 0x10920c
xorl %eax, %eax
movq %rax, -0xe8(%rbp)
jmp 0x10920c
movq -0xe8(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x70(%rbp)
ja 0x109223
jmp 0x109461
cmpq $0x0, -0x70(%rbp)
jbe 0x10922c
jmp 0x10924b
leaq 0x27e70e(%rip), %rdi    # 0x387941
leaq 0x27d683(%rip), %rsi    # 0x3868bd
movl $0x3e43, %edx           # imm = 0x3E43
leaq 0x27e76e(%rip), %rcx    # 0x3879b4
callq 0x2130
cmpq $-0x1, 0x10(%rbp)
je 0x1092b5
movsd -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1092b5
movaps 0x278da9(%rip), %xmm0   # 0x382010
movq -0x70(%rbp), %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movapd 0x278da8(%rip), %xmm0   # 0x382020
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2sd %eax, %xmm0
movsd 0x278f62(%rip), %xmm2   # 0x382208
mulsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jb 0x1092b5
jmp 0x109461
movq -0x60(%rbp), %rax
movq -0x28(%rbp), %rcx
movl (%rcx), %ecx
subq %rcx, %rax
movl %eax, %edi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x28(%rbp), %rax
subl (%rax), %esi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x28(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xb0(%rbp)
movq -0x28(%rbp), %rax
movl 0x4(%rax), %eax
subq -0x68(%rbp), %rax
movl %eax, %edi
movq -0x28(%rbp), %rax
movl 0x4(%rax), %esi
movq -0x28(%rbp), %rax
subl (%rax), %esi
movq -0x28(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x28(%rbp), %rax
subl 0xc(%rax), %edx
callq 0xc14d0
movl %eax, %eax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movb 0x28(%rax), %al
shrb $0x6, %al
andb $0x1, %al
cmpb $0x0, %al
je 0x109357
movq -0xb0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0xb8(%rbp), %rax
movq %rax, -0xb0(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x28(%rbp), %rax
movl 0xc(%rax), %eax
addq -0xb0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
subq -0xb8(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpq -0x78(%rbp), %rax
ja 0x10938a
jmp 0x109461
movq -0xa8(%rbp), %rdi
movq -0x20(%rbp), %rsi
movq -0x38(%rbp), %rdx
movq -0x78(%rbp), %rcx
movq -0x80(%rbp), %r8
movq -0x38(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x48(%rbp), %r9d
movl (%rax,%r9,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
leaq -0xc0(%rbp), %r9
leaq -0xc8(%rbp), %r10
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x108340
movq %rax, -0x88(%rbp)
cmpq $-0x1, 0x10(%rbp)
je 0x1093ec
xorps %xmm0, %xmm0
ucomisd -0x40(%rbp), %xmm0
jbe 0x109404
movq -0x88(%rbp), %rdx
movq -0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
addq (%rax,%rcx,8), %rdx
movq %rdx, (%rax,%rcx,8)
jmp 0x10945f
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl -0x44(%rbp), %ecx
movq (%rax,%rcx,8), %rax
addq -0x88(%rbp), %rax
movq 0x10(%rbp), %rcx
movq -0x38(%rbp), %rdx
movq 0x20(%rdx), %rdx
movl -0x48(%rbp), %esi
movl (%rdx,%rsi,8), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
movl %edx, %edx
imulq %rdx, %rcx
cmpq %rcx, %rax
ja 0x10945d
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0x10945f
jmp 0x109461
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1090d6
cmpq $-0x1, 0x10(%rbp)
je 0x1094dd
movsd -0x40(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x1094dd
movq -0x98(%rbp), %xmm0
movaps 0x278b7d(%rip), %xmm3   # 0x382010
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
movapd 0x278b81(%rip), %xmm2   # 0x382020
subpd %xmm2, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movq -0x90(%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 -0x40(%rbp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x1094dd
movl $0x1, -0x4(%rbp)
jmp 0x1094e4
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq