// File: /workspace/github_cpp_makefile_star2_O3/chhylp123[P]hifiasm/Overlaps.cpp
// Function: asg_arc_del_short_diploid_by_exact(asg_t*, int, ma_hit_t_alloc*)
int asg_arc_del_short_diploid_by_exact(asg_t *g, int max_ext, ma_hit_t_alloc* sources)
{
    double startTime = Get_T();
    kvec_t(uint64_t) b;
    memset(&b, 0, sizeof(b));

	uint32_t v, n_vtx = g->n_seq * 2;
    long long n_cut = 0;
    
    for (v = 0; v < n_vtx; ++v) 
    {
        if(g->seq_vis[v] == 0)
        {
            asg_arc_t *av = asg_arc_a(g, v);
            uint32_t nv = asg_arc_n(g, v);
            if (nv < 2) continue;
            long long i;
            for (i = 0; i < nv; ++i)
            {
                kv_push(uint64_t, b, (uint64_t)((uint64_t)av[i].ol << 32 | (av - g->arc + i)));   
            }
        }
	}

    radix_sort_arch64(b.a, b.a + b.n);

    uint64_t k;
    for (k = 0; k < b.n; k++)
    {
        
        asg_arc_t *a = &g->arc[(uint32_t)b.a[k]];
		///v is self id, w is the id of another end
		uint32_t i, iv, iw, v = (a->ul)>>32, w = a->v^1, to_del = 0;
		uint32_t nv = asg_arc_n(g, v), nw = asg_arc_n(g, w), kv, kw;
		uint32_t ov_max = 0, ow_max = 0, ov_max_i = 0;
		asg_arc_t *av, *aw;
		///nv must be >= 2
		if (nv == 1 && nw == 1) continue;
        av = asg_arc_a(g, v);
		aw = asg_arc_a(g, w);


        ///calculate the longest edge for v and w
		for (i = 0, kv = 0; i < nv; ++i) {
			if (av[i].del) continue;
			if (ov_max < av[i].ol) 
            {
                ov_max = av[i].ol;
                ov_max_i = i;
            }
			++kv;
		}
		if (kv >= 2 && a->ol == ov_max) continue;


		for (i = 0, kw = 0; i < nw; ++i) {
			if (aw[i].del) continue;
			if (ow_max < aw[i].ol) 
            {
                ow_max = aw[i].ol;
            }
			++kw;
		}
		if (kw >= 2 && a->ol == ow_max) continue;

		///if (kv == 1 && kw == 1) continue;
        if (kv <= 1 && kw <= 1) continue;


        ///to see which one is the current edge (from v and w)
		for (iv = 0; iv < nv; ++iv)
			if (av[iv].v == (w^1)) break;
		for (iw = 0; iw < nw; ++iw)
			if (aw[iw].v == (v^1)) break;
        ///if one edge has been deleted, it should be deleted in both direction
        if (av[iv].del && aw[iw].del) continue;


        ///if this edge is an inexact edge
        if(a->el == 0 && 
        sources[v>>1].is_fully_corrected == 1 && 
        sources[w>>1].is_fully_corrected == 1)
        {
            if (kv > 1 && kw > 1) {
				to_del = 1;
            } else if (kw == 1) {
                if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
            } else if (kv == 1) {
                if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
            }
        }

        if(a->el == 0 && 
        sources[v>>1].is_fully_corrected == 1 && 
        sources[w>>1].is_fully_corrected == 0)
        {
            /****************************may have bugs********************************/
            ///if(av[ov_max_i].el == 1 && sources[av[ov_max_i].v>>1].is_fully_corrected)
            /****************************may have bugs********************************/
            if(av[ov_max_i].el == 1 && sources[av[ov_max_i].v>>1].is_fully_corrected == 1)
            {
                if (kv > 1 && kw > 1) {
                    to_del = 1;
                } else if (kw == 1) {
                    if (asg_topocut_aux(g, w^1, max_ext) < max_ext) to_del = 1;
                } else if (kv == 1) {
                    if (asg_topocut_aux(g, v^1, max_ext) < max_ext) to_del = 1;
                }
            }
        }


        
		if (to_del)
			av[iv].del = aw[iw].del = 1, ++n_cut;
        
    }
    
    free(b.a);
	if (n_cut) 
    {
		asg_cleanup(g);
		asg_symm(g);
	}

    if(VERBOSE >= 1)
    {
        fprintf(stderr, "[M::%s] removed %lld inexact overlaps\n", __func__, n_cut);
        fprintf(stderr, "[M::%s] takes %0.2f s\n\n", __func__, Get_T()-startTime);
    }
	return n_cut;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rdx, 0x60(%rsp)
movl %esi, %ebp
movq %rdi, %rbx
callq 0x2f3fc
movl 0x14(%rbx), %eax
addl %eax, %eax
je 0xafca2
movq %rbx, %rsi
movl %ebp, 0x30(%rsp)
movl %eax, %eax
movq %rax, 0x28(%rsp)
xorl %ecx, %ecx
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movabsq $-0x5555555555555555, %r13 # imm = 0xAAAAAAAAAAAAAAAB
xorl %r14d, %r14d
xorl %ebp, %ebp
xorl %edi, %edi
movq %rbx, 0x8(%rsp)
movq 0x30(%rsi), %rax
cmpb $0x0, (%rax,%rcx)
movq %rcx, 0x10(%rsp)
je 0xaf7ac
movq 0x10(%rsp), %rcx
incq %rcx
cmpq 0x28(%rsp), %rcx
jne 0xaf789
jmp 0xaf847
movq 0x28(%rsi), %rax
movq (%rax,%rcx,8), %rcx
testl $0xfffffffe, %ecx       # imm = 0xFFFFFFFE
je 0xaf798
movq %rcx, %r12
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
andq %rax, %r12
je 0xaf798
movq 0x8(%rsi), %rax
shrq $0x20, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%rax,%rcx,8), %rbx
xorl %r15d, %r15d
cmpq %rbp, %r14
jne 0xaf810
leaq (%r14,%r14), %rbp
testq %r14, %r14
movl $0x2, %eax
cmoveq %rax, %rbp
leaq (,%rbp,8), %rsi
callq 0x2370
movl $0x7fffffff, %r8d       # imm = 0x7FFFFFFF
movq 0x8(%rsp), %rsi
movq %rax, %rdi
movq 0x8(%rsi), %rax
leaq (%r15,%r15,2), %rcx
movl 0xc(%rbx,%rcx,8), %ecx
andl %r8d, %ecx
shlq $0x20, %rcx
movq %rbx, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %r13, %rdx
addq %r15, %rdx
orq %rcx, %rdx
movq %rdx, (%rdi,%r14,8)
incq %r14
incq %r15
cmpq %r12, %r15
jne 0xaf7dc
jmp 0xaf798
leaq (%rdi,%r14,8), %rsi
movq %rdi, 0x18(%rsp)
callq 0x9dc69
testq %r14, %r14
je 0xafcaf
movq $0x0, 0x58(%rsp)
xorl %r10d, %r10d
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
movq 0x8(%rdi), %rcx
movq 0x28(%rdi), %rsi
movl (%r9,%r10,8), %eax
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %r11
movq %r11, %r13
shrq $0x20, %r13
movl 0x8(%rcx,%rdx,8), %r15d
movq %r15, %rax
xorq $0x1, %rax
movq (%rsi,%r13,8), %r8
movq (%rsi,%rax,8), %rsi
cmpl $0x1, %r8d
jne 0xaf8b2
cmpl $0x1, %esi
je 0xafc61
movq %r11, 0x50(%rsp)
movq %r10, 0x20(%rsp)
leaq (%rcx,%rdx,8), %rax
movq %rax, 0x38(%rsp)
movq %r8, %rdx
shrq $0x20, %rdx
leaq (%rdx,%rdx,2), %rax
xorl %ebp, %ebp
testl %r8d, %r8d
movq %r15, 0x10(%rsp)
movq %rsi, 0x28(%rsp)
movq %rax, 0x48(%rsp)
je 0xaf963
movq %r13, %rdi
movl %r8d, %r9d
leaq (%rcx,%rax,8), %r10
addq $0xc, %r10
xorl %r11d, %r11d
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebx, %ebx
movl %r15d, %edx
movl (%r10), %esi
cmpl %esi, %r15d
movl %esi, %r15d
cmoval %edx, %r15d
movl %r12d, %r13d
movl %r11d, %r12d
cmovael %r13d, %r12d
xorl %eax, %eax
testl %esi, %esi
setns %al
cmovsl %edx, %r15d
cmovsl %r13d, %r12d
addl %eax, %ebx
incq %r11
addq $0x18, %r10
cmpq %r11, %r9
jne 0xaf8ff
cmpl $0x2, %ebx
jb 0xaf96c
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movb $0x1, %dl
cmpl %r15d, %eax
movq %rdi, %r13
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %rsi
jne 0xaf97b
jmp 0xafa10
xorl %edx, %edx
xorl %r12d, %r12d
xorl %ebx, %ebx
jmp 0xaf97b
xorl %edx, %edx
movq %rdi, %r13
movq 0x10(%rsp), %r15
movq 0x28(%rsp), %rsi
movl %edx, 0x44(%rsp)
movq %rsi, %rax
shrq $0x20, %rax
leaq (%rax,%rax,2), %r9
testl %esi, %esi
je 0xaf9f9
movq %r13, %rdi
movl %esi, %eax
leaq (%rcx,%r9,8), %r10
addq $0xc, %r10
shlq $0x3, %rax
leaq (%rax,%rax,2), %r15
xorl %edx, %edx
xorl %r11d, %r11d
xorl %ebp, %ebp
movl %r11d, %eax
movl (%r10,%rdx), %esi
cmpl %esi, %r11d
movl %esi, %r11d
cmoval %eax, %r11d
xorl %r13d, %r13d
testl %esi, %esi
cmovsl %eax, %r11d
setns %r13b
addl %r13d, %ebp
addq $0x18, %rdx
cmpq %rdx, %r15
jne 0xaf9aa
cmpl $0x2, %ebp
jb 0xaf9fd
movq 0x38(%rsp), %rax
movl 0xc(%rax), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
movb $0x1, %dl
cmpl %r11d, %eax
movq %rdi, %r13
movq 0x10(%rsp), %r15
jne 0xafa07
jmp 0xafa10
xorl %edx, %edx
jmp 0xafa07
xorl %edx, %edx
movq %rdi, %r13
movq 0x10(%rsp), %r15
movl %ebx, %eax
orl %ebp, %eax
cmpl $0x2, %eax
jae 0xafa1f
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
jmp 0xafc5c
movl %edx, 0x34(%rsp)
testl %r8d, %r8d
movq 0x28(%rsp), %rsi
je 0xafa58
movl %r8d, %r10d
movq 0x48(%rsp), %rax
leaq (%rcx,%rax,8), %r11
addq $0x8, %r11
xorl %edx, %edx
cmpl %r15d, (%r11)
je 0xafa53
incq %rdx
addq $0x18, %r11
cmpq %rdx, %r10
jne 0xafa3f
movq %r8, %rdx
movl %edx, %r8d
jmp 0xafa60
xorl %r8d, %r8d
movq 0x48(%rsp), %rax
testl %esi, %esi
je 0xafa9b
movq %rsi, %rdx
movq %rax, %rsi
movl %edx, %r10d
leaq (%rcx,%r9,8), %r11
addq $0x8, %r11
xorl %edx, %edx
movl (%r11), %eax
xorl %r13d, %eax
cmpl $0x1, %eax
je 0xafa93
incq %rdx
addq $0x18, %r11
cmpq %rdx, %r10
jne 0xafa77
movq 0x28(%rsp), %rdx
movl %edx, %r10d
movq %rsi, %rax
jmp 0xafa9e
xorl %r10d, %r10d
leaq (%rcx,%rax,8), %rsi
leaq (%rcx,%r9,8), %r11
leaq (%r8,%r8,2), %r8
cmpl $0x0, 0xc(%rsi,%r8,8)
movl 0x34(%rsp), %edx
jns 0xafac6
leaq (%r10,%r10,2), %rax
cmpl $0x0, 0xc(%r11,%rax,8)
js 0xafa10
movq 0x38(%rsp), %rax
cmpb $0x0, 0x12(%rax)
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %r9
jne 0xafc5c
movq 0x50(%rsp), %rax
shrq $0x21, %rax
leaq (%rax,%rax,2), %rax
movq 0x60(%rsp), %rcx
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xafc5c
movl %r15d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xafb6e
movb $0x1, %al
testb %dl, 0x44(%rsp)
jne 0xafb7d
movq %rsi, 0x28(%rsp)
movl %r15d, %esi
cmpl $0x1, %ebp
je 0xafb2c
cmpl $0x1, %ebx
jne 0xafb72
movl %r13d, %esi
xorl $0x1, %esi
movl 0x30(%rsp), %r15d
movl %r15d, %edx
movq %r10, 0x38(%rsp)
movq %r11, 0x48(%rsp)
movq %r8, 0x50(%rsp)
callq 0xab75d
movq 0x50(%rsp), %r8
movq 0x48(%rsp), %r11
movq 0x38(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x60(%rsp), %rcx
cmpl %r15d, %eax
movq 0x10(%rsp), %r15
setl %al
jmp 0xafb74
xorl %eax, %eax
jmp 0xafb7d
xorl %eax, %eax
movl 0x34(%rsp), %edx
movq 0x28(%rsp), %rsi
movl %eax, 0x10(%rsp)
movl %r15d, %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x0, 0x10(%rcx,%rax,8)
jne 0xafc35
movl %r12d, %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x12(%rsi,%rax,8)
jne 0xafc35
leaq (%rsi,%rax,8), %rax
movl 0x8(%rax), %eax
shrl %eax
leaq (%rax,%rax,2), %rax
cmpb $0x1, 0x10(%rcx,%rax,8)
jne 0xafc35
testb %dl, 0x44(%rsp)
jne 0xafc3c
cmpl $0x1, %ebp
jne 0xafbed
movq 0x8(%rsp), %rdi
movq %rsi, %r13
movl %r15d, %esi
movl 0x30(%rsp), %ebx
movl %ebx, %edx
movq %r10, %r15
movq %r11, %r12
movq %r8, %rbp
callq 0xab75d
movq %rbp, %r8
movq %r13, %rsi
jmp 0xafc1b
cmpl $0x1, %ebx
jne 0xafc35
xorl $0x1, %r13d
movq 0x8(%rsp), %rdi
movq %rsi, %rbp
movl %r13d, %esi
movl 0x30(%rsp), %ebx
movl %ebx, %edx
movq %r10, %r15
movq %r11, %r12
movq %r8, %r13
callq 0xab75d
movq %r13, %r8
movq %rbp, %rsi
movq %r12, %r11
movq %r15, %r10
movq 0x18(%rsp), %r9
cmpl %ebx, %eax
setl %al
movl 0x10(%rsp), %ecx
orb %al, %cl
jne 0xafc3c
jmp 0xafc57
cmpb $0x0, 0x10(%rsp)
je 0xafc57
leaq (%rsi,%r8,8), %rax
addq $0xc, %rax
leaq (%r10,%r10,2), %rcx
orb $-0x80, 0xf(%r11,%rcx,8)
orb $-0x80, 0x3(%rax)
incq 0x58(%rsp)
movq 0x8(%rsp), %rdi
movq 0x20(%rsp), %r10
incq %r10
cmpq %r14, %r10
jne 0xaf874
movq %r9, %rdi
callq 0x2220
movq 0x58(%rsp), %r14
testq %r14, %r14
je 0xafcb9
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xa0a7d
movq %rbx, %rdi
callq 0xa0ab6
movq %rbx, %rdi
callq 0xa0b79
orb $-0x80, 0x17(%rbx)
jmp 0xafcbc
xorl %edi, %edi
xorl %esi, %esi
callq 0x9dc69
xorl %edi, %edi
jmp 0xafcb4
movq 0x18(%rsp), %rdi
callq 0x2220
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq