// File: /workspace/github_c_makefile_star2_O1/lh3[P]minigraph/gfa-bbl.c
// Function: gfa_bubble
gfa_bubble_t *gfa_bubble(const gfa_t *g, int32_t *n_bb_)
{
	uint32_t i, *vs, *vmin, *vtmp = 0;
	int32_t n_bb = 0, m_bb = 0, m_vtmp = 0;
	gfa_bubble_t *bb = 0;
	gfa_scbuf_t *scbuf;

	GFA_MALLOC(vs, g->n_sseq);
	GFA_MALLOC(vmin, g->n_sseq);
	for (i = 0; i < g->n_sseq; ++i)
		vs[i] = (uint32_t)-1, vmin[i] = UINT32_MAX;
	for (i = 0; i < g->n_seg; ++i) {
		const gfa_seg_t *s = &g->seg[i];
		if (s->rank != 0 || s->snid < 0) continue;
		if ((uint32_t)s->soff < vmin[s->snid])
			vmin[s->snid] = s->soff, vs[s->snid] = i<<1;
	}
	free(vmin);

	scbuf = gfa_scbuf_init(g);
	for (i = 0; i < g->n_sseq; ++i) {
		gfa_sub_t *sub;
		int32_t j, jst, max_a, max_soff;
		bb_aux_t *ba;

		if (vs[i] == (uint32_t)-1) continue;
		#if 0
		sub = gfa_sub_from(0, g, vs[i], 0);
		#else
		sub = gfa_scc1(0, g, scbuf, vs[i]);
		#endif
		//gfa_sub_print(stderr, g, sub);
		GFA_CALLOC(ba, sub->n_v);
		for (j = 0; j < sub->n_v; ++j)
			ba[j].sd = INT32_MAX, ba[j].lp = ba[j].sp = -1;
		ba[0].sd = 0;
		for (j = 0; j < sub->n_v; ++j) {
			gfa_subv_t *t = &sub->v[j];
			int32_t k;
			for (k = 0; k < t->n; ++k) {
				uint64_t a = sub->a[t->off + k];
				int32_t jv = (int32_t)(a>>32);
				int32_t l = (int32_t)g->arc[(uint32_t)a].v_lv;
				if (jv <= j) continue; // skip loop or cycle
				if (ba[jv].sd >= ba[j].sd + l)
					ba[jv].sd = ba[j].sd + l, ba[jv].sp = j;
				if (ba[jv].ld < ba[j].ld + l)
					ba[jv].ld = ba[j].ld + l, ba[jv].lp = j;
			}
		}
		for (j = 0, jst = 0, max_a = max_soff = -1; j < sub->n_v; ++j) {
			gfa_subv_t *t = &sub->v[j];
			int32_t k;
			if (j == max_a && g->seg[t->v>>1].soff > max_soff) {
				const gfa_seg_t *sst = &g->seg[sub->v[jst].v>>1];
				const gfa_seg_t *sen = &g->seg[t->v>>1];
				if (sst->snid == i && sen->snid == i) {
					int32_t n, l;
					uint32_t *v;
					gfa_bubble_t *b;

					// basic information
					if (n_bb == m_bb) GFA_EXPAND(bb, m_bb);
					b = &bb[n_bb++];
					b->snid = i;
					b->vs = sub->v[jst].v;
					b->ve = t->v;
					b->ss = sst->soff + sst->len;
					b->se = sen->soff;
					b->len_min = ba[j].sd - ba[jst].sd - sst->len;
					b->len_max = ba[j].ld - ba[jst].ld - sst->len;
					b->n_paths = bb_n_paths(g, sub, jst, j);
					//fprintf(stderr, "X\t%s[%d]\tvs=%c%s\tve=%c%s\tlen_min=%d\n", g->sseq[i].name, i, "><"[b->vs&1], g->seg[b->vs>>1].name, "><"[b->ve&1], g->seg[b->ve>>1].name, b->len_min);
					assert(b->len_min >= 0);
					assert(b->len_max >= 0 && b->len_max >= b->len_min);
					b->n_seg = j - jst + 1;
					l = (b->len_min + 1) + (b->len_max + 1);
					l = (l + 3) / 4 + b->n_seg;
					GFA_CALLOC(b->v, l);
					b->seq_min = (char*)(b->v + b->n_seg);
					b->seq_max = b->seq_min + b->len_min + 1;
					for (k = jst; k <= j; ++k)
						b->v[k - jst] = sub->v[k].v;

					// test bubble involving both strands (mostly inversions)
					if (b->n_seg > m_vtmp) {
						m_vtmp = b->n_seg;
						kroundup32(m_vtmp);
						GFA_REALLOC(vtmp, m_vtmp);
					}
					for (k = 0; k < b->n_seg; ++k) vtmp[k] = b->v[k]>>1;
					radix_sort_gfa32(vtmp, vtmp + b->n_seg);
					for (k = 1; k < b->n_seg; ++k)
						if (vtmp[k] == vtmp[k-1]) break;
					b->is_bidir = (k < b->n_seg);

					// generate sequences and cf_min/cf_max
					GFA_MALLOC(v, j - jst);
					k = j, n = 0;
					while (k > jst) {
						if (k < j) v[n++] = sub->v[k].v;
						k = ba[k].sp;
					}
					bb_write_seq(g, n, v, b->len_min, b->seq_min);
					k = j, n = 0;
					while (k > jst) {
						if (k < j) v[n++] = sub->v[k].v;
						k = ba[k].lp;
					}
					bb_write_seq(g, n, v, b->len_max, b->seq_max);
					free(v);
				} // ~if(sst->snid==i&&sen->snid==i)
				max_a = max_soff = -1, jst = j;
			} // ~if(j==max_a)
			for (k = 0; k < t->n; ++k)
				if ((int32_t)(sub->a[t->off + k]>>32) > max_a)
					max_a = sub->a[t->off + k]>>32;
			if (g->seg[t->v>>1].snid == i && g->seg[t->v>>1].soff > max_soff)
				max_soff = g->seg[t->v>>1].soff;
		}
		free(ba);
		gfa_sub_destroy(sub);
	}
	free(vtmp);
	gfa_scbuf_destroy(scbuf);
	free(vs);
	*n_bb_ = n_bb;
	return bb;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rsi, 0x70(%rsp)
movq %rdi, %r13
movl 0x24(%rdi), %r15d
leaq (,%r15,4), %r14
movq %r14, %rdi
callq 0x2280
movq %rax, %r12
movq %r14, %rdi
callq 0x2280
movq %rax, %rbx
testq %r15, %r15
je 0xac0c
movq %r12, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x2180
movq %rbx, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x2180
movl 0x4(%r13), %eax
testq %rax, %rax
je 0xac50
movq 0x10(%r13), %rcx
addq $0x10, %rcx
addq %rax, %rax
xorl %edx, %edx
cmpl $0x0, (%rcx)
je 0xac36
addq $0x2, %rdx
addq $0x40, %rcx
cmpq %rdx, %rax
jne 0xac22
jmp 0xac50
movslq -0x8(%rcx), %rsi
testq %rsi, %rsi
js 0xac27
movl -0x4(%rcx), %edi
cmpl (%rbx,%rsi,4), %edi
jae 0xac27
movl %edi, (%rbx,%rsi,4)
movl %edx, (%r12,%rsi,4)
jmp 0xac27
movq %rbx, %rdi
callq 0x2040
movq %r13, %rdi
callq 0xa471
movq %rax, 0x58(%rsp)
cmpl $0x0, 0x24(%r13)
je 0xb2f9
xorl %edx, %edx
movq $0x0, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
movl $0x0, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq $0x0, 0x18(%rsp)
movq %r12, 0x78(%rsp)
movq %r13, 0x50(%rsp)
movl (%r12,%rdx,4), %ecx
cmpl $-0x1, %ecx
je 0xb2e7
movq %rdx, 0x40(%rsp)
xorl %edi, %edi
movq %r13, %rsi
movq 0x58(%rsp), %rdx
callq 0xa4f5
movq %rax, 0x8(%rsp)
movslq (%rax), %r14
movl $0x20, %esi
movq %r14, %rdi
callq 0x21e0
movq %rax, %r12
testq %r14, %r14
jle 0xad08
movq %r14, %rax
shlq $0x5, %rax
xorl %ecx, %ecx
movl $0x7fffffff, 0x4(%r12,%rcx) # imm = 0x7FFFFFFF
movq $-0x1, 0xc(%r12,%rcx)
addq $0x20, %rcx
cmpq %rcx, %rax
jne 0xaced
movl $0x0, 0x4(%r12)
testl %r14d, %r14d
jle 0xadb7
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rax
xorl %ecx, %ecx
movq %rcx, %rsi
shlq $0x4, %rsi
movslq 0xc(%rax,%rsi), %rdx
testq %rdx, %rdx
jle 0xadab
addq %rax, %rsi
movslq 0x8(%rsi), %rsi
movq 0x48(%r13), %rdi
movq %rcx, %r8
shlq $0x5, %r8
addq %r12, %r8
shlq $0x3, %rsi
movq 0x8(%rsp), %r9
addq 0x18(%r9), %rsi
xorl %r9d, %r9d
movq (%rsi,%r9,8), %r10
movq %r10, %rbx
sarq $0x20, %rbx
cmpq %rbx, %rcx
jge 0xada3
movl %r10d, %r10d
shlq $0x5, %r10
movl (%rdi,%r10), %r10d
shlq $0x5, %rbx
leaq (%r12,%rbx), %r11
movl 0x4(%r8), %ebp
addl %r10d, %ebp
cmpl %ebp, 0x4(%r12,%rbx)
jl 0xad94
movl %ebp, 0x4(%r11)
movl %ecx, 0x10(%r11)
addl (%r8), %r10d
cmpl %r10d, (%r11)
jge 0xada3
movl %r10d, (%r11)
movl %ecx, 0xc(%r11)
incq %r9
cmpq %r9, %rdx
jne 0xad5b
incq %rcx
cmpq %r14, %rcx
jne 0xad25
movq 0x8(%rsp), %r14
cmpl $0x0, (%r14)
jle 0xb2cd
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %r15d, %r15d
movl $0x1, %r9d
xorl %ebx, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x40(%rsp), %r8
movq 0x10(%r14), %rcx
movq %r15, %r11
shlq $0x4, %r11
addq %rcx, %r11
movl %eax, %edx
cmpq %rdx, %r15
jne 0xaeb5
movq 0x10(%r13), %r14
movl (%r11), %ebp
shrl %ebp
shlq $0x6, %rbp
cmpl %r10d, 0xc(%r14,%rbp)
jle 0xaeb0
movslq %ebx, %rsi
movq %rsi, 0x48(%rsp)
shlq $0x4, %rsi
movl (%rcx,%rsi), %r13d
shrl %r13d
shlq $0x6, %r13
movl 0x8(%r14,%r13), %eax
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpq %rax, %r8
jne 0xaea3
addq %r14, %rbp
movl 0x8(%rbp), %eax
cmpq %rax, %r8
jne 0xaea3
movl 0x30(%rsp), %eax
cmpl %eax, 0x4(%rsp)
movl %r9d, 0x34(%rsp)
movq %r11, 0x68(%rsp)
movq %rsi, 0x60(%rsp)
jne 0xaf17
movl %eax, %ecx
sarl %ecx
addl %eax, %ecx
testl %eax, %eax
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x10(%rsp)
movslq %ecx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,8), %rsi
movq 0x18(%rsp), %rdi
callq 0x22b0
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %r11
movq 0x40(%rsp), %r8
movl 0x10(%rsp), %ecx
movl %ecx, 0x30(%rsp)
jmp 0xaf1c
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %r15d, %ebx
movq 0x50(%rsp), %r13
movq 0x8(%rsp), %r14
movslq 0xc(%r11), %rcx
testq %rcx, %rcx
jle 0xaedd
movslq 0x8(%r11), %rdx
shlq $0x3, %rdx
addq 0x18(%r14), %rdx
xorl %esi, %esi
movl 0x4(%rdx,%rsi,8), %edi
cmpl %edi, %eax
cmovlel %edi, %eax
incq %rsi
cmpq %rsi, %rcx
jne 0xaecc
movq 0x10(%r13), %rcx
movl (%r11), %edx
shrl %edx
shlq $0x6, %rdx
movl 0x8(%rcx,%rdx), %esi
cmpq %rsi, %r8
jne 0xaf00
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpl %r10d, %ecx
cmovgl %ecx, %r10d
incq %r15
movslq (%r14), %rcx
incl %r9d
cmpq %rcx, %r15
jl 0xade1
jmp 0xb2cd
movq 0x18(%rsp), %rax
addq %r13, %r14
movslq 0x4(%rsp), %rcx
leaq (%rcx,%rcx,8), %r10
movl %r8d, (%rax,%r10,8)
movq 0x8(%rsp), %rdx
movq 0x10(%rdx), %r13
movl (%r13,%rsi), %ecx
movl %ecx, 0xc(%rax,%r10,8)
movl (%r11), %ecx
movl %ecx, 0x10(%rax,%r10,8)
movl (%r14), %r8d
movl 0xc(%r14), %ecx
movq %rdx, %r14
addl %r8d, %ecx
movl %ecx, 0x4(%rax,%r10,8)
movl 0xc(%rbp), %ecx
movl %ecx, 0x8(%rax,%r10,8)
movq %r15, %rcx
shlq $0x5, %rcx
movl 0x4(%r12,%rcx), %edx
movq 0x48(%rsp), %rbp
movq %rbp, %rsi
shlq $0x5, %rsi
movl 0x4(%r12,%rsi), %edi
addl %r8d, %edi
subl %edi, %edx
movl %edx, 0x20(%rax,%r10,8)
addl (%r12,%rsi), %r8d
movl (%r12,%rcx), %ecx
subl %r8d, %ecx
movq %rax, 0x18(%rsp)
movq %r10, 0x38(%rsp)
movl %ecx, 0x1c(%rax,%r10,8)
movl %r15d, %eax
subl %ebp, %eax
movq %rax, 0x80(%rsp)
incl %eax
movslq %eax, %rdi
movl $0x8, %esi
movq %rdi, 0x10(%rsp)
callq 0x21e0
movq $0x1, (%rax)
cmpq %rbp, %r15
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
jle 0xb043
movq %rbp, %rcx
movq %rcx, %rsi
shlq $0x4, %rsi
movslq 0xc(%r13,%rsi), %rdx
testq %rdx, %rdx
jle 0xb03b
addq %r13, %rsi
movslq 0x8(%rsi), %rsi
movq %rcx, %rdi
subq %rbp, %rdi
shlq $0x3, %rsi
addq 0x18(%r14), %rsi
xorl %r8d, %r8d
movq (%rsi,%r8,8), %r9
movq %r9, %r10
sarq $0x20, %r10
cmpq %r10, %rcx
jge 0xb033
cmpq %r10, %r15
jl 0xb033
shrq $0x20, %r9
subl %ebx, %r9d
movslq %r9d, %r9
movq (%rax,%rdi,8), %r10
addq (%rax,%r9,8), %r10
cmpq %r11, %r10
cmovgeq %r11, %r10
movq %r10, (%rax,%r9,8)
incq %r8
cmpq %r8, %rdx
jne 0xb001
incq %rcx
cmpq %r15, %rcx
jne 0xafd8
movq 0x18(%rsp), %rcx
movq 0x38(%rsp), %rdx
leaq (%rcx,%rdx,8), %rbp
movslq 0x80(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx,8), %r14
movq %rax, %rdi
callq 0x2040
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
cmpq %rax, %r14
cmovgeq %rax, %r14
movl %r14d, 0x30(%rbp)
movslq 0x20(%rbp), %r14
testq %r14, %r14
js 0xb351
movl 0x1c(%rbp), %eax
testl %eax, %eax
js 0xb370
cmpl %r14d, %eax
jl 0xb370
leal (%r14,%rax), %ecx
addl $0x5, %ecx
addl %r14d, %eax
addl $0x8, %eax
testl %ecx, %ecx
cmovnsl %ecx, %eax
movq 0x10(%rsp), %rcx
movl %ecx, 0x18(%rbp)
sarl $0x2, %eax
addl %ecx, %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x21e0
movq %rax, 0x28(%rbp)
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0x40(%rbp)
leaq 0x1(%r14,%rcx), %rcx
movq %rcx, 0x38(%rbp)
movslq %ebx, %rcx
movq %rcx, 0x48(%rsp)
cmpq %rcx, %r15
movl 0x34(%rsp), %ecx
jl 0xb10d
addq 0x60(%rsp), %r13
subl %ebx, %ecx
xorl %edx, %edx
movl (%r13), %esi
movl %esi, (%rax,%rdx,4)
incq %rdx
addq $0x10, %r13
cmpl %edx, %ecx
jne 0xb0fb
movl 0x18(%rbp), %eax
cmpl 0x2c(%rsp), %eax
jle 0xb158
decl %eax
movl %eax, %ecx
shrl %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x2, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x4, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x8, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x10, %ecx
orl %eax, %ecx
leal 0x1(%rcx), %eax
movl %eax, 0x2c(%rsp)
leaq 0x4(,%rcx,4), %rsi
movq 0x20(%rsp), %rdi
callq 0x22b0
movq %rax, %r14
jmp 0xb15d
movq 0x20(%rsp), %r14
movl 0x18(%rbp), %ecx
testl %ecx, %ecx
jle 0xb17f
movq 0x28(%rbp), %rax
xorl %edx, %edx
movl (%rax,%rdx,4), %ecx
shrl %ecx
movl %ecx, (%r14,%rdx,4)
incq %rdx
movslq 0x18(%rbp), %rcx
cmpq %rcx, %rdx
jl 0xb16a
movslq %ecx, %rax
leaq (%r14,%rax,4), %rsi
movq %r14, %rdi
callq 0xa13a
movslq 0x18(%rbp), %rax
cmpq $0x2, %rax
setge %cl
jl 0xb1cd
movl 0x4(%r14), %edx
cmpl (%r14), %edx
movq 0x38(%rsp), %rdi
je 0xb1d2
movl $0x2, %edx
movq %rdx, %rcx
cmpq %rdx, %rax
je 0xb1c5
movl (%r14,%rcx,4), %esi
leaq 0x1(%rcx), %rdx
cmpl -0x4(%r14,%rcx,4), %esi
jne 0xb1ae
cmpq %rax, %rcx
setb %cl
jmp 0xb1d2
movq 0x38(%rsp), %rdi
movq %r14, 0x20(%rsp)
movzbl %cl, %eax
movl %eax, 0x14(%rbp)
shlq $0x2, %rdi
callq 0x2280
xorl %r13d, %r13d
movl $0x0, %esi
cmpq 0x48(%rsp), %r15
movq 0x8(%rsp), %r14
jle 0xb22c
xorl %esi, %esi
movl %r15d, %ecx
movslq %ecx, %rcx
movl %ecx, %edi
cmpq %rcx, %r15
jle 0xb21f
movq 0x10(%r14), %rcx
movq %rdi, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %ecx
movslq %esi, %rdx
incl %esi
movl %ecx, (%rax,%rdx,4)
shlq $0x5, %rdi
movl 0x10(%r12,%rdi), %ecx
cmpl %ebx, %ecx
jg 0xb1ff
movl 0x20(%rbp), %ecx
movq 0x40(%rbp), %r8
movq 0x50(%rsp), %rdi
movq %rax, %rdx
movq %rax, 0x10(%rsp)
callq 0xb38f
movq 0x10(%rsp), %rdx
cmpq 0x48(%rsp), %r15
jle 0xb285
xorl %r13d, %r13d
movl %r15d, %eax
movslq %eax, %rcx
movl %ecx, %eax
cmpq %rcx, %r15
jle 0xb278
movq 0x10(%r14), %rcx
movq %rax, %rsi
shlq $0x4, %rsi
movl (%rcx,%rsi), %ecx
movslq %r13d, %rsi
incl %r13d
movl %ecx, (%rdx,%rsi,4)
shlq $0x5, %rax
movl 0xc(%r12,%rax), %eax
cmpl %ebx, %eax
jg 0xb257
incl 0x4(%rsp)
movl 0x1c(%rbp), %ecx
movq 0x38(%rbp), %r8
movq 0x50(%rsp), %rdi
movl %r13d, %esi
movq %rdi, %r13
movq %rdx, %rbx
callq 0xb38f
movq %rbx, %rdi
callq 0x2040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movl %r15d, %ebx
movq 0x40(%rsp), %r8
movl 0x34(%rsp), %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x68(%rsp), %r11
jmp 0xaeb5
movq %r12, %rdi
callq 0x2040
movq %r14, %rdi
callq 0xab71
movq 0x40(%rsp), %rdx
movq 0x78(%rsp), %r12
incq %rdx
movl 0x24(%r13), %eax
cmpq %rax, %rdx
jb 0xaca6
jmp 0xb313
movq $0x0, 0x18(%rsp)
movl $0x0, 0x4(%rsp)
movq $0x0, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x2040
movq 0x58(%rsp), %rdi
callq 0xa4cd
movq %r12, %rdi
callq 0x2040
movq 0x70(%rsp), %rax
movl 0x4(%rsp), %ecx
movl %ecx, (%rax)
movq 0x18(%rsp), %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1d3e9(%rip), %rdi     # 0x28741
leaq 0x1d34e(%rip), %rsi     # 0x286ad
leaq 0x1d3eb(%rip), %rcx     # 0x28751
movl $0x13d, %edx            # imm = 0x13D
callq 0x2160
leaq 0x1d40d(%rip), %rdi     # 0x28784
leaq 0x1d32f(%rip), %rsi     # 0x286ad
leaq 0x1d3cc(%rip), %rcx     # 0x28751
movl $0x13e, %edx            # imm = 0x13E
callq 0x2160