// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movl %r9d, 0x10(%rsp)
movq %r8, 0x28(%rsp)
movq %rcx, %r15
movq %rsi, %r12
movq 0x90(%rsp), %r13
movl 0x78(%rsp), %ecx
movl %ecx, %eax
shrl %eax
leaq 0x18ef4e(%rip), %rsi    # 0x296230
movq 0x28(%rsi), %rsi
movb (%rsi,%rax), %sil
xorl %r8d, %r8d
cmpb $0x1, %sil
sete %r8b
leal (%r8,%r8,2), %r8d
decl %r8d
cmpb $0x2, %sil
movl $0x1, %esi
cmovnel %r8d, %esi
movl %esi, 0x14(%rsp)
leaq (%rax,%rax,2), %rsi
movl (%r13), %eax
cmpl 0xc(%rdx,%rsi,8), %eax
jae 0x107583
movq %rdi, 0x20(%rsp)
movl 0xa0(%rsp), %ebx
movq 0x98(%rsp), %r14
leaq (%rdx,%rsi,8), %rbp
movl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movq %rcx, 0x18(%rsp)
movq (%rbp), %rcx
movl %eax, %eax
leaq (%rax,%rax,4), %rsi
movl 0xc(%rcx,%rsi,8), %edi
leaq (%rdi,%rdi,2), %rdx
movl (%r15,%rdx,4), %eax
testl %eax, %eax
js 0x10740b
movq 0x20(%r12), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0x10740b
movl 0x4(%rcx,%rsi,8), %r8d
testl %ebx, %ebx
jne 0x107390
leaq 0x18eeaf(%rip), %r9     # 0x296230
movq 0x28(%r9), %r9
movzbl (%r9,%rdi), %edi
cmpl %edi, 0x14(%rsp)
je 0x10740b
leaq (%rcx,%rsi,8), %rdi
leaq (%r8,%r8,2), %rcx
leaq (%r15,%rcx,4), %rcx
leaq (%r15,%rdx,4), %rdx
movl 0x4(%rcx), %esi
movl (%rcx), %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %ecx
subl %ecx, %esi
movl 0x4(%rdx), %edx
subl %eax, %edx
leaq 0x18e39c(%rip), %rax    # 0x295758
movss 0x158(%rax), %xmm0
movl 0x10(%rsp), %ecx
movl 0x70(%rsp), %r8d
movq %r14, %r9
callq 0xa1d8c
testl %eax, %eax
js 0x10740b
movq 0x30(%rsp), %rax
cmpl %eax, 0x4(%r14)
jne 0x10740b
movl 0x8(%r14), %eax
movl %eax, %ecx
shrl %ecx
movq 0x28(%rsp), %rdx
cmpq %rcx, (%rdx)
jbe 0x10740b
movq 0x8(%rdx), %rdx
movl (%rdx,%rcx,4), %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
cmpq %rdx, %rcx
je 0x10740b
testl %ecx, %ecx
js 0x107423
movl (%r13), %eax
incl %eax
movl %eax, (%r13)
cmpl 0xc(%rbp), %eax
jb 0x107343
jmp 0x107583
movl %ecx, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpl $0x0, 0x88(%rsp)
je 0x107456
movq 0x80(%rsp), %rdi
cmpl (%rdi,%rdx,4), %esi
je 0x107456
incq %rdx
movl 0x88(%rsp), %r8d
cmpq %rdx, %r8
jne 0x10743f
jmp 0x10745f
cmpl 0x88(%rsp), %edx
jne 0x10740b
movq 0x20(%rsp), %r8
movq 0x18(%r8), %rdx
movq 0x20(%rdx), %rdi
cmpl $0x0, (%rdi,%rsi,8)
js 0x10740b
movq 0x10(%r8), %rdi
leaq (%rsi,%rsi,4), %r8
movl 0x4(%rdi,%r8,8), %esi
cmpl %esi, %eax
jne 0x10748a
movl 0x8(%rdi,%r8,8), %edi
jmp 0x10749a
leaq (%rdi,%r8,8), %r8
movl %eax, %edi
cmpl 0x8(%r8), %eax
jne 0x10740b
cmpl %esi, %eax
leal 0x1(%rcx,%rcx), %esi
movq 0x18(%rsp), %r8
cmovel %esi, %r8d
addl %ecx, %ecx
cmpl %edi, %eax
cmovel %ecx, %r8d
movq 0x28(%rdx), %rcx
movq %r8, 0x18(%rsp)
movq (%rcx,%r8,8), %rdi
movq %rdi, %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rsi
je 0x107578
movq 0x8(%rdx), %rax
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdx
leaq (%rax,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r8d, %r8d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x1074f0
cmpl $0x1, %r8d
jne 0x107578
xorl %edi, %edi
cmpl $0x0, (%rdx,%rdi)
js 0x10751f
movl -0x4(%rdx,%rdi), %r8d
movl %r8d, 0xc(%rsp)
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x10750f
movl 0xc(%rsp), %edx
xorl $0x1, %edx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rcx
je 0x107578
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rax
addq $0xc, %rax
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl (%rax,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0x10755c
cmpl $0x1, %esi
je 0x10740b
incl (%r13)
movl $0x1, %eax
jmp 0x107585
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq