// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movl %esi, 0x54(%rsp)
movss %xmm0, 0x5c(%rsp)
movq %rdi, %rbx
callq 0x251c
movl 0x14(%rbx), %eax
leal (%rax,%rax), %ebp
testl %eax, %eax
js 0x74ee4
movq %rbx, %rdi
callq 0x69638
xorps %xmm0, %xmm0
movups %xmm0, 0xf8(%rsp)
movups %xmm0, 0xe8(%rsp)
movups %xmm0, 0xd8(%rsp)
movups %xmm0, 0xc8(%rsp)
movups %xmm0, 0xb8(%rsp)
movups %xmm0, 0xa8(%rsp)
movl %ebp, %r13d
pushq $0x1c
popq %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, 0x88(%rsp)
movq %rax, 0xa0(%rsp)
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %eax, %eax
movq $0x0, 0x8(%rsp)
cmpq %r13, %r14
je 0x74ffa
movq 0x30(%rbx), %rcx
cmpb $0x0, (%rcx,%r14)
jne 0x74ff2
movq 0x8(%rbx), %rdx
movq 0x28(%rbx), %rdi
movq (%rdi,%r14,8), %rsi
movl %esi, %ecx
cmpq $0x1, %rcx
jne 0x74f7f
movl %r14d, %r8d
xorq $0x1, %r8
cmpl $0x1, (%rdi,%r8,8)
je 0x74ff2
shrq $0x20, %rsi
imulq $0x18, %rcx, %rcx
imulq $0x18, %rsi, %rsi
addq %rsi, %rdx
addq $0xc, %rdx
xorl %esi, %esi
xorl %r15d, %r15d
cmpq %rsi, %rcx
je 0x74fae
movl (%rdx,%rsi), %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
addq %rdi, %r15
addq $0x18, %rsi
jmp 0x74f97
cmpq %rax, 0x8(%rsp)
jne 0x74fd9
leaq (%rax,%rax), %r12
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
movq %rbp, %rdi
callq 0x2370
movq %rax, %rbp
movq %r12, %rax
shlq $0x20, %r15
orq %r14, %r15
movq 0x8(%rsp), %rcx
movq %r15, (%rbp,%rcx,8)
incq %rcx
movq %rcx, 0x8(%rsp)
incq %r14
jmp 0x74f45
movq 0x8(%rsp), %rax
leaq (,%rax,8), %rsi
addq %rbp, %rsi
movq %rbp, %rdi
callq 0x66c25
movq $0x0, 0x18(%rsp)
xorl %ecx, %ecx
movq $0x0, 0x10(%rsp)
movq $0x0, 0x70(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
movq %rbx, 0x60(%rsp)
movq %r13, 0x48(%rsp)
movq %rbp, 0x40(%rsp)
cmpq 0x8(%rsp), %rcx
je 0x75318
movq (%rbp,%rcx,8), %rsi
movq 0x20(%rbx), %rax
movl %esi, %r12d
shrl %r12d
cmpl $0x0, (%rax,%r12,8)
js 0x75310
movq %rcx, 0x38(%rsp)
movq %r14, (%rsp)
movl %esi, %r14d
movq 0x28(%rbx), %rax
movq (%rax,%r14,8), %r15
testl $0xfffffffe, %r15d      # imm = 0xFFFFFFFE
je 0x75272
movq %rdx, 0x20(%rsp)
movq %rbx, %rdi
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jb 0x75302
movl %eax, 0x58(%rsp)
movl %r15d, %eax
movq %rax, 0x78(%rsp)
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %r10
movq (%r10,%r14,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
movq %rax, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
addq %rcx, %rax
xorl %ecx, %ecx
movl $0x0, 0x34(%rsp)
xorl %r11d, %r11d
movq $0x0, 0x28(%rsp)
movq %rax, 0x68(%rsp)
cmpq 0x78(%rsp), %rcx
je 0x75280
imulq $0x18, %rcx, %r15
cmpl $0x0, 0xc(%rax,%r15)
js 0x7526a
addq %rax, %r15
movl 0x8(%r15), %r14d
xorq $0x1, %r14
movq (%r10,%r14,8), %r13
testl $0xfffffffe, %r13d      # imm = 0xFFFFFFFE
je 0x75280
movq %rbx, %rdi
movl %r14d, %esi
xorl %edx, %edx
movq %r11, %rbp
movq %rcx, 0x80(%rsp)
callq 0xc2e7a
cmpl $0x2, %eax
jb 0x75280
movq 0x20(%rsp), %rdx
cmpq %rdx, 0x28(%rsp)
jne 0x7517a
leaq (%rdx,%rdx), %rbx
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq (%rsp), %rdi
callq 0x2370
movq %rbp, %r11
movq %rbx, %rdx
jmp 0x75181
movq %rbp, %r11
movq (%rsp), %rax
movl 0x8(%r15), %ecx
movq %rax, (%rsp)
movq 0x28(%rsp), %rsi
movl %ecx, (%rax,%rsi,4)
movq 0x70(%rsp), %rax
cmpq %rax, %r11
movq %rdx, 0x20(%rsp)
jne 0x751cf
leaq (%rax,%rax), %rbx
testq %rax, %rax
pushq $0x2
popq %rax
cmoveq %rax, %rbx
leaq (,%rbx,4), %rsi
movq 0x10(%rsp), %rdi
callq 0x2370
movq %rbp, %r11
movq %rbx, 0x70(%rsp)
movq 0x60(%rsp), %rbx
jmp 0x751d9
movq 0x60(%rsp), %rbx
movq 0x10(%rsp), %rax
movl %r13d, %ecx
incq 0x28(%rsp)
movl %r14d, (%rax,%r11,4)
movq 0x8(%rbx), %rsi
movq 0x28(%rbx), %r10
movl 0x4(%r10,%r14,8), %edi
imulq $0x18, %rcx, %rdx
imulq $0x18, %rdi, %rcx
addq %rcx, %rsi
addq $0xc, %rsi
xorl %edi, %edi
pushq $-0x1
popq %rcx
cmpq %rdi, %rdx
je 0x75230
movl (%rsi,%rdi), %r8d
testl %r8d, %r8d
js 0x7522a
movl -0x4(%rsi,%rdi), %r9d
shrl %r9d
cmpl %ecx, %r8d
cmovael %ecx, %r8d
cmpl %r12d, %r9d
cmovnel %r8d, %ecx
addq $0x18, %rdi
jmp 0x75206
movq %rax, 0x10(%rsp)
movl 0xc(%r15), %edx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %edx
cvtsi2ss %edx, %xmm0
movl %ecx, %ecx
cvtsi2ss %rcx, %xmm1
incq %r11
mulss 0x5c(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x7525d
incl 0x34(%rsp)
movq 0x68(%rsp), %rax
movq 0x80(%rsp), %rcx
incq %rcx
jmp 0x750ef
movq (%rsp), %r14
movq 0x38(%rsp), %rcx
jmp 0x75310
movl 0x34(%rsp), %eax
cmpl 0x58(%rsp), %eax
jne 0x752f8
imulq $0x18, 0x78(%rsp), %r14
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rcx
leaq (%rax,%rcx), %r15
addq $0xc, %r15
xorl %r12d, %r12d
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
cmpq %r12, %r14
je 0x75302
movl (%r15,%r12), %eax
testl %eax, %eax
js 0x752f2
movq 0x18(%rsp), %rcx
incl %ecx
movq %rcx, 0x18(%rsp)
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%r15,%r12)
movl -0x8(%r15,%r12), %edx
movl -0x4(%r15,%r12), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %rbx, %rdi
pushq $0x1
popq %rcx
callq 0x6db50
addq $0x18, %r12
jmp 0x752b5
movq 0x48(%rsp), %r13
movq 0x40(%rsp), %rbp
movq (%rsp), %r14
movq 0x38(%rsp), %rcx
movq 0x20(%rsp), %rdx
incq %rcx
jmp 0x75043
cmpl $0x0, 0x18(%rsp)
je 0x754db
movq %r14, (%rsp)
xorl %r14d, %r14d
leaq 0xa0(%rsp), %r15
movb $0x3, %bpl
cmpq %r13, %r14
je 0x753c9
movq 0x20(%rbx), %rax
movl %r14d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0x753c1
movq 0x28(%rbx), %rax
testl $0xfffffffe, (%rax,%r14,8) # imm = 0xFFFFFFFE
je 0x753c1
movq %rbx, %rdi
movl %r14d, %esi
movl 0x54(%rsp), %edx
movq %r15, %rcx
callq 0x75519
cmpq $0x1, %rax
jne 0x753c1
movq 0x30(%rbx), %rax
movb %bpl, (%rax,%r14)
movq 0x30(%rbx), %rax
movl %r14d, %ecx
xorq $0x1, %rcx
movb %bpl, (%rax,%rcx)
movq 0xd8(%rsp), %rax
movq 0xe8(%rsp), %rcx
xorl %edx, %edx
movl %edx, %edx
cmpq %rdx, %rax
jbe 0x753c1
movq 0x30(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
movb %bpl, (%rsi,%rdi)
movq 0x30(%rbx), %rsi
movl (%rcx,%rdx,4), %edi
xorq $0x1, %rdi
movb %bpl, (%rsi,%rdi)
incl %edx
jmp 0x7539c
incq %r14
jmp 0x75335
xorl %r14d, %r14d
movq 0x40(%rsp), %rbp
cmpq %r13, %r14
je 0x75450
movq 0x30(%rbx), %rax
cmpb $0x3, (%rax,%r14)
jne 0x753eb
incq %r14
movq 0x48(%rsp), %r13
jmp 0x753d1
movq 0x8(%rbx), %rax
movq 0x28(%rbx), %rcx
movq (%rcx,%r14,8), %rcx
movl %ecx, %edx
shrq $0x20, %rcx
imulq $0x18, %rdx, %r15
imulq $0x18, %rcx, %rcx
leaq (%rax,%rcx), %r12
addq $0xc, %r12
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x753e1
movl (%r12,%r13), %eax
testl %eax, %eax
jns 0x7544a
movq 0x30(%rbx), %rcx
movl -0x4(%r12,%r13), %esi
cmpb $0x3, (%rcx,%rsi)
je 0x7544a
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, (%r12,%r13)
xorl $0x1, %esi
movl -0x8(%r12,%r13), %edx
xorl $0x1, %edx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x6db50
addq $0x18, %r13
jmp 0x75410
movq 0xa0(%rsp), %r14
movq 0xb8(%rsp), %r15
movq 0xd0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xe8(%rsp), %r13
movq 0x100(%rsp), %r12
movq %rbp, %rdi
callq 0x2220
movq (%rsp), %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq %r15, %rdi
callq 0x2220
movq 0x28(%rsp), %rdi
callq 0x2220
movq %r13, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2220
cmpl $0x0, 0x18(%rsp)
je 0x75502
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
jmp 0x75502
movq %rbp, %rdi
callq 0x2220
movq %r14, %rdi
callq 0x2220
movq 0x10(%rsp), %rdi
callq 0x2220
movq 0x88(%rsp), %rdi
callq 0x2220
movq 0x18(%rsp), %rax
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq