// File: /workspace/github_cpp_makefile_star2_O3/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, 0x50(%rsp)
movss %xmm0, 0x54(%rsp)
movq %rdi, %r12
callq 0x2f3fc
movl 0x14(%r12), %eax
leal (%rax,%rax), %ebx
testl %eax, %eax
js 0xaeb46
movq %r12, %rdi
callq 0xa0ab6
movq %r12, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%r12)
xorps %xmm0, %xmm0
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0xd8(%rsp)
movups %xmm0, 0xc8(%rsp)
movups %xmm0, 0xb8(%rsp)
movups %xmm0, 0xa8(%rsp)
movups %xmm0, 0x98(%rsp)
movl %ebx, %ebp
movl $0x1c, %esi
movq %rbp, %rdi
callq 0x2120
movq %rax, 0x90(%rsp)
testl %ebx, %ebx
je 0xaf1a4
movl %ebx, 0x20(%rsp)
xorl %ebx, %ebx
movl $0x2, %r14d
xorl %r8d, %r8d
xorl %r15d, %r15d
xorl %r13d, %r13d
movq %r12, 0x18(%rsp)
movq 0x30(%r12), %rax
cmpb $0x0, (%rax,%rbx)
jne 0xaec4f
movq 0x28(%r12), %rcx
movq (%rcx,%rbx,8), %rax
movq %r12, %rdx
movl %eax, %r12d
testl %eax, %eax
je 0xaec16
movq 0x8(%rdx), %rdx
cmpl $0x1, %eax
jne 0xaebe9
movl %ebx, %esi
xorq $0x1, %rsi
cmpl $0x1, (%rcx,%rsi,8)
je 0xaec4a
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rax
addq $0xc, %rax
xorl %ecx, %ecx
movl (%rax), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq %rdx, %rcx
addq $0x18, %rax
decq %r12
jne 0xaebfb
shlq $0x20, %rcx
movq %rcx, %r12
cmpq %r15, %r8
jne 0xaec3f
leaq (%r8,%r8), %r15
testq %r8, %r8
cmoveq %r14, %r15
leaq (,%r15,8), %rsi
movq %r13, %rdi
movq %r8, %r13
callq 0x2370
movq %r13, %r8
movq %rax, %r13
orq %rbx, %r12
movq %r12, (%r13,%r8,8)
incq %r8
movq 0x18(%rsp), %r12
incq %rbx
cmpq %rbp, %rbx
jne 0xaebb2
leaq (,%r8,8), %rsi
addq %r13, %rsi
movq %r13, %rdi
movq %r8, 0x60(%rsp)
callq 0x9dc69
cmpq $0x0, 0x60(%rsp)
je 0xaf1bd
movq $0x0, 0x70(%rsp)
xorl %r10d, %r10d
movq $0x0, 0x68(%rsp)
xorl %r11d, %r11d
xorl %ebx, %ebx
xorl %r15d, %r15d
movq %r13, 0x10(%rsp)
movq %rbp, 0x48(%rsp)
movq (%r13,%rbx,8), %rax
movq 0x20(%r12), %rcx
movl %eax, %r14d
shrl %r14d
cmpl $0x0, (%rcx,%r14,8)
js 0xaefdd
movl %eax, %eax
movq 0x28(%r12), %rcx
movq (%rcx,%rax,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xaefdd
movq %rax, %rdx
movq %rax, %rsi
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rdx
je 0xaefdd
movq 0x8(%r12), %rax
movq %rsi, 0x58(%rsp)
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rax,%rsi,8), %rbp
shlq $0x3, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
xorl %r13d, %r13d
movl 0xc(%rbp,%rsi), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %r13d
addq $0x18, %rsi
cmpq %rsi, %rdx
jne 0xaed0e
cmpl $0x2, %r13d
jb 0xaefd3
movl 0x58(%rsp), %edx
movq %rdx, 0x88(%rsp)
xorl %r12d, %r12d
xorl %r9d, %r9d
xorl %edi, %edi
movl $0x0, 0x24(%rsp)
movl %r15d, 0xc(%rsp)
movq %rbx, 0x38(%rsp)
leaq (%r12,%r12,2), %rdx
cmpl $0x0, 0xc(%rbp,%rdx,8)
js 0xaef07
leaq (,%rdx,8), %rdx
addq %rbp, %rdx
movq %rdx, 0x80(%rsp)
movl 0x8(%rdx), %edx
movq %rdx, %rbx
xorq $0x1, %rbx
movq (%rcx,%rbx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaef1f
movq %rcx, %r15
movl $0xffffffff, %esi       # imm = 0xFFFFFFFF
andq %rsi, %r15
je 0xaef1a
movq %rdi, 0x40(%rsp)
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rax
addq $0xc, %rax
leaq (,%r15,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %esi, %esi
xorl %edi, %edi
movl (%rax,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xaedc5
cmpl $0x2, %edi
jb 0xaef1a
cmpq 0x70(%rsp), %r9
movq %r9, 0x78(%rsp)
jne 0xaee33
leaq (%r9,%r9), %rax
testq %r9, %r9
movl $0x2, %ecx
cmoveq %rcx, %rax
movq %rax, 0x70(%rsp)
leaq (,%rax,4), %rsi
movq %r10, %rdi
movq %r11, 0x28(%rsp)
callq 0x2370
movq 0x78(%rsp), %r9
movq 0x28(%rsp), %r11
movq %rax, %r10
movq 0x80(%rsp), %rax
movl 0x8(%rax), %edx
movq 0x40(%rsp), %rsi
movl %edx, (%r10,%r9,4)
cmpq 0x68(%rsp), %rsi
jne 0xaee7f
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
movl $0x2, %eax
cmoveq %rax, %rcx
movq %rcx, 0x68(%rsp)
leaq (,%rcx,4), %rsi
movq %r11, %rdi
movq %r10, 0x30(%rsp)
callq 0x2370
movq 0x40(%rsp), %rsi
movq 0x78(%rsp), %r9
movq 0x30(%rsp), %r10
movq %rax, %r11
incq %r9
movl %ebx, (%r11,%rsi,4)
movq 0x18(%rsp), %rcx
movq 0x8(%rcx), %rax
movq 0x28(%rcx), %rcx
movl 0x4(%rcx,%rbx,8), %edx
leaq (%rdx,%rdx,2), %rdx
leaq (%rax,%rdx,8), %rsi
addq $0xc, %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq 0x38(%rsp), %rbx
movl (%rsi), %edi
testl %edi, %edi
js 0xaeec5
movl -0x4(%rsi), %r8d
shrl %r8d
cmpl %edx, %edi
cmovael %edx, %edi
cmpl %r14d, %r8d
cmovnel %edi, %edx
addq $0x18, %rsi
decq %r15
jne 0xaeead
movq 0x80(%rsp), %rsi
movl 0xc(%rsi), %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %esi
cvtsi2ss %esi, %xmm0
movl %edx, %edx
cvtsi2ss %rdx, %xmm1
movq 0x40(%rsp), %rdi
incq %rdi
mulss 0x54(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0xaef02
incl 0x24(%rsp)
movl 0xc(%rsp), %r15d
incq %r12
cmpq 0x88(%rsp), %r12
jne 0xaed53
jmp 0xaef24
movl 0xc(%rsp), %r15d
movq 0x38(%rsp), %rbx
cmpl %r13d, 0x24(%rsp)
jne 0xaefce
movl 0x58(%rsp), %eax
xorl %ecx, %ecx
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
leaq (%rcx,%rcx,2), %rdx
movl 0xc(%rbp,%rdx,8), %esi
testl %esi, %esi
js 0xaefc0
leaq (,%rdx,8), %rdx
addq %rbp, %rdx
incl %r15d
orl $0x80000000, %esi       # imm = 0x80000000
movl %esi, 0xc(%rdx)
movl 0x8(%rdx), %esi
xorq $0x1, %rsi
movq 0x28(%r12), %rdi
movq (%rdi,%rsi,8), %rdi
movq %rdi, %rsi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %r8, %rsi
je 0xaefc0
movl 0x4(%rdx), %edx
movq 0x8(%r12), %r8
shrq $0x20, %rdi
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rdi,2), %rdi
leaq (%r8,%rdi,8), %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
movl -0x4(%rdi,%r8), %r9d
xorl %edx, %r9d
cmpl $0x1, %r9d
jne 0xaefb7
orb $-0x80, 0x3(%rdi,%r8)
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xaefa3
incq %rcx
cmpq %rax, %rcx
jne 0xaef3f
jmp 0xaefd8
movq 0x18(%rsp), %r12
movq 0x10(%rsp), %r13
movq 0x48(%rsp), %rbp
incq %rbx
cmpq 0x60(%rsp), %rbx
jne 0xaeca6
testl %r15d, %r15d
sete %bl
je 0xaf1c7
cmpl $0x0, 0x20(%rsp)
je 0xaf1d5
movq %rbp, %r14
xorl %r13d, %r13d
movb $0x3, %bpl
movq %r10, 0x30(%rsp)
movq %r11, 0x28(%rsp)
movq 0x20(%r12), %rax
movl %r13d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xaf0ab
movq 0x28(%r12), %rax
testl $0xfffffffe, (%rax,%r13,8) # imm = 0xFFFFFFFE
je 0xaf0ab
movq %r12, %rdi
movl %r13d, %esi
movl 0x50(%rsp), %edx
leaq 0x90(%rsp), %rcx
callq 0xaf281
cmpq $0x1, %rax
jne 0xaf0ab
movq 0x30(%r12), %rax
movb %bpl, (%rax,%r13)
movq 0x30(%r12), %rax
movl %r13d, %ecx
xorq $0x1, %rcx
movb %bpl, (%rax,%rcx)
movq 0xc8(%rsp), %rax
testq %rax, %rax
je 0xaf0ab
movq 0xd8(%rsp), %rcx
xorl %edx, %edx
movq 0x30(%r12), %rsi
movl (%rcx,%rdx,4), %edi
movb %bpl, (%rsi,%rdi)
movq 0x30(%r12), %rsi
movl (%rcx,%rdx,4), %edi
xorq $0x1, %rdi
movb %bpl, (%rsi,%rdi)
incq %rdx
movl %edx, %esi
cmpq %rsi, %rax
ja 0xaf085
incq %r13
cmpq %r14, %r13
jne 0xaf015
cmpl $0x0, 0x20(%rsp)
je 0xaf1df
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x10(%rsp), %r13
movq 0x30(%r12), %rdx
cmpb $0x3, (%rdx,%rax)
je 0xaf194
movq 0x28(%r12), %rdx
movq (%rdx,%rax,8), %rsi
testl %esi, %esi
je 0xaf194
movq %rsi, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
shlq $0x3, %rdx
addq 0x8(%r12), %rdx
movl %esi, %esi
xorl %edi, %edi
leaq (%rdi,%rdi,2), %r8
movl 0xc(%rdx,%r8,8), %r9d
testl %r9d, %r9d
jns 0xaf188
leaq (%rdx,%r8,8), %r8
movq 0x30(%r12), %r11
movl 0x8(%r8), %r10d
cmpb $0x3, (%r11,%r10)
je 0xaf188
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
movl %r9d, 0xc(%r8)
xorq $0x1, %r10
movq 0x28(%r12), %r9
movq (%r9,%r10,8), %r10
movq %r10, %r9
andq %rcx, %r9
je 0xaf188
movl 0x4(%r8), %r8d
movq 0x8(%r12), %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), %ebp
xorl %r8d, %ebp
cmpl $0x1, %ebp
jne 0xaf17f
andb $0x7f, 0x3(%r10,%r11)
addq $0x18, %r11
cmpq %r11, %r9
jne 0xaf16c
incq %rdi
cmpq %rsi, %rdi
jne 0xaf106
incq %rax
cmpq %r14, %rax
jne 0xaf0ce
movl %ebx, %ebp
jmp 0xaf1e6
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
movb $0x1, %bpl
xorl %r14d, %r14d
xorl %r15d, %r15d
xorl %r13d, %r13d
jmp 0xaf1f0
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0xaf1d0
xorl %r15d, %r15d
movq %r10, %rbx
movq %r11, %r14
movb $0x1, %bpl
jmp 0xaf1f0
xorl %ebp, %ebp
movq %r10, %rbx
movq %r11, %r14
jmp 0xaf1f0
movl %ebx, %ebp
movq 0x10(%rsp), %r13
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %r14
movq %r13, %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2220
movq %r14, %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
testb %bpl, %bpl
jne 0xaf26c
movq %r12, %rdi
callq 0xa0a7d
movq %r12, %rdi
callq 0xa0ab6
movq %r12, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%r12)
movl %r15d, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq