// File: /workspace/github_cpp_makefile_star2_O3/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 $0x98, %rsp
movl %r9d, 0x60(%rsp)
movl %r8d, %ebp
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %r12d
movss %xmm0, 0x3c(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xac01d
movq %rbx, %rsi
movl %r12d, 0x6c(%rsp)
movq %r15, 0x70(%rsp)
movq %r14, 0x78(%rsp)
movl %ebp, 0x54(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %edi, %edi
movq %rsi, 0x20(%rsp)
cmpl $0x0, 0x60(%rsp)
movq %rdx, 0x18(%rsp)
je 0xab958
movq 0x30(%rsi), %rax
cmpb $0x0, (%rax,%rdx)
jne 0xaba1a
cmpl $0x0, 0xd0(%rsp)
je 0xab975
movq 0x20(%rsi), %rax
movl %edx, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xaba1a
movq 0x28(%rsi), %rax
movq (%rax,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaba1a
movq %rcx, %r15
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r15
je 0xaba1a
movq 0x8(%rsi), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbp
xorl %r13d, %r13d
cmpq %rbx, %r12
jne 0xab9e1
leaq (%r12,%r12), %rbx
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbx
leaq (,%rbx,8), %rsi
callq 0x2370
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x20(%rsp), %rsi
movq %rax, %rdi
movq 0x8(%rsi), %rax
leaq (,%r13,2), %rcx
addq %r13, %rcx
movl 0xc(%rbp,%rcx,8), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %rbp, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r14, %rdx
addq %r13, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r13
cmpq %r15, %r13
jne 0xab9ad
movq 0x18(%rsp), %rdx
incq %rdx
cmpq 0x30(%rsp), %rdx
jne 0xab93e
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0x28(%rsp)
callq 0x9dc69
testq %r12, %r12
je 0xac02a
movq $0x0, 0x88(%rsp)
xorl %ebp, %ebp
movq 0x20(%rsp), %r8
movss 0x3c(%rsp), %xmm4
movq 0x28(%rsp), %rdi
movq 0x8(%r8), %r9
movq 0x28(%r8), %rdx
movl (%rdi,%rbp,8), %ecx
leaq (%rcx,%rcx,2), %rcx
movq (%r9,%rcx,8), %r15
shrq $0x20, %r15
movl 0x8(%r9,%rcx,8), %r14d
movq %r14, %rsi
xorq $0x1, %rsi
movq (%rdx,%r15,8), %r10
movq (%rdx,%rsi,8), %rsi
cmpl $0x1, %r10d
jne 0xaba9c
cmpl $0x1, %esi
je 0xabc69
leaq (%r9,%rcx,8), %rax
movq %rax, 0x40(%rsp)
movq %r10, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rax
xorps %xmm0, %xmm0
xorl %edi, %edi
testl %r10d, %r10d
movq %rsi, 0x18(%rsp)
movq %r14, 0x60(%rsp)
movq %r9, 0x30(%rsp)
movq %rax, 0x90(%rsp)
je 0xabb69
movq %r15, %r14
movl %r10d, %ecx
leaq (%r9,%rax,8), %rsi
addq $0xc, %rsi
xorl %r11d, %r11d
xorl %r8d, %r8d
xorl %ebx, %ebx
xorl %edx, %edx
movl %ebx, %r13d
movl (%rsi), %r15d
cmpl %r15d, %ebx
movl %r15d, %ebx
cmoval %r13d, %ebx
movl %r8d, %r9d
movl %r11d, %r8d
cmovael %r9d, %r8d
xorl %eax, %eax
testl %r15d, %r15d
setns %al
cmovsl %r13d, %ebx
cmovsl %r9d, %r8d
addl %eax, %edx
incq %r11
addq $0x18, %rsi
cmpq %r11, %rcx
jne 0xabaed
xorps %xmm1, %xmm1
cvtsi2ss %ebx, %xmm1
cmpl $0x2, %edx
movq 0x18(%rsp), %rsi
jb 0xabb75
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
movb $0x1, %al
ucomiss %xmm3, %xmm2
movq %r14, %r15
movq 0x60(%rsp), %r14
movq 0x30(%rsp), %r9
jbe 0xabb84
jmp 0xabc5f
xorl %eax, %eax
xorl %r8d, %r8d
xorps %xmm1, %xmm1
xorl %edx, %edx
jmp 0xabb84
xorl %eax, %eax
movq %r14, %r15
movq 0x60(%rsp), %r14
movq 0x30(%rsp), %r9
movl %eax, 0x84(%rsp)
movq %rsi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %rax
testl %esi, %esi
movq %rax, 0x48(%rsp)
je 0xabc3a
movq %r15, 0x58(%rsp)
movl %esi, %ebx
leaq (%r9,%rax,8), %r13
addq $0xc, %r13
xorl %r11d, %r11d
xorl %edi, %edi
xorl %esi, %esi
xorl %ecx, %ecx
movl %esi, %eax
movl (%r13), %r9d
cmpl %r9d, %esi
movl %r9d, %esi
cmoval %eax, %esi
movl %edi, %r15d
movl %r11d, %edi
cmovael %r15d, %edi
xorl %r14d, %r14d
testl %r9d, %r9d
setns %r14b
cmovsl %eax, %esi
cmovsl %r15d, %edi
addl %r14d, %ecx
incq %r11
addq $0x18, %r13
cmpq %r11, %rbx
jne 0xabbbb
xorps %xmm0, %xmm0
cvtsi2ss %esi, %xmm0
cmpl $0x2, %ecx
jb 0xabc40
movq 0x40(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
andl %esi, %eax
xorps %xmm2, %xmm2
cvtsi2ss %eax, %xmm2
movaps %xmm0, %xmm3
mulss %xmm4, %xmm3
movb $0x1, %bl
ucomiss %xmm3, %xmm2
movq 0x58(%rsp), %r15
movq 0x60(%rsp), %r14
movq 0x30(%rsp), %r9
movq 0x18(%rsp), %rsi
jbe 0xabc56
jmp 0xabc5f
xorl %ebx, %ebx
xorl %ecx, %ecx
jmp 0xabc56
xorl %ebx, %ebx
movq 0x58(%rsp), %r15
movq 0x60(%rsp), %r14
movq 0x30(%rsp), %r9
movq 0x18(%rsp), %rsi
movl %edx, %eax
orl %ecx, %eax
cmpl $0x2, %eax
jae 0xabc7a
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %rdi
incq %rbp
cmpq %r12, %rbp
jne 0xaba62
jmp 0xabfe8
testl %r10d, %r10d
je 0xabcb3
movl %r10d, %r11d
movq 0x90(%rsp), %rax
leaq (%r9,%rax,8), %r13
addq $0x8, %r13
xorl %esi, %esi
cmpl %r14d, (%r13)
je 0xabca9
incq %rsi
addq $0x18, %r13
cmpq %rsi, %r11
jne 0xabc94
movq %r10, %rsi
movl %esi, %r10d
movq 0x18(%rsp), %rsi
jmp 0xabcbe
xorl %r10d, %r10d
movq 0x90(%rsp), %rax
testl %esi, %esi
je 0xabd03
movl %ebx, 0x58(%rsp)
movq %rax, %rbx
movl %esi, %r11d
movq 0x48(%rsp), %rax
leaq (%r9,%rax,8), %r13
addq $0x8, %r13
xorl %esi, %esi
movl (%r13), %eax
xorl %r15d, %eax
cmpl $0x1, %eax
je 0xabcf8
incq %rsi
addq $0x18, %r13
cmpq %rsi, %r11
jne 0xabcdb
movq 0x18(%rsp), %rsi
movl %esi, %esi
movq %rbx, %rax
movl 0x58(%rsp), %ebx
jmp 0xabd05
xorl %esi, %esi
leaq (%r9,%rax,8), %r11
movq 0x48(%rsp), %rax
leaq (%r9,%rax,8), %r9
leaq (%r10,%r10,2), %r10
cmpl $0x0, 0xc(%r11,%r10,8)
jns 0xabd2e
leaq (%rsi,%rsi,2), %rax
cmpl $0x0, 0xc(%r9,%rax,8)
js 0xabc5f
movl %r8d, %r13d
movl %edi, %edi
movl 0x84(%rsp), %eax
andb %bl, %al
cmpb $0x1, %al
jne 0xabe02
movq %rdi, %rbx
movq 0x40(%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 0x20(%rsp), %r8
movq 0x28(%rsp), %rdi
jbe 0xabc69
mulss %xmm4, %xmm0
ucomiss %xmm2, %xmm0
jbe 0xabc69
cmpl $0x1, 0xd8(%rsp)
jne 0xabfc5
movq %r10, 0x48(%rsp)
movq %r9, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq %r15, 0x58(%rsp)
leaq (,%r13,2), %rax
addq %r13, %rax
movq %r11, 0x40(%rsp)
movl 0x8(%r11,%rax,8), %edi
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r14d, %esi
movq %r8, %rdx
movq 0x70(%rsp), %r13
movq %r13, %rcx
movq 0x78(%rsp), %r15
movq %r15, %r8
movl 0x54(%rsp), %r14d
movl %r14d, %r9d
callq 0xaafd5
cmpq $0x1, %rax
jne 0xabf61
movq 0x20(%rsp), %r8
movss 0x3c(%rsp), %xmm4
jmp 0xabc64
cmpl $0x1, %ecx
movq 0x20(%rsp), %r8
jne 0xabeb5
movq %r10, 0x48(%rsp)
movq %r11, 0x40(%rsp)
movq %r9, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
movq %r8, %rdi
movl %r14d, %esi
movl 0x6c(%rsp), %ebx
movl %ebx, %edx
callq 0xab75d
cmpl %ebx, %eax
setl %cl
cmpl $0x1, 0xd8(%rsp)
jne 0xabf43
cmpl %ebx, %eax
jge 0xabf43
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x40(%rsp), %r15
movl 0x8(%r15,%rax,8), %edi
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r14d, %esi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xaafd5
movq %rbx, %r8
movq %r15, %r11
cmpq $0x1, %rax
movss 0x3c(%rsp), %xmm4
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r9
jmp 0xabfba
cmpl $0x1, %edx
jne 0xabc64
movq %rdi, %r14
movq %r10, 0x48(%rsp)
movq %r11, 0x40(%rsp)
movq %r9, 0x18(%rsp)
movq %rsi, 0x30(%rsp)
xorl $0x1, %r15d
movq %r8, %rdi
movl %r15d, %esi
movl 0x6c(%rsp), %ebx
movl %ebx, %edx
callq 0xab75d
cmpl %ebx, %eax
setl %cl
cmpl $0x1, 0xd8(%rsp)
jne 0xabf43
cmpl %ebx, %eax
jge 0xabf43
leaq (%r14,%r14,2), %rax
movq 0x18(%rsp), %r14
movl 0x8(%r14,%rax,8), %edi
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r15d, %esi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
movq 0x70(%rsp), %rcx
movq 0x78(%rsp), %r8
movl 0x54(%rsp), %r9d
callq 0xaafd5
movq %rbx, %r8
movq %r14, %r9
cmpq $0x1, %rax
jmp 0xabfa5
testb %cl, %cl
movq 0x20(%rsp), %r8
movss 0x3c(%rsp), %xmm4
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x18(%rsp), %r9
jmp 0xabfb5
leaq (%rbx,%rbx,2), %rax
movq 0x18(%rsp), %rcx
movl 0x8(%rcx,%rax,8), %edi
movq 0x58(%rsp), %rsi
xorl $0x1, %esi
movq 0xe0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
movq %r13, %rcx
movq %r15, %r8
movl %r14d, %r9d
callq 0xaafd5
cmpq $0x1, %rax
movq %rbx, %r8
movq 0x18(%rsp), %r9
movss 0x3c(%rsp), %xmm4
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq 0x40(%rsp), %r11
movq 0x48(%rsp), %r10
je 0xabc69
leaq (%r11,%r10,8), %rax
addq $0xc, %rax
leaq (%rsi,%rsi,2), %rcx
orb $-0x80, 0xf(%r9,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x88(%rsp)
jmp 0xabc69
callq 0x2220
movq 0x88(%rsp), %r14
testq %r14, %r14
je 0xac034
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xac037
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xac02f
movq 0x28(%rsp), %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq