// File: /workspace/github_cpp_makefile_star2_O2/chhylp123[P]hifiasm/Purge_Dups.cpp
// Function: purge_g_arc_del_short_diploid_by_score(asg_t*, float)
int purge_g_arc_del_short_diploid_by_score(asg_t *g, float drop_ratio)
{
    e_score_warp b;
    kv_init(b);
    e_score *p = NULL;

	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_pushp(e_score, b, &p);
            p->eid = av - g->arc + i;
            p->score = (uint32_t)av[i].ul; 
            p->score <<= 32;
            p->score |= av[i].ol;
        }
	}

    radix_sort_e_score(b.a, b.a + b.n);

    uint64_t k;
    for (k = 0; k < b.n; k++)
    {
        asg_arc_t *a = &g->arc[b.a[k].eid];
		///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;
		long long ovlp_max = 0, ovlp;
		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;
            ovlp = decode_score((uint32_t)av[i].ul, av[i].ol);
            if (kv == 0 || ovlp_max < ovlp) ovlp_max = ovlp;
			++kv;
		}

        if (kv <= 1) continue;
        ovlp = decode_score((uint32_t)a->ul, a->ol);
		if (kv >= 2)
        {   
            if(ovlp >= 0 && ovlp_max >= 0 && ovlp > ovlp_max * drop_ratio) continue;
        }
         
        a->del = 1;
        asg_arc_del(g, a->v^1, av->ul>>32^1, 1);
        ++n_cut; 
    }

    kv_destroy(b); 
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    
	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, %r12
movl 0x14(%rdi), %edx
addl %edx, %edx
xorl %edi, %edi
pushq $0x18
popq %r8
xorl %esi, %esi
xorl %ebp, %ebp
xorl %r14d, %r14d
movq %r12, 0x18(%rsp)
movq %rdx, 0x20(%rsp)
cmpq %rdx, %rdi
movq %rsi, 0x8(%rsp)
je 0xcf140
movq 0x20(%r12), %rax
movl %edi, %ecx
shrl %ecx
cmpb $0x1, 0x4(%rax,%rcx,8)
je 0xcf121
cmpl $0x0, (%rax,%rcx,8)
js 0xcf121
movq 0x28(%r12), %rax
movq (%rax,%rdi,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xcf121
movq %rdi, 0x28(%rsp)
movq 0x8(%r12), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %r13
addq %rcx, %r13
movl %eax, %edi
leaq (%rsi,%rsi), %r12
movq %r13, %r15
xorl %ebx, %ebx
movq %rdi, 0x30(%rsp)
leaq (%rsi,%rbx), %rax
cmpq %rbx, %rdi
je 0xcf12f
cmpq %rbp, %rax
jne 0xcf0e1
testq %rax, %rax
movq %r12, %rbp
pushq $0x2
popq %rax
cmoveq %rax, %rbp
movq %rbp, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
movq %r8, %r14
callq 0x2370
movq 0x30(%rsp), %rdi
movq %r14, %r8
movq 0x8(%rsp), %rsi
movq %rax, %r14
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rcx
movq %r13, %rax
subq %rcx, %rax
cqto
idivq %r8
addq %rbx, %rax
movq %rax, (%r14,%r12,8)
movq (%r15), %rax
shlq $0x20, %rax
movq %rax, 0x8(%r14,%r12,8)
movl 0xc(%r15), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orq %rax, %rdx
movq %rdx, 0x8(%r14,%r12,8)
incq %rbx
addq $0x18, %r15
addq $0x2, %r12
jmp 0xcf097
movq %rsi, %rax
incq %rdi
movq %rax, %rsi
jmp 0xcf02f
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rdx
movq 0x28(%rsp), %rdi
jmp 0xcf124
movabsq $0x7fffffff00000000, %r13 # imm = 0x7FFFFFFF00000000
shlq $0x4, %rsi
addq %r14, %rsi
movq %r14, %rdi
callq 0xcefde
movq 0x8(%rsp), %r8
xorl %r15d, %r15d
xorl %ebp, %ebp
cmpq %r8, %rbp
je 0xcf278
movq 0x8(%r12), %rsi
movq 0x28(%r12), %rcx
movq %rbp, %rax
shlq $0x4, %rax
imulq $0x18, (%r14,%rax), %rax
movq (%rsi,%rax), %rdx
movq %rdx, %rdi
shrq $0x20, %rdi
movq (%rcx,%rdi,8), %rdi
testl $0xfffffffe, %edi       # imm = 0xFFFFFFFE
je 0xcf270
addq %rsi, %rax
movq %rdi, %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rsi, %rcx
movl %edi, %esi
imulq $0x18, %rsi, %rdi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %esi, %esi
cmpq %r8, %rdi
je 0xcf200
movslq 0xc(%rcx,%r8), %r10
testq %r10, %r10
js 0xcf1fa
movq (%rcx,%r8), %r11
movq %r11, %rbx
shlq $0x20, %rbx
andq %r13, %rbx
orq %r10, %rbx
movq %rbx, %r10
negq %r10
testl %r11d, %r11d
cmovsq %rbx, %r10
cmpq %r10, %rsi
cmovleq %r10, %rsi
testl %r9d, %r9d
cmoveq %r10, %rsi
incl %r9d
addq $0x18, %r8
jmp 0xcf1bc
cmpl $0x2, %r9d
jb 0xcf26b
movl 0xc(%rax), %edi
movl %edi, %r8d
andl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq %rdx, %r9
shlq $0x20, %r9
andq %r13, %r9
orq %r8, %r9
movq %r9, %r8
negq %r8
testl %edx, %edx
cmovsq %r9, %r8
testq %r8, %r8
js 0xcf24b
testq %rsi, %rsi
js 0xcf24b
cvtsi2ss %r8, %xmm0
cvtsi2ss %rsi, %xmm1
mulss 0x14(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0xcf26b
orl $0x80000000, %edi       # imm = 0x80000000
movl %edi, 0xc(%rax)
movl 0x8(%rax), %esi
xorl $0x1, %esi
movl 0x4(%rcx), %edx
xorl $0x1, %edx
movq %r12, %rdi
callq 0xcf2a7
incq %r15
movq 0x8(%rsp), %r8
incq %rbp
jmp 0xcf163
movq %r14, %rdi
callq 0x2220
testq %r15, %r15
je 0xcf295
movq %r12, %rdi
callq 0x694eb
movq %r12, %rdi
callq 0x69638
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq