// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movl %r9d, %ebp
movl %r8d, %r15d
movl %edx, 0x48(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %rsi, 0x68(%rsp)
movq %rdi, %r13
callq 0x251c
movl 0x14(%r13), %esi
addl %esi, %esi
xorl %r8d, %r8d
xorl %edi, %edi
xorl %r12d, %r12d
xorl %edx, %edx
movq %r13, 0x38(%rsp)
movl %ebp, 0x30(%rsp)
movl %r15d, 0x28(%rsp)
movq %rsi, 0x20(%rsp)
cmpq %rsi, %r8
movq %rdx, 0x8(%rsp)
je 0x722f4
testl %r15d, %r15d
je 0x721dd
movq 0x30(%r13), %rax
cmpb $0x0, (%rax,%r8)
jne 0x7222e
testl %ebp, %ebp
je 0x721f1
movq 0x20(%r13), %rax
movl %r8d, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0x7222e
movq 0x28(%r13), %rax
movq (%rax,%r8,8), %rbx
testl $0xfffffffe, %ebx       # imm = 0xFFFFFFFE
je 0x7222e
movq %rdi, (%rsp)
movq %r8, %rsi
movq 0x8(%r13), %r14
movq %r13, %rdi
movq %r8, 0x10(%rsp)
xorl %edx, %edx
callq 0xc2e7a
cmpl $0x2, %eax
jge 0x72236
movq 0x8(%rsp), %rax
movq (%rsp), %rdi
jmp 0x722df
movq %rdx, %rax
jmp 0x722e9
movq %rbx, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rax
leaq (%r14,%rax), %r9
movl %ebx, %ebp
leaq (%r14,%rax), %rbx
addq $0xc, %rbx
movq 0x8(%rsp), %rsi
leaq (,%rsi,8), %r15
xorl %r14d, %r14d
movq (%rsp), %rdi
leaq (%rsi,%r14), %rax
cmpq %r14, %rbp
je 0x722d6
cmpq %r12, %rax
jne 0x722a2
leaq (%r12,%r12), %r13
testq %r12, %r12
pushq $0x2
popq %rax
cmoveq %rax, %r13
leaq (,%r13,8), %rsi
movq %r9, %r12
callq 0x2370
movq %r12, %r9
movq 0x8(%rsp), %rsi
movq %rax, %rdi
movq %r13, %r12
movq 0x38(%rsp), %r13
movl (%rbx), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
shlq $0x20, %rcx
movq %r9, %rax
subq 0x8(%r13), %rax
cqto
pushq $0x18
popq %r8
idivq %r8
addq %r14, %rax
orq %rcx, %rax
leaq (%rdi,%r15), %rcx
movq %rax, (%rcx,%r14,8)
incq %r14
addq $0x18, %rbx
jmp 0x72263
movl 0x30(%rsp), %ebp
movl 0x28(%rsp), %r15d
movq 0x20(%rsp), %rsi
movq 0x10(%rsp), %r8
incq %r8
movq %rax, %rdx
jmp 0x721bf
leaq (%rdi,%rdx,8), %rsi
movq %rdi, (%rsp)
callq 0x66c25
movq 0x8(%rsp), %r10
movq (%rsp), %rdi
movslq 0x48(%rsp), %rax
movq %rax, 0x60(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x40(%rsp)
movss 0x1c(%rsp), %xmm4
cmpq %r10, %rbx
je 0x72619
movq 0x8(%r13), %r9
movq 0x28(%r13), %rsi
movl (%rdi,%rbx,8), %eax
imulq $0x18, %rax, %rcx
movq (%r9,%rcx), %rdx
shrq $0x20, %rdx
movl 0x8(%r9,%rcx), %eax
movq %rax, %r8
xorq $0x1, %r8
movq (%rsi,%rdx,8), %r14
movq (%rsi,%r8,8), %r11
cmpl $0x1, %r14d
jne 0x72369
cmpl $0x1, %r11d
je 0x724c1
movq %rbx, 0x10(%rsp)
addq %r9, %rcx
movq %r14, %r8
shrq $0x20, %r8
imulq $0x18, %r8, %rdi
leaq (%r9,%rdi), %rsi
movq %rsi, 0x28(%rsp)
movq %r11, 0x20(%rsp)
movq %r11, %r10
shrq $0x20, %r10
movq %r14, 0x30(%rsp)
movl %r14d, %r11d
imulq $0x18, %r11, %rbx
movq %rdi, 0x48(%rsp)
leaq (%r9,%rdi), %r15
addq $0xc, %r15
xorl %r12d, %r12d
xorl %r8d, %r8d
xorl %ebp, %ebp
cmpq %r12, %rbx
je 0x723db
movl (%r15,%r12), %r13d
cmpl %r13d, %ebp
movl %r13d, %r14d
cmoval %ebp, %r14d
xorl %esi, %esi
testl %r13d, %r13d
setns %sil
cmovnsl %r14d, %ebp
addl %esi, %r8d
addq $0x18, %r12
jmp 0x723b2
cvtsi2ss %ebp, %xmm0
cmpl $0x2, %r8d
jb 0x72409
movl 0xc(%rcx), %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %esi
cvtsi2ss %esi, %xmm1
movaps %xmm0, %xmm2
mulss %xmm4, %xmm2
ucomiss %xmm2, %xmm1
jbe 0x72409
movq 0x38(%rsp), %r13
jmp 0x724b3
imulq $0x18, %r10, %rdi
leaq (%r9,%rdi), %rsi
movq %rsi, 0x70(%rsp)
movl 0x20(%rsp), %r12d
imulq $0x18, %r12, %rbx
movq %rdi, 0x78(%rsp)
leaq (%r9,%rdi), %r15
addq $0xc, %r15
xorl %ebp, %ebp
xorl %r10d, %r10d
xorl %r13d, %r13d
cmpq %rbp, %rbx
je 0x7245c
movl (%r15,%rbp), %esi
cmpl %esi, %r13d
movl %esi, %r14d
cmoval %r13d, %r14d
xorl %edi, %edi
testl %esi, %esi
setns %dil
cmovnsl %r14d, %r13d
addl %edi, %r10d
addq $0x18, %rbp
jmp 0x72434
cvtsi2ss %r13d, %xmm1
cmpl $0x2, %r10d
jb 0x724a8
movl 0xc(%rcx), %esi
movl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
andl %edi, %esi
cvtsi2ss %esi, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
ucomiss %xmm3, %xmm2
movq 0x38(%rsp), %r13
ja 0x724b3
movq 0x48(%rsp), %rsi
leaq (%r9,%rsi), %rbx
addq $0x8, %rbx
xorl %r14d, %r14d
cmpq %r14, %r11
je 0x724ce
cmpl %eax, (%rbx)
je 0x724c9
incq %r14
addq $0x18, %rbx
jmp 0x72496
cmpl $0x2, %r8d
movq 0x38(%rsp), %r13
jae 0x72486
movq (%rsp), %rdi
movq 0x8(%rsp), %r10
movq 0x10(%rsp), %rbx
incq %rbx
jmp 0x72325
movq %r14, 0x30(%rsp)
movq 0x78(%rsp), %rsi
addq %rsi, %r9
addq $0x8, %r9
xorl %r11d, %r11d
movq 0x10(%rsp), %rbx
cmpq %r11, %r12
je 0x724fa
movl (%r9), %esi
xorl %edx, %esi
cmpl $0x1, %esi
je 0x72501
incq %r11
addq $0x18, %r9
jmp 0x724e2
movq 0x20(%rsp), %r11
jmp 0x72501
movl 0x30(%rsp), %esi
imulq $0x18, %rsi, %r14
movq 0x28(%rsp), %rsi
cmpl $0x0, 0xc(%rsi,%r14)
movl %r11d, %r12d
jns 0x7252d
imulq $0x18, %r12, %rsi
movq 0x70(%rsp), %rdi
cmpl $0x0, 0xc(%rdi,%rsi)
js 0x7260b
cmpl $0x2, %r8d
jb 0x7256c
cmpl $0x2, %r10d
jb 0x7256c
movl 0xc(%rcx), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm2
mulss %xmm4, %xmm0
ucomiss %xmm2, %xmm0
movq (%rsp), %rdi
movq 0x8(%rsp), %r10
jbe 0x724c1
mulss %xmm4, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x724c1
jmp 0x725e3
cmpl $0x1, %r10d
jne 0x7257e
movq %r13, %rdi
movq 0x68(%rsp), %rsi
movl %eax, %edx
jmp 0x72593
cmpl $0x1, %r8d
jne 0x7260b
xorl $0x1, %edx
movq %r13, %rdi
movq 0x68(%rsp), %rsi
leaq 0x5c(%rsp), %rcx
leaq 0x50(%rsp), %r8
leaq 0x90(%rsp), %r9
pushq $0x0
pushq $0x1
leaq 0x90(%rsp), %rax
pushq %rax
leaq 0xa0(%rsp), %rax
pushq %rax
callq 0xc2ebf
movss 0x3c(%rsp), %xmm4
addq $0x20, %rsp
movq 0x60(%rsp), %rax
cmpq %rax, 0x50(%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %r10
jge 0x724c1
movq 0x28(%rsp), %rax
addq %r14, %rax
addq $0xc, %rax
imulq $0x18, %r12, %rcx
movq 0x70(%rsp), %rdx
orb $-0x80, 0xf(%rdx,%rcx)
orb $-0x80, 0x3(%rax)
incq 0x40(%rsp)
jmp 0x724c1
movq (%rsp), %rdi
movq 0x8(%rsp), %r10
jmp 0x724c1
callq 0x2220
cmpq $0x0, 0x40(%rsp)
je 0x72636
movq %r13, %rdi
callq 0x694eb
movq %r13, %rdi
callq 0x69638
movq 0x40(%rsp), %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq