// File: /workspace/github_cpp_makefile_star2_O3/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, 0x8(%rsp)
movl %r8d, 0x18(%rsp)
movl %edx, %ebp
movss %xmm0, 0x10(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xac588
movq %rbx, %r8
movl %ebp, 0x14(%rsp)
movl %eax, %eax
movq %rax, 0x20(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r11 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r14d, %r14d
xorl %r13d, %r13d
xorl %edi, %edi
movq %rbx, (%rsp)
cmpl $0x0, 0x18(%rsp)
movq %rdx, 0x28(%rsp)
je 0xac0ca
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%rdx)
jne 0xac1c0
cmpl $0x0, 0x8(%rsp)
je 0xac0e4
movq 0x20(%r8), %rax
movl %edx, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xac1c0
movq 0x28(%r8), %rax
movq (%rax,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xac1c0
movq %rcx, %rbp
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rbp
je 0xac1c0
movq 0x8(%r8), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
leaq (,%rbp,8), %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%rbx,%rdx), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xac129
cmpl $0x2, %esi
jl 0xac1c0
xorl %r15d, %r15d
cmpq %r13, %r14
jne 0xac18e
leaq (%r14,%r14), %r13
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %r13
leaq (,%r13,8), %rsi
movq %r14, %r12
movq %rbp, %r14
movq %r11, %rbp
callq 0x2370
movq %rbp, %r11
movq %r14, %rbp
movq %r12, %r14
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq (%rsp), %r8
movq %rax, %rdi
movq 0x8(%r8), %rax
leaq (%r15,%r15,2), %rcx
movl 0xc(%rbx,%rcx,8), %ecx
andl %r10d, %ecx
shlq $0x20, %rcx
movq %rbx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r11, %rdx
addq %r15, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r14,8)
incq %r14
incq %r15
cmpq %rbp, %r15
jne 0xac149
movq 0x28(%rsp), %rdx
incq %rdx
cmpq 0x20(%rsp), %rdx
jne 0xac0b0
leaq (%rdi,%r14,8), %rsi
movq %rdi, 0x8(%rsp)
callq 0x9dc69
testq %r14, %r14
je 0xac595
movslq 0x14(%rsp), %rax
movq %rax, 0x40(%rsp)
movq $0x0, 0x48(%rsp)
xorl %ebp, %ebp
movq (%rsp), %r15
movss 0x10(%rsp), %xmm4
movq 0x8(%rsp), %rdi
movq 0x8(%r15), %rcx
movq 0x28(%r15), %r9
movl (%rdi,%rbp,8), %eax
leaq (%rax,%rax,2), %rsi
movq (%rcx,%rsi,8), %rdx
shrq $0x20, %rdx
movl 0x8(%rcx,%rsi,8), %eax
movq %rax, %r8
xorq $0x1, %r8
movq (%r9,%rdx,8), %r13
movq (%r9,%r8,8), %r10
cmpl $0x1, %r13d
jne 0xac248
cmpl $0x1, %r10d
je 0xac3c3
leaq (%rcx,%rsi,8), %rsi
movq %rsi, 0x18(%rsp)
movq %r13, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
xorl %edi, %edi
xorps %xmm0, %xmm0
testl %r13d, %r13d
movq %rsi, 0x20(%rsp)
movq %r10, 0x28(%rsp)
je 0xac2f9
movl %r13d, %r8d
leaq (%rcx,%rsi,8), %rsi
addq $0xc, %rsi
shlq $0x3, %r8
leaq (%r8,%r8,2), %r11
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r9d, %r9d
movl %r15d, %r8d
movl (%rsi,%rbx), %r12d
cmpl %r12d, %r15d
movl %r12d, %r15d
cmoval %r8d, %r15d
xorl %r10d, %r10d
testl %r12d, %r12d
cmovsl %r8d, %r15d
setns %r10b
addl %r10d, %r9d
addq $0x18, %rbx
cmpq %rbx, %r11
jne 0xac28f
xorps %xmm1, %xmm1
cvtsi2ss %r15d, %xmm1
cmpl $0x2, %r9d
movq 0x28(%rsp), %r10
jb 0xac303
movq 0x18(%rsp), %rsi
movl 0xc(%rsi), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
movb $0x1, %sil
ucomiss %xmm3, %xmm2
jbe 0xac305
jmp 0xac3ba
xorl %esi, %esi
xorps %xmm1, %xmm1
xorl %r9d, %r9d
jmp 0xac305
xorl %esi, %esi
movl %esi, 0x14(%rsp)
movq %r10, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %r12
testl %r10d, %r10d
je 0xac3a3
movl %r10d, %edi
movq %r12, 0x58(%rsp)
leaq (%rcx,%r12,8), %rsi
addq $0xc, %rsi
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rbx
xorl %r15d, %r15d
xorl %r8d, %r8d
xorl %edi, %edi
movl %r8d, %r10d
movl (%rsi,%r15), %r11d
cmpl %r11d, %r8d
movl %r11d, %r8d
cmoval %r10d, %r8d
xorl %r12d, %r12d
testl %r11d, %r11d
cmovsl %r10d, %r8d
setns %r12b
addl %r12d, %edi
addq $0x18, %r15
cmpq %r15, %rbx
jne 0xac33d
xorps %xmm0, %xmm0
cvtsi2ss %r8d, %xmm0
cmpl $0x2, %edi
jb 0xac3a7
movq 0x18(%rsp), %rsi
movl 0xc(%rsi), %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
andl %r8d, %esi
xorps %xmm2, %xmm2
cvtsi2ss %esi, %xmm2
movaps %xmm0, %xmm3
mulss %xmm4, %xmm3
movb $0x1, %sil
ucomiss %xmm3, %xmm2
movq 0x58(%rsp), %r12
jbe 0xac3ae
jmp 0xac3ba
xorl %esi, %esi
jmp 0xac3ae
xorl %esi, %esi
movq 0x58(%rsp), %r12
movl %r9d, %r8d
orl %edi, %r8d
cmpl $0x2, %r8d
jae 0xac3d4
movq (%rsp), %r15
movq 0x8(%rsp), %rdi
incq %rbp
cmpq %r14, %rbp
jne 0xac20e
jmp 0xac557
testl %r13d, %r13d
movq 0x28(%rsp), %r11
je 0xac40a
movl %r13d, %ebx
movq 0x20(%rsp), %r8
leaq (%rcx,%r8,8), %r15
addq $0x8, %r15
xorl %r8d, %r8d
cmpl %eax, (%r15)
je 0xac405
incq %r8
addq $0x18, %r15
cmpq %r8, %rbx
jne 0xac3f1
movq %r13, %r8
movl %r8d, %ebx
jmp 0xac40c
xorl %ebx, %ebx
testl %r11d, %r11d
je 0xac440
movl %r11d, %r15d
leaq (%rcx,%r12,8), %r13
addq $0x8, %r13
xorl %r8d, %r8d
movl (%r13), %r10d
xorl %edx, %r10d
cmpl $0x1, %r10d
je 0xac43b
incq %r8
addq $0x18, %r13
cmpq %r8, %r15
jne 0xac41f
movq %r11, %r8
movl %r8d, %r10d
jmp 0xac443
xorl %r10d, %r10d
movq 0x20(%rsp), %r8
leaq (%rcx,%r8,8), %r13
leaq (%rcx,%r12,8), %r12
leaq (%rbx,%rbx,2), %rbx
cmpl $0x0, 0xc(%r13,%rbx,8)
jns 0xac46c
leaq (%r10,%r10,2), %rcx
cmpl $0x0, 0xc(%r12,%rcx,8)
js 0xac3ba
movl 0x14(%rsp), %ecx
andb %sil, %cl
cmpb $0x1, %cl
jne 0xac4b2
movq 0x18(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm4, %xmm1
ucomiss %xmm2, %xmm1
movq 0x8(%rsp), %rdi
jbe 0xac54e
mulss %xmm4, %xmm0
ucomiss %xmm2, %xmm0
movq (%rsp), %r15
ja 0xac52b
jmp 0xac3c3
cmpl $0x1, %edi
jne 0xac4c4
movq (%rsp), %rdi
movq 0x50(%rsp), %rsi
movl %eax, %edx
jmp 0xac4da
cmpl $0x1, %r9d
jne 0xac3ba
xorl $0x1, %edx
movq (%rsp), %rdi
movq 0x50(%rsp), %rsi
leaq 0x3c(%rsp), %rcx
leaq 0x30(%rsp), %r8
leaq 0x70(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0x70(%rsp), %rax
pushq %rax
leaq 0x80(%rsp), %rax
pushq %rax
movq %r10, %r15
callq 0x1053a7
movq %r15, %r10
movss 0x30(%rsp), %xmm4
movq 0x20(%rsp), %r15
addq $0x20, %rsp
movq 0x40(%rsp), %rax
cmpq %rax, 0x30(%rsp)
movq 0x8(%rsp), %rdi
jge 0xac3c3
leaq 0xc(,%rbx,8), %rax
addq %r13, %rax
leaq (%r10,%r10,2), %rcx
orb $-0x80, 0xf(%r12,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x48(%rsp)
jmp 0xac3c3
movq (%rsp), %r15
jmp 0xac3c3
callq 0x2220
movq 0x48(%rsp), %r14
testq %r14, %r14
je 0xac59f
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xac5a2
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xac59a
movq 0x8(%rsp), %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq