// File: /workspace/github_cpp_makefile_star2_O1/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 0xbd886
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r12
movq %rsi, %rax
movq 0x38(%rcx), %rsi
testq %rsi, %rsi
je 0xbd878
movq %r9, %r15
movq %rdx, 0x18(%rsp)
movq %r8, 0x20(%rsp)
movq 0x88(%rsp), %r13
movq 0x80(%rsp), %r14
movq 0xa8(%r13), %rbp
movq 0x48(%r12), %rdi
movq 0x10(%r13), %r8
subq $0x8, %rsp
movq %r9, %rdx
movq %r14, %rcx
xorl %r9d, %r9d
pushq $0x0
movq %rax, 0x20(%rsp)
callq 0xbb724
addq $0x10, %rsp
movl %eax, %ecx
movq %rcx, 0x40(%rsp)
subq $0x8, %rsp
leaq 0x14(%rsp), %rbx
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
movq %r14, %rcx
movq 0x18(%rsp), %r8
movl %eax, %r9d
pushq %rbx
callq 0x108416
movq 0x20(%rsp), %rax
addq $0x10, %rsp
cmpl $0x0, (%rbx)
je 0xbd84a
testq %rbp, %rbp
je 0xbd506
movq 0x48(%rax), %rdx
movl 0x38(%rax), %ecx
movl 0x38(%r12), %eax
subq $0x10, %rsp
leaq 0x50(%rsp), %r10
movsd 0x16c686(%rip), %xmm0   # 0x229b60
movq %r13, %rdi
movq 0x90(%rsp), %rsi
movq 0x28(%rsp), %r8
xorl %r9d, %r9d
pushq $0x1
pushq %r15
pushq %r10
pushq 0x48(%rsp)
pushq %rax
pushq 0x48(%r12)
callq 0xbca10
addq $0x40, %rsp
xorl %ecx, %ecx
cmpq $0x0, 0x38(%r12)
je 0xbd5c8
xorl %eax, %eax
movq 0x10(%r15), %rsi
movq 0x48(%r12), %rdx
movl (%rdx,%rcx,4), %edx
movl %edx, %edi
shrl %edi
leaq (%rdi,%rdi,4), %rdi
cmpl $0x0, 0x10(%rsi,%rdi,8)
je 0xbd5b9
leaq (%rsi,%rdi,8), %rsi
movl 0x10(%rsi), %r10d
testl %r10d, %r10d
je 0xbd5b9
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
xorl %r9d, %r9d
testb $0x1, %dl
jne 0xbd55b
movq 0x18(%rsi), %r11
addq %r8, %r11
jmp 0xbd567
leal (%r10,%rdi), %r11d
shlq $0x3, %r11
addq 0x18(%rsi), %r11
movq (%r11), %r11
movq 0x8(%r13), %rbx
shrq $0x21, %r11
movl (%rbx,%r11,4), %r11d
testq %rbp, %rbp
je 0xbd5a4
testb $0x1, %dl
jne 0xbd589
movq 0x18(%rsi), %r10
addq %r8, %r10
jmp 0xbd594
addl %edi, %r10d
shlq $0x3, %r10
addq 0x18(%rsi), %r10
movq 0x10(%rbp), %rbx
movq (%r10), %r10
shrq $0x21, %r10
orb $0x2, (%rbx,%r10)
addq %r11, %rax
incq %r9
movl 0x10(%rsi), %r10d
addq $0x8, %r8
decl %edi
cmpq %r10, %r9
jb 0xbd54d
incl %ecx
cmpq %rcx, 0x38(%r12)
ja 0xbd516
jmp 0xbd5ca
xorl %eax, %eax
xorl %edx, %edx
movq 0x10(%rsp), %r9
cmpq $0x0, 0x38(%r9)
movq %r15, 0x20(%rsp)
movq %r12, 0x18(%rsp)
je 0xbd717
xorl %ecx, %ecx
xorl %r10d, %r10d
movq 0x10(%r15), %rsi
movq 0x48(%r9), %rdi
movl (%rdi,%rdx,4), %edi
movl %edi, %r8d
shrl %r8d
leaq (%r8,%r8,4), %r8
cmpl $0x0, 0x10(%rsi,%r8,8)
je 0xbd709
leaq (%rsi,%r8,8), %r8
movl 0x10(%r8), %r11d
movl 0xc(%rsp), %esi
cmpl %esi, %r10d
setb %r14b
testl %r11d, %r11d
je 0xbd704
cmpl %esi, %r10d
jae 0xbd704
movq %r10, 0x28(%rsp)
movl %r10d, %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movl $0x1, %r13d
xorl %ebx, %ebx
testb $0x1, %dil
jne 0xbd656
movq 0x18(%r8), %rsi
addq %rbx, %rsi
jmp 0xbd662
leal (%r11,%r10), %esi
shlq $0x3, %rsi
addq 0x18(%r8), %rsi
movq (%rsi), %rsi
movq 0x80(%rsp), %r14
movq 0x20(%r14), %r14
shrq $0x21, %rsi
movl (%r14,%rsi,8), %r14d
testq %rbp, %rbp
je 0xbd6a8
testb $0x1, %dil
jne 0xbd68d
movq 0x18(%r8), %r11
addq %rbx, %r11
jmp 0xbd698
addl %r10d, %r11d
shlq $0x3, %r11
addq 0x18(%r8), %r11
movq 0x10(%rbp), %rsi
movq (%r11), %r11
shrq $0x21, %r11
orb $0x2, (%rsi,%r11)
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
addq %r14, %rcx
leal (%r9,%r13), %esi
movl 0x10(%r8), %r11d
movl 0xc(%rsp), %r12d
cmpl %r12d, %esi
setb %r14b
leaq 0x1(%r13), %r15
cmpq %r11, %r13
jae 0xbd6e2
addq $0x8, %rbx
decl %r10d
movq %r15, %r13
cmpl %r12d, %esi
jb 0xbd647
movq 0x28(%rsp), %r10
addl %r15d, %r10d
decl %r10d
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x88(%rsp), %r13
movq 0x10(%rsp), %r9
testb %r14b, %r14b
je 0xbd719
incl %edx
cmpq %rdx, 0x38(%r9)
ja 0xbd5eb
jmp 0xbd719
xorl %ecx, %ecx
testq %rcx, %rcx
je 0xbd725
xorl %edx, %edx
divq %rcx
jmp 0xbd727
xorl %eax, %eax
movq 0x38(%r9), %r8
testq %r8, %r8
je 0xbd84a
movq 0x10(%r15), %rbx
movq 0x48(%r9), %r9
xorl %edi, %edi
xorl %r14d, %r14d
movq %r8, 0x28(%rsp)
movq %rbx, 0x38(%rsp)
movq %r9, 0x30(%rsp)
movl (%r9,%rdi,4), %r10d
movl %r10d, %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0x10(%rbx,%rcx,8)
je 0xbd83f
leaq (%rbx,%rcx,8), %r11
movl 0x10(%r11), %ecx
movl 0xc(%rsp), %esi
cmpl %esi, %r14d
setb %dl
testl %ecx, %ecx
je 0xbd83b
cmpl %esi, %r14d
jae 0xbd83b
movq 0x80(%rsp), %rdx
movq 0x20(%rdx), %rbx
movq 0x8(%r13), %r13
movq %r14, 0x10(%rsp)
movl %r14d, %ebp
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
movl $0x1, %r9d
xorl %r14d, %r14d
testb $0x1, %r10b
jne 0xbd7c0
movq 0x18(%r11), %rcx
addq %r14, %rcx
jmp 0xbd7ca
addl %esi, %ecx
shlq $0x3, %rcx
addq 0x18(%r11), %rcx
movq (%rcx), %rcx
shrq $0x21, %rcx
movl (%rbx,%rcx,8), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
imull %eax, %edx
addl %edx, (%r13,%rcx,4)
leal (%r9,%rbp), %r12d
movl 0x10(%r11), %ecx
movl 0xc(%rsp), %r8d
cmpl %r8d, %r12d
setb %dl
leaq 0x1(%r9), %r15
cmpq %rcx, %r9
jae 0xbd80f
addq $0x8, %r14
decl %esi
movq %r15, %r9
cmpl %r8d, %r12d
jb 0xbd7b1
movq 0x10(%rsp), %r14
addl %r15d, %r14d
decl %r14d
movq 0x20(%rsp), %r15
movq 0x18(%rsp), %r12
movq 0x88(%rsp), %r13
movq 0x28(%rsp), %r8
movq 0x38(%rsp), %rbx
movq 0x30(%rsp), %r9
testb %dl, %dl
je 0xbd84a
incl %edi
cmpq %rdi, %r8
ja 0xbd750
movq 0x48(%r12), %rdi
movl 0x38(%r12), %esi
movq 0x10(%r13), %r8
subq $0x8, %rsp
movq %r15, %rdx
movq 0x88(%rsp), %rcx
movl $0x1, %r9d
pushq $0x0
callq 0xbb724
addq $0x10, %rsp
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq