// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_complex_false_link(asg_t*, float, float, int, ma_hit_t_alloc*, long long)
int asg_arc_del_complex_false_link(asg_t *g, float drop_ratio, float o_drop_ratio, int max_dist, 
ma_hit_t_alloc* reverse_sources, long long miniedgeLen)
{
    double startTime = Get_T();
    
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));


    kvec_t(uint32_t) b_f;
    memset(&b_f, 0, sizeof(b_f));

    kvec_t(uint32_t) b_r;
    memset(&b_r, 0, sizeof(b_r));


	uint32_t v, w, n_vtx = g->n_seq * 2, n_cut = 0;


    buf_t bub;
    if (!g->is_symm) asg_symm(g);
    memset(&bub, 0, sizeof(buf_t));
    bub.a = (binfo_t*)calloc(n_vtx, sizeof(binfo_t));
    
    for (v = 0; v < n_vtx; ++v) 
    {
        if(g->seq_vis[v] == 0)
        {
            asg_arc_t *av = asg_arc_a(g, v);
            uint32_t nv = asg_arc_n(g, v);

            if(nv == 1 && asg_arc_n(g, v^1) == 1) continue;

            uint64_t t_ol = 0;
            long long i;
            for (i = 0; i < nv; ++i)
            {
                t_ol += av[i].ol;
            }
            kv_push(uint64_t, b, (uint64_t)(t_ol << 32 | v));  
        }
	}


    radix_sort_arch64(b.a, b.a + b.n);

    uint32_t min_edge;
    
    
    uint64_t k, t;
    for (k = 0; k < b.n; k++)
    {
        ///v is the node
        v = (uint32_t)b.a[k];
        if (g->seq[v>>1].del) continue;
        uint32_t nv = asg_arc_n(g, v), nw, to_del;
        if (nv < 2) continue;
        uint32_t kv = get_real_length(g, v, NULL), kw;
        if (kv < 2) continue;///V must have two out-nodes
        uint32_t i;
        asg_arc_t *av = asg_arc_a(g, v), *aw;

        b_f.n = 0;
        b_r.n = 0;
        to_del = 0;
        for (i = 0; i < nv; i++)
        {
            if (av[i].del) continue;
            
            w = av[i].v^1;
            nw = asg_arc_n(g, w);
            if(nw < 2) break;
            kw = get_real_length(g, w, NULL);
            if(kw < 2) break;

            kv_push(uint32_t, b_f, av[i].v);
            kv_push(uint32_t, b_r, w);

            aw = asg_arc_a(g, w);
            min_edge = (uint32_t)-1;
            for (t = 0; t < nw; t++)
            {
                if(aw[t].del) continue;
                if((aw[t].v>>1) == (v>>1)) continue;
                if(aw[t].ol < min_edge) min_edge = aw[t].ol;
            }

            if(av[i].ol < min_edge * drop_ratio) to_del++;
        }

        if(to_del != kv) continue;

        for (i = 0; i < nv; ++i) 
        {
            if (av[i].del) continue;
            ++n_cut;
            av[i].del = 1;
            asg_arc_del(g, av[i].v^1, av[i].ul>>32^1, 1);
        }
    }


    if(n_cut > 0)
    {
        for (v = 0; v < n_vtx; ++v) 
        {
            uint32_t nv = asg_arc_n(g, v);
            if (g->seq[v>>1].del)
            {
                continue;
            } 

            if(nv < 2)
            {
                continue;
            }


            if(asg_bub_finder_without_del_advance(g, v, max_dist, &bub) == 1)
            {
                uint32_t i;
                g->seq_vis[v] = 3;
                g->seq_vis[v^1] = 3;
                for (i = 0; i < bub.b.n; i++)
                {
                    g->seq_vis[bub.b.a[i]] = 3;
                    g->seq_vis[bub.b.a[i]^1] = 3;
                }
            }
        }


        for (v = 0; v < n_vtx; ++v) 
        {
            if(g->seq_vis[v] == 3) continue;

            uint32_t nv = asg_arc_n(g, v);
            asg_arc_t *av = asg_arc_a(g, v);
            uint32_t i;
            for (i = 0; i < nv; ++i) 
            {
                if (av[i].del && g->seq_vis[av[i].v] != 3)
                {
                    av[i].del = 0;
                    asg_arc_del(g, av[i].v^1, av[i].ul>>32^1, 0);
                } 
            }
        }
    }
    


    
    free(b.a); free(b_f.a); free(b_r.a);
    free(bub.a); free(bub.S.a); free(bub.T.a); free(bub.b.a); free(bub.e.a);
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d false overlaps\n", __func__, n_cut);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
	return n_cut;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movl %esi, 0x60(%rsp)
movss %xmm0, 0x64(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
leal (%rax,%rax), %ecx
movl %ecx, 0x4(%rsp)
testl %eax, %eax
js 0xadcf3
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movq %rbx, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xe0(%rsp)
movaps %xmm0, 0xd0(%rsp)
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movq $0x0, 0xf0(%rsp)
movl 0x4(%rsp), %ebx
movl %ebx, %r13d
movl $0x1c, %esi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x90(%rsp)
xorl %ebp, %ebp
testl %ebx, %ebx
je 0xade2d
xorl %r14d, %r14d
movl $0x2, %r15d
movq $0x0, 0x8(%rsp)
xorl %r8d, %r8d
xorl %r12d, %r12d
movq 0x20(%rsp), %rdx
movq 0x30(%rdx), %rcx
cmpb $0x0, (%rcx,%r14)
jne 0xade1b
movq 0x28(%rdx), %rsi
movq (%rsi,%r14,8), %rcx
movl %ecx, %ebx
testl %ecx, %ecx
je 0xadddf
movq 0x8(%rdx), %rdx
cmpl $0x1, %ecx
jne 0xaddb2
movl %r14d, %edi
xorq $0x1, %rdi
cmpl $0x1, (%rsi,%rdi,8)
je 0xade1b
testq %rbx, %rbx
je 0xade29
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rcx
addq $0xc, %rcx
xorl %edx, %edx
movl (%rcx), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
addq %rsi, %rdx
addq $0x18, %rcx
decq %rbx
jne 0xaddc4
shlq $0x20, %rdx
movq %rdx, %rbx
cmpq %r8, 0x8(%rsp)
jne 0xade07
movq %r12, %rdi
leaq (%r8,%r8), %r12
testq %r8, %r8
cmoveq %r15, %r12
leaq (,%r12,8), %rsi
callq 0x2370
movq %r12, %r8
movq %rax, %r12
orq %r14, %rbx
movq 0x8(%rsp), %rax
movq %rbx, (%r12,%rax,8)
incq %rax
movq %rax, 0x8(%rsp)
incq %r14
cmpq %r13, %r14
jne 0xadd75
jmp 0xade39
xorl %ebx, %ebx
jmp 0xadddf
xorl %r12d, %r12d
movq $0x0, 0x8(%rsp)
movq 0x8(%rsp), %rbx
leaq (%r12,%rbx,8), %rsi
movq %r12, %rdi
callq 0x9d53f
testq %rbx, %rbx
movq %r12, 0x48(%rsp)
je 0xae1d4
movq $0x0, 0x70(%rsp)
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %r10d, %r10d
movq $0x0, 0x68(%rsp)
xorl %r9d, %r9d
xorl %eax, %eax
xorl %ebp, %ebp
movq 0x20(%rsp), %r15
movq %r13, 0x30(%rsp)
movq %rax, 0x18(%rsp)
movq (%r12,%rax,8), %rax
movq 0x20(%r15), %rcx
movl %eax, %r13d
shrl %r13d
cmpl $0x0, (%rcx,%r13,8)
js 0xadf00
movl %eax, %eax
movq 0x28(%r15), %rcx
movq (%rcx,%rax,8), %r14
cmpl $0x2, %r14d
jb 0xadf00
movq %rbp, 0x50(%rsp)
movq (%rcx,%rax,8), %rax
movq %rax, %rcx
andq %r11, %rcx
je 0xadef4
movq 0x8(%r15), %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 %ebp, %ebp
movl (%rax,%rdx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %ebp
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xadedf
jmp 0xadef6
xorl %ebp, %ebp
cmpl $0x2, %ebp
jae 0xadf1d
movq 0x50(%rsp), %rbp
movq 0x30(%rsp), %r13
movq 0x18(%rsp), %rax
incq %rax
cmpq 0x8(%rsp), %rax
jne 0xade84
jmp 0xae1df
movq %r14, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rbx
shlq $0x3, %rbx
addq 0x8(%r15), %rbx
cmpl $0x1, %r14d
movl %r14d, %eax
adcl $0x0, %eax
movq %rax, 0x88(%rsp)
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %edi, %edi
movl $0x0, 0x2c(%rsp)
movq %r14, 0x38(%rsp)
movq %rbx, 0x40(%rsp)
leaq (%r12,%r12,2), %rax
cmpl $0x0, 0xc(%rbx,%rax,8)
js 0xae109
leaq (%rbx,%rax,8), %rax
movq %rax, 0x80(%rsp)
movl 0x8(%rax), %ebx
xorq $0x1, %rbx
movq 0x28(%r15), %rax
movq (%rax,%rbx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xae121
movq %rax, %r14
andq %r11, %r14
je 0xae11c
movq %rdi, 0x58(%rsp)
movq 0x8(%r15), %rcx
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rcx,%rax,8), %rax
addq $0xc, %rax
leaq (,%r14,8), %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 0xadfc6
cmpl $0x2, %esi
jb 0xae11c
movq 0x70(%rsp), %rax
cmpq %rax, %r8
movq %r8, 0x78(%rsp)
jne 0xae028
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %r10, %rdi
movq %r9, 0x10(%rsp)
callq 0x2370
movq 0x78(%rsp), %r8
movq 0x10(%rsp), %r9
movq %rax, %r10
movq %r15, 0x70(%rsp)
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rax
movl 0x8(%rax), %eax
movl %eax, (%r10,%r8,4)
movq 0x68(%rsp), %rax
cmpq %rax, %rdi
jne 0xae082
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,4), %rsi
movq %r9, %rdi
movq %r10, 0x10(%rsp)
callq 0x2370
movq 0x58(%rsp), %rdi
movq 0x78(%rsp), %r8
movq 0x10(%rsp), %r10
movq %rax, %r9
movq %r15, 0x68(%rsp)
movq 0x20(%rsp), %r15
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
incq %r8
movl %ebx, (%r9,%rdi,4)
incq %rdi
movq 0x8(%r15), %rax
movq 0x28(%r15), %rcx
movl 0x4(%rcx,%rbx,8), %ecx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rcx
addq $0xc, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x40(%rsp), %rbx
movl (%rcx), %edx
testl %edx, %edx
js 0xae0cf
movl -0x4(%rcx), %esi
shrl %esi
cmpl %eax, %edx
cmovael %eax, %edx
cmpl %r13d, %esi
cmovnel %edx, %eax
addq $0x18, %rcx
decq %r14
jne 0xae0b9
movl %eax, %eax
cvtsi2ss %rax, %xmm0
movq 0x80(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm1
mulss 0x64(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xae104
incl 0x2c(%rsp)
movq 0x38(%rsp), %r14
incq %r12
cmpq 0x88(%rsp), %r12
jne 0xadf5c
jmp 0xae126
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rbx
cmpl %ebp, 0x2c(%rsp)
movq 0x30(%rsp), %r13
jne 0xae1c5
cmpl $0x1, %r14d
adcl $0x0, %r14d
xorl %eax, %eax
movq 0x50(%rsp), %rbp
movq 0x48(%rsp), %r12
leaq (%rax,%rax,2), %rcx
movl 0xc(%rbx,%rcx,8), %edx
testl %edx, %edx
js 0xae1b8
leaq (%rbx,%rcx,8), %rcx
incl %ebp
orl $0x80000000, %edx       # imm = 0x80000000
movl %edx, 0xc(%rcx)
movl 0x8(%rcx), %edx
xorq $0x1, %rdx
movq 0x28(%r15), %rsi
movq (%rsi,%rdx,8), %rsi
movq %rsi, %rdx
andq %r11, %rdx
je 0xae1b8
movl 0x4(%rcx), %ecx
movq 0x8(%r15), %rdi
shrq $0x20, %rsi
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rsi,8), %rsi
addq $0xc, %rsi
xorl %edi, %edi
movl -0x4(%rsi,%rdi), %r8d
xorl %ecx, %r8d
cmpl $0x1, %r8d
jne 0xae1af
orb $-0x80, 0x3(%rsi,%rdi)
addq $0x18, %rdi
cmpq %rdi, %rdx
jne 0xae19c
incq %rax
cmpq %r14, %rax
jne 0xae149
jmp 0xadf05
movq 0x50(%rsp), %rbp
movq 0x48(%rsp), %r12
jmp 0xadf05
xorl %r9d, %r9d
xorl %r10d, %r10d
movq 0x20(%rsp), %r15
movq %r9, 0x10(%rsp)
movq %r10, %r14
testl %ebp, %ebp
sete %al
cmpl $0x0, 0x4(%rsp)
sete %cl
orb %al, %cl
jne 0xae379
xorl %r12d, %r12d
movb $0x3, %bl
movq 0x20(%r15), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xae28a
movq 0x28(%r15), %rax
testl $0xfffffffe, (%rax,%r12,8) # imm = 0xFFFFFFFE
je 0xae28a
movq %r15, %rdi
movl %r12d, %esi
movl 0x60(%rsp), %edx
leaq 0x90(%rsp), %rcx
callq 0xae409
cmpq $0x1, %rax
jne 0xae28a
movq 0x30(%r15), %rax
movb %bl, (%rax,%r12)
movq 0x30(%r15), %rax
movl %r12d, %ecx
xorq $0x1, %rcx
movb %bl, (%rax,%rcx)
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0xae28a
movq 0xd8(%rsp), %rcx
xorl %edx, %edx
movq 0x30(%r15), %rsi
movl (%rcx,%rdx,4), %edi
movb %bl, (%rsi,%rdi)
movq 0x30(%r15), %rsi
movl (%rcx,%rdx,4), %edi
xorq $0x1, %rdi
movb %bl, (%rsi,%rdi)
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xae268
incq %r12
cmpq %r13, %r12
jne 0xae201
cmpl $0x0, 0x4(%rsp)
movq 0x48(%rsp), %r12
je 0xae379
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x30(%r15), %rdx
cmpb $0x3, (%rdx,%rax)
je 0xae36d
movq 0x28(%r15), %rdx
movq (%rdx,%rax,8), %rsi
testl %esi, %esi
je 0xae36d
movq %rsi, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%r15), %rdx
movl %esi, %esi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r8
movl 0xc(%rdx,%r8,8), %r9d
testl %r9d, %r9d
jns 0xae361
leaq (%rdx,%r8,8), %r8
movq 0x30(%r15), %r11
movl 0x8(%r8), %r10d
cmpb $0x3, (%r11,%r10)
je 0xae361
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl %r9d, 0xc(%r8)
xorq $0x1, %r10
movq 0x28(%r15), %r9
movq (%r9,%r10,8), %r10
movq %r10, %r9
andq %rcx, %r9
je 0xae361
movl 0x4(%r8), %r8d
movq 0x8(%r15), %r11
shrq $0x20, %r10
shlq $0x3, %r9
leaq (%r9,%r9,2), %r9
leaq (%r10,%r10,2), %r10
leaq (%r11,%r10,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
movl -0x4(%r10,%r11), %ebx
xorl %r8d, %ebx
cmpl $0x1, %ebx
jne 0xae358
andb $0x7f, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xae345
incq %rdi
cmpq %rsi, %rdi
jne 0xae2e2
incq %rax
cmpq %r13, %rax
jne 0xae2ad
movq %r12, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x90(%rsp), %rdi
callq 0x2220
movq 0xa8(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %rdi
callq 0x2220
movq 0xd8(%rsp), %rdi
callq 0x2220
movq 0xf0(%rsp), %rdi
callq 0x2220
testl %ebp, %ebp
je 0xae3f5
movq %r15, %rdi
callq 0xa02fa
movq %r15, %rdi
callq 0xa0333
movq %r15, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r15)
movl %ebp, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq