// File: 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
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xc8, %rsp
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movl 0x28(%rbp), %eax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movq %rdx, -0x38(%rbp)
movq %rcx, -0x40(%rbp)
movq %r8, -0x48(%rbp)
movss %xmm0, -0x4c(%rbp)
movss %xmm1, -0x50(%rbp)
movq %r9, -0x58(%rbp)
movq 0x38(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq 0x30(%rbp), %rax
movl $0xffffffff, (%rax)     # imm = 0xFFFFFFFF
movq -0x30(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x60(%rbp)
movl 0x28(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x68(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x168e1e
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x68(%rbp), %ecx
movq (%rax,%rcx,8), %rax
cmpl $0x0, %eax
jbe 0x168e1e
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
jg 0x168e2a
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x168e7b
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
xorl $0x1, %esi
leaq -0x6c(%rbp), %rdx
callq 0x166b50
movq -0x60(%rbp), %rdi
movl -0x6c(%rbp), %esi
xorl $0x1, %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x1, %eax
jne 0x168e79
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
jmp 0x168e7b
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
movl -0x68(%rbp), %edx
leaq -0x6c(%rbp), %rcx
leaq -0x80(%rbp), %r8
leaq -0x88(%rbp), %r9
leaq -0x90(%rbp), %r10
leaq -0x98(%rbp), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movl %eax, -0x70(%rbp)
cmpl $0x7, -0x70(%rbp)
je 0x168ed8
movq -0x80(%rbp), %rax
cmpq -0x40(%rbp), %rax
jge 0x168ee4
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
movq -0x80(%rbp), %rax
movl %eax, -0x70(%rbp)
movq -0x60(%rbp), %rdi
movl -0x6c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x0, %eax
jg 0x168f0c
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
movl -0x68(%rbp), %ecx
movq 0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x6c(%rbp), %ecx
movq 0x38(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x6c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x9c(%rbp)
movl $0x0, -0xa0(%rbp)
movq -0x60(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x60(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x6c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xb0(%rbp)
movl $0x0, -0x64(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x9c(%rbp), %eax
jae 0x169001
movq -0xb0(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x168ff1
movl -0xa0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xa0(%rbp)
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0xb0(%rbp), %rax
movl -0x64(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %edx
movq -0x40(%rbp), %r8
movq -0x48(%rbp), %r9
movss -0x4c(%rbp), %xmm0
movl -0x70(%rbp), %eax
xorl %ecx, %ecx
movq %rax, (%rsp)
callq 0xdc920
cmpl $0x1, %eax
jne 0x168fef
movl $0x0, -0xa0(%rbp)
jmp 0x169001
jmp 0x168ff1
jmp 0x168ff3
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x168f6d
cmpl $0x0, -0xa0(%rbp)
jne 0x169016
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
movss -0x50(%rbp), %xmm0
movss -0x4c(%rbp), %xmm1
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %r9
movq 0x18(%rbp), %r15
addq $0x10, %r15
movq 0x18(%rbp), %r14
addq $0x8, %r14
movq 0x18(%rbp), %rbx
movq 0x20(%rbp), %r11
movq -0x30(%rbp), %r10
movl $0x1, %r8d
leaq -0xa4(%rbp), %rax
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x1409b0
cmpl $0x1, %eax
jne 0x1692a0
movl -0xa4(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
cmpl $-0x1, -0xa4(%rbp)
je 0x1690d5
movq 0x18(%rbp), %rax
cmpq $0x1, (%rax)
jne 0x1690d5
movq -0x30(%rbp), %rax
movq 0x10(%rax), %rax
movq 0x18(%rbp), %rcx
movq 0x10(%rcx), %rcx
movl (%rcx), %ecx
shrl %ecx
movl %ecx, %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
jbe 0x1690d5
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
cmpl $-0x1, -0xa4(%rbp)
je 0x169214
movq -0x60(%rbp), %rdi
movl -0x68(%rbp), %esi
movss -0x50(%rbp), %xmm0
movss -0x4c(%rbp), %xmm1
movq -0x40(%rbp), %rdx
movq -0x48(%rbp), %rcx
movq -0x58(%rbp), %r9
movq 0x20(%rbp), %r11
movq -0x30(%rbp), %r10
xorl %r8d, %r8d
xorl %eax, %eax
leaq -0xa8(%rbp), %rax
movq $0x0, (%rsp)
movq $0x0, 0x8(%rsp)
movq $0x0, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x1409b0
movl %eax, -0x74(%rbp)
cmpl $0x1, -0x74(%rbp)
jne 0x169158
movl -0xa8(%rbp), %eax
cmpl -0xa4(%rbp), %eax
jne 0x16915e
cmpl $0x0, -0x74(%rbp)
jne 0x169167
movl $0x0, -0x74(%rbp)
jmp 0x16916e
movl $0x1, -0x74(%rbp)
cmpl $0x1, -0x74(%rbp)
jne 0x1691d6
movq -0x60(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x28(%rbp), %rdx
movl -0x68(%rbp), %ecx
movl -0xa4(%rbp), %r8d
movq -0x38(%rbp), %r9
movq -0x58(%rbp), %r14
movq 0x10(%rbp), %rbx
movq 0x48(%rbp), %r11
movq 0x50(%rbp), %r10
movq -0x40(%rbp), %rax
subq $0x1, %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movl %eax, 0x20(%rsp)
movl $0x1, 0x28(%rsp)
callq 0x1674d0
cmpl $0x0, %eax
jne 0x1691d6
movl $0x0, -0x74(%rbp)
cmpl $0x0, -0x74(%rbp)
jne 0x169212
movq -0x30(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl -0xa4(%rbp), %edx
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, %r8d
movl $0x1, %r9d
callq 0x141d80
movl $0x1, -0x1c(%rbp)
jmp 0x1692a7
jmp 0x169214
movq 0x18(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0x16922f
movl -0xa4(%rbp), %eax
shrl %eax
movl -0x68(%rbp), %ecx
shrl %ecx
cmpl %ecx, %eax
jne 0x169238
movl $0x0, -0x1c(%rbp)
jmp 0x1692a7
movq -0x30(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl -0xa4(%rbp), %edx
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rcx
movq 0x18(%rbp), %rax
movq (%rax), %rax
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x141d80
cmpl $-0x1, -0xa4(%rbp)
je 0x16927d
movl -0xa4(%rbp), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl $0x2, -0x1c(%rbp)
jmp 0x1692a7
movq 0x18(%rbp), %rax
cmpq $0x0, (%rax)
jbe 0x169297
movq 0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl (%rax), %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movl $0x3, -0x1c(%rbp)
jmp 0x1692a7
movl $0x0, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
addq $0xc8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nopl (%rax,%rax)