// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, 0x8(%rsp)
cmpq $0x0, 0x38(%rsi)
je 0x82f09
movq %rsi, %r15
movq 0x38(%rcx), %rsi
testq %rsi, %rsi
je 0x82f09
movq %rdx, 0x28(%rsp)
movq %r8, 0x20(%rsp)
movq 0x78(%rsp), %r13
movq 0x70(%rsp), %r12
movq 0xa8(%r13), %rbp
movq %rcx, 0x18(%rsp)
movq 0x48(%rcx), %rdi
movq 0x10(%r13), %r8
subq $0x10, %rsp
andl $0x0, (%rsp)
movq 0x18(%rsp), %r14
movq %r14, %rdx
movq %r12, %rcx
xorl %r9d, %r9d
callq 0x80ffc
popq %rcx
popq %rdx
movl %eax, %ecx
movq %rcx, 0x30(%rsp)
subq $0x8, %rsp
leaq 0x1c(%rsp), %rbx
movq %r13, %rdi
xorl %esi, %esi
movq %r14, %rdx
movq %r12, %rcx
movq %r15, 0x8(%rsp)
movq %r15, %r8
movl %eax, %r9d
pushq %rbx
callq 0xc9edc
popq %rax
popq %rcx
cmpl $0x0, (%rbx)
je 0x82ee0
testq %rbp, %rbp
je 0x82c92
movq (%rsp), %rax
movq 0x48(%rax), %rdx
movl 0x38(%rax), %ecx
movq 0x18(%rsp), %r11
movl 0x38(%r11), %eax
subq $0x10, %rsp
leaq 0x40(%rsp), %r10
movsd 0x164fee(%rip), %xmm0   # 0x1e7c58
movq %r13, %rdi
movq %r12, %rsi
movq 0x38(%rsp), %r8
xorl %r9d, %r9d
pushq $0x1
pushq 0x20(%rsp)
pushq %r10
pushq 0x48(%rsp)
pushq %rax
pushq 0x48(%r11)
callq 0x82297
addq $0x40, %rsp
xorl %eax, %eax
pushq $-0x1
popq %rcx
xorl %edx, %edx
movq 0x18(%rsp), %r9
movl %edx, %edx
cmpq %rdx, 0x38(%r9)
jbe 0x82d3b
movq 0x8(%rsp), %rsi
movq 0x10(%rsi), %rsi
movq 0x48(%r9), %rdi
movl (%rdi,%rdx,4), %edi
movl %edi, %r8d
shrl %r8d
imulq $0x28, %r8, %r8
movl 0x10(%rsi,%r8), %r10d
testl %r10d, %r10d
je 0x82d34
addq %r8, %rsi
movl %ecx, %r8d
xorl %r9d, %r9d
movl %r10d, %r11d
cmpq %r11, %r9
jae 0x82d2f
leal (%r10,%r8), %r14d
testb $0x1, %dil
movq 0x18(%rsi), %r15
movq %r14, %r11
cmoveq %r9, %r11
movq (%r15,%r11,8), %r11
movq 0x8(%r13), %rbx
shrq $0x21, %r11
movl (%rbx,%r11,4), %r11d
testq %rbp, %rbp
je 0x82d24
testb $0x1, %dil
movq 0x10(%rbp), %r10
cmoveq %r9, %r14
movq (%r15,%r14,8), %rbx
shrq $0x21, %rbx
orb $0x2, (%r10,%rbx)
movl 0x10(%rsi), %r10d
addq %r11, %rax
incq %r9
decl %r8d
jmp 0x82cd7
movq 0x18(%rsp), %r9
incl %edx
jmp 0x82c9e
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edi, %edi
movq (%rsp), %r9
movl %edi, %edi
cmpq %rdi, 0x38(%r9)
jbe 0x82e26
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %r8
movq 0x48(%r9), %rdx
movl (%rdx,%rdi,4), %r9d
movl %r9d, %edx
shrl %edx
imulq $0x28, %rdx, %r10
movl 0x10(%r8,%r10), %edx
testl %edx, %edx
je 0x82e17
movq %rdi, 0x20(%rsp)
addq %r10, %r8
movl %ecx, %ecx
pushq $-0x1
popq %rdi
movl %edi, %r11d
xorl %r10d, %r10d
leal (%rcx,%r10), %ebx
movl %edx, %r15d
movl 0x14(%rsp), %r14d
cmpq %r15, %r10
jae 0x82e04
cmpl %r14d, %ebx
jae 0x82e04
movq %r13, %rdi
leal (%rdx,%r11), %r14d
testb $0x1, %r9b
movq 0x18(%r8), %rbx
movq %r14, %r15
cmoveq %r10, %r15
movq (%rbx,%r15,8), %r15
movq %r12, %r13
movq 0x20(%r12), %r12
shrq $0x21, %r15
movl (%r12,%r15,8), %r15d
testq %rbp, %rbp
je 0x82dec
testb $0x1, %r9b
movq 0x10(%rbp), %rdx
cmoveq %r10, %r14
movq (%rbx,%r14,8), %rbx
shrq $0x21, %rbx
orb $0x2, (%rdx,%rbx)
movl 0x10(%r8), %edx
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
addq %r15, %rsi
incq %r10
decl %r11d
movq %r13, %r12
movq %rdi, %r13
jmp 0x82d8b
cmpl %r14d, %ebx
jae 0x82e22
addq %r10, %rcx
movq (%rsp), %r9
movq 0x20(%rsp), %rdi
jmp 0x82e1b
movq (%rsp), %r9
incl %edi
jmp 0x82d45
movq (%rsp), %r9
xorl %edi, %edi
movl $0x0, %ecx
testq %rsi, %rsi
je 0x82e3a
xorl %edx, %edx
divq %rsi
movq %rax, %rcx
movq 0x38(%r9), %rax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %r8d, %r8d
movl %r8d, %r8d
cmpq %r8, %rax
jbe 0x82ee0
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %r9
movq (%rsp), %rdx
movq 0x48(%rdx), %rdx
movl (%rdx,%r8,4), %r10d
movl %r10d, %edx
shrl %edx
imulq $0x28, %rdx, %r11
movl 0x10(%r9,%r11), %edx
testl %edx, %edx
je 0x82ed8
addq %r11, %r9
movl %edi, %edi
pushq $-0x1
popq %rbp
xorl %r11d, %r11d
leal (%rdi,%r11), %ebx
movl %edx, %r15d
movl 0x14(%rsp), %r14d
cmpq %r15, %r11
jae 0x82ed0
cmpl %r14d, %ebx
jae 0x82ed0
addl %ebp, %edx
testb $0x1, %r10b
movq 0x18(%r9), %rbx
cmoveq %r11, %rdx
movq (%rbx,%rdx,8), %rdx
shrq $0x21, %rdx
movq 0x20(%r12), %rbx
movl (%rbx,%rdx,8), %ebx
andl %esi, %ebx
imull %ecx, %ebx
movq 0x8(%r13), %r14
addl %ebx, (%r14,%rdx,4)
incq %r11
movl 0x10(%r9), %edx
decl %ebp
jmp 0x82e84
cmpl %r14d, %ebx
jae 0x82ee0
addq %r11, %rdi
incl %r8d
jmp 0x82e46
movq 0x18(%rsp), %rax
movq 0x48(%rax), %rdi
movl 0x38(%rax), %esi
movq 0x10(%r13), %r8
subq $0x8, %rsp
pushq $0x1
popq %r9
movq 0x10(%rsp), %rdx
movq %r12, %rcx
pushq $0x0
callq 0x80ffc
popq %rcx
popq %rdx
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq