// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: unitig_arc_del_short_diploid_by_length(asg_t*, float)
int unitig_arc_del_short_diploid_by_length(asg_t *g, float drop_ratio)
{
    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(g->seq[v>>1].c == ALTER_LABLE || g->seq[v>>1].del) 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, v = (a->ul)>>32;
		uint32_t nv = asg_arc_n(g, v), kv;
		uint32_t ov_max = 0;
		asg_arc_t *av = NULL;
		///nv must be >= 2
		if (nv <= 1) continue;
        av = asg_arc_a(g, v);


        ///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 <= 1) continue;
		if (kv >= 2 && a->ol > ov_max * drop_ratio) continue;
        a->del = 1;
        asg_arc_del(g, a->v^1, av->ul>>32^1, 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 $0x28, %rsp
movss %xmm0, 0x14(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movq %rbx, 0x8(%rsp)
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xac850
movl %eax, %eax
movq %rax, 0x18(%rsp)
xorl %edx, %edx
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r15 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %ebp, %ebp
xorl %edi, %edi
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rax
movl %edx, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
movq %rdx, 0x20(%rsp)
je 0xac6c8
cmpl $0x0, (%rax,%rcx,8)
js 0xac6c8
movq 0x8(%rsp), %rax
movq 0x28(%rax), %rax
movq (%rax,%rdx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xac6c8
movq %rcx, %r13
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r13
je 0xac6c8
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
xorl %r14d, %r14d
cmpq %rbp, %r12
jne 0xac697
leaq (%r12,%r12), %rbp
testq %r12, %r12
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
callq 0x2370
movl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
movq %rax, %rdi
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
leaq (%r14,%r14,2), %rcx
movl 0xc(%rbx,%rcx,8), %ecx
andl %esi, %ecx
shlq $0x20, %rcx
movq %rbx, %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 0xac664
movq 0x20(%rsp), %rdx
incq %rdx
cmpq 0x18(%rsp), %rdx
jne 0xac5ff
leaq (%rdi,%r12,8), %rsi
movq %rdi, %r14
callq 0x9dc69
testq %r12, %r12
je 0xac85d
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movss 0x14(%rsp), %xmm2
movq %r14, %rdi
movq 0x8(%rsp), %rdx
movq 0x8(%rdx), %r10
movq 0x28(%rdx), %rsi
movl (%rdi,%rax,8), %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x4(%r10,%rdx,8), %r8d
movq (%rsi,%r8,8), %r8
testl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
je 0xac817
leaq (%r10,%rdx,8), %rsi
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r9
leaq (%r10,%r9,8), %rdx
movl %r8d, %r8d
leaq (%r10,%r9,8), %r9
addq $0xc, %r9
xorl %edi, %edi
xorl %r10d, %r10d
movl %edi, %r11d
movl (%r9), %ebx
cmpl %ebx, %edi
movl %ebx, %edi
cmoval %r11d, %edi
xorl %ebp, %ebp
testl %ebx, %ebx
cmovsl %r11d, %edi
setns %bpl
addl %ebp, %r10d
addq $0x18, %r9
decq %r8
jne 0xac750
cmpl $0x2, %r10d
jb 0xac814
movl 0xc(%rsi), %r8d
movl %r8d, %r9d
andl $0x7fffffff, %r9d       # imm = 0x7FFFFFFF
xorps %xmm0, %xmm0
cvtsi2ss %r9d, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %edi, %xmm1
mulss %xmm2, %xmm1
ucomiss %xmm1, %xmm0
ja 0xac814
orl $0x80000000, %r8d       # imm = 0x80000000
movl %r8d, 0xc(%rsi)
movl 0x8(%rsi), %esi
xorq $0x1, %rsi
movq 0x8(%rsp), %rdi
movq 0x28(%rdi), %rdi
movq (%rdi,%rsi,8), %rdi
movq %rdi, %rsi
andq %rcx, %rsi
je 0xac811
movl 0x4(%rdx), %edx
movq 0x8(%rsp), %r8
movq 0x8(%r8), %r8
shrq $0x20, %rdi
shlq $0x3, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdi,%rdi,2), %rdi
leaq (%r8,%rdi,8), %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
movl -0x4(%rdi,%r8), %r9d
xorl %edx, %r9d
cmpl $0x1, %r9d
jne 0xac808
orb $-0x80, 0x3(%rdi,%r8)
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xac7f4
incq %r15
movq %r14, %rdi
incq %rax
cmpq %r12, %rax
jne 0xac703
callq 0x2220
testq %r15, %r15
je 0xac865
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xac868
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xac860
movq %r14, %rdi
callq 0x2220
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq