// File: /workspace/github_cpp_makefile_star2_O2/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 0x251c
movsd %xmm0, 0x18(%rsp)
movl 0x14(%r14), %ebp
addl %ebp, %ebp
xorl %ebx, %ebx
movl $0x0, 0x8(%rsp)
cmpq %rbp, %rbx
je 0xaaa81
movq 0x28(%r14), %rax
movq (%rax,%rbx,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xaaa79
movq 0x20(%r14), %rcx
movl %ebx, %edx
shrl %edx
cmpl $0x0, (%rcx,%rdx,8)
js 0xaaa79
movq 0x8(%r14), %rcx
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rsi
leaq (%rcx,%rsi), %r15
movl %eax, %eax
imulq $0x18, %rax, %rdx
addq %rsi, %rcx
addq $0xc, %rcx
xorl %esi, %esi
xorl %edi, %edi
cmpq %rsi, %rdx
je 0xaa9c9
movl (%rcx,%rsi), %r8d
notl %r8d
shrl $0x1f, %r8d
addl %r8d, %edi
addq $0x18, %rsi
jmp 0xaa9b0
cmpl $0x2, %edi
jne 0xaaa79
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0xaa9f1
cmpl $0x0, (%rcx)
js 0xaa9e8
movl %esi, %edi
movl %edx, 0x10(%rsp,%rdi,4)
incl %esi
incq %rdx
addq $0x18, %rcx
jmp 0xaa9d6
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 0x70fd8
testq %rax, %rax
jne 0xaaa79
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 0xaaa79
leaq (%r15,%r12), %rcx
addq $0xc, %rcx
orl $0x80000000, %eax       # imm = 0x80000000
movl %eax, (%rcx)
movl -0x8(%rcx), %edx
movl -0x4(%rcx), %esi
xorl $0x1, %esi
xorl $0x1, %edx
movq %r14, %rdi
pushq $0x1
popq %rcx
callq 0x6db50
incl 0x8(%rsp)
incq %rbx
jmp 0xaa95e
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
je 0xaaa99
movq %r14, %rdi
callq 0x694eb
movq %r14, %rdi
callq 0x69638
movq 0x1a8540(%rip), %rbx    # 0x252fe0
movq (%rbx), %rdi
leaq 0x13f789(%rip), %rsi    # 0x1ea233
leaq 0x13f7ad(%rip), %r14    # 0x1ea25e
movq %r14, %rdx
movl %ebp, %ecx
xorl %eax, %eax
callq 0x22b0
movq (%rbx), %r15
callq 0x251c
subsd 0x18(%rsp), %xmm0
leaq 0x13d61f(%rip), %rsi    # 0x1e80f1
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