// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_orthology(asg_t*, ma_hit_t_alloc*, float, long long, R_to_U*)
int asg_arc_del_orthology(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, n_vtx = g->n_seq * 2, n_reduced = 0;
    uint32_t idx[2];

    for (v = 0; v < n_vtx; ++v) 
    {
        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;
        ///some edges could be deleted
        for (i = 0; i < nv; ++i) // asg_bub_pop1() may delete some edges/arcs
            if (!av[i].del) ++n_arc;
        if (n_arc != 2) continue;

        for (i = 0, n_arc = 0; i < nv; i++)
        {
            if (!av[i].del)
            {
                idx[n_arc] = i;
                n_arc++;
            }
        }

        if(check_if_diploid(av[idx[0]].v, av[idx[1]].v, g, reverse_sources, miniedgeLen, ruIndex) == 0)
        {
            float max = av[idx[0]].ol;
            float min = av[idx[1]].ol;

            if(min < drop_ratio * max)
            {
                av[idx[1]].del = 1;
                asg_arc_del(g, av[idx[1]].v^1, av[idx[1]].ul>>32^1, 1);
                n_reduced++;
            }
        }
    }


    
    

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

    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, 0x30(%rsp)
movq %rdx, 0x28(%rsp)
movss %xmm0, 0xc(%rsp)
movq %rsi, 0x20(%rsp)
movq %rdi, %r14
callq 0x300fc
movsd %xmm0, 0x18(%rsp)
movl 0x14(%r14), %eax
movl $0x0, 0x8(%rsp)
addl %eax, %eax
je 0xcae1
movl %eax, %ebp
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xcad5
movq 0x20(%r14), %rcx
movl %ebx, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xcad5
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rcx,%rdx), %r15
movl %eax, %eax
addq %rdx, %rcx
addq $0xc, %rcx
xorl %edx, %edx
movq %rax, %rsi
movq %rcx, %rdi
movl (%rdi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edx
addq $0x18, %rdi
decq %rsi
jne 0xc9d1
cmpl $0x2, %edx
jne 0xcad5
xorl %edx, %edx
xorl %esi, %esi
cmpl $0x0, (%rcx)
js 0xca01
movl %esi, %edi
movl %edx, 0x10(%rsp,%rdi,4)
incl %esi
incq %rdx
addq $0x18, %rcx
cmpq %rdx, %rax
jne 0xc9f4
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
imulq $0x18, %rax, %r13
movl 0x8(%r15,%r13), %edi
imulq $0x18, %rcx, %r12
movl 0x8(%r15,%r12), %esi
movq %r14, %rdx
movq 0x20(%rsp), %rcx
movq 0x28(%rsp), %r8
movq 0x30(%rsp), %r9
callq 0xa9c9f
testq %rax, %rax
jne 0xcad5
movl 0xc(%r15,%r13), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cvtsi2ss %eax, %xmm0
movl 0xc(%r15,%r12), %eax
movl %eax, %ecx
andl %edx, %ecx
cvtsi2ss %ecx, %xmm1
mulss 0xc(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0xcad5
leaq (%r15,%r12), %rcx
addq $0xc, %rcx
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rcx)
movl -0x4(%rcx), %eax
xorq $0x1, %rax
movq 0x28(%r14), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
je 0xcad1
movl 0x4(%r15,%r12), %eax
movq 0x8(%r14), %rsi
shrq $0x20, %rdx
imulq $0x18, %rcx, %rcx
imulq $0x18, %rdx, %rdx
addq %rsi, %rdx
addq $0xc, %rdx
xorl %esi, %esi
movl -0x4(%rdx,%rsi), %edi
xorl %eax, %edi
cmpl $0x1, %edi
jne 0xcac8
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xcab8
incl 0x8(%rsp)
incq %rbx
cmpq %rbp, %rbx
jne 0xc988
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
je 0xcb06
movq %r14, %rdi
callq 0xa02fa
movq %r14, %rdi
callq 0xa0333
movq %r14, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%r14)
movq 0x27d4d3(%rip), %rbx    # 0x289fe0
movq (%rbx), %rdi
leaq 0x21f62c(%rip), %rsi    # 0x22c143
leaq 0x21f650(%rip), %r14    # 0x22c16e
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %r15
callq 0x300fc
subsd 0x18(%rsp), %xmm0
leaq 0x21d4c2(%rip), %rsi    # 0x22a001
movq %r15, %rdi
movq %r14, %rdx
movb $0x1, %al
callq 0x22b0
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq