// File: /workspace/github_cpp_makefile_star2_O1/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, 0x8(%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 0x1884ba(%rip), %rsi    # 0x28b230
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, 0xc(%rsp)
leaq (%rax,%rax,2), %rsi
movl (%r13), %eax
cmpl 0xc(%rdx,%rsi,8), %eax
jae 0x10307d
movq %rdi, 0x18(%rsp)
movl 0xa0(%rsp), %r10d
movq 0x98(%rsp), %r14
movl 0x88(%rsp), %edi
leaq (%rdx,%rsi,8), %rbp
movl %ecx, %ecx
movq %rcx, 0x30(%rsp)
movl %edi, %ecx
movq %rcx, 0x20(%rsp)
movq %rcx, 0x10(%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 0x102ec7
movq 0x20(%r12), %r8
cmpl $0x0, (%r8,%rdi,8)
js 0x102ec7
movl 0x4(%rcx,%rsi,8), %r8d
testl %r10d, %r10d
jne 0x102e38
leaq 0x18840b(%rip), %r9     # 0x28b230
movq 0x28(%r9), %r9
movzbl (%r9,%rdi), %edi
cmpl %edi, 0xc(%rsp)
je 0x102ec7
movl %r10d, %ebx
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 0x1878f1(%rip), %rax    # 0x28a758
movss 0x158(%rax), %xmm0
movl 0x8(%rsp), %ecx
movl 0x70(%rsp), %r8d
movq %r14, %r9
callq 0xa159b
testl %eax, %eax
js 0x102ec4
movq 0x30(%rsp), %rax
cmpl %eax, 0x4(%r14)
jne 0x102ec4
movl 0x8(%r14), %ecx
movl %ecx, %eax
shrl %eax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x28(%rsp), %rsi
cmpq %rax, (%rsi)
movl %ebx, %r10d
jbe 0x102edf
movq 0x8(%rsi), %rsi
movl (%rsi,%rax,4), %eax
cmpl $-0x1, %eax
je 0x102edf
movl %eax, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
testl %eax, %eax
setns %al
jmp 0x102ee1
movl %ebx, %r10d
movl (%r13), %eax
incl %eax
movl %eax, (%r13)
cmpl 0xc(%rbp), %eax
jb 0x102de6
jmp 0x10307d
xorl %eax, %eax
cmpl $-0x1, %edx
sete %sil
orb %al, %sil
jne 0x102ec7
movl 0x88(%rsp), %eax
testl %eax, %eax
movq 0x80(%rsp), %rsi
movq 0x20(%rsp), %rdi
je 0x102f1c
xorl %eax, %eax
cmpl (%rsi,%rax,4), %edx
je 0x102f1e
incq %rax
cmpl %eax, %edi
jne 0x102f07
movl 0x88(%rsp), %eax
jmp 0x102f1e
xorl %eax, %eax
movl 0x88(%rsp), %esi
cmpl %esi, %eax
jne 0x102ec7
movq 0x18(%rsp), %r8
movq 0x18(%r8), %rax
movq 0x20(%rax), %rsi
movl %edx, %edi
cmpl $0x0, (%rsi,%rdi,8)
js 0x102ec7
movq 0x10(%r8), %rsi
leaq (%rdi,%rdi,4), %rdi
cmpl 0x4(%rsi,%rdi,8), %ecx
je 0x102f5a
leaq (%rsi,%rdi,8), %r8
cmpl 0x8(%r8), %ecx
jne 0x102ec7
cmpl 0x4(%rsi,%rdi,8), %ecx
leal (%rdx,%rdx), %r8d
leal 0x1(%rdx,%rdx), %edx
movq 0x10(%rsp), %r9
cmovel %edx, %r9d
cmpl 0x8(%rsi,%rdi,8), %ecx
cmovel %r8d, %r9d
movq 0x28(%rax), %rcx
movq %r9, 0x10(%rsp)
movq (%rcx,%r9,8), %rdx
movq %rdx, %rsi
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
andq %rdi, %rsi
je 0x103072
movq 0x8(%rax), %r8
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdi
leaq (%r8,%rdi,8), %r8
addq $0xc, %r8
leaq (,%rsi,8), %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %r9d, %r9d
xorl %r10d, %r10d
movl (%r8,%r9), %r11d
notl %r11d
shrl $0x1f, %r11d
addl %r11d, %r10d
addq $0x18, %r9
cmpq %r9, %rdx
jne 0x102fbb
cmpl $0x1, %r10d
movq 0x98(%rsp), %r14
jne 0x103072
movl %ebx, %r10d
testq %rsi, %rsi
je 0x103015
movq 0x8(%rax), %rsi
leaq (%rsi,%rdi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
movl 0x4(%rsp), %r8d
cmpl $0x0, (%rsi,%rdi)
js 0x10300a
movl -0x4(%rsi,%rdi), %r8d
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0x102fff
jmp 0x10301a
movl 0x4(%rsp), %r8d
movl %r8d, 0x4(%rsp)
movl %r8d, %edx
xorl $0x1, %edx
movq (%rcx,%rdx,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %rcx
je 0x103072
movq 0x8(%rax), %rax
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 0x103056
cmpl $0x1, %esi
je 0x102ec7
incl (%r13)
movl $0x1, %eax
jmp 0x10307f
xorl %eax, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop