// File: /workspace/github_cpp_makefile_star2_O3/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 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xab726
movl %eax, %r14d
xorl %r12d, %r12d
xorl %r10d, %r10d
movl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r8
movq 0x20(%rbx), %rax
movl %r12d, %ecx
shrl %ecx
cmpl $0x0, (%rax,%rcx,8)
js 0xab59e
movq 0x30(%rbx), %rax
cmpb $0x0, (%rax,%r12)
je 0xab5ab
incq %r12
cmpq %r14, %r12
jne 0xab584
jmp 0xab72e
movq 0x28(%rbx), %rax
movq (%rax,%r12,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0xab59e
movq %rax, %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
andq %rdx, %rcx
je 0xab59e
movq 0x8(%rbx), %rdx
movq %rax, %rsi
shrq $0x20, %rsi
leaq (%rsi,%rsi,2), %rsi
leaq (%rdx,%rsi,8), %r13
shlq $0x3, %rcx
leaq (%rcx,%rcx,2), %rcx
xorl %edx, %edx
xorl %esi, %esi
movl 0xc(%r13,%rdx), %edi
notl %edi
shrl $0x1f, %edi
addl %edi, %esi
addq $0x18, %rdx
cmpq %rdx, %rcx
jne 0xab5e6
cmpl $0x2, %esi
jb 0xab59e
movl %eax, %edx
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movq %rdx, 0x20(%rsp)
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
movslq 0xc(%r13,%rcx,8), %rax
testq %rax, %rax
js 0xab715
leaq (,%rcx,8), %r15
addq %r13, %r15
cmpl $-0x1, %edi
je 0xab679
cmpq %rax, %r10
jle 0xab715
movq %r10, 0x28(%rsp)
movl 0x8(%r15), %esi
movl %edi, 0xc(%rsp)
movq %rbx, %rdx
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r8
movq 0x40(%rsp), %r9
callq 0xaad4d
cmpq $0x1, %rax
jne 0xab6a1
movq 0x10(%rsp), %r8
movq 0x28(%rsp), %r10
jmp 0xab70c
cmpq %r8, %rax
jl 0xab59e
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
mulss 0x1c(%rsp), %xmm0
cvttss2si %xmm0, %r10
movl 0x8(%r15), %edi
cmpq %r8, %r10
cmovgeq %r8, %r10
jmp 0xab715
orb $-0x80, 0xf(%r15)
movl 0x8(%r15), %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 0x10(%rsp), %r8
movq 0x28(%rsp), %r10
je 0xab708
movl 0x4(%r15), %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 0xab6ff
orb $-0x80, 0x3(%rdx,%rsi)
addq $0x18, %rsi
cmpq %rsi, %rcx
jne 0xab6ef
incl 0x8(%rsp)
movq 0x20(%rsp), %rdx
movl 0xc(%rsp), %edi
incq %rbp
cmpq %rdx, %rbp
jne 0xab60e
jmp 0xab59e
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
movl 0x8(%rsp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq