// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_length_trio(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_trio(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, trio_flag, non_trio_flag;
		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);
        kv = get_real_length(g, v, NULL);
        kw = get_real_length(g, w, NULL);
        if (kv <= 1 && kw <= 1) continue;
        trio_flag = get_tip_trio_infor(g, v^1);
        non_trio_flag = (uint32_t)-1;
        if(trio_flag == FATHER) non_trio_flag = MOTHER;
        if(trio_flag == MOTHER) non_trio_flag = FATHER;
        
        ///calculate the longest edge for v and w
		for (i = 0, kv = 0; i < nv; ++i) {
			if (av[i].del) continue;
            kv++; 
            if(get_tip_trio_infor(g, av[i].v) == non_trio_flag) 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;
            kw++;
            if (get_tip_trio_infor(g, aw[i].v) == non_trio_flag) 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;

        ///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 $0xc8, %rsp
movl %r9d, 0x30(%rsp)
movl %r8d, %ebp
movq %rcx, %r14
movq %rdx, %r15
movl %esi, %r12d
movss %xmm0, 0x4c(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xad2d3
movq %rbx, %rsi
movl %r12d, 0x70(%rsp)
movq %r15, 0xa0(%rsp)
movq %r14, 0x78(%rsp)
movl %ebp, 0x48(%rsp)
movl %eax, %eax
movq %rax, 0x68(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %ebx, %ebx
xorl %edi, %edi
movq %rsi, 0x20(%rsp)
cmpl $0x0, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
je 0xacaba
movq 0x30(%rsi), %rax
cmpb $0x0, (%rax,%rdx)
jne 0xacb71
cmpl $0x0, 0x100(%rsp)
je 0xacad7
movq 0x20(%rsi), %rax
movl %edx, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xacb71
movq 0x28(%rsi), %rax
movq (%rax,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xacb71
movq %rcx, %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r13
je 0xacb71
movq 0x8(%rsi), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbp
xorl %r14d, %r14d
cmpq %rbx, %r12
jne 0xacb3f
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 (%r14,%r14,2), %rcx
movl 0xc(%rbp,%rcx,8), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %rbp, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
addq %r14, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r14
cmpq %r13, %r14
jne 0xacb0b
movq 0x28(%rsp), %rdx
incq %rdx
cmpq 0x68(%rsp), %rdx
jne 0xacaa0
leaq (%rdi,%r12,8), %rsi
movq %rdi, 0xb0(%rsp)
callq 0x9dc69
testq %r12, %r12
je 0xad2e0
movq $0x0, 0xc0(%rsp)
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq 0x20(%rsp), %r10
movq 0x8(%r10), %r13
movq 0x28(%r10), %rcx
movq 0xb0(%rsp), %rax
movl (%rax,%rbx,8), %eax
leaq (%rax,%rax,2), %rax
movq (%r13,%rax,8), %rsi
shrq $0x20, %rsi
movl 0x8(%r13,%rax,8), %r15d
movq %r15, %rdx
xorq $0x1, %rdx
movq (%rcx,%rsi,8), %rdi
movq (%rcx,%rdx,8), %rcx
cmpl $0x1, %edi
jne 0xacbf9
cmpl $0x1, %ecx
je 0xad232
movq %rcx, 0x60(%rsp)
movq %rdi, %rcx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rbp
xorl %ecx, %ecx
movq %rdi, 0x58(%rsp)
movl $0x0, %edx
andq %r11, %rdi
movq %rdi, 0x28(%rsp)
je 0xacc57
leaq 0xc(,%rbp,8), %r14
addq %r13, %r14
movq 0x28(%rsp), %rdx
leaq (,%rdx,8), %rdx
leaq (%rdx,%rdx,2), %rdi
xorl %r8d, %r8d
xorl %edx, %edx
movl (%r14,%r8), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %edx
addq $0x18, %r8
cmpq %r8, %rdi
jne 0xacc40
movq 0x60(%rsp), %r8
movq %r8, %rdi
shrq $0x20, %rdi
leaq (%rdi,%rdi,2), %rdi
movq %rdi, 0x98(%rsp)
andq %r11, %r8
movq %r8, 0x68(%rsp)
je 0xaccb9
movq 0x98(%rsp), %rcx
leaq 0xc(,%rcx,8), %r14
addq %r13, %r14
movq 0x68(%rsp), %rcx
leaq (,%rcx,8), %rcx
leaq (%rcx,%rcx,2), %rdi
xorl %r8d, %r8d
xorl %ecx, %ecx
movl (%r14,%r8), %r9d
notl %r9d
shrl $0x1f, %r9d
addl %r9d, %ecx
addq $0x18, %r8
cmpq %r8, %rdi
jne 0xacca2
orl %edx, %ecx
cmpl $0x2, %ecx
movq 0x58(%rsp), %r14
jb 0xad232
movq %r15, 0x80(%rsp)
movq %rbx, 0x18(%rsp)
leaq (,%rax,8), %rax
addq %r13, %rax
movq %rax, 0xa8(%rsp)
xorl $0x1, %esi
movq %r10, %rdi
movq %rsi, 0x50(%rsp)
callq 0xac87a
xorl %ecx, %ecx
cmpb $0x1, %al
sete %cl
leal (%rcx,%rcx,2), %ecx
decl %ecx
cmpb $0x2, %al
movl $0x1, %eax
cmovel %eax, %ecx
movl %ecx, 0x30(%rsp)
xorps %xmm2, %xmm2
movl $0x0, 0x40(%rsp)
testl %r14d, %r14d
movq %r13, 0x90(%rsp)
movq %rbp, 0x88(%rsp)
je 0xacdf5
leaq 0xc(,%rbp,8), %rbx
addq %r13, %rbx
xorl %r13d, %r13d
movl $0x0, 0x44(%rsp)
xorl %ebp, %ebp
movl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %r10
movl (%rbx), %r14d
testl %r14d, %r14d
js 0xacd97
incl 0x3c(%rsp)
movl -0x4(%rbx), %esi
movq %r10, %rdi
callq 0xac87a
movzbl %al, %eax
cmpl %eax, 0x30(%rsp)
je 0xacd8f
cmpl %r14d, %ebp
cmovbel %r14d, %ebp
movl 0x44(%rsp), %eax
cmovbl %r13d, %eax
movl %eax, 0x44(%rsp)
movq 0x20(%rsp), %r10
xorps %xmm2, %xmm2
incq %r13
addq $0x18, %rbx
cmpq %r13, 0x28(%rsp)
jne 0xacd5c
cvtsi2ss %ebp, %xmm3
cmpl $0x2, 0x3c(%rsp)
movq 0x60(%rsp), %r8
jb 0xace16
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
cvtsi2ss %eax, %xmm0
movaps %xmm3, %xmm1
mulss 0x4c(%rsp), %xmm1
movb $0x1, %al
ucomiss %xmm1, %xmm0
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %rbp
movq 0x58(%rsp), %r14
jbe 0xace2d
jmp 0xad227
xorl %eax, %eax
movl $0x0, 0x44(%rsp)
xorps %xmm3, %xmm3
movl $0x0, 0x3c(%rsp)
movq 0x20(%rsp), %r10
movq 0x60(%rsp), %r8
jmp 0xace2d
xorl %eax, %eax
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %rbp
movq 0x58(%rsp), %r14
movl %eax, 0xbc(%rsp)
testl %r8d, %r8d
je 0xacf1f
movq 0x98(%rsp), %rax
leaq 0xc(%r13,%rax,8), %r13
xorl %ebp, %ebp
movl $0x0, 0x40(%rsp)
xorl %r14d, %r14d
xorl %r9d, %r9d
movss %xmm3, 0x74(%rsp)
movl (%r13), %ebx
testl %ebx, %ebx
js 0xacea3
incl %r9d
movl %r9d, %r15d
movl -0x4(%r13), %esi
movq %r10, %rdi
callq 0xac87a
movzbl %al, %eax
cmpl %eax, 0x30(%rsp)
je 0xace95
cmpl %ebx, %r14d
cmovbel %ebx, %r14d
movl 0x40(%rsp), %eax
cmovbl %ebp, %eax
movl %eax, 0x40(%rsp)
movq 0x20(%rsp), %r10
movss 0x74(%rsp), %xmm3
movl %r15d, %r9d
incq %rbp
addq $0x18, %r13
cmpq %rbp, 0x68(%rsp)
jne 0xace60
xorps %xmm2, %xmm2
cvtsi2ss %r14d, %xmm2
cmpl $0x2, %r9d
movq 0x50(%rsp), %rdi
movq 0x80(%rsp), %rsi
jb 0xacf3e
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movaps %xmm2, %xmm1
mulss 0x4c(%rsp), %xmm1
movb $0x1, %al
ucomiss %xmm1, %xmm0
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rbx
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %rbp
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %r14
jbe 0xacf65
jmp 0xad232
xorl %eax, %eax
xorl %r9d, %r9d
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rbx
movq 0x50(%rsp), %rdi
movq 0x80(%rsp), %rsi
jmp 0xacf65
xorl %eax, %eax
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rbx
movq 0x90(%rsp), %r13
movq 0x88(%rsp), %rbp
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %r14
movl 0x3c(%rsp), %ecx
orl %r9d, %ecx
cmpl $0x2, %ecx
jb 0xad232
testl %r14d, %r14d
movss 0x4c(%rsp), %xmm1
je 0xacfa6
leaq 0x8(,%rbp,8), %rdx
addq %r13, %rdx
xorl %ecx, %ecx
cmpl %esi, (%rdx)
je 0xacfa2
incq %rcx
addq $0x18, %rdx
cmpq %rcx, 0x28(%rsp)
jne 0xacf8d
movq %r14, %rcx
movl %ecx, %ecx
jmp 0xacfa8
xorl %ecx, %ecx
testl %r8d, %r8d
je 0xacfdb
movq 0x98(%rsp), %rdx
leaq 0x8(,%rdx,8), %rsi
addq %r13, %rsi
xorl %edx, %edx
cmpl %edi, (%rsi)
je 0xacfd7
incq %rdx
addq $0x18, %rsi
cmpq %rdx, 0x68(%rsp)
jne 0xacfc2
movq %r8, %rdx
movl %edx, %esi
jmp 0xacfdd
xorl %esi, %esi
leaq (,%rbp,8), %r14
addq %r13, %r14
movq 0x98(%rsp), %rdx
leaq (,%rdx,8), %rbx
addq %r13, %rbx
leaq (%rcx,%rcx,2), %r15
cmpl $0x0, 0xc(%r14,%r15,8)
jns 0xad016
leaq (%rsi,%rsi,2), %rcx
cmpl $0x0, 0xc(%rbx,%rcx,8)
js 0xad227
movl 0x44(%rsp), %ebp
movl 0x40(%rsp), %r13d
movl 0xbc(%rsp), %ecx
andb %al, %cl
cmpb $0x1, %cl
jne 0xad0d4
movq 0xa8(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
andl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss %xmm1, %xmm3
ucomiss %xmm0, %xmm3
jbe 0xad227
mulss %xmm1, %xmm2
ucomiss %xmm0, %xmm2
jbe 0xad227
cmpl $0x1, 0x108(%rsp)
jne 0xad20a
movq %rbx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
leaq (,%rbp,2), %rax
addq %rbp, %rax
movl 0x8(%r14,%rax,8), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x80(%rsp), %rsi
movq %r10, %rdx
movq 0xa0(%rsp), %rbx
movq %rbx, %rcx
movq 0x78(%rsp), %r8
movl 0x48(%rsp), %ebp
movl %ebp, %r9d
callq 0xaafd5
cmpq $0x1, %rax
jne 0xad240
movq 0x20(%rsp), %r10
jmp 0xad227
cmpl $0x1, %r9d
jne 0xad167
movq %rbx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %r10, %rdi
movq 0x80(%rsp), %r13
movl %r13d, %esi
movl 0x70(%rsp), %ebx
movl %ebx, %edx
callq 0xab75d
cmpl %ebx, %eax
setl %cl
cmpl $0x1, 0x108(%rsp)
jne 0xad1f7
cmpl %ebx, %eax
jge 0xad1f7
leaq (,%rbp,2), %rax
addq %rbp, %rax
movl 0x8(%r14,%rax,8), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movl %r13d, %esi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
movq 0xa0(%rsp), %rcx
movq 0x78(%rsp), %r8
movl 0x48(%rsp), %r9d
callq 0xaafd5
movq %rbx, %r10
cmpq $0x1, %rax
jmp 0xad1fe
cmpl $0x1, 0x3c(%rsp)
jne 0xad227
movq %rbx, 0x30(%rsp)
movq %rsi, 0x28(%rsp)
movq %r10, %rdi
movq 0x50(%rsp), %rsi
movl 0x70(%rsp), %ebx
movl %ebx, %edx
callq 0xab75d
cmpl %ebx, %eax
setl %cl
cmpl $0x1, 0x108(%rsp)
jne 0xad1f7
cmpl %ebx, %eax
jge 0xad1f7
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x30(%rsp), %r13
movl 0x8(%r13,%rax,8), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rsi
movq 0x20(%rsp), %rbx
movq %rbx, %rdx
movq 0xa0(%rsp), %rcx
movq 0x78(%rsp), %r8
movl 0x48(%rsp), %r9d
callq 0xaafd5
movq %rbx, %r10
movq %r13, %rbx
cmpq $0x1, %rax
jmp 0xad289
testb %cl, %cl
movq 0x20(%rsp), %r10
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rbx
je 0xad227
leaq (%r14,%r15,8), %rax
addq $0xc, %rax
leaq (%rsi,%rsi,2), %rcx
orb $-0x80, 0xf(%rbx,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0xc0(%rsp)
movl $0xffffffff, %r11d      # imm = 0xFFFFFFFF
movq 0x18(%rsp), %rbx
incq %rbx
cmpq %r12, %rbx
jne 0xacbb7
jmp 0xad296
leaq (,%r13,2), %rax
addq %r13, %rax
movq 0x30(%rsp), %r8
movl 0x8(%r8,%rax,8), %edi
movq 0x110(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x50(%rsp), %rsi
movq 0x20(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
movq %r8, %rbx
movq 0x78(%rsp), %r8
movl %ebp, %r9d
callq 0xaafd5
cmpq $0x1, %rax
movq %r13, %r10
movq 0x28(%rsp), %rsi
jne 0xad20a
jmp 0xad227
movq 0xb0(%rsp), %rdi
callq 0x2220
movq 0xc0(%rsp), %r14
testq %r14, %r14
je 0xad2f0
movq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xad2f3
xorl %ebx, %ebx
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
jmp 0xad2e8
movq 0xb0(%rsp), %rbx
movq %rbx, %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq