// File: 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
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movss %xmm0, -0x14(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
callq 0x2510
movsd %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movl 0x14(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
shll %eax
movl %eax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
cmpl -0x40(%rbp), %eax
jae 0x13fa17
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movl -0x34(%rbp), %ecx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x13f82b
jmp 0x13fa09
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl %eax, -0x50(%rbp)
movq -0x8(%rbp), %rax
movq 0x8(%rax), %rax
movq -0x8(%rbp), %rcx
movq 0x28(%rcx), %rcx
movl -0x34(%rbp), %edx
movq (%rcx,%rdx,8), %rcx
shrq $0x20, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x58(%rbp)
cmpl $0x2, -0x50(%rbp)
jb 0x13f88a
movq -0x8(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x34(%rbp), %ecx
shrl %ecx
movl %ecx, %ecx
movl (%rax,%rcx,8), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
je 0x13f88f
jmp 0x13fa09
movq -0x8(%rbp), %rdi
movl -0x34(%rbp), %esi
xorl %eax, %eax
movl %eax, %edx
callq 0x166b50
movl %eax, -0x4c(%rbp)
cmpl $0x2, -0x4c(%rbp)
jae 0x13f8ad
jmp 0x13fa09
movl $0xffffffff, -0x38(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x50(%rbp), %eax
jae 0x13fa07
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
shrl $0x1f, %eax
cmpl $0x0, %eax
jne 0x13f9f7
cmpl $-0x1, -0x38(%rbp)
jne 0x13f92a
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
movl %eax, -0x38(%rbp)
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movl %eax, -0x3c(%rbp)
jmp 0x13f9f5
movl -0x38(%rbp), %edi
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
movq -0x8(%rbp), %rdx
movq -0x10(%rbp), %rcx
movq -0x20(%rbp), %r8
movq -0x28(%rbp), %r9
callq 0xdbff0
cmpq $0x0, %rax
jne 0x13f9f3
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
leaq (%rcx,%rcx,2), %rcx
movl 0xc(%rax,%rcx,8), %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cvtsi2ss %eax, %xmm1
movss -0x14(%rbp), %xmm0
movl -0x3c(%rbp), %eax
cvtsi2ss %rax, %xmm2
mulss %xmm2, %xmm0
ucomiss %xmm1, %xmm0
jbe 0x13f9f1
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
andl $0x7fffffff, %ecx       # imm = 0x7FFFFFFF
orl $0x80000000, %ecx       # imm = 0x80000000
movl %ecx, 0xc(%rax)
movq -0x8(%rbp), %rdi
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x8(%rax), %esi
xorl $0x1, %esi
movq -0x58(%rbp), %rax
movl -0x48(%rbp), %ecx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
xorq $0x1, %rax
movl %eax, %edx
movl $0x1, %ecx
callq 0xd6530
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x13f9f3
jmp 0x13f9f5
jmp 0x13f9f7
jmp 0x13f9f9
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x13f8c9
jmp 0x13fa09
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x13f806
cmpl $0x0, -0x44(%rbp)
je 0x13fa2f
movq -0x8(%rbp), %rdi
callq 0xcdd10
movq -0x8(%rbp), %rdi
callq 0xce020
movl -0x44(%rbp), %eax
addq $0x60, %rsp
popq %rbp
retq
nopl (%rax,%rax)