// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: collect_trans_cov(char const*, buf_t*, unsigned long, buf_t*, unsigned long, ma_ug_t*, asg_t*, hap_cov_t*)
void collect_trans_cov(const char* cmd, buf_t* pri, uint64_t pri_offset, buf_t* aux, uint64_t aux_offset,
ma_ug_t *ug, asg_t *read_sg, hap_cov_t *cov)
{
    uint32_t i, k, rid, occ, ori, thre_pri;
    uint64_t len_aux, uLen, uCov;
    ma_utg_t* u = NULL;
    trans_chain* t_ch = cov->t_ch;
    if(pri->b.n == 0 || aux->b.n == 0) return;

    len_aux = set_utg_offset(aux->b.a, aux->b.n, ug, read_sg, cov->pos_idx, 0, 0);
    chain_trans_ovlp(cov, NULL, ug, read_sg, pri, len_aux, &thre_pri);
    if(thre_pri > 0)
    {
        /*******************************for debug************************************/
        // fprintf(stderr, "\n%s, thre_pri: %u, len_aux: %lu\n", cmd, thre_pri, len_aux);
        // print_buf_t(ug, pri, "pri");
        // print_buf_t(ug, aux, "aux");
        /*******************************for debug************************************/
        
        if(t_ch)
        {
            chain_origin_trans_uid_by_distance(cov, read_sg, pri->b.a, pri->b.n, pri_offset, NULL, 
            aux->b.a, aux->b.n, aux_offset, &len_aux, ug, RC_1, -1024, cmd);
        } 
        

        for (i = uCov = 0; i < aux->b.n; i++)
        {
            u = &(ug->u.a[aux->b.a[i]>>1]);
            if(u->n == 0) continue;
            ori = aux->b.a[i] & 1;
            for (k = 0; k < u->n; k++)
            {
                rid = (ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33));
                uCov += cov->cov[rid];

                if(t_ch) t_ch->ir_het[(ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33))] |= P_HET;
            }
        }

        for (i = uLen = occ = 0; i < pri->b.n; i++)
        {
            u = &(ug->u.a[pri->b.a[i]>>1]);
            if(u->n == 0) continue;
            ori = pri->b.a[i] & 1;
            for (k = 0; k < u->n; k++, occ++)
            {
                if(occ >= thre_pri) break;
                ///rid = u->a[k]>>33;
                rid = (ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33));
                uLen += read_sg->seq[rid].len;

                if(t_ch) t_ch->ir_het[(ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33))] |= P_HET;
            }
            if(occ >= thre_pri) break;
        }

        uCov = (uLen == 0? 0 : uCov / uLen);

        for (i = occ = 0; i < pri->b.n; i++)
        {
            u = &(ug->u.a[pri->b.a[i]>>1]);
            if(u->n == 0) continue;
            ori = pri->b.a[i] & 1;
            for (k = 0; k < u->n; k++, occ++)
            {
                if(occ >= thre_pri) break;
                ///rid = u->a[k]>>33;
                rid = (ori == 1?(u->a[u->n-k-1]>>33):(u->a[k]>>33));
                cov->cov[rid] += (uCov * read_sg->seq[rid].len);
            }
            if(occ >= thre_pri) break;
        }
    }
    set_utg_offset(aux->b.a, aux->b.n, ug, read_sg, cov->pos_idx, 1, 0);
}

