// File: /workspace/github_cpp_makefile_star2_O1/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, 0x1c(%rsp)
movl 0x14(%rdi), %eax
addl %eax, %eax
movq %rdi, 0x8(%rsp)
je 0x10df23
movl %eax, %esi
xorl %r11d, %r11d
movabsq $-0x5555555555555555, %rbx # imm = 0xAAAAAAAAAAAAAAAB
xorl %r8d, %r8d
xorl %eax, %eax
movq $0x0, 0x10(%rsp)
movq %rsi, 0x20(%rsp)
movq 0x20(%rdi), %rcx
movl %r11d, %edx
shrl %edx
cmpb $0x1, 0x4(%rcx,%rdx,8)
je 0x10df15
cmpl $0x0, (%rcx,%rdx,8)
js 0x10df15
movq 0x28(%rdi), %rcx
movq (%rcx,%r11,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0x10df15
movq %rcx, %r13
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %r13
je 0x10df15
movq %r11, 0x28(%rsp)
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rsi
shlq $0x3, %rsi
addq 0x8(%rdi), %rsi
movq 0x10(%rsp), %r12
shlq $0x4, %r12
movq %rsi, %rbp
xorl %r15d, %r15d
movq %rsi, 0x30(%rsp)
movq 0x10(%rsp), %rcx
addq %r15, %rcx
cmpq %rax, %rcx
jne 0x10dec2
leaq (%rax,%rax), %r14
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %r14
movq %r14, %rsi
shlq $0x4, %rsi
movq %r8, %rdi
callq 0x2370
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdi
movq %rax, %r8
movq %r14, %rax
movq %rsi, %rcx
subq 0x8(%rdi), %rcx
sarq $0x3, %rcx
imulq %rbx, %rcx
addq %r15, %rcx
movq %rcx, (%r8,%r12)
movq (%rbp), %rcx
shlq $0x20, %rcx
movq %rcx, 0x8(%r8,%r12)
movl 0xc(%rbp), %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
orq %rcx, %rdx
movq %rdx, 0x8(%r8,%r12)
incq %r15
addq $0x10, %r12
addq $0x18, %rbp
cmpq %r15, %r13
jne 0x10de86
addq %r15, 0x10(%rsp)
movq 0x20(%rsp), %rsi
movq 0x28(%rsp), %r11
incq %r11
cmpq %rsi, %r11
jne 0x10de1a
jmp 0x10df2f
movq $0x0, 0x10(%rsp)
xorl %r8d, %r8d
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
addq %r8, %rsi
movq %r8, %rbp
movq %r8, %rdi
callq 0x10ddb2
testq %rbx, %rbx
je 0x10e0d5
movabsq $0x7fffffff00000000, %rax # imm = 0x7FFFFFFF00000000
xorl %ecx, %ecx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movq 0x8(%rsp), %r11
movq %rbp, %rdi
movq 0x8(%r11), %rsi
movq 0x28(%r11), %r9
movq %rcx, %r8
shlq $0x4, %r8
movq (%rdi,%r8), %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 0x10e0c2
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 0x10dff4
movq (%r11), %r14
movq %r14, %r13
shlq $0x20, %r13
andq %rax, %r13
orq %r12, %r13
movq %r13, %r12
negq %r12
testl %r14d, %r14d
cmovsq %r13, %r12
cmpq %r12, %r9
cmovleq %r12, %r9
testl %ebx, %ebx
cmoveq %r12, %r9
incl %ebx
addq $0x18, %r11
decq %r10
jne 0x10dfbf
cmpl $0x2, %ebx
jae 0x10e00c
movq 0x8(%rsp), %r11
jmp 0x10e0c2
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 0x10e059
testq %r9, %r9
js 0x10e059
xorps %xmm0, %xmm0
cvtsi2ss %r11, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %r9, %xmm1
mulss 0x1c(%rsp), %xmm1
ucomiss %xmm1, %xmm0
ja 0x10e002
orl $0x80000000, %r10d      # imm = 0x80000000
movl %r10d, 0xc(%rdi)
movl 0x8(%rdi), %edi
xorq $0x1, %rdi
movq 0x8(%rsp), %r11
movq 0x28(%r11), %r8
movq (%r8,%rdi,8), %r8
movq %r8, %rdi
andq %rdx, %rdi
je 0x10e0bf
movl 0x4(%rsi), %esi
movq 0x8(%r11), %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 0x10e0b6
orb $-0x80, 0x3(%r8,%r9)
addq $0x18, %r9
cmpq %r9, %rdi
jne 0x10e0a2
incq %r15
incq %rcx
cmpq 0x10(%rsp), %rcx
movq %rbp, %rdi
jne 0x10df6e
jmp 0x10e0db
xorl %r15d, %r15d
movq %rbp, %rdi
callq 0x2220
testq %r15, %r15
je 0x10e0fa
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa047e
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq