// File: /workspace/github_cpp_makefile_star2_O1/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, %r12
movl %r8d, %ebp
movq %rcx, 0x68(%rsp)
movq %rdx, 0xa0(%rsp)
movss %xmm1, 0x48(%rsp)
movss %xmm0, 0x8(%rsp)
movl %esi, 0x1c(%rsp)
movq %rdi, 0x10(%rsp)
movq 0x108(%rsp), %rax
movq 0xf8(%rsp), %rdi
movq 0xe8(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
testq %rdx, %rdx
setne %al
testq %rsi, %rsi
setne %cl
andb %al, %cl
cmpq $0x0, 0xf0(%rsp)
setne %r15b
andb %cl, %r15b
xorl %eax, %eax
movl $0x0, %ecx
cmpb $0x1, %r15b
jne 0xe84dd
movq (%rsi), %rcx
movq (%rdx), %rax
movq %rcx, 0x58(%rsp)
movq %rax, 0x60(%rsp)
movabsq $0x3ffffffffffffff, %r13 # imm = 0x3FFFFFFFFFFFFFF
movq 0x10(%rsp), %rbx
movl 0x14(%rbx), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
xorl %esi, %esi
callq 0x20e0
movl $0x1, %edi
movl $0x20, %esi
callq 0x2120
movq %rax, %r14
movq (%rax), %rax
andq %r13, %rax
btsq $0x3b, %rax
movq %rax, (%r14)
movq $0x3, 0x10(%r14)
movl $0x10, %edi
callq 0x2280
movq %rax, %r13
movq %rax, 0x18(%r14)
movq $0x0, 0x38(%r12)
leaq 0x70(%rsp), %rax
leaq 0x78(%rsp), %r10
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x80(%rsp), %r9
movq %rbx, %rdi
movq 0x100(%rsp), %rsi
movl 0x1c(%rsp), %edx
pushq %r12
pushq $0x1
pushq %rax
pushq %r10
callq 0x10109f
addq $0x20, %rsp
cmpl $0x7, %eax
jne 0xe85ac
movq %r13, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x20(%rsp)
movb %r15b, 0x7(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x90(%rsp)
movl %eax, %ebx
cmpq $0x0, 0x38(%r12)
movq 0xf8(%rsp), %r8
je 0xe861f
movl $0x1, %eax
xorl %edx, %edx
movq 0x48(%r12), %rcx
movl (%rcx,%rdx,4), %esi
movl %esi, %edi
movl %ebp, %ecx
shll %cl, %edi
shrl %esi
andb $0x1, %dil
incb %dil
orb %dil, (%r8,%rsi)
movq 0x48(%r12), %rcx
movl (%rcx,%rdx,4), %edx
movl %edx, %esi
notl %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%r8,%rdx)
movl %eax, %edx
incl %eax
cmpq %rdx, 0x38(%r12)
ja 0xe85dc
leaq 0xc(%rsp), %rax
movl (%rax), %eax
movl %eax, 0x88(%rsp)
movq 0x20(%rsp), %rdi
movl 0x1c(%rsp), %esi
callq 0xe8be3
movq 0xf8(%rsp), %r8
cvtsi2ss %rbx, %xmm0
mulss 0x48(%rsp), %xmm0
movss %xmm0, 0x3c(%rsp)
xorps %xmm0, %xmm0
cvtsi2ss %rbx, %xmm0
mulss 0x8(%rsp), %xmm0
movss %xmm0, 0x8c(%rsp)
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq $0x0, 0x28(%rsp)
movl $0x0, 0x38(%rsp)
xorl %edx, %edx
movq 0x68(%rsp), %r14
movq 0x20(%rsp), %rbx
movq 0x8(%rbx), %r13
movl %edx, %eax
movq %rax, 0x40(%rsp)
xorl %r15d, %r15d
movl %ecx, %eax
movl %eax, %ecx
movq %r13, %rax
addq %r15, %rax
movl %ecx, 0x8(%rsp)
je 0xe8b13
movq 0x18(%rbx), %rcx
movq (%rbx), %rdx
movabsq $0x3ffffffffffffff, %rsi # imm = 0x3FFFFFFFFFFFFFF
andq %rsi, %rdx
addq %rdx, %rax
decq %rax
andq 0x10(%rbx), %rax
movl (%rcx,%rax,4), %edx
cmpl %r15d, 0x40(%rsp)
movq %rdx, 0x48(%rsp)
je 0xe8872
movq $0x0, 0x38(%r12)
movq 0x10(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %r12
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
cmpl $0x7, %eax
je 0xe8b1c
movq 0x30(%rsp), %rax
movl %eax, %ecx
testl %eax, %eax
je 0xe8784
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movss 0x3c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
ja 0xe8784
cmpq %r14, %rcx
jge 0xe8764
cmpq 0xa0(%rsp), %rcx
jl 0xe8784
ucomiss 0x3c(%rsp), %xmm0
jb 0xe8784
decq %r15
cmpl $-0x1, 0x8(%rsp)
movq 0xf8(%rsp), %r8
movq 0x48(%rsp), %rax
je 0xe869d
jmp 0xe8b38
leaq (%r15,%r13), %rdx
decq %rdx
xorps %xmm0, %xmm0
cvtsi2ss %rcx, %xmm0
movq %rdx, 0x8(%rbx)
ucomiss 0x8c(%rsp), %xmm0
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0xf8(%rsp), %r8
ja 0xe8b48
movl 0x38(%rsp), %ecx
addl %eax, %ecx
cmpl 0x90(%rsp), %ecx
ja 0xe8b48
movl %ecx, %eax
movq 0x40(%rsp), %rcx
subq %r15, %rcx
incq %rcx
movq %rcx, 0x50(%rsp)
cmpb $0x0, 0x7(%rsp)
movl %eax, 0x38(%rsp)
je 0xe8887
cmpq $0x0, 0x38(%r12)
movq 0x28(%rsp), %rdx
je 0xe8887
movl $0x1, %r14d
xorl %r13d, %r13d
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rcx
cmpq %rcx, %rdx
jne 0xe8845
movq %rdx, 0x28(%rsp)
leaq (%rcx,%rcx), %rbx
testq %rcx, %rcx
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
callq 0x2370
movq 0xf8(%rsp), %r8
movq %rax, %rdi
movq %rbx, %rcx
movq 0x28(%rsp), %rdx
movq 0x48(%r12), %rax
movl (%rax,%r13,4), %eax
movl %eax, (%rdi,%rdx,4)
incq %rdx
movl %r14d, %r13d
incl %r14d
cmpq %r13, 0x38(%r12)
ja 0xe880b
movq %rdx, 0x28(%rsp)
movq %rcx, 0x58(%rsp)
movq %rdi, 0x60(%rsp)
jmp 0xe8887
leaq (%r15,%r13), %rax
decq %rax
movq %rax, 0x8(%rbx)
movl $0x1, %eax
movq %rax, 0x50(%rsp)
movl 0xc(%rsp), %eax
movq 0x10(%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%rax,8), %rax
testl %eax, %eax
je 0xe89ae
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %r13
shlq $0x3, %r13
movq 0x10(%rsp), %rcx
addq 0x8(%rcx), %r13
movl %eax, %edi
xorl %r14d, %r14d
movq %rdi, 0x98(%rsp)
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%r13,%rax,8)
js 0xe89a2
leaq (,%rax,8), %rbx
addq %r13, %rbx
movl 0x8(%rbx), %esi
cmpl 0x1c(%rsp), %esi
je 0xe8af9
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
jne 0xe89a2
movq 0x20(%rsp), %rdi
callq 0xe8be3
movq $0x0, 0x38(%r12)
movl 0x8(%rbx), %edx
movq 0x10(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %r12
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x10109f
movq 0xb8(%rsp), %rdi
movq 0x118(%rsp), %r8
addq $0x20, %rsp
cmpq $0x0, 0x38(%r12)
je 0xe89a2
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x48(%r12), %rdx
movl (%rdx,%rcx,4), %edx
movl %edx, %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%r8,%rdx)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r12)
ja 0xe897c
incq %r14
cmpq %rdi, %r14
jne 0xe88c5
testl %ebp, %ebp
movq 0x68(%rsp), %r14
movl 0x8(%rsp), %ecx
movq 0x50(%rsp), %rdx
je 0xe8688
cmpl %r15d, 0x40(%rsp)
je 0xe8688
movq 0x48(%rsp), %rsi
xorq $0x1, %rsi
movq 0x10(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%rsi,8), %rax
testl %eax, %eax
je 0xe8688
movq %rax, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rbx
shlq $0x3, %rbx
movq 0x10(%rsp), %rcx
addq 0x8(%rcx), %rbx
movl %eax, %r15d
xorl %r14d, %r14d
leaq (%r14,%r14,2), %rax
cmpl $0x0, 0xc(%rbx,%rax,8)
js 0xe8ada
leaq (%rbx,%rax,8), %r13
movl 0x8(%r13), %esi
cmpl 0x1c(%rsp), %esi
je 0xe8af9
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
jne 0xe8ada
movq 0x20(%rsp), %rdi
callq 0xe8be3
movq $0x0, 0x38(%r12)
movl 0x8(%r13), %edx
movq 0x10(%rsp), %rdi
movq 0x100(%rsp), %rsi
leaq 0xc(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x80(%rsp), %r9
pushq %r12
pushq $0x1
leaq 0x80(%rsp), %rax
pushq %rax
leaq 0x90(%rsp), %rax
pushq %rax
callq 0x10109f
movq 0x118(%rsp), %r8
addq $0x20, %rsp
cmpq $0x0, 0x38(%r12)
je 0xe8ada
movl $0x1, %eax
xorl %ecx, %ecx
movq 0x48(%r12), %rdx
movl (%rdx,%rcx,4), %edx
movl %edx, %esi
movl %ebp, %ecx
shll %cl, %esi
shrl %edx
andb $0x1, %sil
incb %sil
orb %sil, (%r8,%rdx)
movl %eax, %ecx
incl %eax
cmpq %rcx, 0x38(%r12)
ja 0xe8ab4
incq %r14
cmpq %r15, %r14
jne 0xe8a0b
movq 0x68(%rsp), %r14
movl 0x8(%rsp), %ecx
movq 0x50(%rsp), %rdx
jmp 0xe8688
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq 0x108(%rsp), %rbx
movb 0x7(%rsp), %bpl
movq 0x20(%rsp), %r14
jmp 0xe8b58
addl 0x40(%rsp), %r13d
xorl %eax, %eax
jmp 0xe8b44
leaq (%r15,%r13), %rax
decq %rax
movq %rax, 0x8(%rbx)
movq 0x18(%rbx), %rdi
callq 0x2220
movq %rbx, %rdi
jmp 0xe8593
addq %r15, %r13
movq %r13, %rax
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %rax, 0x8(%rbx)
movq %rbx, %r14
movq 0x108(%rsp), %rbx
movb 0x7(%rsp), %bpl
movq 0x18(%r14), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
testb %bpl, %bpl
movq 0x60(%rsp), %rdx
je 0xe8b9e
movq 0xf0(%rsp), %rax
movq 0x28(%rsp), %rcx
movq %rcx, (%rax)
movq 0xe8(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, (%rax)
movq 0xe0(%rsp), %rax
movq %rdx, (%rax)
movl 0x8(%rsp), %esi
movl %esi, (%rbx)
xorl %eax, %eax
cmpl $-0x1, %r13d
je 0xe859a
cmpl $0x1, %r13d
je 0xe859a
movl $0x1, %eax
testb %bpl, %bpl
je 0xe859a
movl 0x88(%rsp), %edi
movq 0x28(%rsp), %rcx
movq 0x10(%rsp), %r8
callq 0xe8201
jmp 0xe859a