// File: /workspace/github_cpp_makefile_star2_O1/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, 0xc(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
addl %eax, %eax
movq %rbx, (%rsp)
je 0xab686
movq %rbx, %rsi
movl %eax, %eax
movq %rax, 0x10(%rsp)
xorl %r10d, %r10d
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
xorl %r12d, %r12d
xorl %eax, %eax
xorl %edi, %edi
movq 0x20(%rsi), %rcx
movl %r10d, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
movq %r10, 0x18(%rsp)
je 0xab671
cmpl $0x0, (%rcx,%rdx,8)
js 0xab671
movq 0x28(%rsi), %rcx
movq (%rcx,%r10,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xab671
movq %rcx, %r13
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r13
je 0xab671
movq 0x8(%rsi), %rdx
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rcx,8), %r11
leaq (%rdx,%rcx,8), %rbx
addq $0xc, %rbx
xorl %r14d, %r14d
cmpq %rax, %r12
jne 0xab640
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
movq %r12, 0x20(%rsp)
movq %r11, %r12
callq 0x2370
movq %r12, %r11
movq 0x20(%rsp), %r12
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq (%rsp), %rsi
movq %rax, %rdi
movq %r15, %rax
movl (%rbx), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %r11, %rdx
subq 0x8(%rsi), %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
addq %r14, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r12,8)
incq %r12
incq %r14
addq $0x18, %rbx
cmpq %r14, %r13
jne 0xab5fe
movq 0x18(%rsp), %r10
incq %r10
cmpq 0x10(%rsp), %r10
jne 0xab59b
jmp 0xab68b
xorl %edi, %edi
xorl %r12d, %r12d
leaq (%rdi,%r12,8), %rsi
movq %rdi, %r14
callq 0x9d53f
testq %r12, %r12
je 0xab7d3
xorl %eax, %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movq (%rsp), %r10
movss 0xc(%rsp), %xmm2
movq %r14, %rdi
movq 0x8(%r10), %r11
movq 0x28(%r10), %rsi
movl (%rdi,%rax,8), %edx
leaq (%rdx,%rdx,2), %rdx
movl 0x4(%r11,%rdx,8), %r8d
movq (%rsi,%r8,8), %r8
testl $0xfffffffe, %r8d       # imm = 0xFFFFFFFE
je 0xab7c5
leaq (%r11,%rdx,8), %rsi
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %r9
leaq (%r11,%r9,8), %rdx
movl %r8d, %r8d
leaq (%r11,%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 0xab6ff
cmpl $0x2, %r10d
jae 0xab734
movq (%rsp), %r10
jmp 0xab7c2
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
movq (%rsp), %r10
ja 0xab7c2
orl $0x80000000, %r8d       # imm = 0x80000000
movl %r8d, 0xc(%rsi)
movl 0x8(%rsi), %esi
xorq $0x1, %rsi
movq 0x28(%r10), %rdi
movq (%rdi,%rsi,8), %rdi
movq %rdi, %rsi
andq %rcx, %rsi
je 0xab7bf
movl 0x4(%rdx), %edx
movq 0x8(%r10), %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 0xab7b6
orb $-0x80, 0x3(%rdi,%r8)
addq $0x18, %r8
cmpq %r8, %rsi
jne 0xab7a2
incq %r15
movq %r14, %rdi
incq %rax
cmpq %r12, %rax
jne 0xab6b7
jmp 0xab7d9
xorl %r15d, %r15d
movq %r14, %rdi
callq 0x2220
testq %r15, %r15
je 0xab803
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movl %r15d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq