// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_num_edges2existing_nodes_advance(ma_ug_t*, asg_t*, ma_hit_t_alloc*, ma_sub_t*, R_to_U*, int, int, unsigned int, unsigned int*, unsigned int*, unsigned int, unsigned int)
inline uint32_t get_num_edges2existing_nodes_advance(ma_ug_t *ug, asg_t *g, ma_hit_t_alloc* sources, ma_sub_t *coverage_cut, 
R_to_U* ruIndex, int max_hang, int min_ovlp, uint32_t query, uint32_t* oLen, uint32_t* skip_uId, uint32_t skip_uId_n, uint32_t ignore_trio_flag)
{
    (*oLen) = 0;
    uint32_t qn = query>>1;
    int32_t r;
    asg_arc_t t;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    ma_hit_t_alloc* x = &(sources[qn]);
    uint32_t i, k, occ = 0, uId, is_Unitig, v, w;

    uint32_t trio_flag = R_INF.trio_flag[qn], non_trio_flag = (uint32_t)-1;
    if(ignore_trio_flag == 0)
    {
        if(trio_flag == FATHER) non_trio_flag = MOTHER;
        if(trio_flag == MOTHER) non_trio_flag = FATHER;
    }



    for (i = 0; i < x->length; i++)
    {
        h = &(x->buffer[i]);
        sq = &(coverage_cut[Get_qn(*h)]);
        st = &(coverage_cut[Get_tn(*h)]);
        ///sq has already been removed
        ///st must not be removed
        ///g-seq must not be removed
        if(st->del || g->seq[Get_tn(*h)].del) continue;
        if(R_INF.trio_flag[Get_tn(*h)] == non_trio_flag) continue;

        r = ma_hit2arc(h, sq->e - sq->s, st->e - st->s, max_hang, 
                                asm_opt.max_hang_rate, min_ovlp, &t);
        
        ///if it is a contained read, skip
        if(r < 0) continue;

        if((t.ul>>32) != query) continue;
        get_R_to_U(ruIndex, t.v>>1, &uId, &is_Unitig);
        /****************************may have bugs********************************/
        if(uId == (uint32_t)-1 || is_Unitig == 0) continue;
        /****************************may have bugs********************************/
        for (k = 0; k < skip_uId_n; k++)
        {
            if(uId == skip_uId[k]) break;
        }
        if(k != skip_uId_n) continue;
        // if(uId == skip_uId1) continue;
        // if(uId == skip_uId2) continue;
        /****************************may have bugs********************************/
        if(ug->g->seq[uId].del) continue;
        /****************************may have bugs********************************/
        
        if((t.v != ug->u.a[uId].start) 
            && 
           (t.v != ug->u.a[uId].end))
        {
            continue;
        }
        /**********for debug*************/
        if(t.v == ug->u.a[uId].start)
        {
            ///v = uId<<1;
            v = (uId<<1)^1;
        }

        if(t.v == ug->u.a[uId].end)
        {
            ///v = (uId<<1)^1;
            v = uId<<1;
        }

        if(get_real_length(ug->g, v, NULL)==1)
        {
            get_real_length(ug->g, v, &w);
            if(get_real_length(ug->g, w^1, NULL)==1)
            {
                continue;
            }
        }
        /**********for debug*************/

        occ++;
        (*oLen) += t.ol;
    }

    return occ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, 0xc(%rsp)
movq %rcx, %r15
movq %rsi, %r12
movq %rdi, %r11
movl 0x78(%rsp), %eax
movl 0x98(%rsp), %ebx
movq 0x80(%rsp), %rcx
andl $0x0, (%rcx)
shrl %eax
imulq $0x18, %rax, %rbp
addq %rdx, %rbp
leaq 0x18fea8(%rip), %rcx    # 0x254230
movq 0x28(%rcx), %rcx
movb (%rcx,%rax), %al
xorl %ecx, %ecx
cmpb $0x1, %al
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpb $0x2, %al
pushq $0x1
popq %rax
cmovnel %ecx, %eax
movl %eax, 0x10(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x8(%rsp)
movl 0xc(%rbp), %eax
cmpq %rax, %r14
jae 0xc459d
movq (%rbp), %rdi
imulq $0x28, %r14, %rdx
movl 0xc(%rdi,%rdx), %esi
imulq $0xc, %rsi, %rcx
movl (%r15,%rcx), %eax
testl %eax, %eax
js 0xc4590
movq 0x20(%r12), %r9
cmpl $0x0, (%r9,%rsi,8)
js 0xc4590
movl 0x4(%rdi,%rdx), %r10d
testl %ebx, %ebx
jne 0xc440d
leaq 0x18fe36(%rip), %r9     # 0x254230
movq 0x28(%r9), %r9
movzbl (%r9,%rsi), %esi
cmpl %esi, 0x10(%rsp)
je 0xc4590
movq %r11, %r13
movq %r8, %rbx
addq %rdx, %rdi
imulq $0xc, %r10, %rdx
addq %r15, %rdx
addq %r15, %rcx
movl 0x4(%rdx), %esi
movl (%rdx), %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %edx
subl %edx, %esi
movl 0x4(%rcx), %edx
subl %eax, %edx
leaq 0x18f31c(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0xc(%rsp), %ecx
movl 0x70(%rsp), %r8d
leaq 0x20(%rsp), %r9
callq 0x6a4a7
testl %eax, %eax
js 0xc4583
movl 0x78(%rsp), %eax
cmpl %eax, 0x24(%rsp)
movq %rbx, %r8
movq %r13, %r11
jne 0xc4589
movl 0x28(%rsp), %eax
movl %eax, %ecx
shrl %ecx
cmpq %rcx, (%r8)
jbe 0xc4589
movq 0x8(%r8), %rdx
movl (%rdx,%rcx,4), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0xc4589
testl %ecx, %ecx
jns 0xc4589
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
movl 0x90(%rsp), %edi
cmpq %rsi, %rdi
je 0xc44d8
movq 0x88(%rsp), %rdi
cmpl (%rdi,%rsi,4), %edx
je 0xc44c9
incq %rsi
jmp 0xc44ab
movl 0x90(%rsp), %edi
cmpl %esi, %edi
jne 0xc4589
movq 0x18(%r11), %rdi
movq 0x20(%rdi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xc4589
movq 0x10(%r11), %r8
imulq $0x28, %rdx, %rsi
movl 0x4(%r8,%rsi), %edx
cmpl %edx, %eax
jne 0xc4502
movl 0x8(%r8,%rsi), %r9d
jmp 0xc4512
addq %rsi, %r8
movl %eax, %r9d
cmpl 0x8(%r8), %eax
jne 0xc4598
cmpl %edx, %eax
leal 0x1(%rcx,%rcx), %edx
movl 0x4(%rsp), %esi
cmovel %edx, %esi
addl %ecx, %ecx
cmpl %r9d, %eax
cmovel %ecx, %esi
movl %esi, 0x4(%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0xc456a
movq %r13, 0x18(%rsp)
movq 0x18(%r13), %rdi
movl 0x4(%rsp), %esi
leaq 0x14(%rsp), %rdx
callq 0xc2e7a
movq 0x18(%rsp), %rax
movq 0x18(%rax), %rdi
movl 0x14(%rsp), %esi
xorl $0x1, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
je 0xc4583
incl 0x8(%rsp)
movl 0x2c(%rsp), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
movq 0x80(%rsp), %rcx
addl %eax, (%rcx)
movq %rbx, %r8
movq %r13, %r11
movl 0x98(%rsp), %ebx
incq %r14
jmp 0xc43b2
movq %rbx, %r8
jmp 0xc4589
movl 0x8(%rsp), %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq