// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: explore_graph(asg_t*, unsigned int, float, float, long long, long long, unsigned int, buf_t*, unsigned int**, unsigned long*, unsigned long*, unsigned char*, ma_ug_t*, unsigned int*)
int explore_graph(asg_t *nsg, uint32_t vBeg, float single_threshold,
float l_untig_rate_threshold, long long minLongUntig, long long maxShortUntig, uint32_t ignore_d,
buf_t* bb, uint32_t** r, size_t* rm, size_t* rn, uint8_t* visit, ma_ug_t *ug, uint32_t* r_ID)
{
    ///the ID of the end long unitig
    (*r_ID) = (uint32_t)-1;
    uint32_t nv, vBeg_end;
    asg_arc_t *av;

    kvec_t(uint32_t) u_vecs;
    kv_init(u_vecs);
    if(r && rm && rn) kv_reuse(u_vecs, 0, (*rm), (*r));
    
    memset(visit, 0, nsg->n_seq);
    kdq_t(uint32_t) *buf;
    buf = kdq_init(uint32_t);

    uint32_t vEnd, threshold, num_reads = 0, i, k, v, end = (uint32_t)-1, in = 0, vELen, tmp;
    long long nodeLen, baseLen, max_stop_nodeLen, max_stop_baseLen;

    bb->b.n = 0;
    if(get_unitig(nsg, ug, vBeg, &vEnd, &nodeLen, &baseLen, 
                                        &max_stop_nodeLen, &max_stop_baseLen, 1, bb)==LOOP)
    {
        ///the length of LOO is infinite
        kdq_destroy(uint32_t, buf); 
        return 0;
    }
    vBeg_end = vEnd;
    threshold = nodeLen;
    for (i = 0; i < bb->b.n; i++)
    {
        Set_vis(visit, bb->b.a[i], ignore_d);
        Set_vis(visit, bb->b.a[i]^1, ignore_d);
    }    
    v = vBeg;

    kdq_push(uint32_t, buf, v);
    while (kdq_size(buf) != 0)
    {
        in++;
        v = *(kdq_pop(uint32_t, buf));

        ///in == 1 means the start node, it is useless
        if(in != 1)
        {
            ///get current untig length
            bb->b.n = 0;
            if(get_unitig(nsg, ug, v, &vEnd, &nodeLen, &baseLen, 
                                        &max_stop_nodeLen, &max_stop_baseLen, 1, bb)==LOOP)
            {
                kdq_destroy(uint32_t, buf);
                return 0;
            }
            vELen = nodeLen;

            ///first long unitig except the start node
            if(if_long_tip_length(nsg, ug, v, &vELen, 
                    minLongUntig, maxShortUntig, l_untig_rate_threshold, threshold)==1)
            {
                if(end == (uint32_t)-1)
                {
                    end = v;
                    continue;
                }
                else
                {
                    in = (uint32_t)-1;
                    break;
                }
            }

            if(vELen > (threshold * single_threshold))
            {
                in = (uint32_t)-1;
                break;
            }

            num_reads = num_reads + vELen;
            if(num_reads > threshold)
            {
                in = (uint32_t)-1;
                break;
            }

            if(r && rm && rn)
            {
                for (i = 0; i < bb->b.n; i++)
                {
                    kv_push(uint32_t, u_vecs, bb->b.a[i]);
                }   
            } 
        }

        tmp = v^1;
        v = vEnd;
        nv = asg_arc_n(nsg, v);
        av = asg_arc_a(nsg, v);
        for(k = 0; k < nv; k++)
        {
            if(av[k].del) continue;

            if(av[k].v == vBeg)
            {
                in = (uint32_t)-1;
                goto termi;
            }
            
            if(Get_vis(visit,av[k].v,ignore_d)==0)
            {
                kdq_push(uint32_t, buf, av[k].v);

                bb->b.n = 0;
                get_unitig(nsg, ug, av[k].v, &vEnd, &nodeLen, &baseLen, 
                                        &max_stop_nodeLen, &max_stop_baseLen, 1, bb);

                for (i = 0; i < bb->b.n; i++)
                {
                    Set_vis(visit, bb->b.a[i], ignore_d);
                }
            }
        }

        ///for start node, we just need one direction
        if(in != 1 && ignore_d)
        {
            v = tmp;
            nv = asg_arc_n(nsg, v);
            av = asg_arc_a(nsg, v);
            for(k = 0; k < nv; k++)
            {
                if(av[k].del) continue;

                if(av[k].v == vBeg)
                {
                    in = (uint32_t)-1;
                    goto termi;
                }

                if(Get_vis(visit,av[k].v,ignore_d)==0)
                {
                    kdq_push(uint32_t, buf, av[k].v);

                    bb->b.n = 0;
                    get_unitig(nsg, ug, av[k].v, &vEnd, &nodeLen, &baseLen, 
                                        &max_stop_nodeLen, &max_stop_baseLen, 1, bb);


                    for (i = 0; i < bb->b.n; i++)
                    {
                        Set_vis(visit, bb->b.a[i], ignore_d);
                    }
                }
            }
        }
    }

    termi:
    kdq_destroy(uint32_t, buf);
    if(r && rm && rn)
    {
        (*rn) = u_vecs.n;
        (*rm) = u_vecs.m;
        (*r) = u_vecs.a;
    }


    (*r_ID) = end;
    ///in == 1 means the end subgraph is the long untig itself
    ///so here is no tangles
    if(in == (uint32_t)-1 || in == 1)
    {
        return 0;
    }
    else
    {
        if(r && rm && rn) return double_check_tangle(vBeg_end, (*r_ID), u_vecs.a, u_vecs.n, nsg);

        return 1;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, %r15
movl %r8d, %ebp
movq %rcx, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movss %xmm1, 0x84(%rsp)
movss %xmm0, 0x8(%rsp)
movl %esi, 0x1c(%rsp)
movq %rdi, %r12
movq 0x108(%rsp), %rax
movq 0x100(%rsp), %r14
movq 0xf8(%rsp), %r8
movq 0xe8(%rsp), %rdx
orl $-0x1, (%rax)
movq 0xe0(%rsp), %rsi
testq %rsi, %rsi
setne %al
testq %rdx, %rdx
setne %cl
andb %al, %cl
cmpq $0x0, 0xf0(%rsp)
setne %al
andb %cl, %al
xorl %ecx, %ecx
movl $0x0, %edi
movq %rdi, 0x50(%rsp)
movb %al, 0x7(%rsp)
cmpb $0x1, %al
jne 0xab5ac
movq (%rdx), %rax
movq %rax, 0x50(%rsp)
movq (%rsi), %rcx
movq %rcx, 0x48(%rsp)
movl 0x14(%r12), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %ebx, %ebx
movq %r8, %rdi
xorl %esi, %esi
callq 0x20e0
callq 0xabb23
movq %rax, %r13
andq $0x0, 0x38(%r15)
leaq 0x60(%rsp), %rax
leaq 0x68(%rsp), %r10
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %r12, %rdi
movq %r14, %rsi
movl 0x1c(%rsp), %edx
pushq %r15
pushq $0x1
pushq %rax
pushq %r10
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
jne 0xab629
movq %r13, %rdi
callq 0xabb58
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r13, 0x10(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x30(%rsp), %eax
movq %rax, 0x58(%rsp)
movq 0xf8(%rsp), %rdi
movl %ebx, %eax
cmpq %rax, 0x38(%r15)
jbe 0xab68a
movq 0x48(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%rdi,%rdx)
movq 0x48(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, %esi
notl %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%rdi,%rdx)
leal 0x1(%rax), %ebx
jmp 0xab647
movq 0x10(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0xabb74
cvtsi2ssq 0x58(%rsp), %xmm0
mulss 0x8(%rsp), %xmm0
movss %xmm0, 0x80(%rsp)
pushq $-0x1
popq %rax
movq $0x0, 0x28(%rsp)
movl $0x0, 0x40(%rsp)
xorl %ecx, %ecx
movq %r12, 0x20(%rsp)
movl %eax, %ebx
movl %ecx, %r13d
movl %ebx, 0x8(%rsp)
movq 0x10(%rsp), %rdi
cmpq $0x0, 0x8(%rdi)
je 0xaba97
callq 0xabcc1
movl (%rax), %ebx
testl %r13d, %r13d
je 0xab787
andq $0x0, 0x38(%r15)
movq %r12, %rdi
movq %r14, %rsi
movl %ebx, %edx
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %r15
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
je 0xaba75
movl 0x30(%rsp), %eax
movl %eax, 0x44(%rsp)
subq $0x8, %rsp
movq %r12, %rdi
movq %r14, %rsi
movl %ebx, %edx
leaq 0x4c(%rsp), %rcx
movq 0xa0(%rsp), %r8
movq 0xa8(%rsp), %r9
movss 0x8c(%rsp), %xmm0
pushq 0x60(%rsp)
callq 0x714e8
popq %rcx
popq %rdx
testl %eax, %eax
je 0xab794
incl %r13d
cmpl $-0x1, 0x8(%rsp)
je 0xab6ce
jmp 0xaba7f
movq 0xf8(%rsp), %r8
jmp 0xab848
movl 0x44(%rsp), %eax
cvtsi2ss %rax, %xmm0
ucomiss 0x80(%rsp), %xmm0
movq 0xf8(%rsp), %r8
ja 0xaba7f
movl 0x40(%rsp), %ecx
addl %eax, %ecx
cmpl 0x58(%rsp), %ecx
ja 0xaba7f
cmpb $0x0, 0x7(%rsp)
movl %ecx, 0x40(%rsp)
je 0xab848
xorl %eax, %eax
movq 0x48(%rsp), %rdi
movq 0x50(%rsp), %rcx
movl %eax, %r14d
cmpq %r14, 0x38(%r15)
jbe 0xab836
cmpq %rcx, 0x28(%rsp)
jne 0xab818
leaq (%rcx,%rcx), %r12
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,4), %rsi
callq 0x2370
movq 0xf8(%rsp), %r8
movq %rax, %rdi
movq %r12, %rcx
movq 0x20(%rsp), %r12
movq 0x48(%r15), %rax
movl (%rax,%r14,4), %eax
movq 0x28(%rsp), %rdx
movl %eax, (%rdi,%rdx,4)
incq %rdx
movq %rdx, 0x28(%rsp)
leal 0x1(%r14), %eax
jmp 0xab7da
movq %rcx, 0x50(%rsp)
movq %rdi, 0x48(%rsp)
movq 0x100(%rsp), %r14
xorq $0x1, %rbx
movl 0xc(%rsp), %eax
movq 0x28(%r12), %rcx
movq (%rcx,%rax,8), %rax
movl %eax, %edi
shrq $0x20, %rax
imulq $0x18, %rax, %r9
addq 0x8(%r12), %r9
leal 0x1(%r13), %eax
movl %eax, 0x3c(%rsp)
xorl %r12d, %r12d
movq %rdi, 0x90(%rsp)
movq %r9, 0x88(%rsp)
cmpq %rdi, %r12
je 0xab959
imulq $0x18, %r12, %r14
cmpl $0x0, 0xc(%r9,%r14)
js 0xab8c1
addq %r9, %r14
movl 0x8(%r14), %esi
cmpl 0x1c(%rsp), %esi
je 0xaba87
movl %esi, %eax
shrl %eax
movl %esi, %edx
movl %ebp, %ecx
shll %cl, %edx
movzbl (%r8,%rax), %eax
andl $0x1, %edx
incl %edx
testl %eax, %edx
je 0xab8ce
incq %r12
movq 0x100(%rsp), %r14
jmp 0xab883
movq 0x10(%rsp), %rdi
callq 0xabb74
andq $0x0, 0x38(%r15)
movl 0x8(%r14), %edx
movq 0x20(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %r15
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0xa8(%rsp), %r9
movq 0xb0(%rsp), %rdi
movq 0x118(%rsp), %r8
addq $0x20, %rsp
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x38(%r15)
jbe 0xab8c1
movq 0x48(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%r8,%rdx)
incl %eax
jmp 0xab933
testl %r13d, %r13d
sete %al
testl %ebp, %ebp
sete %cl
orb %al, %cl
movq 0x20(%rsp), %r12
movl 0x8(%rsp), %eax
movl 0x3c(%rsp), %ecx
jne 0xab6c9
movq 0x28(%r12), %rax
movq (%rax,%rbx,8), %rax
movl %eax, %ebx
shrq $0x20, %rax
movq %r12, %rcx
imulq $0x18, %rax, %r12
addq 0x8(%rcx), %r12
xorl %r13d, %r13d
movq 0xf8(%rsp), %rdi
cmpq %rbx, %r13
je 0xaba63
imulq $0x18, %r13, %r14
cmpl $0x0, 0xc(%r12,%r14)
js 0xab9db
addq %r12, %r14
movl 0x8(%r14), %esi
cmpl 0x1c(%rsp), %esi
je 0xaba87
movl %esi, %eax
shrl %eax
movl %esi, %edx
movl %ebp, %ecx
shll %cl, %edx
movzbl (%rdi,%rax), %eax
andl $0x1, %edx
incl %edx
testl %eax, %edx
je 0xab9e8
incq %r13
movq 0x100(%rsp), %r14
jmp 0xab99e
movq 0x10(%rsp), %rdi
callq 0xabb74
andq $0x0, 0x38(%r15)
movl 0x8(%r14), %edx
movq 0x20(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq %r15
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0x118(%rsp), %rdi
addq $0x20, %rsp
xorl %eax, %eax
movl %eax, %eax
cmpq %rax, 0x38(%r15)
jbe 0xab9db
movq 0x48(%r15), %rcx
movl (%rcx,%rax,4), %edx
movl %edx, %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%rdi,%rdx)
incl %eax
jmp 0xaba3d
movq 0x20(%rsp), %r12
movl 0x8(%rsp), %eax
movl 0x3c(%rsp), %ecx
jmp 0xab6c9
movq 0x10(%rsp), %rdi
jmp 0xab610
pushq $-0x1
popq %rax
movl %eax, %r13d
jmp 0xaba92
pushq $-0x1
popq %rax
movl %eax, %r13d
movq 0x20(%rsp), %r12
movq 0x10(%rsp), %rdi
callq 0xabb58
movb 0x7(%rsp), %cl
testb %cl, %cl
je 0xabad4
movq 0xf0(%rsp), %rax
movq 0x28(%rsp), %rdx
movq %rdx, (%rax)
movq 0xe8(%rsp), %rax
movq 0x50(%rsp), %rdx
movq %rdx, (%rax)
movq 0x48(%rsp), %rax
movq 0xe0(%rsp), %rdx
movq %rax, (%rdx)
movq 0x108(%rsp), %rax
movl 0x8(%rsp), %edx
movl %edx, (%rax)
xorl %eax, %eax
cmpl $-0x1, %r13d
je 0xab617
cmpl $0x1, %r13d
je 0xab617
testb %cl, %cl
je 0xabb1b
movl 0x7c(%rsp), %edi
movl 0x8(%rsp), %esi
movq 0x48(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq %r12, %r8
callq 0xab313
jmp 0xab617
pushq $0x1
popq %rax
jmp 0xab617