// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movss %xmm0, 0x14(%rsp)
movq %rdi, (%rsp)
movl 0x14(%rdi), %eax
addl %eax, %eax
je 0x1130dd
movl %eax, %esi
xorl %r9d, %r9d
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
xorl %r14d, %r14d
xorl %eax, %eax
xorl %r8d, %r8d
movq %rsi, 0x18(%rsp)
movq (%rsp), %rcx
movq 0x20(%rcx), %rcx
movl %r9d, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x112f04
cmpl $0x0, (%rcx,%rdx,8)
js 0x112f04
movq (%rsp), %rcx
movq 0x28(%rcx), %rcx
movq (%rcx,%r9,8), %rdx
testl $0xfffffffe, %edx       # imm = 0xFFFFFFFE
je 0x112f04
movq %rdx, %r13
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rcx, %r13
je 0x112f04
movq %r9, 0x20(%rsp)
movq (%rsp), %rcx
movq 0x8(%rcx), %rcx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r9
movq %r8, %r12
shlq $0x4, %r12
movq %r9, %rbp
xorl %ebx, %ebx
movq %r9, 0x8(%rsp)
leaq (%r8,%rbx), %rdx
cmpq %rax, %rdx
jne 0x112eb0
leaq (%rax,%rax), %r15
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq %r14, %rdi
movq %r8, %r14
callq 0x2370
movq 0x8(%rsp), %r9
movabsq $-0x5555555555555555, %rdi # imm = 0xAAAAAAAAAAAAAAAB
movq %r14, %r8
movq %rax, %r14
movq (%rsp), %rax
movq 0x8(%rax), %rcx
movq %r15, %rax
movq %r9, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %rdi, %rdx
addq %rbx, %rdx
movq %rdx, (%r14,%r12)
movq (%rbp), %rdx
shlq $0x20, %rdx
movq %rdx, 0x8(%r14,%r12)
movl 0xc(%rbp), %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
orq %rdx, %rsi
movq %rsi, 0x8(%r14,%r12)
incq %rbx
addq $0x10, %r12
addq $0x18, %rbp
cmpq %rbx, %r13
jne 0x112e65
addq %rbx, %r8
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %r9
incq %r9
cmpq %rsi, %r9
jne 0x112df0
movq %r8, %rsi
shlq $0x4, %rsi
addq %r14, %rsi
movq %r14, %rdi
movq %r8, %r15
callq 0x112d8f
testq %r15, %r15
je 0x1130e9
movq %r15, %r11
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
movq $0x0, 0x8(%rsp)
movq (%rsp), %rdi
movq 0x8(%rdi), %rsi
movq 0x28(%rdi), %r9
movq %rcx, %rdi
shlq $0x4, %rdi
movq (%r14,%rdi), %rdi
leaq (%rdi,%rdi,2), %rdi
movq (%rsi,%rdi,8), %r8
movq %r8, %r10
shrq $0x20, %r10
movq (%r9,%r10,8), %r9
testl $0xfffffffe, %r9d       # imm = 0xFFFFFFFE
je 0x1130a9
leaq (%rsi,%rdi,8), %rdi
movq %r9, %r10
shrq $0x20, %r10
leaq (%r10,%r10,2), %r10
leaq (%rsi,%r10,8), %rsi
movl %r9d, %r10d
xorl %r9d, %r9d
movq %rsi, %r11
xorl %ebx, %ebx
movslq 0xc(%r11), %r12
testq %r12, %r12
js 0x112fd5
movq (%r11), %r13
movq %r13, %rbp
shlq $0x20, %rbp
andq %rax, %rbp
orq %r12, %rbp
movq %rbp, %r12
negq %r12
testl %r13d, %r13d
cmovsq %rbp, %r12
cmpq %r12, %r9
cmovleq %r12, %r9
testl %ebx, %ebx
cmoveq %r12, %r9
incl %ebx
addq $0x18, %r11
decq %r10
jne 0x112fa0
cmpl $0x2, %ebx
jae 0x112feb
movq %r15, %r11
jmp 0x1130a9
movl 0xc(%rdi), %r10d
movl %r10d, %r11d
andl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq %r8, %rbx
shlq $0x20, %rbx
andq %rax, %rbx
orq %r11, %rbx
movq %rbx, %r11
negq %r11
testl %r8d, %r8d
cmovsq %rbx, %r11
testq %r11, %r11
js 0x113038
testq %r9, %r9
js 0x113038
xorps %xmm0, %xmm0
cvtsi2ss %r11, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r9, %xmm1
mulss 0x14(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x112fe3
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%rdi)
movl 0x8(%rdi), %edi
xorq $0x1, %rdi
movq (%rsp), %r8
movq 0x28(%r8), %r8
movq (%r8,%rdi,8), %r8
movq %r8, %rdi
andq %rdx, %rdi
movq %r15, %r11
je 0x1130a4
movl 0x4(%rsi), %esi
movq (%rsp), %r9
movq 0x8(%r9), %r9
shrq $0x20, %r8
shlq $0x3, %rdi
leaq (%rdi,%rdi,2), %rdi
leaq (%r8,%r8,2), %r8
leaq (%r9,%r8,8), %r8
addq $0xc, %r8
xorl %r9d, %r9d
movl -0x4(%r8,%r9), %r10d
xorl %esi, %r10d
cmpl $0x1, %r10d
jne 0x11309b
orb $-0x80, 0x3(%r8,%r9)
addq $0x18, %r9
cmpq %r9, %rdi
jne 0x113087
incq 0x8(%rsp)
incq %rcx
cmpq %r11, %rcx
jne 0x112f4b
movq %r14, %rdi
callq 0x2220
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x1130f1
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0c1e
jmp 0x1130f4
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
callq 0x112d8f
movq %r14, %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq