// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xe8, %rsp
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movq %r8, -0x40(%rbp)
movq %r9, -0x48(%rbp)
movq $0x0, -0x80(%rbp)
movq 0x18(%rbp), %rax
movq 0xa8(%rax), %rax
movq %rax, -0x88(%rbp)
movq -0x28(%rbp), %rax
cmpq $0x0, 0x38(%rax)
je 0xfabd0
movq -0x38(%rbp), %rax
cmpq $0x0, 0x38(%rax)
jne 0xfabd5
jmp 0xfb19d
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %esi
movq -0x48(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %rax
movq 0x10(%rax), %r8
xorl %r9d, %r9d
movl $0x0, (%rsp)
callq 0xf7330
movl %eax, %eax
movq %rax, -0x68(%rbp)
movq 0x18(%rbp), %rdi
movq -0x48(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x68(%rbp), %rax
movl %eax, %r9d
xorl %eax, %eax
movl %eax, %esi
leaq -0x60(%rbp), %rax
movq %rax, (%rsp)
callq 0x1738b0
cmpl $0x0, -0x60(%rbp)
jbe 0xfb167
cmpq $0x0, -0x88(%rbp)
je 0xfacc2
movq 0x18(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rdx
movq -0x28(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %ecx
movq -0x30(%rbp), %rax
movl %eax, %r8d
movq -0x38(%rbp), %rax
movq 0x48(%rax), %r15
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %r14d
movq -0x40(%rbp), %rax
movl %eax, %ebx
movq -0x48(%rbp), %r10
movq -0x20(%rbp), %rax
xorl %r9d, %r9d
leaq -0x68(%rbp), %r11
movsd 0x28bb9f(%rip), %xmm0   # 0x386838
movq %r15, (%rsp)
movl %r14d, 0x8(%rsp)
movl %ebx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0xf9a10
movq $0x0, -0x78(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x38(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xfae45
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x38(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xfad16
jmp 0xfae37
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfae35
cmpl $0x1, -0x5c(%rbp)
jne 0xfad6f
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x90(%rbp)
jmp 0xfad89
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rax
movl %eax, -0x54(%rbp)
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %eax
addq -0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0xfae25
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x98(%rbp)
cmpl $0x1, -0x5c(%rbp)
jne 0xfadf3
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xa0(%rbp)
jmp 0xfae0d
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rax
movq -0xa0(%rbp), %rcx
movzbl (%rax,%rcx), %edx
orl $0x2, %edx
movb %dl, (%rax,%rcx)
jmp 0xfae27
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0xfad31
jmp 0xfae37
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xfacd1
movl $0x0, -0x58(%rbp)
movq $0x0, -0x70(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xfaff6
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xfaea0
jmp 0xfafe8
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfafdc
movl -0x58(%rbp), %eax
cmpl -0x60(%rbp), %eax
jb 0xfaed8
jmp 0xfafdc
cmpl $0x1, -0x5c(%rbp)
jne 0xfaf06
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xa8(%rbp)
jmp 0xfaf20
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl %eax, -0x54(%rbp)
movq 0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
addq -0x70(%rbp), %rax
movq %rax, -0x70(%rbp)
cmpq $0x0, -0x88(%rbp)
je 0xfafc3
movq -0x88(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0xb0(%rbp)
cmpl $0x1, -0x5c(%rbp)
jne 0xfaf91
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xb8(%rbp)
jmp 0xfafab
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xb8(%rbp)
movq -0xb0(%rbp), %rax
movq -0xb8(%rbp), %rcx
movzbl (%rax,%rcx), %edx
orl $0x2, %edx
movb %dl, (%rax,%rcx)
jmp 0xfafc5
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xfaebb
movl -0x58(%rbp), %eax
cmpl -0x60(%rbp), %eax
jb 0xfafe6
jmp 0xfaff6
jmp 0xfafe8
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xfae5b
cmpq $0x0, -0x70(%rbp)
jne 0xfb008
xorl %eax, %eax
movq %rax, -0xc0(%rbp)
jmp 0xfb01b
movq -0x78(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x70(%rbp)
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpq 0x38(%rcx), %rax
jae 0xfb165
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x28(%rbp), %rcx
movq 0x48(%rcx), %rcx
movl -0x4c(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
cmpl $0x0, 0x10(%rax)
jne 0xfb079
jmp 0xfb157
movq -0x28(%rbp), %rax
movq 0x48(%rax), %rax
movl -0x4c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
andl $0x1, %eax
movl %eax, -0x5c(%rbp)
movl $0x0, -0x50(%rbp)
movl -0x50(%rbp), %eax
movq -0x80(%rbp), %rcx
cmpl 0x10(%rcx), %eax
jae 0xfb14b
movl -0x58(%rbp), %eax
cmpl -0x60(%rbp), %eax
jb 0xfb0b1
jmp 0xfb14b
cmpl $0x1, -0x5c(%rbp)
jne 0xfb0df
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movq -0x80(%rbp), %rcx
movl 0x10(%rcx), %ecx
subl -0x50(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xc8(%rbp)
jmp 0xfb0f9
movq -0x80(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x50(%rbp), %ecx
movq (%rax,%rcx,8), %rax
shrq $0x21, %rax
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl %eax, -0x54(%rbp)
movq -0x78(%rbp), %rsi
movq 0x10(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
imulq %rax, %rsi
movq 0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x54(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addq %rsi, %rdx
movl %edx, (%rax,%rcx,4)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
movl -0x58(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xfb094
movl -0x58(%rbp), %eax
cmpl -0x60(%rbp), %eax
jb 0xfb155
jmp 0xfb165
jmp 0xfb157
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xfb034
jmp 0xfb167
movq -0x38(%rbp), %rax
movq 0x48(%rax), %rdi
movq -0x38(%rbp), %rax
movq 0x38(%rax), %rax
movl %eax, %esi
movq -0x48(%rbp), %rdx
movq 0x10(%rbp), %rcx
movq 0x18(%rbp), %rax
movq 0x10(%rax), %r8
movl $0x1, %r9d
xorl %eax, %eax
movl $0x0, (%rsp)
callq 0xf7330
addq $0xe8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)