// Assembly:
cmpq $0x0, 0x38(%rsi)
je 0xbe8c0
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, %rbx
movq %rsi, %r13
movq 0x38(%rcx), %rsi
testq %rsi, %rsi
je 0xbe8b2
movq %r9, %r12
movq %rdx, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movq 0x78(%rsp), %r15
movq 0x70(%rsp), %r14
movq 0xa8(%r15), %rbp
movq 0x48(%rbx), %rdi
movq 0x10(%r15), %r8
subq $0x8, %rsp
movq %r9, %rdx
movq %r14, %rcx
xorl %r9d, %r9d
pushq $0x0
callq 0xbc79b
addq $0x10, %rsp
movl %eax, %ecx
movq %rcx, 0x30(%rsp)
subq $0x8, %rsp
movq %rbx, 0x18(%rsp)
leaq 0xc(%rsp), %rbx
movq %r15, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %r13, %r8
movl %eax, %r9d
pushq %rbx
callq 0x10cd5c
addq $0x10, %rsp
cmpl $0x0, (%rbx)
movq 0x10(%rsp), %rbx
je 0xbe88a
testq %rbp, %rbp
je 0xbe604
movq 0x48(%r13), %rdx
movl 0x38(%r13), %ecx
movl 0x38(%rbx), %eax
subq $0x10, %rsp
leaq 0x40(%rsp), %r10
movsd 0x17777e(%rip), %xmm0   # 0x235d58
movq %r15, %rdi
movq 0x80(%rsp), %rsi
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq $0x1
pushq %r12
pushq %r10
pushq 0x48(%rsp)
pushq %rax
pushq 0x48(%rbx)
callq 0xbdac5
addq $0x40, %rsp
movq 0x38(%rbx), %rsi
xorl %ecx, %ecx
xorl %eax, %eax
testq %rsi, %rsi
je 0xbe68d
movq 0x10(%r12), %rdi
movq 0x48(%rbx), %rdx
movl (%rdx,%rcx,4), %edx
movl %edx, %r8d
shrl %r8d
leaq (%r8,%r8,4), %r9
movl 0x10(%rdi,%r9,8), %r8d
testl %r8d, %r8d
je 0xbe686
leaq (%rdi,%r9,8), %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
movq 0x18(%rsi), %r10
testb $0x1, %dl
jne 0xbe64c
movq (%r10,%r9,8), %r10
jmp 0xbe654
leal (%r8,%rdi), %r11d
movq (%r10,%r11,8), %r10
shrq $0x21, %r10
movq 0x8(%r15), %r11
movl (%r11,%r10,4), %r11d
addq %r11, %rax
testq %rbp, %rbp
je 0xbe675
movq 0x10(%rbp), %r8
orb $0x2, (%r8,%r10)
movl 0x10(%rsi), %r8d
incq %r9
movl %r8d, %r10d
decl %edi
cmpq %r10, %r9
jb 0xbe63d
movq 0x38(%rbx), %rsi
incl %ecx
cmpq %rcx, %rsi
ja 0xbe611
movq 0x38(%r13), %r8
testq %r8, %r8
je 0xbe88a
xorl %edx, %edx
xorl %ecx, %ecx
xorl %esi, %esi
movq %r12, 0x28(%rsp)
movq %r13, 0x8(%rsp)
movq 0x10(%r12), %r9
movq 0x48(%r13), %rdi
movl (%rdi,%rdx,4), %edi
movl %edi, %r10d
shrl %r10d
leaq (%r10,%r10,4), %rbx
movl 0x10(%r9,%rbx,8), %r10d
testl %r10d, %r10d
je 0xbe77d
movl 0x4(%rsp), %r11d
cmpl %r11d, %esi
jae 0xbe799
leaq (%r9,%rbx,8), %r8
movl %esi, %esi
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
xorl %r13d, %r13d
leal (%r10,%r9), %r14d
testb $0x1, %dil
movq 0x18(%r8), %r15
movq %r14, %rbx
cmoveq %r13, %rbx
movq (%r15,%rbx,8), %rbx
movq 0x70(%rsp), %r12
movq 0x20(%r12), %r12
shrq $0x21, %rbx
movl (%r12,%rbx,8), %ebx
testq %rbp, %rbp
je 0xbe73b
testb $0x1, %dil
movq 0x10(%rbp), %r10
cmoveq %r13, %r14
movq (%r15,%r14,8), %r11
shrq $0x21, %r11
orb $0x2, (%r10,%r11)
movl 0x10(%r8), %r10d
movl 0x4(%rsp), %r11d
andl $0x7fffffff, %ebx       # imm = 0x7FFFFFFF
addq %rbx, %rcx
leaq 0x1(%r13), %rbx
leal (%rsi,%r13), %r14d
incl %r14d
movl %r10d, %r15d
cmpq %r15, %rbx
jae 0xbe762
decl %r9d
movq %rbx, %r13
cmpl %r11d, %r14d
jb 0xbe6eb
cmpl %r11d, %r14d
jae 0xbe78a
addq %rbx, %rsi
movq 0x8(%rsp), %r13
movq 0x38(%r13), %r8
movq 0x28(%rsp), %r12
movq 0x78(%rsp), %r15
incl %edx
cmpq %rdx, %r8
ja 0xbe6aa
jmp 0xbe799
movq 0x28(%rsp), %r12
movq 0x78(%rsp), %r15
movq 0x8(%rsp), %r13
testq %rcx, %rcx
je 0xbe7a5
xorl %edx, %edx
divq %rcx
jmp 0xbe7a7
xorl %eax, %eax
movq 0x10(%rsp), %rbx
movq 0x38(%r13), %rcx
movq %rcx, 0x20(%rsp)
testq %rcx, %rcx
je 0xbe88a
movq 0x10(%r12), %rsi
movq 0x48(%r13), %rcx
movq %rcx, 0x8(%rsp)
xorl %edi, %edi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
xorl %r9d, %r9d
movq %rsi, 0x18(%rsp)
movq 0x8(%rsp), %rcx
movl (%rcx,%rdi,4), %r10d
movl %r10d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rdx
movl 0x10(%rsi,%rdx,8), %ecx
testl %ecx, %ecx
je 0xbe87d
cmpl 0x4(%rsp), %r9d
jae 0xbe88a
leaq (%rsi,%rdx,8), %r11
movq 0x70(%rsp), %rdx
movq 0x20(%rdx), %r13
movq 0x8(%r15), %rbp
movq 0x18(%r11), %rsi
movl %r9d, %r9d
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
xorl %edx, %edx
addl %ebx, %ecx
testb $0x1, %r10b
cmoveq %rdx, %rcx
movq (%rsi,%rcx,8), %rcx
shrq $0x21, %rcx
movl (%r13,%rcx,8), %r14d
andl %r8d, %r14d
imull %eax, %r14d
addl %r14d, (%rbp,%rcx,4)
leal (%r9,%rdx), %r14d
incl %r14d
incq %rdx
movl 0x10(%r11), %ecx
movl 0x4(%rsp), %r15d
cmpq %rcx, %rdx
jae 0xbe866
decl %ebx
cmpl %r15d, %r14d
jb 0xbe824
cmpl %r15d, %r14d
movq 0x10(%rsp), %rbx
movq 0x78(%rsp), %r15
movq 0x18(%rsp), %rsi
jae 0xbe88a
addq %rdx, %r9
incl %edi
cmpq %rdi, 0x20(%rsp)
ja 0xbe7dc
movq 0x48(%rbx), %rdi
movl 0x38(%rbx), %esi
movq 0x10(%r15), %r8
subq $0x8, %rsp
movq %r12, %rdx
movq 0x78(%rsp), %rcx
movl $0x1, %r9d
pushq $0x0
callq 0xbc79b
addq $0x10, %rsp
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq