// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/bubble.c
// Function: mag_vh_simplify_bubble
void mag_vh_simplify_bubble(mag_t *g, uint64_t idd, int max_vtx, int max_dist, mogb_aux_t *a)
{
	int i, n_pending = 0;
	magv_t *p, *q;

	p = &g->v.a[idd>>1];
	if (p->len < 0 || p->nei[idd&1].n < 2) return; // stop if p is deleted or it has 0 or 1 neighbor
	// reset aux data
	a->stack.n = a->pool.n = 0;
	if (kh_n_buckets(a->h) >= 64) {
		kh_destroy(64, a->h);
		a->h = kh_init(64);
	} else kh_clear(64, a->h);
	// add the initial vertex
	p->ptr = tip_alloc(&a->pool, idd>>1);
	tiptr(p)->d[(idd&1)^1][0] = -p->len;
	tiptr(p)->n[(idd&1)^1][0] = -p->nsr;
	kv_push(uint64_t, a->stack, idd^1);
	// essentially a topological sorting
	while (a->stack.n) {
		uint64_t x, y;
		ku128_v *r;
		if (a->stack.n == 1 && a->stack.a[0] != (idd^1) && n_pending == 0) break; // found the other end of the bubble
		x = kv_pop(a->stack);
		p = &g->v.a[x>>1];
		//printf("%lld:%lld\n", p->k[0], p->k[1]);
		r = &p->nei[(x&1)^1]; // we will look the the neighbors from the other end of the unitig
		if (a->pool.n > max_vtx || tiptr(p)->d[x&1][0] > max_dist || tiptr(p)->d[x&1][1] > max_dist || r->n == 0) break; // we failed
		// set the distance to p's neighbors
		for (i = 0; i < r->n; ++i) {
			int nsr, dist, which;
			if ((int64_t)r->a[i].x < 0) continue;
			y = mag_tid2idd(g->h, r->a[i].x);
			if (y == (idd^1)) { // there is a loop involving the initial vertex
				a->stack.n = 0;
				break; // not a bubble; stop; this will jump out of the while() loop
			}
			q = &g->v.a[y>>1];
			if (q->ptr == 0) { // has not been attempted
				q->ptr = tip_alloc(&a->pool, y>>1), ++n_pending;
				mag_v128_clean(&q->nei[y&1]); // make sure there are no deleted edges
			}
			nsr  = tiptr(p)->n[x&1][0] + p->nsr; which = 0;
			dist = tiptr(p)->d[x&1][0] + p->len - r->a[i].y;
			//printf("01 [%d]\t[%d,%d]\t[%d,%d]\n", i, tiptr(q)->n[y&1][0], tiptr(q)->n[y&1][1], tiptr(q)->d[y&1][0], tiptr(q)->d[y&1][1]);
			// test and possibly update the tentative distance
			if (nsr > tiptr(q)->n[y&1][0]) { // then move the best to the 2nd best and update the best
				tiptr(q)->n[y&1][1] = tiptr(q)->n[y&1][0]; tiptr(q)->n[y&1][0] = nsr;
				tiptr(q)->v[y&1][1] = tiptr(q)->v[y&1][0]; tiptr(q)->v[y&1][0] = (x^1)<<32|i<<1|which;
				tiptr(q)->d[y&1][1] = tiptr(q)->d[y&1][0]; tiptr(q)->d[y&1][0] = dist;
				nsr  = tiptr(p)->n[x&1][1] + p->nsr; which = 1; // now nsr is the 2nd best
				dist = tiptr(p)->d[x&1][1] + p->len - r->a[i].y;
			}
			if (nsr > tiptr(q)->n[y&1][1]) // update the 2nd best
				tiptr(q)->n[y&1][1] = nsr, tiptr(q)->v[y&1][1] = (x^1)<<32|i<<1|which, tiptr(q)->d[y&1][1] = dist;
			if (++tiptr(q)->cnt[y&1] == q->nei[y&1].n) { // all q's predecessors have been processed; then push
				kv_push(uint64_t, a->stack, y);
				--n_pending;
			}
		}
	}
	if (n_pending == 0 && a->stack.n == 1) { // found a bubble
		uint64_t x = a->stack.a[0];
		p = &g->v.a[x>>1];
		//printf("(%d,%d)\t(%d,%d)\n", tiptr(p)->n[x&1][0], tiptr(p)->n[x&1][1], tiptr(p)->d[x&1][0], tiptr(p)->d[x&1][1]);
		backtrace(g, tiptr(p)->v[x&1][0], idd, a->h);
		backtrace(g, tiptr(p)->v[x&1][1], idd, a->h);
	}
	for (i = 0; i < a->pool.n; ++i) // reset p->ptr
		g->v.a[a->pool.buf[i]->id].ptr = 0;
	if (kh_size(a->h)) { // bubble detected; then remove verticies not in the top two paths
		for (i = 1; i < a->pool.n; ++i) { // i=0 corresponds to the initial vertex which we want to exclude
			uint64_t id = a->pool.buf[i]->id;
			if (id != a->stack.a[0]>>1 && kh_get(64, a->h, id) == kh_end(a->h)) // not in the top two paths
				mag_v_del(g, &g->v.a[id]);
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, 0x10(%rsp)
movl %ecx, 0x4(%rsp)
movl %edx, 0x1c(%rsp)
movq 0x10(%rdi), %rbx
movq %rsi, %r13
shrq %r13
imulq $0x68, %r13, %rax
cmpl $0x0, (%rbx,%rax)
js 0x10371
addq %rax, %rbx
movl %esi, %r14d
andl $0x1, %r14d
imulq $0x18, %r14, %rax
cmpq $0x2, 0x20(%rbx,%rax)
jb 0x10371
movq %rdi, %r12
movq %rsi, 0x20(%rsp)
movq 0x10(%rsp), %r15
andl $0x0, (%r15)
andq $0x0, 0x10(%r15)
movq 0x28(%r15), %rdi
cmpl $0x40, (%rdi)
jb 0xff20
callq 0xafbb
callq 0xafb0
movq %rax, 0x28(%r15)
jmp 0xff25
callq 0xafe9
movq %r15, %rdi
movl %r13d, %esi
callq 0x10380
movq %rax, 0x60(%rbx)
xorl %ecx, %ecx
xorl %edx, %edx
subl (%rbx), %edx
xorl $0x1, %r14d
movl %edx, 0x20(%rax,%r14,8)
subl 0x4(%rbx), %ecx
movl %ecx, 0x10(%rax,%r14,8)
movq 0x10(%r15), %rcx
pushq $0x2
popq %rax
cmpq 0x18(%r15), %rcx
movq %r15, %r13
jne 0xff81
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
cmoveq %rax, %rsi
movq %rsi, 0x18(%r13)
movq 0x20(%r13), %rdi
shlq $0x3, %rsi
callq 0x21c0
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rcx
jmp 0xff85
movq 0x20(%r13), %rax
movl 0x4(%rsp), %esi
movq %r12, %r8
movq 0x20(%rsp), %rdi
xorq $0x1, %rdi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%r13)
movq %rdi, 0x38(%rsp)
movq %rdi, (%rax,%rcx,8)
movq $0x0, 0x8(%rsp)
movq %r12, 0x28(%rsp)
movq 0x10(%r13), %rcx
cmpq $0x1, %rcx
movq %r8, %rbp
je 0xffd0
testq %rcx, %rcx
je 0x10277
movq 0x20(%r13), %rdx
jmp 0xffe9
movq 0x20(%r13), %rdx
movq 0x38(%rsp), %rax
cmpq %rax, (%rdx)
je 0xffe9
cmpl $0x0, 0x8(%rsp)
je 0x1028d
leaq -0x1(%rcx), %rax
movq %rax, 0x10(%r13)
movl 0x1c(%rsp), %edi
cmpl %edi, (%r13)
jg 0x1027a
movq -0x8(%rdx,%rcx,8), %rdi
movq %rdi, %rcx
shrq %rcx
imulq $0x68, %rcx, %r11
addq 0x10(%r8), %r11
movl %edi, %r10d
andl $0x1, %r10d
movq 0x60(%r11), %rcx
cmpl %esi, 0x20(%rcx,%r10,8)
jg 0x1027a
cmpl %esi, 0x24(%rcx,%r10,8)
jg 0x1027a
movq %r10, %rcx
xorq $0x1, %rcx
imulq $0x18, %rcx, %rcx
leaq (%rcx,%r11), %rbx
addq $0x20, %rbx
movq (%rbx), %rcx
testq %rcx, %rcx
je 0x1027a
movq %rdi, %rdx
xorq $0x1, %rdx
shlq $0x20, %rdx
movq %rdx, 0x30(%rsp)
xorl %r14d, %r14d
xorl %edx, %edx
movq %rbx, 0x40(%rsp)
movq %r10, 0x50(%rsp)
cmpq %rdx, %rcx
jbe 0x1025a
movq 0x10(%rbx), %rax
movq (%rax,%r14,8), %rsi
testq %rsi, %rsi
js 0x1024b
movq %rdx, 0x48(%rsp)
movq %r13, %rbx
movq %r11, %rbp
movq 0x20(%r8), %rdi
movq %r8, %r13
callq 0xd0f4
cmpq 0x38(%rsp), %rax
je 0x10263
movq %rax, %r12
movq 0x10(%r13), %rax
movq %r12, %rsi
shrq %rsi
imulq $0x68, %rsi, %rcx
leaq (%rax,%rcx), %r15
movq 0x60(%rax,%rcx), %rax
testq %rax, %rax
je 0x100d5
movl %r12d, %r13d
andl $0x1, %r13d
jmp 0x10109
movq %rbx, %rdi
callq 0x10380
movq %rax, 0x60(%r15)
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
movl %r12d, %r13d
andl $0x1, %r13d
imulq $0x18, %r13, %rax
leaq (%r15,%rax), %rdi
addq $0x20, %rdi
callq 0xcfd1
movq 0x60(%r15), %rax
movq %rbp, %r11
movq 0x60(%rbp), %rdi
movl 0x4(%rbp), %esi
movq 0x50(%rsp), %r10
addl 0x10(%rdi,%r10,8), %esi
movl (%rbp), %ecx
addl 0x20(%rdi,%r10,8), %ecx
movq 0x40(%rsp), %rbx
movq 0x10(%rbx), %r8
subl 0x8(%r8,%r14,8), %ecx
movl 0x10(%rax,%r13,8), %edx
cmpl %edx, %esi
jle 0x1018d
movl %edx, 0x14(%rax,%r13,8)
movl %esi, 0x10(%rax,%r13,8)
movl %r13d, %esi
shll $0x4, %esi
movq 0x30(%rax,%rsi), %r9
movq %r9, 0x38(%rax,%rsi)
movslq %r14d, %r9
orq 0x30(%rsp), %r9
movq %r9, 0x30(%rax,%rsi)
movl 0x20(%rax,%r13,8), %esi
movl %esi, 0x24(%rax,%r13,8)
movl %ecx, 0x20(%rax,%r13,8)
movl (%r11), %ecx
movl 0x4(%r11), %esi
addl 0x14(%rdi,%r10,8), %esi
addl 0x24(%rdi,%r10,8), %ecx
subl 0x8(%r8,%r14,8), %ecx
pushq $0x1
popq %rdi
jmp 0x10194
movl 0x14(%rax,%r13,8), %edx
xorl %edi, %edi
cmpl %edx, %esi
jle 0x101b8
movl %esi, 0x14(%rax,%r13,8)
movslq %r14d, %rdx
orq %rdx, %rdi
orq 0x30(%rsp), %rdi
movl %r13d, %edx
shll $0x4, %edx
movq %rdi, 0x38(%rax,%rdx)
movl %ecx, 0x24(%rax,%r13,8)
movslq 0x8(%rax,%r13,4), %rcx
incq %rcx
movl %ecx, 0x8(%rax,%r13,4)
imulq $0x18, %r13, %rax
cmpq %rcx, 0x20(%r15,%rax)
jne 0x1023c
movq 0x10(%rsp), %r13
movq 0x10(%r13), %rcx
movq 0x20(%r13), %rax
cmpq 0x18(%r13), %rcx
movq 0x28(%rsp), %r8
jne 0x10222
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x18(%r13)
shlq $0x3, %rsi
movq %rax, %rdi
movq %r13, %rbx
movq %r8, %r13
callq 0x21c0
movq %r13, %r8
movq %rbx, %r13
movq 0x40(%rsp), %rbx
movq %rbp, %r11
movq %rax, 0x20(%r13)
movq 0x10(%r13), %rcx
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%r13)
movq %r12, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
jmp 0x10246
movq 0x10(%rsp), %r13
movq 0x28(%rsp), %r8
movq 0x48(%rsp), %rdx
incq %rdx
movq (%rbx), %rcx
addq $0x2, %r14
jmp 0x10071
movl 0x4(%rsp), %esi
jmp 0xffb4
andq $0x0, 0x10(%rbx)
movl 0x4(%rsp), %esi
movq %r13, %r8
movq %rbx, %r13
jmp 0xffb4
movq %rcx, %rax
cmpl $0x0, 0x8(%rsp)
setne %cl
cmpq $0x1, %rax
setne %al
orb %cl, %al
jne 0x102e2
movq 0x20(%r13), %rax
movq 0x28(%r13), %rcx
movq (%rax), %rbx
movq 0x10(%r8), %r14
movq %rbx, %rax
shrq %rax
imulq $0x68, %rax, %r15
movq 0x60(%r14,%r15), %rax
andl $0x1, %ebx
shll $0x4, %ebx
movq 0x30(%rax,%rbx), %rsi
movq %r8, %rdi
movq 0x20(%rsp), %r12
movq %r12, %rdx
callq 0x10419
movq 0x60(%r14,%r15), %rax
movq 0x38(%rax,%rbx), %rsi
movq 0x28(%r13), %rcx
movq %rbp, %rdi
movq %r12, %rdx
callq 0x10419
movq %rbp, %r8
movl (%r13), %eax
xorl %ecx, %ecx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
cmpq %rcx, %rdx
je 0x10312
movq 0x10(%r8), %rsi
movq 0x8(%r13), %rdi
movq (%rdi,%rcx,8), %rdi
imulq $0x68, (%rdi), %rdi
andq $0x0, 0x60(%rsi,%rdi)
incq %rcx
jmp 0x102f2
movq 0x28(%r13), %rcx
cmpl $0x0, 0x4(%rcx)
je 0x10371
pushq $0x1
popq %rbx
movq 0x10(%rsp), %r14
cltq
cmpq %rax, %rbx
jge 0x10371
movq 0x8(%r14), %rax
movq 0x20(%r14), %rcx
movq (%rax,%rbx,8), %rax
movq (%rax), %r15
movq (%rcx), %rax
shrq %rax
cmpq %rax, %r15
je 0x10369
movq 0x28(%r14), %rdi
movq %r15, %rsi
callq 0xb01f
movq 0x28(%r14), %rcx
cmpl (%rcx), %eax
jne 0x10369
movq %rbp, %rdi
imulq $0x68, %r15, %rsi
addq 0x10(%rbp), %rsi
callq 0xdbb0
incq %rbx
movl (%r14), %eax
jmp 0x10324
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq