// File: /workspace/github_cpp_makefile_star2_O1/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, %r11
movq 0xd8(%rsp), %rcx
movq 0xd0(%rsp), %r15
movl 0xc8(%rsp), %r10d
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %eax, (%rcx)
movl %eax, (%r15)
movq 0x18(%rsi), %r14
movq 0x20(%r14), %rax
movl %r10d, %r12d
shrl %r12d
xorl %r13d, %r13d
cmpl $0x0, (%rax,%r12,8)
js 0x102714
movq %r9, %rbp
movq 0x28(%r14), %rax
movl %r10d, %ecx
movq (%rax,%rcx,8), %rcx
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
andq %rcx, %r9
je 0x102714
movq %rsi, %rbx
movq %r8, 0x18(%rsp)
movq %rdi, 0x50(%rsp)
movq 0x8(%r14), %rsi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rsi,%rcx,8), %rcx
addq $0xc, %rcx
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
xorl %esi, %esi
xorl %edi, %edi
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %r9
jne 0x1022ce
xorl %r13d, %r13d
testl %edi, %edi
jle 0x102714
movq %rdx, 0x40(%rsp)
movl %r10d, %ecx
xorl $0x1, %ecx
movq (%rax,%rcx,8), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rcx, %rdx
je 0x1023cc
movq 0x8(%r14), %rdi
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
leaq (%rdi,%rsi,8), %rdi
addq $0xc, %rdi
leaq (,%rdx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %r8d, %r8d
xorl %r9d, %r9d
movl (%rdi,%r8), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %r9d
addq $0x18, %r8
cmpq %r8, %rcx
jne 0x102333
cmpl $0x1, %r9d
jne 0x1023cc
testq %rdx, %rdx
je 0x10237a
movq 0x8(%r14), %rdx
leaq (%rdx,%rsi,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
cmpl $0x0, (%rdx,%rsi)
js 0x102371
movl -0x4(%rdx,%rsi), %edi
movl %edi, 0x4(%rsp)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0x102363
movl 0x4(%rsp), %ecx
xorq $0x1, %rcx
movq (%rax,%rcx,8), %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0x1023cc
movq 0x8(%r14), %rdx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,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 0x1023b0
cmpl $0x1, %esi
je 0x102711
movss %xmm1, 0x8(%rsp)
movss %xmm0, 0xc(%rsp)
movq %r11, 0x10(%rsp)
leaq 0x60(%rsp), %rax
leaq 0x68(%rsp), %r10
leaq 0x4(%rsp), %rcx
leaq 0x58(%rsp), %r8
leaq 0x70(%rsp), %r9
movq %r14, %rdi
movq %rbx, %rsi
movl 0xc8(%rsp), %edx
pushq $0x0
pushq $0x1
pushq %rax
pushq %r10
callq 0x10109f
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x102714
movq 0x58(%rsp), %rax
cmpq 0x10(%rsp), %rax
jl 0x102714
movl 0x4(%rsp), %edx
movq 0x28(%r14), %rcx
movq (%rcx,%rdx,8), %rcx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %rcx, %r8
movss 0xc(%rsp), %xmm1
movss 0x8(%rsp), %xmm0
je 0x102714
movq 0x8(%r14), %rdi
movq %rcx, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %rdi
addq $0xc, %rdi
shlq $0x3, %r8
leaq (%r8,%r8,2), %r8
xorl %r9d, %r9d
xorl %r10d, %r10d
movl (%rdi,%r9), %r11d
notl %r11d
shrl $0x1f, %r11d
addl %r11d, %r10d
addq $0x18, %r9
cmpq %r9, %r8
jne 0x102478
testl %r10d, %r10d
jle 0x102711
movl 0xc8(%rsp), %edi
movl %edi, (%r15)
movq 0xd8(%rsp), %r8
movl %edx, (%r8)
testl %ecx, %ecx
je 0x102711
movq %rbp, 0x20(%rsp)
movq %rbx, 0x30(%rsp)
movq 0x8(%r14), %rdx
movl %eax, %eax
movq %rax, 0x38(%rsp)
movl %ecx, %eax
leaq (%rdx,%rsi,8), %r13
addq $0xc, %r13
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %rcx
cmpl $0x0, (%r13,%rbp)
js 0x102546
movl -0x4(%r13,%rbp), %edx
subq $0x8, %rsp
xorl %ebx, %ebx
movq %r14, %rdi
movq 0x38(%rsp), %rsi
xorl %ecx, %ecx
movq 0x20(%rsp), %r9
movaps %xmm1, %xmm0
pushq 0x40(%rsp)
callq 0xaa292
movss 0x18(%rsp), %xmm0
movss 0x1c(%rsp), %xmm1
movq 0x28(%rsp), %rcx
movl 0xd8(%rsp), %edi
movq 0x20(%rsp), %r8
addq $0x10, %rsp
testl %eax, %eax
jne 0x102554
incl %r15d
addq $0x18, %rbp
movl %r15d, %ebx
cmpq %rbp, 0x48(%rsp)
jne 0x1024f0
xorl %r13d, %r13d
testl %ebx, %ebx
je 0x102714
movq 0xb8(%rsp), %r11
movq %r8, %rdx
leaq 0x10(%r11), %r15
leaq 0x8(%r11), %rax
movl %edi, %esi
leaq 0x2c(%rsp), %r10
movq %r14, %rdi
movl $0x1, %r8d
movq 0x20(%rsp), %r9
pushq %r10
movq 0x38(%rsp), %rbx
pushq %rbx
pushq 0xd0(%rsp)
pushq %r11
pushq %rax
pushq %r15
callq 0xe844d
addq $0x30, %rsp
testl %eax, %eax
je 0x102714
movl 0x2c(%rsp), %r9d
movq 0xe0(%rsp), %rbp
movl %r9d, (%rbp)
cmpl $-0x1, %r9d
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movss 0xc(%rsp), %xmm1
movss 0x8(%rsp), %xmm0
je 0x1026c2
movq 0xb8(%rsp), %rax
cmpq $0x1, (%rax)
jne 0x102605
movq 0x10(%rbx), %rax
movq (%r15), %rsi
movl (%rsi), %esi
shrl %esi
leaq (%rsi,%rsi,4), %rsi
cmpl $0x0, 0x8(%rax,%rsi,8)
jne 0x102714
cmpl $-0x1, %r9d
je 0x1026c2
subq $0x30, %rsp
leaq 0x58(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rbx, 0x20(%rsp)
movq 0xf0(%rsp), %rax
movq %rax, 0x18(%rsp)
xorps %xmm2, %xmm2
movups %xmm2, (%rsp)
movq $0x0, 0x10(%rsp)
movq %r14, %rdi
movl 0xf8(%rsp), %esi
xorl %r8d, %r8d
movl %r9d, %ebp
movq 0x50(%rsp), %r9
callq 0xe844d
movl %ebp, %r9d
movq 0x40(%rsp), %r10
addq $0x30, %rsp
testl %eax, %eax
je 0x102726
cmpl %r9d, 0x28(%rsp)
jne 0x102726
decl %r10d
movq %r14, %rdi
movq %rbx, %rsi
movq 0x50(%rsp), %rdx
movl 0xc8(%rsp), %ecx
movl %r9d, %r8d
movq 0x40(%rsp), %r9
pushq $0x1
pushq %r10
pushq 0x100(%rsp)
pushq 0x100(%rsp)
pushq 0xd0(%rsp)
pushq 0x48(%rsp)
callq 0x1015d1
movl %ebp, %r9d
addq $0x30, %rsp
testl %eax, %eax
je 0x102726
movq 0xb8(%rsp), %rax
movq (%rax), %r8
testq %r8, %r8
sete %al
movl %r9d, %ecx
shrl %ecx
cmpl %r12d, %ecx
sete %cl
orb %al, %cl
jne 0x102714
movl 0x4(%rsp), %esi
movq (%r15), %rcx
movq %rbx, %rdi
movl %r9d, %edx
movl %r9d, %ebx
xorl %r9d, %r9d
callq 0xe9168
cmpl $-0x1, %ebx
je 0x102752
movq 0xe0(%rsp), %rcx
movl %ebx, (%rcx)
movl $0x2, %r13d
jmp 0x102714
xorl %r13d, %r13d
movl %r13d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x4(%rsp), %esi
movq 0xb8(%rsp), %rax
movq 0x10(%rax), %rcx
movl (%rax), %r8d
movl $0x1, %r13d
movq %rbx, %rdi
movl %r9d, %edx
movl $0x1, %r9d
callq 0xe9168
jmp 0x102714
movl $0x3, %r13d
movq 0xb8(%rsp), %rax
cmpq $0x0, (%rax)
je 0x102714
movq (%r15), %rax
movl (%rax), %eax
movq 0xe0(%rsp), %rcx
movl %eax, (%rcx)
jmp 0x102714