// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Overlaps.cpp
// Function: detect_chimeric_by_topo(asg_t*, ma_ug_t*, asg_t*, ma_hit_t_alloc*, long long, unsigned int, float, R_to_U*, utg_trans_t*, unsigned char*)
int detect_chimeric_by_topo(asg_t *g, ma_ug_t *ug, asg_t *read_sg, 
ma_hit_t_alloc* reverse_sources, long long miniedgeLen, uint32_t stops_threshold, float drop_rate,
R_to_U* ruIndex, utg_trans_t *o, uint8_t* is_r_het)
{
    double startTime = Get_T();
    uint32_t i, k, v_i, v_beg, v_end, selfLen, w1, w2, wv, nw, n_vtx = g->n_seq * 2, n_reduced = 0, convex, convex_T, read_num;
    asg_arc_t *aw;
    long long ll, tmp, max_stop_nodeLen, max_stop_baseLen;

    buf_t b_0, b_1;
    memset(&b_0, 0, sizeof(buf_t));
    memset(&b_1, 0, sizeof(buf_t));


    for (v_i = 0; v_i < n_vtx; ++v_i) 
    {
        v_beg = v_i;
        ///some node could be deleted
        if (g->seq[v_beg>>1].del || g->seq[v_beg>>1].c == ALTER_LABLE) continue;
        if(get_real_length(g, v_beg, NULL) != 1) continue;

        get_real_length(g, v_beg, &w1);
        if(get_real_length(g, w1^1, NULL)<=1) continue;

        if(get_unitig(g, ug, v_beg^1, &v_end, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
        1, NULL)==LOOP)
        {
            continue;
        }
        selfLen = ll;
        if(get_real_length(g, v_end, NULL) != 1) continue;


        get_real_length(g, v_end, &w2);
        if(get_real_length(g, w2^1, NULL)<=1) continue;

        get_unitig(g, ug, w1, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
        stops_threshold, NULL);
        if(ll*drop_rate < selfLen) continue;
        if(ll*0.4 > max_stop_nodeLen) continue;

        get_unitig(g, ug, w2, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
        stops_threshold, NULL);
        if(ll*drop_rate < selfLen) continue;
        if(ll*0.4 > max_stop_nodeLen) continue;

        stops_threshold++;


        w1 = w1^1;wv=v_beg^1;aw = asg_arc_a(g, w1); nw = asg_arc_n(g, w1);convex_T = (uint32_t)-1;
        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;

            get_unitig(g, ug, aw[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
            stops_threshold, NULL);
            if(ll*drop_rate < selfLen) break;
            if(ll*0.4 > max_stop_nodeLen) continue;

            if((aw[i].v>>1)==(v_beg>>1))
            {
                if(convex_T != (uint32_t)-1) break;
                convex_T = convex;
            }
        }
        if(i!=nw) continue;

        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;
            if((aw[i].v>>1) == (v_beg>>1)) continue;

            b_0.b.n = 0;
            get_unitig(g, ug, aw[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
            stops_threshold, &b_0);
            ///if(convex == convex_T) break;
            for (k = 0; k < b_0.b.n; k++)
            {
                if((b_0.b.a[k]>>1) == (convex_T>>1)) break;
            }
            if(k != b_0.b.n) break;
            
            if(check_different_haps(g, ug, read_sg, wv, aw[i].v, reverse_sources, &b_0, &b_1, 
            ruIndex, is_r_het, miniedgeLen, stops_threshold)==PLOID)
            {
                break;
            }
        }
        if(i!=nw) continue;





        w2 = w2^1;wv=v_end^1;aw = asg_arc_a(g, w2); nw = asg_arc_n(g, w2);convex_T = (uint32_t)-1;
        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;

            
            get_unitig(g, ug, aw[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
            stops_threshold, NULL);
            if(ll*drop_rate < selfLen) break;
            if(ll*0.4 > max_stop_nodeLen) continue;

            if((aw[i].v>>1) == (v_end>>1))
            {
                if(convex_T != (uint32_t)-1) break;
                convex_T = convex;
            }
        }
        if(i!=nw) continue;

        for (i = 0; i < nw; i++)
        {
            if(aw[i].del) continue;
            if((aw[i].v>>1) == (v_end>>1)) continue;

            b_0.b.n = 0;
            get_unitig(g, ug, aw[i].v, &convex, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 
            stops_threshold, &b_0);
            ///if(convex == convex_T) break;
            for (k = 0; k < b_0.b.n; k++)
            {
                if((b_0.b.a[k]>>1) == (convex_T>>1)) break;
            }
            if(k != b_0.b.n) break;

            if(check_different_haps(g, ug, read_sg, wv, aw[i].v, reverse_sources, &b_0, &b_1, 
            ruIndex, is_r_het, miniedgeLen, stops_threshold)==PLOID)
            {
                break;
            }
        }
        if(i!=nw) continue;

        n_reduced++;
        b_0.b.n = 0;
        read_num = 0;
        get_unitig(g, ug, v_beg^1, &v_end, &ll, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 1, &b_0);
        for (k = 0; k < b_0.b.n; k++)
        {
            g->seq[b_0.b.a[k]>>1].c = ALTER_LABLE;
            read_num += ug->u.a[b_0.b.a[k]>>1].n;
        }

        for (k = 0; k < b_0.b.n; k++)
        {
            asg_seq_drop(g, b_0.b.a[k]>>1);
            if(o) asg_seq_del(o->cug->g, b_0.b.a[k]>>1);
        }

        if(read_num <= CHIMERIC_TRIM_THRES)
        {
            for (k = 0; k < b_0.b.n; k++)
            {
                g->seq[b_0.b.a[k]>>1].c = CUT;
            }
        }

    }

    if(o) asg_cleanup(o->cug->g);
    asg_cleanup(g);
    free(b_0.b.a);
    free(b_1.b.a);
    
    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d long tips\n", 
        __func__, n_reduced);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
    

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movss %xmm0, 0x30(%rsp)
movq %r9, 0x28(%rsp)
movq %r8, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %rdx, 0x88(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %r15d
addl %r15d, %r15d
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
andq $0x0, 0x60(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
leaq 0x110(%rsp), %rax
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movaps %xmm0, 0x40(%rax)
movaps %xmm0, 0x50(%rax)
andq $0x0, 0x60(%rax)
xorl %r13d, %r13d
leaq 0x4(%rsp), %rbp
movl $0x0, 0x6c(%rsp)
cmpl %r15d, %r13d
je 0x9583b
movq 0x20(%rbx), %rax
movl %r13d, %r12d
shrl %r12d
cmpl $0x0, (%rax,%r12,8)
js 0x95221
cmpb $0x1, 0x4(%rax,%r12,8)
je 0x95221
movq %rbx, %rdi
movl %r13d, %esi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x95221
movq %rbx, %rdi
movl %r13d, %esi
movq %rbp, %rdx
callq 0xc2e7a
movl 0x4(%rsp), %esi
xorl $0x1, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x95221
movl %r13d, %r14d
xorl $0x1, %r14d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl %r14d, %edx
leaq 0xc(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x95221
movl 0x10(%rsp), %ebp
movl 0xc(%rsp), %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x1, %eax
jne 0x9521c
movl 0xc(%rsp), %esi
movq %rbx, %rdi
leaq 0x34(%rsp), %rdx
callq 0xc2e7a
movl 0x34(%rsp), %esi
xorl $0x1, %esi
movq %rbx, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jl 0x9521c
movl 0x4(%rsp), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq $0x0
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0x10(%rsp), %rax
cvtsi2ss %rax, %xmm0
movl %ebp, %ecx
cvtsi2ss %rcx, %xmm1
mulss 0x30(%rsp), %xmm0
movss %xmm1, 0x4c(%rsp)
ucomiss %xmm0, %xmm1
jbe 0x95229
leaq 0x4(%rsp), %rbp
incl %r13d
jmp 0x950db
cvtsi2sd %rax, %xmm0
mulsd 0x14e2da(%rip), %xmm0   # 0x1e3510
cvtsi2sdq 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
leaq 0x4(%rsp), %rbp
ja 0x95221
movl 0x34(%rsp), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq $0x0
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0x10(%rsp), %rax
cvtsi2ss %rax, %xmm0
mulss 0x30(%rsp), %xmm0
movss 0x4c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
ja 0x95221
cvtsi2sd %rax, %xmm0
mulsd 0x14e26a(%rip), %xmm0   # 0x1e3510
cvtsi2sdq 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x95221
movq 0x28(%rsp), %rax
incl %eax
movq %rax, 0x28(%rsp)
movl 0x4(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x4(%rsp)
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
movq %rdx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rsi
movq %rsi, 0x78(%rsp)
movq %rdx, 0x70(%rsp)
movl %edx, %edx
movq %rdx, 0x60(%rsp)
leaq (%rcx,%rax), %rbp
addq $0x8, %rbp
xorl %ecx, %ecx
pushq $-0x1
popq %r8
cmpq %rcx, 0x60(%rsp)
je 0x953c7
cmpl $0x0, 0x4(%rbp)
js 0x953b1
movl (%rbp), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %rcx, 0x50(%rsp)
leaq 0x8(%rsp), %rcx
movl %r8d, 0x58(%rsp)
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq $0x0
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0x70(%rsp), %rcx
movl 0x78(%rsp), %r8d
addq $0x20, %rsp
movq 0x10(%rsp), %rax
cvtsi2ss %rax, %xmm0
mulss 0x30(%rsp), %xmm0
movss 0x4c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
ja 0x953bd
cvtsi2sd %rax, %xmm0
mulsd 0x14e181(%rip), %xmm0   # 0x1e3510
cvtsi2sdq 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x953b1
movl (%rbp), %eax
shrl %eax
cmpl %r12d, %eax
jne 0x953b1
cmpl $-0x1, %r8d
jne 0x953bd
movl 0x8(%rsp), %r8d
incq %rcx
addq $0x18, %rbp
jmp 0x95309
cmpl %ecx, 0x70(%rsp)
jne 0x9521c
xorl %ecx, %ecx
movl %r8d, 0x58(%rsp)
cmpq 0x60(%rsp), %rcx
je 0x954f0
imulq $0x18, %rcx, %rbp
movq 0x78(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rbp)
js 0x954de
addq 0x78(%rsp), %rbp
movl 0x8(%rbp), %edx
movl %edx, %eax
shrl %eax
cmpl %r12d, %eax
je 0x954de
movq %rcx, 0x50(%rsp)
andq $0x0, 0xd8(%rsp)
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0xa0(%rsp), %rax
pushq %rax
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
movl 0x78(%rsp), %r8d
addq $0x20, %rsp
movq 0xd8(%rsp), %rax
movq 0xe8(%rsp), %rcx
xorl %esi, %esi
movl %esi, %edx
cmpq %rdx, %rax
jbe 0x95475
movl (%rcx,%rdx,4), %edi
xorl %r8d, %edi
leal 0x1(%rdx), %esi
cmpl $0x1, %edi
ja 0x95460
cmpq %rdx, %rax
movq 0x50(%rsp), %rcx
jne 0x954e6
movl 0x8(%rbp), %r8d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq 0x88(%rsp), %rdx
movl %r14d, %ecx
movq 0x90(%rsp), %r9
pushq 0x28(%rsp)
pushq 0xa0(%rsp)
pushq 0x1d0(%rsp)
pushq 0x1c8(%rsp)
leaq 0x130(%rsp), %rax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0xc3307
movq 0x80(%rsp), %rcx
addq $0x30, %rsp
testl %eax, %eax
je 0x954e6
incq %rcx
jmp 0x953ce
cmpl 0x70(%rsp), %ecx
jne 0x9521c
movl 0x34(%rsp), %eax
xorq $0x1, %rax
movl %eax, 0x34(%rsp)
movl 0xc(%rsp), %ecx
xorl $0x1, %ecx
movl %ecx, 0x50(%rsp)
movq 0x8(%rbx), %rcx
movq 0x28(%rbx), %rdx
movq (%rdx,%rax,8), %rdx
movq %rdx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%rcx,%rax), %rsi
movq %rsi, 0x58(%rsp)
movq %rdx, 0x80(%rsp)
movl %edx, %edx
movq %rdx, 0x60(%rsp)
leaq (%rcx,%rax), %rbp
addq $0x8, %rbp
xorl %ecx, %ecx
pushq $-0x1
popq %r8
cmpq %rcx, 0x60(%rsp)
je 0x95606
cmpl $0x0, 0x4(%rbp)
js 0x955ed
movl (%rbp), %edx
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %rcx, 0x78(%rsp)
leaq 0x8(%rsp), %rcx
movl %r8d, %r12d
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
pushq $0x0
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
movq 0x98(%rsp), %rcx
movl %r12d, %r8d
addq $0x20, %rsp
movq 0x10(%rsp), %rax
cvtsi2ss %rax, %xmm0
mulss 0x30(%rsp), %xmm0
movss 0x4c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
ja 0x955f9
cvtsi2sd %rax, %xmm0
mulsd 0x14df47(%rip), %xmm0   # 0x1e3510
cvtsi2sdq 0x20(%rsp), %xmm1
ucomisd %xmm1, %xmm0
ja 0x955ed
movl 0xc(%rsp), %eax
xorl (%rbp), %eax
cmpl $0x1, %eax
ja 0x955ed
cmpl $-0x1, %r8d
jne 0x955f9
movl 0x8(%rsp), %r8d
incq %rcx
addq $0x18, %rbp
jmp 0x95544
cmpl %ecx, 0x80(%rsp)
jne 0x9521c
xorl %r12d, %r12d
movl %r8d, 0x70(%rsp)
cmpq 0x60(%rsp), %r12
je 0x95728
imulq $0x18, %r12, %rbp
movq 0x58(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rbp)
js 0x95712
addq 0x58(%rsp), %rbp
movl 0x8(%rbp), %edx
movl 0xc(%rsp), %eax
xorl %edx, %eax
cmpl $0x2, %eax
jb 0x95712
andq $0x0, 0xd8(%rsp)
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
leaq 0x8(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0xa0(%rsp), %rax
pushq %rax
pushq 0x30(%rsp)
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
movl 0x90(%rsp), %r8d
addq $0x20, %rsp
movq 0xd8(%rsp), %rax
movq 0xe8(%rsp), %rcx
xorl %esi, %esi
movl %esi, %edx
cmpq %rdx, %rax
jbe 0x956b5
movl (%rcx,%rdx,4), %edi
xorl %r8d, %edi
leal 0x1(%rdx), %esi
cmpl $0x1, %edi
ja 0x956a0
cmpq %rdx, %rax
jne 0x9571a
movl 0x8(%rbp), %r8d
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq 0x88(%rsp), %rdx
movl 0x50(%rsp), %ecx
movq 0x90(%rsp), %r9
pushq 0x28(%rsp)
pushq 0xa0(%rsp)
pushq 0x1d0(%rsp)
pushq 0x1c8(%rsp)
leaq 0x130(%rsp), %rax
pushq %rax
leaq 0xc8(%rsp), %rax
pushq %rax
callq 0xc3307
addq $0x30, %rsp
testl %eax, %eax
je 0x9571a
incq %r12
jmp 0x9560e
cmpl 0x80(%rsp), %r12d
jne 0x9521c
andq $0x0, 0xd8(%rsp)
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movl %r14d, %edx
leaq 0xc(%rsp), %rcx
leaq 0x10(%rsp), %r8
leaq 0x40(%rsp), %r9
leaq 0xa0(%rsp), %rax
pushq %rax
pushq $0x1
leaq 0x48(%rsp), %rax
pushq %rax
leaq 0x38(%rsp), %rax
pushq %rax
callq 0xc2ebf
addq $0x20, %rsp
movq 0xd8(%rsp), %rax
movq 0xe8(%rsp), %rcx
xorl %edx, %edx
xorl %ebp, %ebp
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x957a9
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0x1, 0x4(%rsi,%rdi,8)
movq 0x18(%rsp), %rsi
movq 0x10(%rsi), %rsi
imulq $0x28, %rdi, %rdi
addl 0x10(%rsi,%rdi), %ebp
incl %edx
jmp 0x9577f
xorl %r14d, %r14d
movl %r14d, %r14d
cmpq %r14, %rax
jbe 0x95802
movq 0xe8(%rsp), %rax
movl (%rax,%r14,4), %esi
shrl %esi
movq %rbx, %rdi
callq 0x80870
movq 0x1b8(%rsp), %rax
testq %rax, %rax
je 0x957f5
movq 0x108(%rax), %rax
movq 0x18(%rax), %rdi
movq 0xe8(%rsp), %rax
movl (%rax,%r14,4), %esi
shrl %esi
callq 0x6f8da
incl %r14d
movq 0xd8(%rsp), %rax
jmp 0x957ac
incl 0x6c(%rsp)
cmpl $0x4, %ebp
ja 0x9521c
xorl %edx, %edx
movq 0xe8(%rsp), %rcx
leaq 0x4(%rsp), %rbp
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x95221
movq 0x20(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
shrl %edi
movb $0xb, 0x4(%rsi,%rdi,8)
incl %edx
jmp 0x9581e
movq 0x1b8(%rsp), %rax
testq %rax, %rax
je 0x95858
movq 0x108(%rax), %rax
movq 0x18(%rax), %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x694eb
movq 0xe8(%rsp), %rdi
callq 0x2220
movq 0x158(%rsp), %rdi
callq 0x2220
movl 0x6c(%rsp), %eax
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq