// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_orthology_multiple_way(asg_t*, ma_hit_t_alloc*, float, long long, R_to_U*)
int asg_arc_del_orthology_multiple_way(asg_t *g, ma_hit_t_alloc* reverse_sources, float drop_ratio, 
long long miniedgeLen, R_to_U* ruIndex)
{
    double startTime = Get_T();
    ///the reason is that each read has two direction (query->target, target->query)
    uint32_t v, v_max, v_maxLen, n_vtx = g->n_seq * 2, n_reduced = 0;

    for (v = 0; v < n_vtx; ++v) 
    {
        if (g->seq_vis[v] != 0) continue;
        uint32_t i, n_arc = 0, nv = asg_arc_n(g, v);
        asg_arc_t *av = asg_arc_a(g, v);
        ///some node could be deleted
        if (nv < 2 || g->seq[v>>1].del) continue;
        n_arc = get_real_length(g, v, NULL);
        if (n_arc < 2) continue;
        v_max = (uint32_t)-1;
        v_maxLen = 0;

        for (i = 0, n_arc = 0; i < nv; i++)
        {
            if (!av[i].del)
            {
                if(v_max == (uint32_t)-1)
                {
                    v_max = av[i].v;
                    v_maxLen = av[i].ol;
                }
                else if(check_if_diploid(v_max, av[i].v, g, reverse_sources, miniedgeLen, ruIndex) == 0)
                {
                    if(av[i].ol < drop_ratio * v_maxLen)
                    {
                        ///fprintf(stderr, "v: %u, v_max: %u, av[%d].v: %u\n", v>>1, v_max>>1, i, av[i].v>>1);
                        
                        // av[i].ol = 1;///should be a bug
                        av[i].del = 1;
                        asg_arc_del(g, av[i].v^1, av[i].ul>>32^1, 1);
                        n_reduced++;
                    }
                }
            }
        }
    }

    if (n_reduced) {
        asg_cleanup(g);
        asg_symm(g);
    }

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %d different hap overlaps\n", 
        __func__, n_reduced);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }

    return n_reduced;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rcx, 0x28(%rsp)
movq %rdx, 0x20(%rsp)
movss %xmm0, 0x14(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xe9112
movq %rbx, %r8
movl %eax, %eax
movq %rax, 0x30(%rsp)
xorl %r14d, %r14d
movl $0x0, 0xc(%rsp)
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%r14)
je 0xe8f6e
incq %r14
cmpq 0x30(%rsp), %r14
jne 0xe8f54
jmp 0xe90e9
movq 0x28(%r8), %rax
movq (%rax,%r14,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xe8f5f
movq 0x20(%r8), %rcx
movl %r14d, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xe8f5f
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xe8f5f
movq 0x8(%r8), %rdx
movq %rax, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r15
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%r15,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xe8fb8
cmpl $0x2, %esi
jb 0xe8f5f
movl %eax, %ebx
xorl %ebp, %ebp
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movl $0x0, 0x10(%rsp)
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movl 0xc(%r15,%rcx,8), %eax
testl %eax, %eax
js 0xe90d8
leaq (%r15,%rcx,8), %r12
movl 0x8(%r12), %esi
cmpl $-0x1, %edi
je 0xe90d2
movl %edi, 0x8(%rsp)
movq %r8, %r13
movq %r8, %rdx
movq 0x18(%rsp), %rcx
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
callq 0xaad4d
testq %rax, %rax
jne 0xe90c9
movl 0xc(%r12), %eax
movl %eax, %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
xorps %xmm0, %xmm0
cvtsi2ss %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2ssl 0x10(%rsp), %xmm1
mulss 0x14(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0xe90c9
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, 0xc(%r12)
movl 0x8(%r12), %eax
xorq $0x1, %rax
movq %r13, %r8
movq 0x28(%r13), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0xe90c3
movl 0x4(%r12), %eax
movq 0x8(%r8), %rsi
shrq $0x20, %rdx
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rdx,%rdx,2), %rdx
leaq (%rsi,%rdx,8), %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %edi
xorl %eax, %edi
cmpl $0x1, %edi
jne 0xe90ba
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xe90aa
incl 0xc(%rsp)
jmp 0xe90cc
movq %r13, %r8
movl 0x8(%rsp), %edi
jmp 0xe90d8
movl %eax, 0x10(%rsp)
movl %esi, %edi
incq %rbp
cmpq %rbx, %rbp
jne 0xe8fe3
jmp 0xe8f5f
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
je 0xe9112
movq %r8, %rdi
movq %r8, %rbx
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xe9114
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq