// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_length(asg_t*, float, int, ma_hit_t_alloc*, long long, unsigned int, int, int, int, R_to_U*)
int asg_arc_del_short_diploid_by_length(asg_t *g, float drop_ratio, int max_ext, 
ma_hit_t_alloc* reverse_sources, long long miniedgeLen, uint32_t stops_threshold,
int if_skip_bubble, int if_drop, int if_check_hap, R_to_U* ruIndex)
{
    double startTime = Get_T();
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));

	uint32_t v, n_vtx = g->n_seq * 2;
    long long n_cut = 0;
    
    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;
        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, ov_max_i = 0, ow_max_i = 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, ov_max_i = i; 
			++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, ow_max_i = i;
			++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;
            if(if_check_hap == 1 && to_del == 1)
            {
                
                if(check_if_diploid_primary_complex(av[ov_max_i].v, w^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1 
                   || 
                   check_if_diploid_primary_complex(aw[ow_max_i].v, v^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
        
		} else if (kw == 1) {
			if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
            ///kv > 1
            if(if_check_hap == 1 && to_del == 1)
            {
                if(check_if_diploid_primary_complex(av[ov_max_i].v, w^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
		} else if (kv == 1) {
			if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
            ///kw > 1
            if(if_check_hap == 1 && to_del == 1)
            {
                if(check_if_diploid_primary_complex(aw[ow_max_i].v, v^1, g, reverse_sources, 
                miniedgeLen, stops_threshold, if_drop, ruIndex) == 1)
                {
                    to_del = 0;
                }
            }
		}
		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 $0x88, %rsp
movl %r9d, 0x50(%rsp)
movl %r8d, 0x5c(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, 0x30(%rsp)
movl %esi, 0x58(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %rdi, %r14
callq 0x300fc
movl 0x14(%r14), %eax
addl %eax, %eax
movq %r14, 0x70(%rsp)
je 0xaaa50
movl %eax, %eax
movq %rax, 0x38(%rsp)
xorl %esi, %esi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %eax, %eax
xorl %edi, %edi
cmpl $0x0, 0x50(%rsp)
movq %rsi, 0x10(%rsp)
je 0xaa963
movq 0x30(%r14), %rcx
cmpb $0x0, (%rcx,%rsi)
jne 0xaaa3b
cmpl $0x0, 0xc0(%rsp)
je 0xaa980
movq 0x20(%r14), %rcx
movl %esi, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0xaaa3b
movq 0x28(%r14), %rcx
movq (%rcx,%rsi,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaaa3b
movq %rcx, %r10
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r10
je 0xaaa3b
movq 0x8(%r14), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %rsi
leaq (%rdx,%rcx,8), %rbp
addq $0xc, %rbp
xorl %r13d, %r13d
movq %rsi, 0x40(%rsp)
cmpq %rax, %r12
jne 0xaaa09
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r12, %rbx
movq %r10, %r12
callq 0x2370
movq %r12, %r10
movq %rbx, %r12
movq 0x40(%rsp), %rsi
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rax, %rdi
movq %r14, %rax
movq 0x70(%rsp), %r14
movl (%rbp), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %rsi, %rdx
subq 0x8(%r14), %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %r13, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r13
addq $0x18, %rbp
cmpq %r13, %r10
jne 0xaa9c5
movq 0x10(%rsp), %rsi
incq %rsi
cmpq 0x38(%rsp), %rsi
jne 0xaa949
jmp 0xaaa55
xorl %edi, %edi
xorl %r12d, %r12d
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x20(%rsp)
callq 0x9d53f
testq %r12, %r12
je 0xaafd1
movq $0x0, 0x68(%rsp)
xorl %r10d, %r10d
movq 0x30(%rsp), %r13
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
movq 0x8(%r14), %rbx
movq 0x28(%r14), %rax
movl (%r11,%r10,8), %edx
leaq (%rdx,%rdx,2), %rbp
movq (%rbx,%rbp,8), %r15
shrq $0x20, %r15
movl 0x8(%rbx,%rbp,8), %esi
movq %rsi, %rdx
xorq $0x1, %rdx
movq (%rax,%r15,8), %rdi
movq (%rax,%rdx,8), %r8
cmpl $0x1, %edi
jne 0xaaac2
cmpl $0x1, %r8d
je 0xaaf1a
movq %r10, 0x10(%rsp)
movq %rdi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
testl %edi, %edi
movq %r15, 0x50(%rsp)
movq %rsi, 0x40(%rsp)
movq %rax, 0x38(%rsp)
je 0xaab63
movq %r8, 0x28(%rsp)
movl %edi, %r9d
movq %rbx, %r8
leaq (%rbx,%rax,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
xorl %ebx, %ebx
xorl %edx, %edx
xorl %ecx, %ecx
movl %edx, %r13d
movl (%r10), %r15d
cmpl %r15d, %edx
movl %r15d, %edx
cmoval %r13d, %edx
movl %ebx, %eax
movl %r11d, %ebx
cmovael %eax, %ebx
xorl %r14d, %r14d
testl %r15d, %r15d
setns %r14b
cmovsl %r13d, %edx
cmovsl %eax, %ebx
addl %r14d, %ecx
incq %r11
addq $0x18, %r10
cmpq %r11, %r9
jne 0xaab01
cvtsi2ss %edx, %xmm0
movl %ebx, %r9d
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %r11
movq 0x50(%rsp), %r15
movq 0x40(%rsp), %rsi
movq %r8, %rbx
movq 0x28(%rsp), %r8
jmp 0xaab6b
xorps %xmm0, %xmm0
xorl %ecx, %ecx
xorl %r9d, %r9d
leaq (%rbx,%rbp,8), %r10
cmpl $0x2, %ecx
jb 0xaab96
movl 0xc(%r10), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movaps %xmm0, %xmm2
mulss %xmm4, %xmm2
ucomiss %xmm2, %xmm1
ja 0xaaf15
movq %r9, 0x80(%rsp)
movq %r8, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
testl %r8d, %r8d
movq %rbx, 0x48(%rsp)
movq %rax, 0x28(%rsp)
je 0xaac41
movq %r10, 0x60(%rsp)
movq %r8, %r9
movl %r8d, %r11d
leaq (%rbx,%rax,8), %rbx
addq $0xc, %rbx
xorl %r13d, %r13d
xorl %edx, %edx
xorl %r8d, %r8d
xorl %ebp, %ebp
movl %r8d, %eax
movl (%rbx), %r14d
cmpl %r14d, %r8d
movl %r14d, %r8d
cmoval %eax, %r8d
movl %edx, %r15d
movl %r13d, %edx
cmovael %r15d, %edx
xorl %r10d, %r10d
testl %r14d, %r14d
setns %r10b
cmovsl %eax, %r8d
cmovsl %r15d, %edx
addl %r10d, %ebp
incq %r13
addq $0x18, %rbx
cmpq %r13, %r11
jne 0xaabd9
xorps %xmm1, %xmm1
cvtsi2ss %r8d, %xmm1
movl %edx, %ebx
movq 0x70(%rsp), %r14
movq 0x30(%rsp), %r13
movq 0x20(%rsp), %r11
movq 0x50(%rsp), %r15
movq 0x40(%rsp), %rsi
movq %r9, %r8
movq 0x60(%rsp), %r10
jmp 0xaac48
xorps %xmm1, %xmm1
xorl %ebp, %ebp
xorl %ebx, %ebx
cmpl $0x2, %ebp
jb 0xaac6f
movl 0xc(%r10), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
ucomiss %xmm3, %xmm2
ja 0xaaf15
movl %ecx, %eax
orl %ebp, %eax
cmpl $0x2, %eax
jb 0xaaf15
testl %edi, %edi
movq 0x48(%rsp), %r9
je 0xaaca8
movq 0x38(%rsp), %rax
leaq (%r9,%rax,8), %rax
addq $0x8, %rax
xorl %edx, %edx
cmpl %esi, (%rax)
je 0xaaca4
incl %edx
addq $0x18, %rax
cmpl %edx, %edi
jne 0xaac94
movl %edi, %edx
movl %edx, %edi
jmp 0xaacaa
xorl %edi, %edi
testl %r8d, %r8d
movq %rbx, 0x60(%rsp)
je 0xaaceb
movq %r10, %rbx
movq 0x28(%rsp), %rax
leaq (%r9,%rax,8), %r10
addq $0x8, %r10
xorl %edx, %edx
movl (%r10), %eax
xorl %r15d, %eax
cmpl $0x1, %eax
je 0xaacdf
incl %edx
addq $0x18, %r10
cmpl %edx, %r8d
jne 0xaacc6
movl %r8d, %edx
movl %edx, %edx
movq %rbx, %r10
movq 0x30(%rsp), %r13
jmp 0xaaced
xorl %edx, %edx
movq 0x38(%rsp), %rax
leaq (%r9,%rax,8), %rbx
movq 0x28(%rsp), %rax
leaq (%r9,%rax,8), %r8
leaq (%rdi,%rdi,2), %rdi
movq %rbx, 0x38(%rsp)
cmpl $0x0, 0xc(%rbx,%rdi,8)
jns 0xaad1f
leaq (%rdx,%rdx,2), %rax
cmpl $0x0, 0xc(%r8,%rax,8)
js 0xaaf15
cmpl $0x2, %ecx
jb 0xaade4
cmpl $0x2, %ebp
jb 0xaade4
movq %r8, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movq %r13, %r8
movl 0xc(%r10), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
mulss %xmm4, %xmm0
mulss %xmm4, %xmm1
movaps %xmm2, %xmm3
cmpltps %xmm1, %xmm3
cmpltps %xmm0, %xmm2
andps %xmm3, %xmm2
movd %xmm2, %edx
cmpl $0x1, 0xc8(%rsp)
jne 0xaaf2b
testb $0x1, %dl
movq 0xd0(%rsp), %rbp
movq 0x10(%rsp), %r10
je 0xaaf97
movq %rdi, 0x40(%rsp)
movq 0x80(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rcx
movl 0x8(%rcx,%rax,8), %edi
movq %rbp, 0x8(%rsp)
movq %r14, %rdx
movq %r8, %r13
movq %r8, %rcx
movq 0x78(%rsp), %r15
movq %r15, %r8
movl 0x5c(%rsp), %ebx
movl %ebx, %r9d
callq 0xa9f5e
cmpq $0x1, %rax
jne 0xaaf32
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
jmp 0xaaf15
cmpl $0x1, %ebp
movq 0x60(%rsp), %rax
jne 0xaae67
movq %rdi, %rbp
movq %r8, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movq %r14, %rdi
movl 0x58(%rsp), %ebx
movl %ebx, %edx
callq 0xaa77e
cmpl %ebx, %eax
setl %dl
cmpl $0x1, 0xc8(%rsp)
jne 0xaaf7f
cmpl %ebx, %eax
movq 0x30(%rsp), %r8
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
movq 0xd0(%rsp), %rcx
movq 0x10(%rsp), %r10
movq 0x40(%rsp), %rsi
movq %rbp, %rdi
jge 0xaaf97
movq 0x80(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x38(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
addq $0x8, %rax
jmp 0xaaeeb
cmpl $0x1, %ecx
jne 0xaaf15
movq %rdi, %rbp
movq %r8, 0x28(%rsp)
movq %rdx, 0x48(%rsp)
movq %rax, %r13
xorl $0x1, %r15d
movq %r14, %rdi
movl %r15d, %esi
movl 0x58(%rsp), %ebx
movl %ebx, %edx
callq 0xaa77e
cmpl %ebx, %eax
setl %dl
cmpl $0x1, 0xc8(%rsp)
jne 0xaaf7f
cmpl %ebx, %eax
movq 0x30(%rsp), %r8
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
movq 0xd0(%rsp), %rcx
movq 0x10(%rsp), %r10
movq %rbp, %rdi
jge 0xaaf97
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x28(%rsp), %rdx
leaq (%rdx,%rax,8), %rax
addq $0x8, %rax
movl %r15d, %esi
movl (%rax), %edi
movq %rcx, 0x8(%rsp)
movq %r14, %rdx
movq %r8, %rcx
movq 0x78(%rsp), %r8
movl 0x5c(%rsp), %r9d
callq 0xa9f5e
movq 0x30(%rsp), %r8
cmpq $0x1, %rax
setne %dl
jmp 0xaaf84
movq 0x10(%rsp), %r10
incq %r10
cmpq %r12, %r10
jne 0xaaa88
jmp 0xaafdf
movq 0x10(%rsp), %r10
jmp 0xaaf97
movq 0x60(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq 0x28(%rsp), %rcx
movl 0x8(%rcx,%rax,8), %edi
movq 0x50(%rsp), %rsi
xorl $0x1, %esi
movq %rbp, 0x8(%rsp)
movq %r14, %rdx
movq %r13, %rcx
movq %r15, %r8
movl %ebx, %r9d
callq 0xa9f5e
cmpq $0x1, %rax
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0x40(%rsp), %rdi
je 0xaaf1a
jmp 0xaafa3
movq 0x30(%rsp), %r8
movss 0x1c(%rsp), %xmm4
movq 0x20(%rsp), %r11
movq 0x10(%rsp), %r10
movq %rbp, %rdi
testb $0x1, %dl
movq %r8, %r13
je 0xaaf1a
movq 0x38(%rsp), %rax
leaq (%rax,%rdi,8), %rax
addq $0xc, %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rcx,2), %rdx
movq 0x28(%rsp), %rcx
orb $-0x80, 0xf(%rcx,%rdx,8)
orb $-0x80, 0x3(%rax)
incq 0x68(%rsp)
jmp 0xaaf1a
movq $0x0, 0x68(%rsp)
movq 0x20(%rsp), %r11
movq %r11, %rdi
callq 0x2220
movq 0x68(%rsp), %rbx
testq %rbx, %rbx
je 0xab00e
movq %r14, %rdi
callq 0xa02fa
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
movl %ebx, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq