// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: get_edge2existing_node_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*, asg_arc_t*, unsigned int)
inline uint32_t get_edge2existing_node_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* skip_uId, uint32_t skip_uId_n, 
uint32_t* index, asg_arc_t* t, uint32_t ignore_trio_flag)
{
    uint32_t qn = query>>1, uId, is_Unitig, v, w, k;
    int32_t r;
    ma_hit_t *h = NULL;
    ma_sub_t *sq = NULL;
    ma_sub_t *st = NULL;
    ma_hit_t_alloc* x = &(sources[qn]);

    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 (; (*index) < x->length; (*index)++)
    {
        h = &(x->buffer[(*index)]);
        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*************/

        (*index)++;
        return 1;
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r9d, 0x4(%rsp)
movq %rcx, %r15
movq %rsi, %rbx
movq %rdi, %r11
movl 0x58(%rsp), %eax
movq 0x70(%rsp), %r13
shrl %eax
imulq $0x18, %rax, %rbp
addq %rdx, %rbp
leaq 0x18fa12(%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
cmovel %eax, %ecx
movl %ecx, 0x8(%rsp)
movl (%r13), %eax
movq %r8, 0x10(%rsp)
cmpl 0xc(%rbp), %eax
jae 0xc49f2
movq (%rbp), %rdi
movl %eax, %eax
imulq $0x28, %rax, %rdx
movl 0xc(%rdi,%rdx), %esi
imulq $0xc, %rsi, %rcx
movl (%r15,%rcx), %eax
testl %eax, %eax
js 0xc494e
movq 0x20(%rbx), %r9
cmpl $0x0, (%r9,%rsi,8)
js 0xc494e
movl 0x4(%rdi,%rdx), %r10d
cmpl $0x0, 0x80(%rsp)
jne 0xc48a5
leaq 0x18f99e(%rip), %r9     # 0x254230
movq 0x28(%r9), %r9
movzbl (%r9,%rsi), %esi
cmpl %esi, 0x8(%rsp)
je 0xc494e
movq %r11, %r14
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 0x18ee87(%rip), %rax    # 0x253758
movss 0x158(%rax), %xmm0
movl 0x4(%rsp), %ecx
movl 0x50(%rsp), %r8d
movq 0x78(%rsp), %r12
movq %r12, %r9
callq 0x6a4a7
testl %eax, %eax
js 0xc494b
movl 0x58(%rsp), %eax
cmpl %eax, 0x4(%r12)
movq 0x10(%rsp), %r8
movq %r14, %r11
jne 0xc494e
movl 0x8(%r12), %eax
movl %eax, %ecx
shrl %ecx
cmpq %rcx, (%r8)
jbe 0xc494e
movq 0x8(%r8), %rdx
movl (%rdx,%rcx,4), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0xc494e
testl %ecx, %ecx
jns 0xc494e
movl %ecx, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
movl 0x68(%rsp), %edi
cmpq %rsi, %rdi
je 0xc4965
movq 0x60(%rsp), %rdi
cmpl (%rdi,%rsi,4), %edx
je 0xc495d
incq %rsi
jmp 0xc4933
movq %r14, %r11
movl (%r13), %eax
incl %eax
movl %eax, (%r13)
jmp 0xc4846
movl 0x68(%rsp), %edi
cmpl %esi, %edi
jne 0xc494e
movq 0x18(%r11), %rdi
movq 0x20(%rdi), %rsi
cmpl $0x0, (%rsi,%rdx,8)
js 0xc494e
movq 0x10(%r11), %r9
imulq $0x28, %rdx, %rsi
movl 0x4(%r9,%rsi), %edx
cmpl %edx, %eax
jne 0xc498b
movl 0x8(%r9,%rsi), %r10d
jmp 0xc4997
addq %rsi, %r9
movl %eax, %r10d
cmpl 0x8(%r9), %eax
jne 0xc494e
cmpl %edx, %eax
leal 0x1(%rcx,%rcx), %edx
movl (%rsp), %esi
cmovel %edx, %esi
addl %ecx, %ecx
cmpl %r10d, %eax
cmovel %ecx, %esi
movl %esi, (%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0xc49e9
movq 0x18(%r14), %rdi
movl (%rsp), %esi
leaq 0xc(%rsp), %rdx
callq 0xc2e7a
movq 0x18(%r14), %rdi
movl 0xc(%rsp), %esi
xorl $0x1, %esi
xorl %edx, %edx
callq 0xc2e7a
movq %r14, %r11
cmpl $0x1, %eax
je 0xc494e
incl (%r13)
pushq $0x1
popq %rax
jmp 0xc49f4
xorl %eax, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq