// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rdx, 0x48(%rsp)
movl %esi, 0x2c(%rsp)
movq %rdi, %rbx
callq 0x251c
movq %rbx, %r8
movl 0x14(%rbx), %edx
addl %edx, %edx
xorl %esi, %esi
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
pushq $0x18
popq %r15
xorl %edi, %edi
xorl %ecx, %ecx
xorl %r10d, %r10d
movq %rbx, (%rsp)
movq %rdx, 0x38(%rsp)
cmpq %rdx, %rsi
movq %r10, 0x18(%rsp)
je 0x759db
movq 0x30(%r8), %rax
cmpb $0x0, (%rax,%rsi)
je 0x758f7
movq %r10, %rax
jmp 0x759d0
movq 0x28(%r8), %rax
movq (%rax,%rsi,8), %rax
testl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
je 0x758ef
movq %rsi, 0x10(%rsp)
movq 0x8(%r8), %rsi
movq %rax, %rdx
shrq $0x20, %rdx
imulq $0x18, %rdx, %rdx
leaq (%rsi,%rdx), %r13
movl %eax, %r14d
leaq (%rsi,%rdx), %rbp
addq $0xc, %rbp
leaq (,%r10,8), %rbx
xorl %r12d, %r12d
movq %r14, 0x8(%rsp)
leaq (%r10,%r12), %rax
cmpq %r12, %r14
je 0x759c6
cmpq %rcx, %rax
jne 0x75996
movq %rbx, %r14
movq %r13, %rbx
movq %r15, %r13
leaq (%rcx,%rcx), %r15
testq %rcx, %rcx
pushq $0x2
popq %rax
cmoveq %rax, %r15
leaq (,%r15,8), %rsi
callq 0x2370
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x18(%rsp), %r10
movq (%rsp), %r8
movq %rax, %rdi
movq 0x8(%r8), %rsi
movq %r15, %rcx
movq %r13, %r15
movq %rbx, %r13
movq %r14, %rbx
movq 0x8(%rsp), %r14
movl (%rbp), %r9d
andl %r11d, %r9d
shlq $0x20, %r9
movq %r13, %rax
subq %rsi, %rax
cqto
idivq %r15
addq %r12, %rax
orq %r9, %rax
leaq (%rdi,%rbx), %rdx
movq %rax, (%rdx,%r12,8)
incq %r12
addq $0x18, %rbp
jmp 0x75939
movq 0x38(%rsp), %rdx
movq 0x10(%rsp), %rsi
incq %rsi
movq %rax, %r10
jmp 0x758d7
leaq (%rdi,%r10,8), %rsi
movq %rdi, 0x20(%rsp)
callq 0x66c25
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %r8
movq (%rsp), %rdi
xorl %esi, %esi
movq $0x0, 0x40(%rsp)
cmpq %r9, %rsi
je 0x75d58
movq 0x8(%rdi), %r13
movq 0x28(%rdi), %rax
movl (%r8,%rsi,8), %ecx
imulq $0x18, %rcx, %rcx
movq (%r13,%rcx), %rbx
movq %rbx, %r10
shrq $0x20, %r10
movl 0x8(%r13,%rcx), %r11d
movq %r11, %rdx
xorq $0x1, %rdx
movq (%rax,%r10,8), %r14
movq (%rax,%rdx,8), %rdx
cmpl $0x1, %r14d
jne 0x75a4a
cmpl $0x1, %edx
je 0x75d50
movq %rbx, 0x60(%rsp)
movq %r11, 0x50(%rsp)
movq %r10, 0x58(%rsp)
movq %rsi, 0x8(%rsp)
addq %r13, %rcx
movq %r14, %rax
shrq $0x20, %rax
imulq $0x18, %rax, %rdi
leaq (%rdi,%r13), %rsi
movq %rdx, 0x10(%rsp)
movq %rdx, %r10
shrq $0x20, %r10
movq %r14, 0x38(%rsp)
movl %r14d, %r8d
leaq (%rdi,%r13), %r14
addq $0xc, %r14
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r11d, %r11d
xorl %ebx, %ebx
cmpq %rbp, %r8
je 0x75aca
movl (%r14), %eax
cmpl %eax, %r11d
movl %eax, %r9d
cmoval %r11d, %r9d
movl %ebx, %r15d
cmovbl %ebp, %r15d
xorl %edx, %edx
testl %eax, %eax
cmovnsl %r9d, %r11d
cmovnsl %r15d, %ebx
setns %dl
addl %edx, %r12d
incq %rbp
addq $0x18, %r14
jmp 0x75a96
cmpl $0x2, %r12d
jb 0x75adf
movl 0xc(%rcx), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cmpl %r11d, %eax
je 0x75b4a
movq %rsi, 0x30(%rsp)
imulq $0x18, %r10, %rdx
leaq (%rdx,%r13), %rax
movq %rax, 0x68(%rsp)
movl 0x10(%rsp), %r10d
imulq $0x18, %r10, %rax
movq %rdx, 0x70(%rsp)
leaq (%rdx,%r13), %r9
addq $0xc, %r9
xorl %r11d, %r11d
xorl %ebp, %ebp
xorl %r14d, %r14d
cmpq %r11, %rax
je 0x75b36
movl (%r9,%r11), %edx
cmpl %edx, %r14d
movl %edx, %r15d
cmoval %r14d, %r15d
xorl %esi, %esi
testl %edx, %edx
setns %sil
cmovnsl %r15d, %r14d
addl %esi, %ebp
addq $0x18, %r11
jmp 0x75b0f
cmpl $0x2, %ebp
jb 0x75b5d
movl 0xc(%rcx), %eax
movl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
andl %edx, %eax
cmpl %r14d, %eax
jne 0x75b63
movq (%rsp), %rdi
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
jmp 0x75d4b
cmpl $0x2, %r12d
jb 0x75b4a
leaq (%rdi,%r13), %rax
addq $0x8, %rax
xorl %r9d, %r9d
movq 0x58(%rsp), %r11
movq 0x50(%rsp), %r14
movq 0x68(%rsp), %r15
cmpq %r9, %r8
je 0x75b95
cmpl %r14d, (%rax)
je 0x75b90
incq %r9
addq $0x18, %rax
jmp 0x75b7d
movq %r9, 0x38(%rsp)
movq 0x70(%rsp), %rax
addq %r13, %rax
addq $0x8, %rax
xorl %esi, %esi
movq (%rsp), %rdi
cmpq %rsi, %r10
je 0x75bbf
movl (%rax), %edx
xorl %r11d, %edx
cmpl $0x1, %edx
je 0x75bc6
incq %rsi
addq $0x18, %rax
jmp 0x75ba7
movq 0x10(%rsp), %rsi
jmp 0x75bc6
movl 0x38(%rsp), %eax
imulq $0x18, %rax, %r13
movq 0x30(%rsp), %rdx
cmpl $0x0, 0xc(%rdx,%r13)
movl %esi, %r10d
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
jns 0x75bf8
imulq $0x18, %r10, %rax
cmpl $0x0, 0xc(%r15,%rax)
js 0x75d4b
cmpb $0x0, 0x12(%rcx)
jne 0x75d4b
movq 0x60(%rsp), %rax
shrq $0x21, %rax
imulq $0x18, %rax, %rax
movq 0x48(%rsp), %rcx
cmpb $0x1, 0x10(%rcx,%rax)
jne 0x75d4b
movl %r14d, %eax
shrl %eax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x10(%rcx,%rax)
jne 0x75c83
cmpl $0x2, %r12d
jb 0x75c3c
movb $0x1, %al
cmpl $0x1, %ebp
ja 0x75c8e
movl %r14d, %esi
cmpl $0x1, %ebp
je 0x75c50
cmpl $0x1, %r12d
jne 0x75c87
movl %r11d, %esi
xorl $0x1, %esi
movl 0x2c(%rsp), %r14d
movl %r14d, %edx
movq %r10, 0x10(%rsp)
callq 0x71a0e
movq 0x10(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %r8
cmpl %r14d, %eax
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %r11
setl %al
jmp 0x75c89
xorl %eax, %eax
jmp 0x75c8e
xorl %eax, %eax
movq 0x30(%rsp), %rdx
movl %eax, 0x10(%rsp)
movl %r14d, %eax
shrl %eax
imulq $0x18, %rax, %rax
movq 0x48(%rsp), %rcx
cmpb $0x0, 0x10(%rcx,%rax)
movq %r15, %rsi
jne 0x75d26
movl %ebx, %eax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x12(%rdx,%rax)
jne 0x75d26
addq %rdx, %rax
movl 0x8(%rax), %eax
shrl %eax
imulq $0x18, %rax, %rax
cmpb $0x1, 0x10(%rcx,%rax)
jne 0x75d26
cmpl $0x2, %r12d
jb 0x75cd5
cmpl $0x1, %ebp
ja 0x75d2d
cmpl $0x1, %ebp
jne 0x75ce3
movq (%rsp), %rdi
movl %r14d, %esi
jmp 0x75cf4
cmpl $0x1, %r12d
jne 0x75d26
xorl $0x1, %r11d
movq (%rsp), %rdi
movl %r11d, %esi
movl 0x2c(%rsp), %ebx
movl %ebx, %edx
movq %r10, %r14
callq 0x71a0e
movq %r14, %r10
movq %r15, %rsi
movq 0x30(%rsp), %rdx
movq 0x18(%rsp), %r9
movq 0x20(%rsp), %r8
cmpl %ebx, %eax
setl %al
movl 0x10(%rsp), %ecx
orb %al, %cl
jne 0x75d2d
jmp 0x75d47
cmpb $0x0, 0x10(%rsp)
je 0x75d47
leaq (%rdx,%r13), %rax
addq $0xc, %rax
imulq $0x18, %r10, %rcx
orb $-0x80, 0xf(%rsi,%rcx)
orb $-0x80, 0x3(%rax)
incq 0x40(%rsp)
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
incq %rsi
jmp 0x75a02
movq %r8, %rdi
callq 0x2220
cmpq $0x0, 0x40(%rsp)
je 0x75d7c
movq (%rsp), %rbx
movq %rbx, %rdi
callq 0x694eb
movq %rbx, %rdi
callq 0x69638
movq 0x40(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq