// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: walk_through(asg_t*, ma_ug_t*, ma_hit_t_alloc*, long long, long long, float, float, buf_t*, buf_t*, kvec_t_u32_warp*, unsigned char*, unsigned int, unsigned int*, unsigned int*, unsigned int*, R_to_U*, unsigned char*)
inline uint32_t walk_through(asg_t *read_g, ma_ug_t *ug, ma_hit_t_alloc* reverse_sources, long long minLongUntig, 
long long maxShortUntig, float l_untig_rate, float max_node_threshold, buf_t* b_0, buf_t* b_1,
kvec_t_u32_warp* u_vecs, uint8_t* visit, uint32_t v, uint32_t* r_beg, uint32_t* r_end, 
uint32_t* r_next_uID, R_to_U* ruIndex, uint8_t* is_r_het)
{
    (*r_beg) = (*r_end) = (uint32_t)-1;
    asg_t* nsg = ug->g;
    uint32_t i, beg, end, primaryLen, returnFlag;
    long long nodeLen, baseLen, max_stop_nodeLen, max_stop_baseLen;

    /*****************************simple checking**********************************/
    beg = v;
    if(nsg->seq[beg>>1].del || asg_arc_n(nsg, beg) <= 0 || get_real_length(nsg, beg, NULL)<=0)
    {
        return 0;
    }
    if(get_real_length(nsg, beg^1, NULL) == 1)///check if beg is the tig end
    {
        get_real_length(nsg, beg^1, &end);
        if(get_real_length(nsg, end^1, NULL) == 1)
        {
            return 0;
        }
    }

    ///if the contig here is too small
    primaryLen = get_unitig(nsg, ug, beg, &end, &nodeLen, &baseLen, 
                                        &max_stop_nodeLen, &max_stop_baseLen, 1, NULL);
    if(primaryLen == LOOP || nodeLen < minLongUntig)
    {
        return 0;
    } 
    primaryLen = nodeLen;
    
    if(get_real_length(nsg, end, NULL) <= 0)///if it is already a simple contig
    {
        return 0;
    }
    /*****************************simple checking**********************************/
    (*r_beg) = beg; (*r_end) = end;
    
    /*****************************adjacent checking**********************************/
    uint32_t nw = asg_arc_n(nsg, end), n_arc = 0, next_uID, next_uID_verify;
    asg_arc_t *aw = asg_arc_a(nsg, end);
    for (i = 0; i < nw; i++)
    {
        if(!aw[i].del)
        {
            n_arc++;
            ////we don't want any long tip here
            if(if_long_tip_length(nsg, ug, aw[i].v, NULL, 
                minLongUntig, maxShortUntig, l_untig_rate, primaryLen)==1)
            {
                n_arc = 0;
                break;
            }
        }
    }

    if(n_arc == 0)
    {
        return 0;
    }
    /*****************************adjacent checking**********************************/

    ///here all out-nodes of v are small untigs
    if(explore_graph(nsg, beg, max_node_threshold, l_untig_rate, 
    minLongUntig, maxShortUntig, 1, b_0, &(u_vecs->a.a), &(u_vecs->a.m), 
    &(u_vecs->a.n), visit, ug, &next_uID) == 1)
    {
        (*r_next_uID) = next_uID;
        if(next_uID != (uint32_t)-1 && u_vecs->a.n == 1 
                        && IsMerge(ug->u, u_vecs->a.a[0]>>1) > 0)
        {
            return 0;
        }
        ///if next_uID == (uint32_t)-1, that means we found an end subgraph
        if(next_uID != (uint32_t)-1)
        {
            ///need to avoid missassembly
            ///merge all tangle as two types: 1. convex; 2, unroll them as a line
            ///should do somthing here, but we just skip it for covenience
            returnFlag = explore_graph(nsg, beg, max_node_threshold, l_untig_rate, 
            minLongUntig, maxShortUntig, 0, b_0, NULL, NULL, NULL, visit, ug, 
            &next_uID_verify);

            if((returnFlag == 1 && next_uID_verify != next_uID) || (returnFlag == 0))
            {
                //output_tangles(beg, next_uID, u_vecs->a.a, u_vecs->a.n, (char*)("###"));
                returnFlag = 0;
            }
            else
            {
                returnFlag = 1;
            }
            
            // #define UNAVAILABLE (uint32_t)-1
            // #define PLOID 0
            // #define NON_PLOID 1
            if(returnFlag == 1 && check_different_haps(nsg, ug, read_g, beg, next_uID, 
                reverse_sources, b_0, b_1, ruIndex, is_r_het, minLongUntig-1, 1) == PLOID)
            {
                ///output_tangles(beg, next_uID, u_vecs->a.a, u_vecs->a.n, (char*)("???"));
                returnFlag = 0;
            }

            if(returnFlag == 0)
            {
                merge_nodes(ug, end, next_uID, u_vecs->a.a, u_vecs->a.n, CONVEX);
                return CONVEX_M;
            }
        }

        ///actually it is not possible here
        if(u_vecs->a.n <= 0 || next_uID>>1 == beg>>1)
        {
            return 0;
        }
        ///output_tangles(beg, next_uID, u_vecs->a.a, u_vecs->a.n, (char*)(""));
        merge_nodes(ug, end, next_uID, u_vecs->a.a, u_vecs->a.n, UNROLL);
        if(next_uID != (uint32_t)-1)
        {
            (*r_next_uID) = next_uID;
            return UNROLL_M;
        }
        else ///end tangle
        {
            if(u_vecs->a.n > 0)
            {
                (*r_next_uID) = u_vecs->a.a[0];
            } 
            return UNROLL_E;
        }
    }

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r15
movq %rsi, %rbx
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %rsi
movl 0xc8(%rsp), %r11d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rcx)
movl %eax, (%rsi)
movq 0x18(%rbx), %r14
movq 0x20(%r14), %rax
movl %r11d, %r12d
shrl %r12d
xorl %r13d, %r13d
cmpl $0x0, (%rax,%r12,8)
js 0x1068a2
movq %r9, %rbp
movq 0x28(%r14), %rax
movl %r11d, %ecx
movq (%rax,%rcx,8), %r9
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %r9, %rsi
je 0x1068a2
movq %rdi, 0x48(%rsp)
movq %rdx, 0x50(%rsp)
movq 0x8(%r14), %rcx
shrq $0x20, %r9
leaq (%r9,%r9,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r10d, %r10d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r10d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x1065bd
xorl %r13d, %r13d
testl %r10d, %r10d
jle 0x1068a2
movl %r11d, %edx
xorl $0x1, %edx
movq (%rax,%rdx,8), %rdx
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rdx, %rsi
je 0x106697
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %rdx
addq $0xc, %rdx
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorl %r10d, %r10d
movl (%rdx,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r10d
addq $0x18, %rdi
cmpq %rdi, %rsi
jne 0x106615
cmpl $0x1, %r10d
jne 0x106697
xorl %r9d, %r9d
cmpl $0x0, (%rdx,%r9)
js 0x106645
movl -0x4(%rdx,%r9), %edi
movl %edi, 0x10(%rsp)
addq $0x18, %r9
cmpq %r9, %rsi
jne 0x106635
xorl $0x1, %edi
movq (%rax,%rdi,8), %rax
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rax, %rdx
je 0x106697
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rax
addq $0xc, %rax
shlq $0x3, %rdx
leaq (%rdx,%rdx,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 0x10667b
cmpl $0x1, %esi
je 0x10689f
movss %xmm1, 0x14(%rsp)
movss %xmm0, 0x4(%rsp)
movq %r8, 0x28(%rsp)
leaq 0x60(%rsp), %rax
leaq 0x68(%rsp), %r10
leaq 0x10(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %r14, %rdi
movq %rbx, %rsi
movl %r11d, %edx
pushq $0x0
pushq $0x1
pushq %rax
pushq %r10
callq 0x1053a7
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x1068a2
movq 0x58(%rsp), %rax
cmpq %r15, %rax
jl 0x1068a2
movl 0x10(%rsp), %edx
movq 0x28(%r14), %rcx
movq (%rcx,%rdx,8), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %rcx, %r9
movl 0xc8(%rsp), %esi
movss 0x4(%rsp), %xmm1
movss 0x14(%rsp), %xmm0
je 0x1068a2
movq %r15, 0x18(%rsp)
movq 0x8(%r14), %rdi
movq %rcx, %r8
shrq $0x20, %r8
leaq (%r8,%r8,2), %r8
leaq (%rdi,%r8,8), %r15
addq $0xc, %r15
shlq $0x3, %r9
leaq (%r9,%r9,2), %r10
xorl %edi, %edi
xorl %r8d, %r8d
movl (%r15,%rdi), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %r8d
addq $0x18, %rdi
cmpq %rdi, %r10
jne 0x106748
testl %r8d, %r8d
jle 0x10689f
movq 0xd0(%rsp), %rdi
movl %esi, (%rdi)
movq 0xd8(%rsp), %rdi
movl %edx, (%rdi)
testl %ecx, %ecx
je 0x10688f
movq %rbp, 0x20(%rsp)
movl %eax, %eax
movq %rax, 0x38(%rsp)
movl %ecx, %eax
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x40(%rsp)
xorl %ebp, %ebp
xorl %r13d, %r13d
movq 0x28(%rsp), %r9
cmpl $0x0, (%r15,%rbp)
js 0x106802
movq %rbx, %rsi
movl -0x4(%r15,%rbp), %edx
subq $0x8, %rsp
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
xorl %ecx, %ecx
movq 0x20(%rsp), %r8
movaps %xmm1, %xmm0
pushq 0x40(%rsp)
callq 0xab27f
addq $0x10, %rsp
testl %eax, %eax
jne 0x106898
incl %r13d
movl 0xc8(%rsp), %esi
movq 0x28(%rsp), %r9
movss 0x4(%rsp), %xmm1
movss 0x14(%rsp), %xmm0
addq $0x18, %rbp
cmpq %rbp, 0x40(%rsp)
jne 0x1067a9
testl %r13d, %r13d
je 0x10688f
movq 0xb8(%rsp), %rbp
leaq 0x10(%rbp), %r15
leaq 0x8(%rbp), %rax
movq %r9, %rcx
leaq 0x34(%rsp), %r10
movq %r14, %rdi
movq 0x18(%rsp), %rdx
movl $0x1, %r8d
movq 0x20(%rsp), %r9
pushq %r10
pushq %rbx
pushq 0xd0(%rsp)
pushq %rbp
pushq %rax
pushq %r15
callq 0xe9c3b
addq $0x30, %rsp
testl %eax, %eax
je 0x10688f
movl 0x34(%rsp), %r9d
movq 0xe0(%rsp), %rax
movl %r9d, (%rax)
cmpl $-0x1, %r9d
je 0x106981
cmpq $0x1, (%rbp)
jne 0x1068b4
movq 0x10(%rbx), %rax
movq (%r15), %rcx
movl (%rcx), %ecx
shrl %ecx
leaq (%rcx,%rcx,4), %rcx
cmpl $0x0, 0x8(%rax,%rcx,8)
je 0x1068b4
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %r13
jmp 0x1068a2
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
subq $0x30, %rsp
leaq 0x60(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
movl 0xf8(%rsp), %esi
movss 0x44(%rsp), %xmm0
movss 0x34(%rsp), %xmm1
movq 0x48(%rsp), %rdx
movq 0x58(%rsp), %rcx
xorl %r8d, %r8d
movl %r9d, 0x34(%rsp)
movq 0x50(%rsp), %r9
callq 0xe9c3b
movl 0x34(%rsp), %r9d
addq $0x30, %rsp
testl %eax, %eax
je 0x1069d6
cmpl %r9d, 0x30(%rsp)
jne 0x1069d6
movq 0x18(%rsp), %rax
decl %eax
movq %r14, %rdi
movq %rbx, %rsi
movq 0x48(%rsp), %rdx
movl 0xc8(%rsp), %ecx
movl %r9d, %r8d
movq 0x50(%rsp), %r9
pushq $0x1
pushq %rax
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0xd0(%rsp)
pushq 0x48(%rsp)
callq 0x105913
movl 0x34(%rsp), %r9d
addq $0x30, %rsp
testl %eax, %eax
je 0x1069d6
movq (%rbp), %r8
testq %r8, %r8
sete %al
movl %r9d, %ecx
shrl %ecx
cmpl %r12d, %ecx
sete %cl
orb %al, %cl
movl $0x0, %eax
movq %rax, 0x8(%rsp)
jne 0x106898
movl 0x10(%rsp), %esi
movq (%r15), %rcx
movq %rbx, %rdi
movl %r9d, %edx
movl %r9d, %ebx
xorl %r9d, %r9d
callq 0xeaaf8
cmpl $-0x1, %ebx
je 0x106a02
movq 0xe0(%rsp), %rax
movl %ebx, (%rax)
movl $0x2, %eax
jmp 0x106a24
movl 0x10(%rsp), %esi
movq 0x10(%rbp), %rcx
movl (%rbp), %r8d
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movl %r9d, %edx
movl $0x1, %r9d
callq 0xeaaf8
jmp 0x106898
movq 0xb8(%rsp), %rax
cmpq $0x0, (%rax)
movq 0xe0(%rsp), %rcx
je 0x106a1f
movq (%r15), %rax
movl (%rax), %eax
movl %eax, (%rcx)
movl $0x3, %eax
movq %rax, 0x8(%rsp)
jmp 0x106898