// File: /workspace/github_c_makefile_star2_O3/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, 0x78(%rsp)
movq %rdi, 0x38(%rsp)
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 0xb00e
movq %r12, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x2180
movq %rbx, %rdi
movl $0xff, %esi
movq %r14, %rdx
callq 0x2180
movq 0x38(%rsp), %r15
movl 0x4(%r15), %eax
testq %rax, %rax
je 0xb057
movq 0x10(%r15), %rcx
addq $0x10, %rcx
addq %rax, %rax
xorl %edx, %edx
cmpl $0x0, (%rcx)
je 0xb03d
addq $0x2, %rdx
addq $0x40, %rcx
cmpq %rdx, %rax
jne 0xb029
jmp 0xb057
movslq -0x8(%rcx), %rsi
testq %rsi, %rsi
js 0xb02e
movl -0x4(%rcx), %edi
cmpl (%rbx,%rsi,4), %edi
jae 0xb02e
movl %edi, (%rbx,%rsi,4)
movl %edx, (%r12,%rsi,4)
jmp 0xb02e
movq %rbx, %rdi
callq 0x2040
movq %r15, %rdi
callq 0xa866
movq %rax, 0x60(%rsp)
movl 0x24(%r15), %eax
testl %eax, %eax
je 0xb764
xorl %edx, %edx
xorl %edi, %edi
movl $0x0, (%rsp)
movl $0x0, 0x48(%rsp)
movl $0x0, 0x44(%rsp)
xorl %ebx, %ebx
movq %r12, 0x80(%rsp)
movl (%r12,%rdx,4), %ecx
cmpl $-0x1, %ecx
je 0xb754
movq %rdi, 0x28(%rsp)
movq %rdx, 0x50(%rsp)
xorl %edi, %edi
movq %r15, %rsi
movq 0x60(%rsp), %rdx
callq 0xa8e4
movq %rax, 0x8(%rsp)
movslq (%rax), %r14
movl $0x20, %esi
movq %r14, %rdi
callq 0x21e0
movq %rax, %r12
testq %r14, %r14
movq %rbx, 0x30(%rsp)
jle 0xb724
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 0xb0f2
movl $0x0, 0x4(%r12)
testl %r14d, %r14d
jle 0xb724
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 0xb1b0
addq %rax, %rsi
movslq 0x8(%rsi), %rsi
movq 0x48(%r15), %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 0xb1a8
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 0xb199
movl %ebp, 0x4(%r11)
movl %ecx, 0x10(%r11)
addl (%r8), %r10d
cmpl %r10d, (%r11)
jge 0xb1a8
movl %r10d, (%r11)
movl %ecx, 0xc(%r11)
incq %r9
cmpq %r9, %rdx
jne 0xb160
incq %rcx
cmpq %r14, %rcx
jne 0xb12a
testl %r14d, %r14d
jle 0xb724
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
xorl %ebp, %ebp
movl $0x1, %r9d
xorl %ebx, %ebx
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x50(%rsp), %r8
movq 0x8(%rsp), %r14
movq 0x10(%r14), %r13
movq %rbp, %r11
shlq $0x4, %r11
addq %r13, %r11
movl %eax, %ecx
cmpq %rcx, %rbp
jne 0xb6c5
movq 0x10(%r15), %rsi
movl (%r11), %ecx
movl %ecx, %r15d
shrl %r15d
shlq $0x6, %r15
movl 0xc(%rsi,%r15), %edx
cmpl %r10d, %edx
jle 0xb2c9
movslq %ebx, %rax
movq %rax, 0x18(%rsp)
shlq $0x4, %rax
movq %rax, 0x70(%rsp)
movl (%r13,%rax), %eax
movl %eax, 0x10(%rsp)
movl %eax, %r14d
shrl %r14d
shlq $0x6, %r14
movl 0x8(%rsi,%r14), %edi
movl %ebp, 0x4(%rsp)
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
cmpq %rdi, %r8
jne 0xb2d3
movq %rsi, 0x20(%rsp)
addq %rsi, %r15
movl 0x8(%r15), %esi
cmpq %rsi, %r8
jne 0xb2d3
movq %r11, 0x68(%rsp)
movl (%rsp), %esi
cmpl 0x48(%rsp), %esi
movl %r9d, 0x4c(%rsp)
jne 0xb2e7
movl %esi, %ecx
sarl %ecx
addl %esi, %ecx
testl %esi, %esi
movl $0x10, %eax
cmovel %eax, %ecx
movl %ecx, 0x48(%rsp)
movslq %ecx, %rax
shlq $0x3, %rax
leaq (%rax,%rax,8), %rsi
movq 0x30(%rsp), %rdi
callq 0x22b0
movq 0x50(%rsp), %r9
movl (%rsp), %esi
movq 0x8(%rsp), %rcx
movq 0x10(%rcx), %r13
movq 0x70(%rsp), %rcx
movl (%r13,%rcx), %edi
movq 0x68(%rsp), %rcx
movl (%rcx), %ecx
movl 0xc(%r15), %edx
jmp 0xb2f3
movq 0x38(%rsp), %r15
jmp 0xb6c5
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x38(%rsp), %r15
movq 0x8(%rsp), %r14
jmp 0xb6c1
movq 0x30(%rsp), %rax
movq %r8, %r9
movl 0x10(%rsp), %edi
movq 0x20(%rsp), %r8
addq %r14, %r8
movslq %esi, %rsi
leaq (%rsi,%rsi,8), %r15
movl %r9d, (%rax,%r15,8)
movl %edi, 0xc(%rax,%r15,8)
movl %ecx, 0x10(%rax,%r15,8)
movl (%r8), %esi
movl 0xc(%r8), %ecx
addl %esi, %ecx
movl %ecx, 0x4(%rax,%r15,8)
movl %edx, 0x8(%rax,%r15,8)
movq %rbp, %rcx
shlq $0x5, %rcx
movq 0x18(%rsp), %r14
movq %r14, %rdx
shlq $0x5, %rdx
movq (%r12,%rcx), %xmm0
movq (%r12,%rdx), %xmm1
movd %esi, %xmm2
pshufd $0x50, %xmm2, %xmm2     # xmm2 = xmm2[0,0,1,1]
paddd %xmm1, %xmm2
psubd %xmm2, %xmm0
movq %rax, 0x30(%rsp)
movq %xmm0, 0x1c(%rax,%r15,8)
movl 0x4(%rsp), %eax
subl %r14d, %eax
movq %rax, 0x20(%rsp)
incl %eax
movl %eax, 0x5c(%rsp)
movslq %eax, %rdi
movl $0x8, %esi
movq %rdi, 0x10(%rsp)
callq 0x21e0
movq $0x1, (%rax)
cmpq %r14, %rbp
movl $0x7fffffff, %r11d      # imm = 0x7FFFFFFF
movq 0x8(%rsp), %r14
jle 0xb40c
movq 0x18(%rsp), %rcx
movq %rcx, %rsi
shlq $0x4, %rsi
movslq 0xc(%r13,%rsi), %rdx
testq %rdx, %rdx
jle 0xb404
addq %r13, %rsi
movslq 0x8(%rsi), %rsi
movq %rcx, %rdi
subq 0x18(%rsp), %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 0xb3fc
cmpq %r10, %rbp
jl 0xb3fc
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 0xb3ca
incq %rcx
cmpq %rbp, %rcx
jne 0xb39f
movq 0x30(%rsp), %rcx
leaq (%rcx,%r15,8), %r15
movslq 0x20(%rsp), %rcx
movq %rcx, 0x20(%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(%r15)
movslq 0x20(%r15), %r14
testq %r14, %r14
js 0xb7a5
movl 0x1c(%r15), %eax
cmpl %r14d, %eax
jl 0xb7c4
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(%r15)
sarl $0x2, %eax
addl %ecx, %eax
movslq %eax, %rdi
movl $0x4, %esi
callq 0x21e0
movq %rax, 0x28(%r15)
movq 0x10(%rsp), %rcx
leaq (%rax,%rcx,4), %rcx
movq %rcx, 0x40(%r15)
addq %r14, %rcx
incq %rcx
movq %r15, 0x10(%rsp)
movq %rcx, 0x38(%r15)
cmpq 0x18(%rsp), %rbp
movq 0x28(%rsp), %rdi
movq 0x8(%rsp), %r14
movl 0x4c(%rsp), %ecx
movq 0x38(%rsp), %r15
movl 0x5c(%rsp), %edx
jl 0xb4e3
addq 0x70(%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 0xb4c9
movq 0x10(%rsp), %rax
movl 0x18(%rax), %edx
cmpl 0x44(%rsp), %edx
jle 0xb52f
decl %edx
movl %edx, %eax
shrl %eax
orl %edx, %eax
movl %eax, %ecx
shrl $0x2, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x4, %eax
orl %ecx, %eax
movl %eax, %ecx
shrl $0x8, %ecx
orl %eax, %ecx
movl %ecx, %eax
shrl $0x10, %eax
orl %ecx, %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x44(%rsp)
leaq 0x4(,%rax,4), %rsi
callq 0x22b0
movq %rax, %rdi
movq 0x10(%rsp), %r13
movl 0x18(%r13), %edx
jmp 0xb534
movq 0x10(%rsp), %r13
testl %edx, %edx
jle 0xb554
movq 0x28(%r13), %rax
xorl %ecx, %ecx
movl (%rax,%rcx,4), %edx
shrl %edx
movl %edx, (%rdi,%rcx,4)
incq %rcx
movslq 0x18(%r13), %rdx
cmpq %rdx, %rcx
jl 0xb53e
jmp 0xb557
movslq %edx, %rdx
leaq (%rdi,%rdx,4), %rsi
movq %rdi, 0x28(%rsp)
callq 0xa50e
movslq 0x18(%r13), %rax
xorl %edx, %edx
cmpq $0x2, %rax
jl 0xb5aa
movq 0x28(%rsp), %rsi
movl 0x4(%rsi), %ecx
movl $0x1, %edx
cmpl (%rsi), %ecx
je 0xb5aa
movl $0x2, %edx
movq 0x28(%rsp), %r8
movq %rdx, %rsi
cmpq %rdx, %rax
je 0xb5a2
movl (%r8,%rsi,4), %edi
leaq 0x1(%rsi), %rdx
cmpl %ecx, %edi
movl %edi, %ecx
jne 0xb58c
xorl %edx, %edx
cmpq %rax, %rsi
setb %dl
movl %edx, 0x14(%r13)
movq 0x20(%rsp), %rdi
shlq $0x2, %rdi
callq 0x2280
cmpq 0x18(%rsp), %rbp
jle 0xb65f
xorl %esi, %esi
movl 0x4(%rsp), %edx
movslq %edx, %rcx
cmpq %rcx, %rbp
jle 0xb5ef
movq 0x10(%r14), %rcx
movl %edx, %edi
movq %rdi, %rdx
shlq $0x4, %rdx
movl (%rcx,%rdx), %ecx
movslq %esi, %rdx
incl %esi
movl %ecx, (%rax,%rdx,4)
jmp 0xb5f1
movl %edx, %edi
shlq $0x5, %rdi
movl 0x10(%r12,%rdi), %edx
cmpl %ebx, %edx
jg 0xb5cd
movl 0x20(%r13), %ecx
movq 0x40(%r13), %r8
movq %r15, %rdi
movq %rax, %rdx
movq %rax, %r13
callq 0xb7e3
movq %r13, %rdx
xorl %r13d, %r13d
cmpq 0x18(%rsp), %rbp
movl (%rsp), %esi
jle 0xb680
xorl %r13d, %r13d
movl 0x4(%rsp), %eax
movslq %eax, %rcx
cmpq %rcx, %rbp
jle 0xb64e
movq 0x10(%r14), %rcx
movl %eax, %eax
movq %rax, %rdi
shlq $0x4, %rdi
movl (%rcx,%rdi), %ecx
movslq %r13d, %rdi
incl %r13d
movl %ecx, (%rdx,%rdi,4)
jmp 0xb650
movl %eax, %eax
shlq $0x5, %rax
movl 0xc(%r12,%rax), %eax
cmpl %ebx, %eax
jg 0xb62b
jmp 0xb680
movl 0x20(%r13), %ecx
movq 0x40(%r13), %r8
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
movq %rax, %rbx
callq 0xb7e3
movq %rbx, %rdx
movl (%rsp), %esi
incl %esi
movl %esi, (%rsp)
movq 0x10(%rsp), %rax
movl 0x1c(%rax), %ecx
movq 0x38(%rax), %r8
movq %r15, %rdi
movl %r13d, %esi
movq %rdx, %rbx
callq 0xb7e3
movq %rbx, %rdi
callq 0x2040
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
movq 0x50(%rsp), %r8
movl 0x4c(%rsp), %r9d
movl $0xffffffff, %r10d      # imm = 0xFFFFFFFF
movq 0x68(%rsp), %r11
movl 0x4(%rsp), %ebx
movslq 0xc(%r11), %rcx
testq %rcx, %rcx
jle 0xb6ed
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 0xb6dc
movq 0x10(%r15), %rcx
movl (%r11), %edx
shrl %edx
shlq $0x6, %rdx
movl 0x8(%rcx,%rdx), %esi
cmpq %rsi, %r8
jne 0xb710
addq %rdx, %rcx
movl 0xc(%rcx), %ecx
cmpl %r10d, %ecx
cmovgl %ecx, %r10d
incq %rbp
movslq (%r14), %rcx
incl %r9d
cmpq %rcx, %rbp
jl 0xb1e4
jmp 0xb729
movq 0x8(%rsp), %r14
movq %r12, %rdi
callq 0x2040
movq %r14, %rdi
callq 0xaf71
movq 0x50(%rsp), %rdx
movl 0x24(%r15), %eax
movq 0x80(%rsp), %r12
movq 0x30(%rsp), %rbx
movq 0x28(%rsp), %rdi
incq %rdx
movl %eax, %ecx
cmpq %rcx, %rdx
jb 0xb09d
jmp 0xb76f
xorl %ebx, %ebx
movl $0x0, (%rsp)
xorl %edi, %edi
callq 0x2040
movq 0x60(%rsp), %rdi
callq 0xa8bc
movq %r12, %rdi
callq 0x2040
movq 0x78(%rsp), %rax
movl (%rsp), %ecx
movl %ecx, (%rax)
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1cf95(%rip), %rdi     # 0x28741
leaq 0x1cefa(%rip), %rsi     # 0x286ad
leaq 0x1cf97(%rip), %rcx     # 0x28751
movl $0x13d, %edx            # imm = 0x13D
callq 0x2160
leaq 0x1cfb9(%rip), %rdi     # 0x28784
leaq 0x1cedb(%rip), %rsi     # 0x286ad
leaq 0x1cf78(%rip), %rcx     # 0x28751
movl $0x13e, %edx            # imm = 0x13E
callq 0x2160