// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: unitig_arc_del_short_diploid_by_length_topo(asg_t*, ma_ug_t*, float, int, ma_hit_t_alloc*, int, int)
int unitig_arc_del_short_diploid_by_length_topo(asg_t *g, ma_ug_t *ug, float drop_ratio, 
int max_ext, ma_hit_t_alloc* reverse_sources, int if_skip_bubble, int if_drop)
{
    double startTime = Get_T();
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));

	uint32_t v, n_vtx = g->n_seq * 2, convex;
    long long n_cut = 0, tmp, nodeLen, max_stop_nodeLen, max_stop_baseLen;
    
    for (v = 0; v < n_vtx; ++v) 
    {
        if(if_skip_bubble && g->seq_vis[v] != 0) continue;
        if(if_drop && g->seq[v>>1].c == ALTER_LABLE) continue;
        asg_arc_t *av = asg_arc_a(g, v);
        uint32_t nv = asg_arc_n(g, v);
        if (nv < 2) continue;
        if(get_real_length(g, v, NULL) < 2) continue;
        uint64_t i;
        for (i = 0; i < nv; ++i)
        {
            kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i)));   
        }
	}

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

    uint64_t k;
    for (k = 0; k < b.n; k++)
    {
        
        asg_arc_t *a = &g->arc[(uint32_t)b.a[k]];
		///v is self id, w is the id of another end
		uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0;
		uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw;
		uint32_t ov_max = 0, ow_max = 0;
		asg_arc_t *av, *aw;
		///nv must be >= 2
		if (nv == 1 && nw == 1) continue;
        av = asg_arc_a(g, v);
		aw = asg_arc_a(g, w);


        ///calculate the longest edge for v and w
		for (i = 0, kv = 0; i < nv; ++i) {
			if (av[i].del) continue;
			if (ov_max < av[i].ol) ov_max = av[i].ol; 
			++kv;
		}
		if (kv >= 2 && a->ol > ov_max * drop_ratio) continue;


		for (i = 0, kw = 0; i < nw; ++i) {
			if (aw[i].del) continue;
			if (ow_max < aw[i].ol) ow_max = aw[i].ol;
			++kw;
		}
		if (kw >= 2 && a->ol > ow_max * drop_ratio) continue;
		///if (kv == 1 && kw == 1) continue;
        if (kv <= 1 && kw <= 1) continue;


        ///to see which one is the current edge (from v and w)
		for (iv = 0; iv < nv; ++iv)
			if (av[iv].v == (w^1)) break;
		for (iw = 0; iw < nw; ++iw)
			if (aw[iw].v == (v^1)) break;
        ///if one edge has been deleted, it should be deleted in both direction
        if (av[iv].del && aw[iw].del) continue;

        ///kv and kw is the avialiable 
		if (kv > 1 && kw > 1) {
			if (a->ol < ov_max * drop_ratio && a->ol < ow_max * drop_ratio)
            {
                to_del = 1;
            }
		} else if (kw == 1) {
            get_unitig(g, ug, w^1, &convex, &nodeLen, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 1, NULL);
            if(nodeLen < max_ext) to_del = 1;
            ///if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
		} else if (kv == 1) {
            get_unitig(g, ug, v^1, &convex, &nodeLen, &tmp, &max_stop_nodeLen, &max_stop_baseLen, 1, NULL);
            if(nodeLen < max_ext) to_del = 1;
			///if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
		}
		if (to_del)
			av[iv].del = aw[iw].del = 1, ++n_cut;
    }
    
    free(b.a);
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %lld short 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 $0x78, %rsp
movl %r9d, 0x20(%rsp)
movl %r8d, 0x10(%rsp)
movl %edx, 0x38(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %rsi, 0x58(%rsp)
movq %rdi, %r12
callq 0x300fc
movl 0x14(%r12), %eax
addl %eax, %eax
movq %r12, 0x8(%rsp)
je 0xab1ad
movl %eax, %eax
movq %rax, (%rsp)
xorl %ebx, %ebx
xorl %r14d, %r14d
xorl %eax, %eax
xorl %edi, %edi
cmpl $0x0, 0x10(%rsp)
movq %rbx, 0x28(%rsp)
je 0xab08d
movq 0x30(%r12), %rcx
cmpb $0x0, (%rcx,%rbx)
jne 0xab199
cmpl $0x0, 0x20(%rsp)
je 0xab0a8
movq 0x20(%r12), %rcx
movl %ebx, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xab199
movq 0x28(%r12), %rcx
movq (%rcx,%rbx,8), %rbp
testl $0xfffffffe, %ebp       # imm = 0xFFFFFFFE
je 0xab199
movq 0x8(%r12), %rcx
movq %rbp, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
xorl %esi, %esi
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
andq %r8, %rbp
je 0xab10f
leaq (%rcx,%rdx,8), %r11
addq $0xc, %r11
leaq (,%rbp,8), %r8
leaq (%r8,%r8,2), %r8
xorl %r9d, %r9d
movl (%r11,%r9), %r10d
notl %r10d
shrl $0x1f, %r10d
addl %r10d, %esi
addq $0x18, %r9
cmpq %r9, %r8
jne 0xab0f1
cmpl $0x2, %esi
setge %sil
testb %sil, %sil
je 0xab199
testq %rbp, %rbp
je 0xab199
leaq (%rcx,%rdx,8), %r13
leaq (%rcx,%rdx,8), %rbx
addq $0xc, %rbx
xorl %r15d, %r15d
cmpq %rax, %r14
jne 0xab159
leaq (%rax,%rax), %r12
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r12
leaq (,%r12,8), %rsi
callq 0x2370
movq %rax, %rdi
movq %r12, %rax
movq 0x8(%rsp), %r12
movl (%rbx), %ecx
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %ecx
shlq $0x20, %rcx
movq %r13, %rdx
subq 0x8(%r12), %rdx
sarq $0x3, %rdx
movabsq $-0x5555555555555555, %rsi # imm = 0xAAAAAAAAAAAAAAAB
imulq %rsi, %rdx
addq %r15, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r14,8)
incq %r14
incq %r15
addq $0x18, %rbx
cmpq %r15, %rbp
jne 0xab12c
movq 0x28(%rsp), %rbx
incq %rbx
cmpq (%rsp), %rbx
jne 0xab072
jmp 0xab1b2
xorl %edi, %edi
xorl %r14d, %r14d
leaq (%rdi,%r14,8), %rsi
movq %rdi, (%rsp)
callq 0x9d53f
testq %r14, %r14
je 0xab501
movslq 0x38(%rsp), %rax
movq %rax, 0x50(%rsp)
movq $0x0, 0x30(%rsp)
xorl %ebp, %ebp
movss 0x1c(%rsp), %xmm4
movq (%rsp), %rdi
movq 0x8(%r12), %rcx
movq 0x28(%r12), %rsi
movl (%rdi,%rbp,8), %eax
leaq (%rax,%rax,2), %r9
movq (%rcx,%r9,8), %rdx
shrq $0x20, %rdx
movl 0x8(%rcx,%r9,8), %r15d
movq %r15, %rax
xorq $0x1, %rax
movq (%rsi,%rdx,8), %r10
movq (%rsi,%rax,8), %r8
cmpl $0x1, %r10d
jne 0xab224
cmpl $0x1, %r8d
je 0xab4f3
movq %r10, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rax
testl %r10d, %r10d
movq %r15, 0x28(%rsp)
movq %rax, 0x10(%rsp)
je 0xab299
movl %r10d, %esi
leaq (%rcx,%rax,8), %r11
addq $0xc, %r11
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rbx
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %esi, %esi
movl %r12d, %edi
movl (%r11,%r15), %r13d
cmpl %r13d, %r12d
movl %r13d, %r12d
cmoval %edi, %r12d
xorl %eax, %eax
testl %r13d, %r13d
cmovsl %edi, %r12d
setns %al
addl %eax, %esi
addq $0x18, %r15
cmpq %r15, %rbx
jne 0xab259
xorps %xmm0, %xmm0
cvtsi2ss %r12d, %xmm0
movq 0x8(%rsp), %r12
movq (%rsp), %rdi
movq 0x28(%rsp), %r15
jmp 0xab29e
xorps %xmm0, %xmm0
xorl %esi, %esi
leaq (%rcx,%r9,8), %r13
cmpl $0x2, %esi
jb 0xab2cb
movl 0xc(%r13), %eax
movl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
andl %r9d, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movaps %xmm0, %xmm2
mulss %xmm4, %xmm2
ucomiss %xmm2, %xmm1
ja 0xab4f3
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rdi
testl %r8d, %r8d
movq %rdi, 0x20(%rsp)
je 0xab340
movq %r13, 0x38(%rsp)
movl %r8d, %eax
leaq (%rcx,%rdi,8), %rbx
addq $0xc, %rbx
shlq $0x3, %rax
leaq (%rax,%rax,2), %r15
xorl %r13d, %r13d
xorl %edi, %edi
xorl %r11d, %r11d
movl %edi, %eax
movl (%rbx,%r13), %r9d
cmpl %r9d, %edi
movl %r9d, %edi
cmoval %eax, %edi
xorl %r12d, %r12d
testl %r9d, %r9d
cmovsl %eax, %edi
setns %r12b
addl %r12d, %r11d
addq $0x18, %r13
cmpq %r13, %r15
jne 0xab300
xorps %xmm1, %xmm1
cvtsi2ss %edi, %xmm1
movq 0x8(%rsp), %r12
movq 0x28(%rsp), %r15
movq 0x38(%rsp), %r13
jmp 0xab346
xorps %xmm1, %xmm1
xorl %r11d, %r11d
cmpl $0x2, %r11d
movq 0x10(%rsp), %r9
jb 0xab373
movl 0xc(%r13), %eax
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
ucomiss %xmm3, %xmm2
ja 0xab4ef
movl %esi, %eax
orl %r11d, %eax
cmpl $0x2, %eax
jb 0xab4ef
testl %r10d, %r10d
je 0xab3a8
leaq (%rcx,%r9,8), %rbx
addq $0x8, %rbx
xorl %edi, %edi
cmpl %r15d, (%rbx)
je 0xab3a3
incl %edi
addq $0x18, %rbx
cmpl %edi, %r10d
jne 0xab390
movl %r10d, %edi
movl %edi, %r10d
jmp 0xab3ab
xorl %r10d, %r10d
testl %r8d, %r8d
je 0xab3dd
movq 0x20(%rsp), %rax
leaq (%rcx,%rax,8), %rdi
addq $0x8, %rdi
xorl %ebx, %ebx
movl (%rdi), %eax
xorl %edx, %eax
cmpl $0x1, %eax
je 0xab3d6
incl %ebx
addq $0x18, %rdi
cmpl %ebx, %r8d
jne 0xab3bf
movl %r8d, %ebx
movq %r13, %rdi
movl %ebx, %ebx
jmp 0xab3e2
movq %r13, %rdi
xorl %ebx, %ebx
leaq (%rcx,%r9,8), %r13
movq 0x20(%rsp), %rax
leaq (%rcx,%rax,8), %r8
leaq (%r10,%r10,2), %r12
cmpl $0x0, 0xc(%r13,%r12,8)
jns 0xab40b
leaq (%rbx,%rbx,2), %rax
cmpl $0x0, 0xc(%r8,%rax,8)
js 0xab4ea
cmpl $0x2, %esi
jb 0xab465
cmpl $0x2, %r11d
jb 0xab465
movl 0xc(%rdi), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm4, %xmm0
ucomiss %xmm2, %xmm0
movq (%rsp), %rdi
jbe 0xab45b
mulss %xmm4, %xmm1
ucomiss %xmm2, %xmm1
jbe 0xab45b
leaq 0xc(,%r12,8), %rax
addq %r13, %rax
leaq (%rbx,%rbx,2), %rcx
orb $-0x80, 0xf(%r8,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x30(%rsp)
movq 0x8(%rsp), %r12
jmp 0xab4f3
cmpl $0x1, %r11d
jne 0xab47f
movq %r8, 0x10(%rsp)
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rsi
movl %r15d, %edx
jmp 0xab496
cmpl $0x1, %esi
jne 0xab4ea
movq %r8, 0x10(%rsp)
xorl $0x1, %edx
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %rsi
leaq 0x4c(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
callq 0x10109f
addq $0x20, %rsp
movq 0x50(%rsp), %rax
cmpq %rax, 0x40(%rsp)
setge %al
testb %al, %al
movss 0x1c(%rsp), %xmm4
movq (%rsp), %rdi
movq 0x10(%rsp), %r8
je 0xab43d
jmp 0xab45b
movq 0x8(%rsp), %r12
movq (%rsp), %rdi
incq %rbp
cmpq %r14, %rbp
jne 0xab1e7
jmp 0xab50e
movq $0x0, 0x30(%rsp)
movq (%rsp), %rdi
callq 0x2220
movq 0x30(%rsp), %rbx
testq %rbx, %rbx
je 0xab53b
movq %r12, %rdi
callq 0xa02fa
movq %r12, %rdi
callq 0xa0333
movq %r12, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r12)
movl %ebx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq