// File: /workspace/github_cpp_makefile_star2_O2/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 $0x88, %rsp
movq %rcx, 0x28(%rsp)
movq 0xe8(%rsp), %rbp
orl $-0x1, (%rbp)
movq 0xe0(%rsp), %r15
orl $-0x1, (%r15)
movl 0xd8(%rsp), %ebx
movq %rsi, 0x10(%rsp)
movq 0x18(%rsi), %r14
movq 0x20(%r14), %rax
movl %ebx, %r12d
shrl %r12d
xorl %r13d, %r13d
cmpl $0x0, (%rax,%r12,8)
js 0xc430e
movss %xmm1, 0x34(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %r8, 0x20(%rsp)
movq %rdi, 0x50(%rsp)
movq %rdx, 0x58(%rsp)
movq %r9, 0x60(%rsp)
movq 0x28(%r14), %rax
movl %ebx, %ecx
cmpl $0x0, (%rax,%rcx,8)
je 0xc430e
xorl %r13d, %r13d
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
jle 0xc430e
movl %ebx, %r13d
xorl $0x1, %r13d
movq %r14, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0xc4020
leaq 0xc(%rsp), %rax
movq %r14, %rdi
movl %r13d, %esi
movq %rax, %r13
movq %rax, %rdx
callq 0xc2e7a
movl (%r13), %esi
xorl $0x1, %esi
xorl %r13d, %r13d
movq %r14, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
je 0xc430e
leaq 0x70(%rsp), %rax
leaq 0x78(%rsp), %r10
leaq 0xc(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x80(%rsp), %r9
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movl %ebx, %edx
pushq $0x0
pushq $0x1
pushq %rax
pushq %r10
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
jne 0xc4063
xorl %r13d, %r13d
jmp 0xc430e
movq 0x68(%rsp), %rbx
movl $0x0, %r13d
cmpq 0x28(%rsp), %rbx
jl 0xc430e
movl 0xc(%rsp), %esi
xorl %r13d, %r13d
movq %r14, %rdi
xorl %edx, %edx
callq 0xc2e7a
testl %eax, %eax
movl 0xd8(%rsp), %eax
jle 0xc430e
movl %eax, (%r15)
movl 0xc(%rsp), %eax
movl %eax, (%rbp)
movq 0x28(%r14), %rcx
movq (%rcx,%rax,8), %rax
movq 0x8(%r14), %rcx
movl %eax, %edx
shrq $0x20, %rax
movl %ebx, %esi
movq %rsi, 0x40(%rsp)
imulq $0x18, %rdx, %rdx
movq %rdx, 0x48(%rsp)
imulq $0x18, %rax, %rax
leaq (%rax,%rcx), %r15
addq $0xc, %r15
xorl %ebx, %ebx
xorl %ebp, %ebp
movq 0x20(%rsp), %r9
movss 0x1c(%rsp), %xmm1
cmpq %rbx, 0x48(%rsp)
je 0xc4132
cmpl $0x0, (%r15,%rbx)
js 0xc412c
movl -0x4(%r15,%rbx), %edx
subq $0x8, %rsp
xorl %r13d, %r13d
movq %r14, %rdi
movq 0x18(%rsp), %rsi
xorl %ecx, %ecx
movq 0x30(%rsp), %r8
movaps %xmm1, %xmm0
pushq 0x48(%rsp)
callq 0x714e8
popq %rcx
popq %rdx
testl %eax, %eax
jne 0xc430e
incl %ebp
movq 0x20(%rsp), %r9
movss 0x1c(%rsp), %xmm1
addq $0x18, %rbx
jmp 0xc40e0
testl %ebp, %ebp
je 0xc405b
movq 0xc8(%rsp), %rbp
leaq 0x10(%rbp), %r15
leaq 0x8(%rbp), %rax
leaq 0x3c(%rsp), %r10
pushq $0x1
popq %r13
movq %r14, %rdi
movl 0xd8(%rsp), %esi
movss 0x34(%rsp), %xmm0
movq 0x28(%rsp), %rdx
movq %r9, %rcx
movl %r13d, %r8d
movq 0x60(%rsp), %rbx
movq %rbx, %r9
pushq %r10
pushq 0x18(%rsp)
pushq 0xe0(%rsp)
pushq %rbp
pushq %rax
pushq %r15
callq 0xab509
addq $0x30, %rsp
testl %eax, %eax
je 0xc405b
movl 0x3c(%rsp), %r9d
movq 0xf0(%rsp), %rax
movl %r9d, (%rax)
cmpl $-0x1, %r9d
je 0xc42a4
cmpq $0x1, (%rbp)
jne 0xc41d8
movq 0x10(%rsp), %rax
movq 0x10(%rax), %rax
movq (%r15), %rcx
movl (%rcx), %ecx
shrl %ecx
imulq $0x28, %rcx, %rcx
cmpl $0x0, 0x8(%rax,%rcx)
jne 0xc405b
subq $0x30, %rsp
leaq 0x68(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x18(%rsp)
xorps %xmm0, %xmm0
movups %xmm0, (%rsp)
andq $0x0, 0x10(%rsp)
movq %r14, %rdi
movl 0x108(%rsp), %esi
movss 0x64(%rsp), %xmm0
movss 0x4c(%rsp), %xmm1
movq 0x58(%rsp), %rdx
movq 0x50(%rsp), %rcx
xorl %r8d, %r8d
movl %r9d, 0x50(%rsp)
movq %rbx, %r9
callq 0xab509
movl 0x50(%rsp), %r9d
addq $0x30, %rsp
testl %eax, %eax
je 0xc42f2
cmpl %r9d, 0x38(%rsp)
jne 0xc42f2
movq 0x28(%rsp), %rax
decl %eax
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq 0x50(%rsp), %rdx
movl 0xd8(%rsp), %ecx
movl %r9d, %r8d
movq 0x58(%rsp), %r9
pushq $0x1
pushq %rax
pushq 0x110(%rsp)
pushq 0x110(%rsp)
pushq 0xe0(%rsp)
pushq %rbx
callq 0xc3307
movl 0x50(%rsp), %r9d
addq $0x30, %rsp
testl %eax, %eax
je 0xc42f2
movq (%rbp), %r8
testq %r8, %r8
sete %al
movl %r9d, %ecx
shrl %ecx
cmpl %r12d, %ecx
sete %cl
orb %al, %cl
movl $0x0, %r13d
jne 0xc430e
movl 0xc(%rsp), %esi
movq (%r15), %rcx
movq 0x10(%rsp), %rdi
movl %r9d, %edx
movl %r9d, %ebx
xorl %r9d, %r9d
callq 0xac163
cmpl $-0x1, %ebx
je 0xc4323
movq 0xf0(%rsp), %rax
movl %ebx, (%rax)
pushq $0x2
popq %r13
jmp 0xc430e
movl 0xc(%rsp), %esi
movq 0x10(%rbp), %rcx
movl (%rbp), %r8d
movq 0x10(%rsp), %rdi
movl %r9d, %edx
movl %r13d, %r9d
callq 0xac163
movl %r13d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x3
popq %r13
movq 0xc8(%rsp), %rax
cmpq $0x0, (%rax)
je 0xc430e
movq (%r15), %rax
movl (%rax), %eax
movq 0xf0(%rsp), %rcx
movl %eax, (%rcx)
jmp 0xc430e