// File: /workspace/github_cpp_makefile_star2_O2/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 $0xa8, %rsp
movl %r9d, %ebx
movl %r8d, 0x4c(%rsp)
movq %rcx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
movl %esi, 0x5c(%rsp)
movss %xmm0, 0x34(%rsp)
movq %rdi, %r15
movl 0xe0(%rsp), %ebp
callq 0x251c
movl 0x14(%r15), %esi
addl %esi, %esi
xorl %r8d, %r8d
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
pushq $0x18
popq %r11
xorl %edi, %edi
xorl %ecx, %ecx
xorl %r9d, %r9d
movq %r15, 0x40(%rsp)
movl %ebx, 0x50(%rsp)
movq %rsi, 0x70(%rsp)
cmpq %rsi, %r8
movq %r9, 0x20(%rsp)
je 0x71cb4
testl %ebx, %ebx
je 0x71bac
movq 0x30(%r15), %rax
cmpb $0x0, (%rax,%r8)
jne 0x71c8a
testl %ebp, %ebp
je 0x71bc4
movq 0x20(%r15), %rax
movl %r8d, %edx
shrl %edx
cmpb $0x1, 0x4(%rax,%rdx,8)
je 0x71c8a
movq 0x28(%r15), %rax
movq (%rax,%r8,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x71c8a
movq %r8, 0x18(%rsp)
movq 0x8(%r15), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %rbp
movl %eax, %r12d
leaq (%rsi,%rdx), %r15
addq $0xc, %r15
leaq (,%r9,8), %r13
xorl %r14d, %r14d
movq %r12, 0x38(%rsp)
leaq (%r9,%r14), %rax
cmpq %r14, %r12
je 0x71c8f
cmpq %rcx, %rax
jne 0x71c5e
movq %r13, %r12
leaq (%rcx,%rcx), %r13
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r13
leaq (,%r13,8), %rsi
movq %r11, %rbx
callq 0x2370
movq %rbx, %r11
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x20(%rsp), %r9
movq %rax, %rdi
movq 0x40(%rsp), %rax
movq 0x8(%rax), %rsi
movq %r13, %rcx
movq %r12, %r13
movq 0x38(%rsp), %r12
movl (%r15), %r8d
andl %r10d, %r8d
shlq $0x20, %r8
movq %rbp, %rax
subq %rsi, %rax
cqto
idivq %r11
addq %r14, %rax
orq %r8, %rax
leaq (%rdi,%r13), %rdx
movq %rax, (%rdx,%r14,8)
incq %r14
addq $0x18, %r15
jmp 0x71c0a
movq %r9, %rax
jmp 0x71ca9
movq 0x40(%rsp), %r15
movl 0x50(%rsp), %ebx
movl 0xe0(%rsp), %ebp
movq 0x70(%rsp), %rsi
movq 0x18(%rsp), %r8
incq %r8
movq %rax, %r9
jmp 0x71b8b
leaq (%rdi,%r9,8), %rsi
movq %rdi, 0x28(%rsp)
callq 0x66c25
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %rdi
xorl %r9d, %r9d
movq $0x0, 0x78(%rsp)
movss 0x34(%rsp), %xmm4
cmpq %rsi, %r9
je 0x7213a
movq 0x8(%r15), %r8
movq 0x28(%r15), %rcx
movl (%rdi,%r9,8), %eax
imulq $0x18, %rax, %r10
movq (%r8,%r10), %r14
shrq $0x20, %r14
movl 0x8(%r8,%r10), %eax
movq %rax, %rdx
xorq $0x1, %rdx
movq (%rcx,%r14,8), %r11
movq (%rcx,%rdx,8), %rdx
cmpl $0x1, %r11d
jne 0x71d22
cmpl $0x1, %edx
je 0x71fc2
movq %rax, 0x70(%rsp)
movq %r9, 0x18(%rsp)
addq %r8, %r10
movq %r10, 0x38(%rsp)
movq %r11, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
leaq (%r8,%rcx), %rax
movq %rax, 0x68(%rsp)
movq %rdx, 0x50(%rsp)
movq %rdx, %r10
shrq $0x20, %r10
movq %r11, 0x98(%rsp)
movl %r11d, %r9d
movq %rcx, 0xa0(%rsp)
leaq (%r8,%rcx), %r11
addq $0xc, %r11
xorl %ebx, %ebx
xorl %edx, %edx
xorl %ebp, %ebp
xorl %edi, %edi
cmpq %rbx, %r9
je 0x71daa
movl (%r11), %ecx
cmpl %ecx, %ebp
movl %ecx, %r12d
cmoval %ebp, %r12d
movl %edi, %r13d
cmovbl %ebx, %r13d
xorl %esi, %esi
testl %ecx, %ecx
cmovnsl %r12d, %ebp
cmovnsl %r13d, %edi
setns %sil
addl %esi, %edx
incq %rbx
addq $0x18, %r11
jmp 0x71d77
cvtsi2ss %ebp, %xmm0
cmpl $0x2, %edx
jb 0x71dd6
movq 0x38(%rsp), %rax
movl 0xc(%rax), %ecx
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andl %eax, %ecx
cvtsi2ss %ecx, %xmm1
movaps %xmm0, %xmm2
mulss %xmm4, %xmm2
ucomiss %xmm2, %xmm1
ja 0x71fb3
imulq $0x18, %r10, %rcx
leaq (%r8,%rcx), %rax
movq %rax, 0x60(%rsp)
movl 0x50(%rsp), %r11d
movq %rcx, 0x90(%rsp)
leaq (%r8,%rcx), %rbx
addq $0xc, %rbx
xorl %r12d, %r12d
xorl %r10d, %r10d
xorl %ecx, %ecx
xorl %r15d, %r15d
cmpq %r12, %r11
je 0x71e35
movl (%rbx), %esi
cmpl %esi, %ecx
movl %esi, %r13d
cmoval %ecx, %r13d
movl %r15d, %ebp
cmovbl %r12d, %ebp
xorl %eax, %eax
testl %esi, %esi
cmovnsl %r13d, %ecx
cmovnsl %ebp, %r15d
setns %al
addl %eax, %r10d
incq %r12
addq $0x18, %rbx
jmp 0x71e03
cvtsi2ss %ecx, %xmm1
cmpl $0x2, %r10d
jb 0x71e68
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm2
movaps %xmm1, %xmm3
mulss %xmm4, %xmm3
ucomiss %xmm3, %xmm2
movq 0x70(%rsp), %r13
jbe 0x71e76
jmp 0x71fae
cmpl $0x2, %edx
movq 0x70(%rsp), %r13
jb 0x71fae
movq 0xa0(%rsp), %rax
leaq (%r8,%rax), %rcx
addq $0x8, %rcx
xorl %ebx, %ebx
cmpq %rbx, %r9
je 0x71e9b
cmpl %r13d, (%rcx)
je 0x71ea3
incq %rbx
addq $0x18, %rcx
jmp 0x71e88
movq 0x98(%rsp), %rbx
movq 0x90(%rsp), %rax
leaq (%r8,%rax), %rcx
addq $0x8, %rcx
xorl %r8d, %r8d
cmpq %r8, %r11
je 0x71ece
movl (%rcx), %eax
xorl %r14d, %eax
cmpl $0x1, %eax
je 0x71ed5
incq %r8
addq $0x18, %rcx
jmp 0x71eb6
movq 0x50(%rsp), %r8
jmp 0x71ed5
movl %ebx, %eax
imulq $0x18, %rax, %rbp
movq 0x68(%rsp), %rax
cmpl $0x0, 0xc(%rax,%rbp)
movl %r8d, %esi
jns 0x71efe
imulq $0x18, %rsi, %rax
movq 0x60(%rsp), %rcx
cmpl $0x0, 0xc(%rcx,%rax)
js 0x71fae
movl %edi, %r12d
movl %r15d, %ebx
cmpl $0x2, %edx
jb 0x71fca
cmpl $0x2, %r10d
jb 0x71fca
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm2
mulss %xmm4, %xmm0
ucomiss %xmm2, %xmm0
movq 0x40(%rsp), %r15
movl 0x4c(%rsp), %r9d
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rcx
movq 0xf0(%rsp), %r10
jbe 0x71fb3
mulss %xmm4, %xmm1
ucomiss %xmm2, %xmm1
jbe 0x71fb3
movq %rsi, 0x38(%rsp)
cmpl $0x1, 0xe8(%rsp)
jne 0x720c7
movq %rbp, 0x50(%rsp)
imulq $0x18, %r12, %rax
movq 0x68(%rsp), %rdx
movl 0x8(%rdx,%rax), %edi
movq %r10, 0x8(%rsp)
movl %r13d, %esi
movq %r15, %rdx
movl %r9d, %ebp
movq %r8, %r12
movq %rcx, %r13
callq 0x71242
cmpq $0x1, %rax
jne 0x720f1
movss 0x34(%rsp), %xmm4
jmp 0x71fb3
movq 0x40(%rsp), %r15
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r9
incq %r9
jmp 0x71cde
cmpl $0x1, %r10d
movq 0x40(%rsp), %r15
jne 0x7203a
movq %rsi, 0x38(%rsp)
movq %r15, %rdi
movl %r13d, %esi
movl 0x5c(%rsp), %ebx
movl %ebx, %edx
callq 0x71a0e
cmpl %ebx, %eax
setl %dl
cmpl $0x1, 0xe8(%rsp)
jne 0x720b9
cmpl %ebx, %eax
movl 0x4c(%rsp), %r9d
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rcx
movq 0xf0(%rsp), %rsi
jge 0x720b9
imulq $0x18, %r12, %rax
movq 0x68(%rsp), %rdx
movl 0x8(%rdx,%rax), %edi
movq %rsi, 0x8(%rsp)
movl %r13d, %esi
jmp 0x720a8
cmpl $0x1, %edx
jne 0x71fb3
movq %rsi, 0x38(%rsp)
movq %rbp, %r12
xorl $0x1, %r14d
movq %r15, %rdi
movl %r14d, %esi
movl 0x5c(%rsp), %ebp
movl %ebp, %edx
callq 0x71a0e
cmpl %ebp, %eax
setl %dl
cmpl $0x1, 0xe8(%rsp)
jne 0x720b6
cmpl %ebp, %eax
movl 0x4c(%rsp), %r9d
movq 0x88(%rsp), %r8
movq 0x80(%rsp), %rcx
movq 0xf0(%rsp), %rsi
movq %r12, %rbp
jge 0x720b9
imulq $0x18, %rbx, %rax
movq 0x60(%rsp), %rdx
movl 0x8(%rdx,%rax), %edi
movq %rsi, 0x8(%rsp)
movl %r14d, %esi
movq %r15, %rdx
callq 0x71242
cmpq $0x1, %rax
jmp 0x720bb
movq %r12, %rbp
testb %dl, %dl
movss 0x34(%rsp), %xmm4
je 0x71fb3
movq 0x68(%rsp), %rax
addq %rbp, %rax
addq $0xc, %rax
imulq $0x18, 0x38(%rsp), %rcx
movq 0x60(%rsp), %rdx
orb $-0x80, 0xf(%rdx,%rcx)
orb $-0x80, 0x3(%rax)
incq 0x78(%rsp)
jmp 0x71fb3
imulq $0x18, %rbx, %rax
movq 0x60(%rsp), %rcx
movl 0x8(%rcx,%rax), %edi
xorl $0x1, %r14d
movq 0xf0(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r14d, %esi
movq %r15, %rdx
movq %r13, %rcx
movq %r12, %r8
movl %ebp, %r9d
callq 0x71242
cmpq $0x1, %rax
movss 0x34(%rsp), %xmm4
movq 0x50(%rsp), %rbp
je 0x71fb3
jmp 0x720c7
callq 0x2220
cmpq $0x0, 0x78(%rsp)
je 0x72157
movq %r15, %rdi
callq 0x694eb
movq %r15, %rdi
callq 0x69638
movq 0x78(%rsp), %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq