// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movss %xmm0, 0x14(%rsp)
movq %rdi, %r14
callq 0x251c
movl 0x14(%r14), %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 %r14, 0x18(%rsp)
movq %rsi, 0x20(%rsp)
cmpq %rsi, %r8
movq %r9, 0x8(%rsp)
je 0x727a5
movq 0x20(%r14), %rax
movl %r8d, %edx
shrl %edx
cmpb $0x1, 0x4(%rax,%rdx,8)
je 0x72786
cmpl $0x0, (%rax,%rdx,8)
js 0x72786
movq 0x28(%r14), %rax
movq (%rax,%r8,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x72786
movq %r8, 0x28(%rsp)
movq 0x8(%r14), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %r14
movl %eax, %r13d
leaq (%rsi,%rdx), %rbx
addq $0xc, %rbx
leaq (,%r9,8), %rbp
xorl %r12d, %r12d
movq %r13, 0x30(%rsp)
leaq (%r9,%r12), %rax
cmpq %r12, %r13
je 0x72794
cmpq %rcx, %rax
jne 0x72757
movq %r14, %r13
leaq (%rcx,%rcx), %r14
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r14
leaq (,%r14,8), %rsi
movq %r11, %r15
callq 0x2370
movq %r15, %r11
movl $0x7fffffff, %r10d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r9
movq %rax, %rdi
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rsi
movq %r14, %rcx
movq %r13, %r14
movq 0x30(%rsp), %r13
movl (%rbx), %r8d
andl %r10d, %r8d
shlq $0x20, %r8
movq %r14, %rax
subq %rsi, %rax
cqto
idivq %r11
addq %r12, %rax
orq %r8, %rax
leaq (%rdi,%rbp), %rdx
movq %rax, (%rdx,%r12,8)
incq %r12
addq $0x18, %rbx
jmp 0x726ff
movq %r9, %rax
incq %r8
movq %rax, %r9
jmp 0x7268d
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %r8
jmp 0x72789
leaq (%rdi,%r9,8), %rsi
movq %rdi, %r12
callq 0x66c25
movq 0x8(%rsp), %r8
movq %r12, %rdi
xorl %r15d, %r15d
pushq $0x1
popq %rbp
xorl %r13d, %r13d
cmpq %r8, %r13
je 0x72894
movq 0x8(%r14), %rdx
movq 0x28(%r14), %rcx
movl (%rdi,%r13,8), %eax
imulq $0x18, %rax, %rax
movl 0x4(%rdx,%rax), %esi
movq (%rcx,%rsi,8), %rsi
testl $0xfffffffe, %esi       # imm = 0xFFFFFFFE
je 0x7288c
addq %rdx, %rax
movq %rsi, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rdi
leaq (%rdx,%rdi), %rcx
movl %esi, %esi
imulq $0x18, %rsi, %rsi
addq %rdx, %rdi
addq $0xc, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %edx, %edx
cmpq %r8, %rsi
je 0x7283e
movl (%rdi,%r8), %r10d
cmpl %r10d, %edx
movl %r10d, %r11d
cmoval %edx, %r11d
xorl %ebx, %ebx
testl %r10d, %r10d
setns %bl
cmovnsl %r11d, %edx
addl %ebx, %r9d
addq $0x18, %r8
jmp 0x72816
cmpl $0x2, %r9d
jb 0x72884
movl 0xc(%rax), %esi
movl %esi, %edi
andl $0x7fffffff, %edi       # imm = 0x7FFFFFFF
cvtsi2ss %edi, %xmm0
cvtsi2ss %edx, %xmm1
mulss 0x14(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x72884
orl $0x80000000, %esi       # imm = 0x80000000
movl %esi, 0xc(%rax)
movl 0x8(%rax), %esi
xorl $0x1, %esi
movl 0x4(%rcx), %edx
xorl $0x1, %edx
movq %r14, %rdi
movl %ebp, %ecx
callq 0x6db50
incq %r15
movq %r12, %rdi
movq 0x8(%rsp), %r8
incq %r13
jmp 0x727c2
callq 0x2220
testq %r15, %r15
je 0x728ae
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq