// File: /workspace/github_cpp_makefile_star2_O1/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_too_short_overlaps(asg_t*, long long, float, ma_hit_t_alloc*, long long, R_to_U*)
int asg_arc_del_too_short_overlaps(asg_t *g, long long dropLen, float drop_ratio, 
ma_hit_t_alloc* reverse_sources, long long min_edge_length, R_to_U* ruIndex)
{
    double startTime = Get_T();

	uint32_t v, v_max, v_maxLen, n_vtx = g->n_seq * 2, n_short = 0;
    long long drop_ratio_Len = 0;
    
    for (v = 0; v < n_vtx; ++v) 
    {
        if (g->seq[v>>1].del) continue;
        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;

        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;
                    if(v_maxLen < dropLen) break;
                    drop_ratio_Len = v_maxLen * drop_ratio;
                    if(dropLen < drop_ratio_Len)
                    {
                        drop_ratio_Len = dropLen;
                    }
                }
                else if(av[i].ol < drop_ratio_Len && 
                check_if_diploid(v_max, av[i].v, g, reverse_sources, min_edge_length, ruIndex) != 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_short;
                }
            }
        }
    }


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

	return n_short;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r8, 0x40(%rsp)
movq %rcx, 0x38(%rsp)
movq %rdx, 0x30(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %rsi, 0x10(%rsp)
movq %rdi, %rbx
callq 0x300fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xaa747
movl %eax, %r8d
xorl %r12d, %r12d
xorl %r9d, %r9d
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r10
movq %r8, 0x20(%rsp)
movq 0x20(%rbx), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xaa739
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%r12)
jne 0xaa739
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %r13
cmpl $0x2, %r13d
jb 0xaa739
movq %r13, %rax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
andq %rcx, %rax
je 0xaa739
movq 0x8(%rbx), %rcx
movq %r13, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rdx
leaq (%rcx,%rdx,8), %r15
shlq $0x3, %rax
leaq (%rax,%rax,2), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movl 0xc(%r15,%rcx), %esi
notl %esi
shrl $0x1f, %esi
addl %esi, %edx
addq $0x18, %rcx
cmpq %rcx, %rax
jne 0xaa5fb
cmpl $0x2, %edx
jb 0xaa739
cmpl $0x1, %r13d
adcl $0x0, %r13d
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movslq 0xc(%r15,%rcx,8), %rax
testq %rax, %rax
js 0xaa72d
leaq (%r15,%rcx,8), %r14
cmpl $-0x1, %edi
je 0xaa691
cmpq %rax, %r9
jle 0xaa72d
movq %r9, 0x28(%rsp)
movl 0x8(%r14), %esi
movl %edi, 0xc(%rsp)
movq %rbx, %rdx
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0xa9c9f
cmpq $0x1, %rax
jne 0xaa6b9
movq 0x10(%rsp), %r10
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
jmp 0xaa729
cmpq %r10, %rax
jl 0xaa739
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x1c(%rsp), %xmm0
cvttss2si %xmm0, %r9
movl 0x8(%r14), %edi
cmpq %r10, %r9
cmovgeq %r10, %r9
jmp 0xaa72d
orb $-0x80, 0xf(%r14)
movl 0x8(%r14), %eax
xorq $0x1, %rax
movq 0x28(%rbx), %rcx
movq (%rcx,%rax,8), %rdx
movq %rdx, %rcx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %rcx
movq 0x20(%rsp), %r8
movq 0x28(%rsp), %r9
je 0xaa720
movl 0x4(%r14), %eax
movq 0x8(%rbx), %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 0xaa717
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xaa707
incl 0x8(%rsp)
movq 0x10(%rsp), %r10
movl 0xc(%rsp), %edi
incq %rbp
cmpq %r13, %rbp
jne 0xaa628
incq %r12
cmpq %r8, %r12
jne 0xaa597
jmp 0xaa74f
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
callq 0xa02fa
movq %rbx, %rdi
callq 0xa0333
movq %rbx, %rdi
callq 0xa03e9
orb $-0x80, 0x17(%rbx)
movl 0x8(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq