// File: 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
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movss %xmm0, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movl %r8d, -0x24(%rbp)
movl %r9d, -0x28(%rbp)
callq 0x2510
movsd %xmm0, -0x30(%rbp)
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x18, %edx
callq 0x2100
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x50(%rbp)
movq $0x0, -0x60(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0xddf3c
cmpl $0x0, -0x24(%rbp)
je 0xdddac
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x4c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xdddac
jmp 0xddf2e
cmpl $0x0, -0x28(%rbp)
je 0xdddd0
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x4c(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movzbl 0x4(%rax,%rcx,8), %eax
cmpl $0x1, %eax
jne 0xdddd0
jmp 0xddf2e
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x4c(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x4c(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x8c(%rbp)
cmpl $0x2, -0x8c(%rbp)
jae 0xdde1c
jmp 0xddf2e
movq -0x8(%rbp), %rdi
movl -0x4c(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
cmpl $0x2, %eax
jge 0xdde36
jmp 0xddf2e
movq $0x0, -0x98(%rbp)
movq -0x98(%rbp), %rax
movl -0x8c(%rbp), %ecx
cmpq %rcx, %rax
jae 0xddf2c
jmp 0xdde59
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0xddea8
cmpq $0x0, -0x40(%rbp)
je 0xdde7a
movq -0x40(%rbp), %rax
shlq %rax
movq %rax, -0xf0(%rbp)
jmp 0xdde88
movl $0x2, %eax
movq %rax, -0xf0(%rbp)
jmp 0xdde88
movq -0xf0(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
shlq $0x3, %rsi
callq 0x2360
movq %rax, -0x38(%rbp)
movq -0x88(%rbp), %rax
imulq $0x18, -0x98(%rbp), %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, %eax
shlq $0x20, %rax
movq %rax, -0xf8(%rbp)
movq -0x88(%rbp), %rax
movq -0x8(%rbp), %rcx
movq 0x8(%rcx), %rcx
subq %rcx, %rax
movl $0x18, %ecx
cqto
idivq %rcx
movq -0xf8(%rbp), %rdx
addq -0x98(%rbp), %rax
orq %rax, %rdx
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rsi
addq $0x1, %rsi
movq %rsi, -0x48(%rbp)
movq %rdx, (%rax,%rcx,8)
jmp 0xddf15
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
jmp 0xdde41
jmp 0xddf2e
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0xddd81
movq -0x38(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x48(%rbp), %rax
shlq $0x3, %rax
addq %rax, %rsi
callq 0xc90e0
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0xde50a
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x38(%rbp), %rcx
movq -0xa0(%rbp), %rdx
movq (%rcx,%rdx,8), %rcx
movl %ecx, %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0xb8(%rbp)
movq -0xa8(%rbp), %rax
movl 0x8(%rax), %eax
xorl $0x1, %eax
movl %eax, -0xbc(%rbp)
movl $0x0, -0xc0(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0xb8(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xc4(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0xbc(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0xc8(%rbp)
movl $0x0, -0xd4(%rbp)
movl $0x0, -0xd8(%rbp)
cmpl $0x1, -0xc4(%rbp)
jne 0xde023
cmpl $0x1, -0xc8(%rbp)
jne 0xde023
jmp 0xde4f3
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0xb8(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe0(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0xbc(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xe8(%rbp)
movl $0x0, -0xac(%rbp)
movl $0x0, -0xcc(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jae 0xde12e
movq -0xe0(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xde0c2
jmp 0xde11a
movl -0xd4(%rbp), %eax
movq -0xe0(%rbp), %rcx
movl -0xac(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xde10b
movq -0xe0(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xd4(%rbp)
movl -0xcc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xcc(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xde08f
cmpl $0x2, -0xcc(%rbp)
jb 0xde164
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2ss %eax, %xmm0
movl -0xd4(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x14(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0xde164
jmp 0xde4f3
movl $0x0, -0xac(%rbp)
movl $0x0, -0xd0(%rbp)
movl -0xac(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jae 0xde217
movq -0xe8(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xde1ab
jmp 0xde203
movl -0xd8(%rbp), %eax
movq -0xe8(%rbp), %rcx
movl -0xac(%rbp), %edx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
cmpl %ecx, %eax
jae 0xde1f4
movq -0xe8(%rbp), %rax
movl -0xac(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0xd8(%rbp)
movl -0xd0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd0(%rbp)
movl -0xac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xac(%rbp)
jmp 0xde178
cmpl $0x2, -0xd0(%rbp)
jb 0xde24d
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2ss %eax, %xmm0
movl -0xd8(%rbp), %eax
cvtsi2ss %rax, %xmm1
mulss -0x14(%rbp), %xmm1
ucomiss %xmm1, %xmm0
jbe 0xde24d
jmp 0xde4f3
cmpl $0x1, -0xcc(%rbp)
ja 0xde264
cmpl $0x1, -0xd0(%rbp)
ja 0xde264
jmp 0xde4f3
movl $0x0, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jae 0xde2b5
movq -0xe0(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0xbc(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0xde2a2
jmp 0xde2b5
jmp 0xde2a4
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
jmp 0xde26e
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xc8(%rbp), %eax
jae 0xde306
movq -0xe8(%rbp), %rax
movl -0xb4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl -0xb8(%rbp), %ecx
xorl $0x1, %ecx
cmpl %ecx, %eax
jne 0xde2f3
jmp 0xde306
jmp 0xde2f5
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0xde2bf
movq -0xe0(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xde349
movq -0xe8(%rbp), %rax
movl -0xb4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0xde349
jmp 0xde4f3
cmpl $0x1, -0xcc(%rbp)
jbe 0xde3ba
cmpl $0x1, -0xd0(%rbp)
jbe 0xde3ba
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2ss %eax, %xmm1
movl -0xd4(%rbp), %eax
cvtsi2ss %rax, %xmm0
mulss -0x14(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xde3b5
movq -0xa8(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2ss %eax, %xmm1
movl -0xd8(%rbp), %eax
cvtsi2ss %rax, %xmm0
mulss -0x14(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xde3b5
movl $0x1, -0xc0(%rbp)
jmp 0xde490
cmpl $0x1, -0xd0(%rbp)
jne 0xde423
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0xbc(%rbp), %edx
xorl $0x1, %edx
leaq -0x54(%rbp), %rcx
leaq -0x70(%rbp), %r8
leaq -0x68(%rbp), %r9
leaq -0x78(%rbp), %r10
leaq -0x80(%rbp), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movq -0x70(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jge 0xde421
movl $0x1, -0xc0(%rbp)
jmp 0xde48e
cmpl $0x1, -0xcc(%rbp)
jne 0xde48c
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl -0xb8(%rbp), %edx
xorl $0x1, %edx
leaq -0x54(%rbp), %rcx
leaq -0x70(%rbp), %r8
leaq -0x68(%rbp), %r9
leaq -0x78(%rbp), %r10
leaq -0x80(%rbp), %rax
xorl %r11d, %r11d
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
movl $0x1, 0x10(%rsp)
movq $0x0, 0x18(%rsp)
callq 0x166c30
movq -0x70(%rbp), %rax
movslq -0x18(%rbp), %rcx
cmpq %rcx, %rax
jge 0xde48a
movl $0x1, -0xc0(%rbp)
jmp 0xde48c
jmp 0xde48e
jmp 0xde490
cmpl $0x0, -0xc0(%rbp)
je 0xde4f1
movq -0xe8(%rbp), %rax
movl -0xb4(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0xe0(%rbp), %rax
movl -0xb0(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x60(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x60(%rbp)
jmp 0xde4f3
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0xddf5f
movq -0x38(%rbp), %rdi
callq 0x2210
cmpq $0x0, -0x60(%rbp)
je 0xde52c
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movq -0x60(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax)