// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movl %edx, 0x30(%rsp)
movq 0x10(%rdi), %r14
movq %rsi, %r15
shrq %r15
imulq $0x68, %r15, %rax
cmpl $0x0, (%r14,%rax)
js 0x12cf7
addq %rax, %r14
movl %esi, %ebx
andl $0x1, %ebx
leaq (%rbx,%rbx,2), %rax
cmpq $0x2, 0x20(%r14,%rax,8)
jb 0x12cf7
movq %r8, %rbp
movq %rdi, %r12
movq %rsi, 0x38(%rsp)
movl $0x0, (%r8)
movq $0x0, 0x10(%r8)
movq 0x28(%r8), %rdi
cmpl $0x40, (%rdi)
jb 0x1279a
callq 0xcda7
callq 0xcd98
movq %rax, 0x28(%rbp)
jmp 0x1279f
callq 0xcdd5
movl (%rbp), %eax
cmpl 0x4(%rbp), %eax
movq %r12, 0x40(%rsp)
jne 0x12807
leal (%rax,%rax), %ecx
testl %eax, %eax
movl $0x100, %eax            # imm = 0x100
cmovnel %ecx, %eax
movq 0x8(%rbp), %rdi
movl %eax, 0x20(%rsp)
movslq %eax, %r12
leaq (,%r12,8), %rsi
callq 0x21c0
movq %rax, 0x8(%rbp)
movslq 0x4(%rbp), %r13
cmpl %r12d, %r13d
jge 0x127f8
movl $0x50, %edi
callq 0x21a0
movq 0x8(%rbp), %rcx
movq %rax, (%rcx,%r13,8)
incq %r13
cmpq %r13, %r12
jne 0x127de
movl 0x20(%rsp), %eax
movl %eax, 0x4(%rbp)
movl (%rbp), %eax
movq 0x40(%rsp), %r12
movq 0x8(%rbp), %rcx
leal 0x1(%rax), %edx
movl %edx, (%rbp)
cltq
movq (%rcx,%rax,8), %rax
leaq 0x5662(%rip), %rsi      # 0x17e80
movl $0xa, %ecx
movq %rax, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %ecx
movq %rcx, (%rax)
movq %rax, 0x60(%r14)
xorl %ecx, %ecx
xorl %edx, %edx
subl (%r14), %edx
xorl $0x1, %ebx
movl %edx, 0x20(%rax,%rbx,8)
subl 0x4(%r14), %ecx
movl %ecx, 0x10(%rax,%rbx,8)
movq 0x10(%rbp), %rcx
cmpq 0x18(%rbp), %rcx
jne 0x1287e
leaq (%rcx,%rcx), %rax
testq %rcx, %rcx
movl $0x2, %esi
cmovneq %rax, %rsi
movq %rsi, 0x18(%rbp)
movq 0x20(%rbp), %rdi
shlq $0x3, %rsi
callq 0x21c0
movq %rax, 0x20(%rbp)
movq 0x10(%rbp), %rcx
jmp 0x12882
movq 0x20(%rbp), %rax
movq 0x38(%rsp), %rsi
xorq $0x1, %rsi
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbp)
movq %rsi, 0x50(%rsp)
movq %rsi, (%rax,%rcx,8)
movq $0x0, 0x8(%rsp)
movq 0x10(%rbp), %rcx
cmpq $0x1, %rcx
je 0x128c2
testq %rcx, %rcx
movl 0x14(%rsp), %esi
je 0x12c01
movq 0x20(%rbp), %rdx
jmp 0x128df
movq 0x20(%rbp), %rdx
movq 0x50(%rsp), %rax
cmpq %rax, (%rdx)
movl 0x14(%rsp), %esi
je 0x128df
cmpl $0x0, 0x8(%rsp)
je 0x12c17
leaq -0x1(%rcx), %rax
movq %rax, 0x10(%rbp)
movl 0x30(%rsp), %edi
cmpl %edi, (%rbp)
jg 0x12c04
movq -0x8(%rdx,%rcx,8), %rdi
movq %rdi, %rcx
shrq %rcx
imulq $0x68, %rcx, %r10
addq 0x10(%r12), %r10
movl %edi, %r11d
andl $0x1, %r11d
movq 0x60(%r10), %rcx
cmpl %esi, 0x20(%rcx,%r11,8)
jg 0x12c04
cmpl %esi, 0x24(%rcx,%r11,8)
jg 0x12c04
movq %r11, %rcx
xorq $0x1, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq (%r10,%rcx,8), %r9
addq $0x20, %r9
cmpq $0x0, (%r9)
je 0x12c04
movq %rdi, %rdx
xorq $0x1, %rdx
shlq $0x20, %rdx
movq %rdx, 0x48(%rsp)
xorl %r14d, %r14d
movq %r10, 0x60(%rsp)
movq %r11, 0x58(%rsp)
movq %r9, 0x18(%rsp)
movq 0x10(%r9), %rax
movq %r14, %r13
shlq $0x4, %r13
movq (%rax,%r13), %rsi
testq %rsi, %rsi
js 0x12be3
movq 0x20(%r12), %rdi
callq 0xf0c7
cmpq 0x50(%rsp), %rax
je 0x12bf4
movq %rax, %r8
movq 0x10(%r12), %rax
movq %r8, %r15
shrq %r15
imulq $0x68, %r15, %rcx
leaq (%rax,%rcx), %rdx
movq %rdx, 0x20(%rsp)
movq 0x60(%rax,%rcx), %rax
testq %rax, %rax
movq %r8, 0x28(%rsp)
je 0x129ce
movq %r12, %rbx
movl %r8d, %r15d
andl $0x1, %r15d
jmp 0x12aa5
movl (%rbp), %ecx
movq 0x8(%rbp), %rax
cmpl 0x4(%rbp), %ecx
jne 0x12a49
leal (%rcx,%rcx), %edx
testl %ecx, %ecx
movl $0x100, %ecx            # imm = 0x100
cmovel %ecx, %edx
movl %edx, 0x34(%rsp)
movslq %edx, %r12
leaq (,%r12,8), %rsi
movq %rax, %rdi
callq 0x21c0
movq %rax, 0x8(%rbp)
movq %rbp, %rbx
movslq 0x4(%rbp), %rbp
cmpl %r12d, %ebp
jge 0x12a2c
movl $0x50, %edi
callq 0x21a0
movq 0x8(%rbx), %rcx
movq %rax, (%rcx,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x12a0e
movq 0x8(%rbx), %rax
movl 0x34(%rsp), %ecx
movl %ecx, 0x4(%rbx)
movl (%rbx), %ecx
movq %rbx, %rbp
movq 0x40(%rsp), %rbx
movq 0x8(%rsp), %r9
movq 0x28(%rsp), %r8
jmp 0x12a51
movq %r12, %rbx
movq 0x8(%rsp), %r9
leal 0x1(%rcx), %edx
movl %edx, (%rbp)
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movl $0xa, %ecx
movq %rax, %rdi
leaq 0x5413(%rip), %rsi      # 0x17e80
rep  movsq (%rsi), %es:(%rdi)
movl %r15d, %ecx
movq %rcx, (%rax)
movq 0x20(%rsp), %r12
movq %rax, 0x60(%r12)
incl %r9d
movq %r9, 0x8(%rsp)
movl %r8d, %r15d
andl $0x1, %r15d
leaq (%r15,%r15,2), %rax
leaq (%r12,%rax,8), %rdi
addq $0x20, %rdi
callq 0xef98
movq 0x60(%r12), %rax
movq 0x18(%rsp), %r9
movq 0x60(%rsp), %r10
movq 0x60(%r10), %rdi
movl 0x4(%r10), %esi
movq 0x58(%rsp), %r11
addl 0x10(%rdi,%r11,8), %esi
movl (%r10), %ecx
addl 0x20(%rdi,%r11,8), %ecx
movq 0x10(%r9), %r8
subl 0x8(%r8,%r13), %ecx
movl 0x10(%rax,%r15,8), %edx
cmpl %edx, %esi
jle 0x12b3c
movl %edx, 0x14(%rax,%r15,8)
movl %esi, 0x10(%rax,%r15,8)
movl %r15d, %esi
shll $0x4, %esi
movq 0x30(%rax,%rsi), %r9
movq %r9, 0x38(%rax,%rsi)
leal (%r14,%r14), %r9d
movslq %r9d, %r9
orq 0x48(%rsp), %r9
movq %r9, 0x30(%rax,%rsi)
movq 0x18(%rsp), %r9
movl 0x20(%rax,%r15,8), %esi
movl %esi, 0x24(%rax,%r15,8)
movl %ecx, 0x20(%rax,%r15,8)
movl (%r10), %ecx
movl 0x4(%r10), %esi
addl 0x14(%rdi,%r11,8), %esi
addl 0x24(%rdi,%r11,8), %ecx
leaq (%r8,%r13), %rdi
addq $0x8, %rdi
subl (%rdi), %ecx
movl $0x1, %edi
jmp 0x12b43
movl 0x14(%rax,%r15,8), %edx
xorl %edi, %edi
cmpl %edx, %esi
jle 0x12b6b
movl %esi, 0x14(%rax,%r15,8)
leal (%r14,%r14), %edx
movslq %edx, %rdx
orq %rdx, %rdi
orq 0x48(%rsp), %rdi
movl %r15d, %edx
shll $0x4, %edx
movq %rdi, 0x38(%rax,%rdx)
movl %ecx, 0x24(%rax,%r15,8)
movslq 0x8(%rax,%r15,4), %rcx
incq %rcx
movl %ecx, 0x8(%rax,%r15,4)
leaq (%r15,%r15,2), %rax
movq 0x20(%rsp), %rdx
cmpq %rcx, 0x20(%rdx,%rax,8)
jne 0x12be0
movq 0x10(%rbp), %rcx
movq 0x20(%rbp), %rax
cmpq 0x18(%rbp), %rcx
jne 0x12bc3
leaq (%rcx,%rcx), %rsi
testq %rcx, %rcx
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x18(%rbp)
shlq $0x3, %rsi
movq %rax, %rdi
callq 0x21c0
movq 0x18(%rsp), %r9
movq %rax, 0x20(%rbp)
movq 0x10(%rbp), %rcx
movq 0x28(%rsp), %r12
leaq 0x1(%rcx), %rdx
movq %rdx, 0x10(%rbp)
movq %r12, (%rax,%rcx,8)
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
movq %rbx, %r12
incq %r14
cmpq %r14, (%r9)
ja 0x12968
jmp 0x128a5
movq $0x0, 0x10(%rbp)
jmp 0x128a5
movq %rcx, %rax
cmpl $0x0, 0x8(%rsp)
setne %cl
cmpq $0x1, %rax
setne %al
orb %cl, %al
jne 0x12c6a
movq 0x20(%rbp), %rax
movq 0x28(%rbp), %rcx
movq (%rax), %rbx
movq 0x10(%r12), %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 %r12, %rdi
movq 0x38(%rsp), %r13
movq %r13, %rdx
callq 0x12d06
movq 0x60(%r14,%r15), %rax
movq 0x38(%rax,%rbx), %rsi
movq 0x28(%rbp), %rcx
movq %r12, %rdi
movq %r13, %rdx
callq 0x12d06
movslq (%rbp), %rax
testq %rax, %rax
jle 0x12c97
movq 0x10(%r12), %rcx
movq 0x8(%rbp), %rdx
xorl %esi, %esi
movq (%rdx,%rsi,8), %rdi
imulq $0x68, (%rdi), %rdi
movq $0x0, 0x60(%rcx,%rdi)
incq %rsi
cmpq %rsi, %rax
jne 0x12c7e
movq 0x28(%rbp), %rax
cmpl $0x0, 0x4(%rax)
je 0x12cf7
cmpl $0x2, (%rbp)
jl 0x12cf7
movl $0x1, %ebx
movq 0x8(%rbp), %rax
movq 0x20(%rbp), %rcx
movq (%rax,%rbx,8), %rax
movq (%rax), %r15
movq (%rcx), %rax
shrq %rax
cmpq %rax, %r15
je 0x12ceb
movq 0x28(%rbp), %rdi
movq %r15, %rsi
callq 0xce10
movq 0x28(%rbp), %rcx
cmpl (%rcx), %eax
jne 0x12ceb
imulq $0x68, %r15, %rsi
addq 0x10(%r12), %rsi
movq %r12, %rdi
callq 0x100b2
incq %rbx
movslq (%rbp), %rax
cmpq %rax, %rbx
jl 0x12cac
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq