// File: /workspace/github_c_makefile_star2_O1/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 $0x68, %rsp
movl %ecx, 0xc(%rsp)
movl %edx, 0x3c(%rsp)
movq 0x10(%rdi), %r14
movq %rsi, %r15
shrq %r15
imulq $0x68, %r15, %rax
cmpl $0x0, (%r14,%rax)
js 0x12696
movq %rsi, %r13
addq %rax, %r14
movl %r13d, %r12d
andl $0x1, %r12d
leaq (%r12,%r12,2), %rax
cmpq $0x2, 0x20(%r14,%rax,8)
jb 0x12696
movq %r8, %rbx
movq %rdi, %rbp
movl $0x0, (%r8)
movq $0x0, 0x10(%r8)
movq 0x28(%r8), %rdi
cmpl $0x40, (%rdi)
jb 0x12136
callq 0xc86b
callq 0xc85c
movq %rax, 0x28(%rbx)
jmp 0x1213b
callq 0xc899
movl 0x4(%rbx), %eax
cmpl %eax, (%rbx)
movq %rbp, 0x30(%rsp)
movq %r13, 0x28(%rsp)
jne 0x121a9
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x100, %eax            # imm = 0x100
cmovnel %ecx, %eax
movq 0x8(%rbx), %rdi
movl %eax, 0x20(%rsp)
movslq %eax, %r13
leaq (,%r13,8), %rsi
callq 0x21d0
movq %rax, 0x8(%rbx)
movslq 0x4(%rbx), %rbp
cmpl %r13d, %ebp
jge 0x12198
movl $0x50, %edi
callq 0x21b0
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq %rbp, %r13
jne 0x1217e
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rbx)
movq 0x30(%rsp), %rbp
movq 0x28(%rsp), %r13
movq 0x8(%rbx), %rax
movslq (%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, (%rbx)
movq (%rax,%rcx,8), %rax
leaq 0x5bf8(%rip), %rsi      # 0x17db8
movl $0xa, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %ecx
movq %rcx, (%rax)
movq %rax, 0x60(%r14)
xorl %r15d, %r15d
xorl %ecx, %ecx
subl (%r14), %ecx
xorl $0x1, %r12d
movl %ecx, 0x20(%rax,%r12,8)
xorl %ecx, %ecx
subl 0x4(%r14), %ecx
movl %ecx, 0x10(%rax,%r12,8)
movq 0x18(%rbx), %rax
cmpq %rax, 0x10(%rbx)
jne 0x12220
leaq (%rax,%rax), %rcx
testq %rax, %rax
movl $0x2, %esi
cmovneq %rcx, %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
shlq $0x3, %rsi
callq 0x21d0
movq %rax, 0x20(%rbx)
movq %r13, %rsi
xorq $0x1, %rsi
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rbx)
movq %rsi, 0x20(%rsp)
movq %rsi, (%rcx,%rax,8)
movl 0xc(%rsp), %esi
movq 0x10(%rbx), %rcx
cmpq $0x1, %rcx
je 0x122a1
testq %rcx, %rcx
je 0x125b0
movq 0x20(%rbx), %rax
leaq -0x1(%rcx), %rdx
movq %rdx, 0x10(%rbx)
movl 0x3c(%rsp), %edx
cmpl %edx, (%rbx)
jg 0x12297
movq -0x8(%rax,%rcx,8), %rcx
movq %rcx, %rax
shrq %rax
imulq $0x68, %rax, %rdx
addq 0x10(%rbp), %rdx
movl %ecx, %r11d
andl $0x1, %r11d
movq 0x60(%rdx), %rax
cmpl %esi, 0x20(%rax,%r11,8)
jg 0x12297
cmpl %esi, 0x24(%rax,%r11,8)
jle 0x122c1
xorl %edx, %edx
movl %r15d, %eax
jmp 0x125a3
movq 0x20(%rbx), %rax
movq 0x20(%rsp), %rdx
cmpq %rdx, (%rax)
je 0x12257
xorl %edx, %edx
movl $0x0, %eax
testl %r15d, %r15d
je 0x125a3
jmp 0x12257
movq %r11, %rax
xorq $0x1, %rax
leaq (%rax,%rax,2), %rax
leaq (%rdx,%rax,8), %rdi
addq $0x20, %rdi
cmpq $0x0, (%rdi)
je 0x1259a
movq %rdx, 0x60(%rsp)
movb $0x1, %dl
cmpq $0x0, (%rdi)
je 0x1259c
xorq $0x1, %rcx
shlq $0x20, %rcx
movq %rcx, 0x40(%rsp)
xorl %r13d, %r13d
movq %r11, 0x48(%rsp)
movq %rdi, 0x18(%rsp)
movq 0x10(%rdi), %rax
movq %r13, %r12
shlq $0x4, %r12
movq (%rax,%r12), %rsi
testq %rsi, %rsi
js 0x1233c
movq 0x20(%rbp), %rdi
callq 0xeb55
cmpq 0x20(%rsp), %rax
jne 0x12343
movq $0x0, 0x10(%rbx)
movb $0x1, %al
jmp 0x12569
xorl %eax, %eax
jmp 0x12580
movq %rax, %r9
movq 0x10(%rbp), %rax
movq %r9, %r10
shrq %r10
imulq $0x68, %r10, %rcx
leaq (%rax,%rcx), %r14
cmpq $0x0, 0x60(%rax,%rcx)
movq 0x48(%rsp), %r11
movq %r9, 0x10(%rsp)
jne 0x12431
movl 0x4(%rbx), %eax
cmpl %eax, (%rbx)
jne 0x123e1
movq %r10, 0x50(%rsp)
movq %r14, 0x58(%rsp)
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x100, %eax            # imm = 0x100
cmovel %eax, %ecx
movq 0x8(%rbx), %rdi
movl %ecx, 0x38(%rsp)
movslq %ecx, %r14
leaq (,%r14,8), %rsi
callq 0x21d0
movq %rax, 0x8(%rbx)
movslq 0x4(%rbx), %rbp
cmpl %r14d, %ebp
jge 0x123cb
movl $0x50, %edi
callq 0x21b0
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x123b1
movl 0x38(%rsp), %eax
movl %eax, 0x4(%rbx)
movq 0x10(%rsp), %r9
movq 0x58(%rsp), %r14
movq 0x50(%rsp), %r10
movq 0x8(%rbx), %rax
movslq (%rbx), %rcx
leal 0x1(%rcx), %edx
movl %edx, (%rbx)
movq (%rax,%rcx,8), %rax
movl $0xa, %ecx
movq %rax, %rdi
leaq 0x59b8(%rip), %rsi      # 0x17db8
rep  movsq (%rsi), %es:(%rdi)
movl %r10d, %ecx
movq %rcx, (%rax)
movq %rax, 0x60(%r14)
incl %r15d
movl %r9d, %eax
andl $0x1, %eax
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,8), %rdi
addq $0x20, %rdi
callq 0xea2b
movq 0x10(%rsp), %r9
movq 0x48(%rsp), %r11
movq 0x60(%rsp), %r10
movq 0x60(%r10), %rdi
movl 0x4(%r10), %esi
addl 0x10(%rdi,%r11,8), %esi
movl (%r10), %edx
addl 0x20(%rdi,%r11,8), %edx
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r8
subl 0x8(%r8,%r12), %edx
movq %r14, %rbp
movq 0x60(%r14), %rax
movl %r9d, %ecx
andl $0x1, %ecx
movl %ecx, %r9d
movl 0x10(%rax,%r9,8), %r9d
cmpl %r9d, %esi
jle 0x124ce
movl %r9d, 0x14(%rax,%rcx,8)
movl %esi, 0x10(%rax,%rcx,8)
movl %ecx, %esi
shll $0x4, %esi
movq 0x30(%rax,%rsi), %r9
movq %r9, 0x38(%rax,%rsi)
leal (,%r13,2), %r9d
movslq %r9d, %r9
orq 0x40(%rsp), %r9
movq %r9, 0x30(%rax,%rsi)
movl 0x20(%rax,%rcx,8), %esi
movl %esi, 0x24(%rax,%rcx,8)
movl %edx, 0x20(%rax,%rcx,8)
movl (%r10), %edx
movl 0x4(%r10), %esi
addl 0x14(%rdi,%r11,8), %esi
addl 0x24(%rdi,%r11,8), %edx
leaq (%r8,%r12), %rdi
addq $0x8, %rdi
subl (%rdi), %edx
movl $0x1, %edi
jmp 0x124d0
xorl %edi, %edi
cmpl 0x14(%rax,%rcx,8), %esi
movq 0x10(%rsp), %r8
jle 0x12500
movl %esi, 0x14(%rax,%rcx,8)
leal (,%r13,2), %esi
movslq %esi, %rsi
orq 0x40(%rsp), %rdi
orq %rsi, %rdi
movl %ecx, %esi
shll $0x4, %esi
movq %rdi, 0x38(%rax,%rsi)
movl %edx, 0x24(%rax,%rcx,8)
movslq 0x8(%rax,%rcx,4), %rdx
incq %rdx
movl %edx, 0x8(%rax,%rcx,4)
leaq (%rcx,%rcx,2), %rax
cmpq %rdx, 0x20(%rbp,%rax,8)
jne 0x12572
movq 0x18(%rbx), %rax
cmpq %rax, 0x10(%rbx)
movq 0x30(%rsp), %rbp
jne 0x12550
leaq (%rax,%rax), %rsi
testq %rax, %rax
movl $0x2, %eax
cmoveq %rax, %rsi
movq %rsi, 0x18(%rbx)
movq 0x20(%rbx), %rdi
shlq $0x3, %rsi
callq 0x21d0
movq 0x10(%rsp), %r8
movq %rax, 0x20(%rbx)
movq 0x10(%rbx), %rax
movq 0x20(%rbx), %rcx
leaq 0x1(%rax), %rdx
movq %rdx, 0x10(%rbx)
movq %r8, (%rcx,%rax,8)
decl %r15d
xorl %eax, %eax
movb $0x1, %dl
movq 0x18(%rsp), %rdi
jmp 0x12580
xorl %eax, %eax
movb $0x1, %dl
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rbp
testb %al, %al
jne 0x12590
incq %r13
cmpq %r13, (%rdi)
ja 0x12309
movl %r15d, %eax
movq 0x28(%rsp), %r13
jmp 0x1259f
xorl %edx, %edx
movl %r15d, %eax
movl 0xc(%rsp), %esi
movl %eax, %r15d
testb %dl, %dl
jne 0x12244
jmp 0x125b3
movl %r15d, %eax
testl %eax, %eax
jne 0x1260d
cmpq $0x1, 0x10(%rbx)
jne 0x1260d
movq 0x20(%rbx), %rax
movq 0x28(%rbx), %rcx
movq (%rax), %r14
movq 0x10(%rbp), %r15
movq %r14, %rax
shrq %rax
imulq $0x68, %rax, %r12
movq 0x60(%r15,%r12), %rax
andl $0x1, %r14d
shll $0x4, %r14d
movq 0x30(%rax,%r14), %rsi
movq %rbp, %rdi
movq %r13, %rdx
callq 0x126a5
movq 0x60(%r15,%r12), %rax
movq 0x38(%rax,%r14), %rsi
movq 0x28(%rbx), %rcx
movq %rbp, %rdi
movq %r13, %rdx
callq 0x126a5
movslq (%rbx), %rax
testq %rax, %rax
jle 0x12638
movq 0x10(%rbp), %rcx
movq 0x8(%rbx), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
imulq $0x68, (%rdi), %rdi
movq $0x0, 0x60(%rcx,%rdi)
incq %rsi
cmpq %rsi, %rax
jne 0x1261f
movq 0x28(%rbx), %rax
cmpl $0x0, 0x4(%rax)
je 0x12696
cmpl $0x2, (%rbx)
jl 0x12696
movl $0x1, %r14d
movq 0x8(%rbx), %rax
movq 0x20(%rbx), %rcx
movq (%rax,%r14,8), %rax
movq (%rax), %r15
movq (%rcx), %rax
shrq %rax
cmpq %rax, %r15
je 0x1268b
movq 0x28(%rbx), %rdi
movq %r15, %rsi
callq 0xc8d4
movq 0x28(%rbx), %rcx
cmpl (%rcx), %eax
jne 0x1268b
imulq $0x68, %r15, %rsi
addq 0x10(%rbp), %rsi
movq %rbp, %rdi
callq 0xfaf0
incq %r14
movslq (%rbx), %rax
cmpq %rax, %r14
jl 0x1264d
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq