// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/bubble.c
// Function: mag_v_pop_open
void mag_v_pop_open(mag_t *g, magv_t *p, int min_elen)
{
	int i, j, k, l, dir, max_l, l_qry;
	magv_t *q, *t;
	ku128_v *r, *s;
	uint8_t *seq;
	int8_t mat[16];

	if (p->len < 0 || p->len >= min_elen) return;
	//if (p->nei[0].n && p->nei[1].n) return; // FIXME: between this and the next line, which is better?
	if (p->nei[0].n + p->nei[1].n != 1) return;
	dir = p->nei[0].n? 0 : 1;
	// initialize the scoring system
	for (i = k = 0; i < 4; ++i)
		for (j = 0; j < 4; ++j)
			mat[k++] = i == j? 5 : -4;
	
	s = &p->nei[dir];
	for (l = 0; l < s->n; ++l) { // if we use "if (p->nei[0].n + p->nei[1].n != 1)", s->n == 1
		uint64_t v;
		kswq_t *qry;
		if ((int64_t)s->a[l].x < 0) continue;
		v = mag_tid2idd(g->h, s->a[l].x);
		q = &g->v.a[v>>1];
		if (q == p || q->nei[v&1].n == 1) continue;
		// get the query ready
		max_l = (p->len - s->a[l].y) * 2;
		seq = malloc(max_l + 1);
		if (dir == 0) { // forward strand
			for (j = s->a[l].y, k = 0; j < p->len; ++j)
				seq[k++] = p->seq[j] - 1;
		} else { // reverse
			for (j = p->len - s->a[l].y - 1, k = 0; j >= 0; --j)
				seq[k++] = 4 - p->seq[j];
		}
		l_qry = k;
		qry = ksw_qinit(2, l_qry, seq, 4, mat);
		//fprintf(stderr, "===> %lld:%lld:%d[%d], %d, %ld <===\n", p->k[0], p->k[1], s->n, l, p->nsr, q->nei[v&1].n);
		//for (j = 0; j < k; ++j) fputc("ACGTN"[(int)seq[j]], stderr); fputc('\n', stderr);

		r = &q->nei[v&1];
		for (i = 0; i < r->n; ++i) {
			uint64_t w;
			kswr_t aln;
			if (r->a[i].x == p->k[dir] || (int64_t)r->a[i].x < 0) continue;
			w = mag_tid2idd(g->h, r->a[i].x);
			// get the target sequence
			t = &g->v.a[w>>1];
			if (w&1) { // reverse strand
				for (j = t->len - r->a[i].y - 1, k = 0; j >= 0 && k < max_l; --j)
					seq[k++] = 4 - t->seq[j];
			} else {
				for (j = r->a[i].y, k = 0; j < t->len && k < max_l; ++j)
					seq[k++] = t->seq[j] - 1;
			}
			aln = ksw_align(0, 0, k, seq, 4, mat, 5, 2, 0, &qry);
			//for (j = 0; j < k; ++j) fputc("ACGTN"[(int)seq[j]], stderr); fprintf(stderr, "\t%d\t%f\n", aln.score, (l_qry * 5. - aln.score) / (5. + 4.));
			if (aln.score >= l_qry * 5 / 2) {
				double r_diff, n_diff;
				n_diff = (l_qry * 5. - aln.score) / (5. + 4.); // 5: matching score; -4: mismatchig score
				r_diff = n_diff / l_qry;
				if (n_diff < MAX_N_DIFF || r_diff < MAX_R_DIFF) break;
			}
		}

		if (i != r->n) {
			// mark delete in p and delete in q
			edge_mark_del(s->a[l]);
			for (i = 0; i < r->n; ++i)
				if (r->a[i].x == p->k[dir])
					edge_mark_del(r->a[i]);
		}
		free(seq); free(qry);
	}

	for (i = 0; i < s->n; ++i)
		if (!edge_is_del(s->a[i])) break;
	if (i == s->n) mag_v_del(g, p); // p is not connected to any other vertices
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, 0x10(%rsp)
movl (%rsi), %eax
testl %eax, %eax
sets %cl
cmpl %edx, %eax
setge %al
orb %cl, %al
jne 0x10e41
movq %rsi, %rbx
movq 0x20(%rsi), %rcx
movq 0x38(%rsi), %rax
movq %rcx, 0x18(%rsp)
addq %rcx, %rax
cmpq $0x1, %rax
jne 0x10e41
leaq 0x20(%rbx), %r12
xorl %eax, %eax
pushq $0x5
popq %rcx
xorl %edx, %edx
cmpl $0x4, %edx
je 0x10af6
cltq
xorl %esi, %esi
cmpl $0x4, %esi
je 0x10af2
cmpl %esi, %edx
movl $0xfc, %edi
cmovel %ecx, %edi
movb %dil, 0x50(%rsp,%rax)
incq %rax
incl %esi
jmp 0x10ad7
incl %edx
jmp 0x10ace
xorl %ecx, %ecx
cmpq $0x0, 0x18(%rsp)
sete %cl
movq %rcx, 0x28(%rsp)
imulq $0x18, %rcx, %rax
addq %rax, %r12
xorl %r13d, %r13d
movq %r12, 0x38(%rsp)
movq %rbx, 0x20(%rsp)
movq (%r12), %rax
cmpq %r13, %rax
jbe 0x10e06
movq 0x10(%r12), %rax
movq %r13, %rbp
shlq $0x4, %rbp
movq (%rax,%rbp), %rsi
testq %rsi, %rsi
js 0x10db6
movq 0x10(%rsp), %r15
movq 0x20(%r15), %rdi
callq 0xd0f4
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %r14
addq 0x10(%r15), %r14
cmpq %rbx, %r14
je 0x10db6
andl $0x1, %eax
imulq $0x18, %rax, %rax
cmpq $0x1, 0x20(%r14,%rax)
je 0x10db6
movq %rax, (%rsp)
movq %r13, 0x48(%rsp)
movslq (%rbx), %r15
movq 0x10(%r12), %rax
movq %rbp, 0x40(%rsp)
movq 0x8(%rax,%rbp), %r12
movl %r15d, %eax
subl %r12d, %eax
movq %rax, 0x8(%rsp)
leal 0x1(,%rax,2), %eax
movslq %eax, %rdi
callq 0x21a0
movq %rax, %rbp
cmpq $0x0, 0x18(%rsp)
je 0x10be4
movslq %r12d, %rax
xorl %r13d, %r13d
movq (%rsp), %rdx
cmpq %r15, %rax
jge 0x10bdd
movq 0x50(%rbx), %rcx
movb (%rcx,%rax), %cl
decb %cl
movb %cl, (%rbp,%r13)
incq %r13
incq %rax
jmp 0x10bc2
movq 0x8(%rsp), %r15
jmp 0x10c14
notl %r12d
addl %r15d, %r12d
xorl %r13d, %r13d
movq 0x8(%rsp), %r15
testl %r12d, %r12d
js 0x10c10
movq 0x50(%rbx), %rax
movl %r12d, %ecx
movb $0x4, %dl
subb (%rax,%rcx), %dl
movb %dl, (%rbp,%r13)
incq %r13
decl %r12d
jmp 0x10bf2
movq (%rsp), %rdx
addq %rdx, %r14
addq $0x20, %r14
addl %r15d, %r15d
pushq $0x2
popq %rdi
movl %r13d, %esi
movq %rbp, %rdx
pushq $0x4
popq %rcx
leaq 0x50(%rsp), %r8
callq 0x10ec8
movq %rax, 0x30(%rsp)
leal (%r13,%r13,4), %eax
shrl %eax
movl %eax, 0x8(%rsp)
cvtsi2sd %r13d, %xmm0
movsd %xmm0, (%rsp)
movslq %r15d, %r15
xorl %r13d, %r13d
movq (%r14), %rax
cmpq %r13, %rax
jbe 0x10d95
movq 0x10(%r14), %rax
movq %r13, %r12
shlq $0x4, %r12
movq (%rax,%r12), %rsi
testq %rsi, %rsi
js 0x10d8a
movq 0x28(%rsp), %rax
cmpq 0x10(%rbx,%rax,8), %rsi
je 0x10d8a
movq 0x10(%rsp), %rbx
movq 0x20(%rbx), %rdi
callq 0xd0f4
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %rdx
addq 0x10(%rbx), %rdx
testb $0x1, %al
jne 0x10cdd
movq 0x10(%r14), %rax
movslq 0x8(%rax,%r12), %rax
xorl %ecx, %ecx
movq 0x20(%rsp), %rbx
movslq (%rdx), %rsi
cmpq %rsi, %rax
jge 0x10d13
cmpq %r15, %rcx
jge 0x10d13
movq 0x50(%rdx), %rsi
movb (%rsi,%rax), %sil
decb %sil
movb %sil, (%rbp,%rcx)
incq %rcx
incq %rax
jmp 0x10cb8
movq 0x10(%r14), %rax
movl 0x8(%rax,%r12), %eax
notl %eax
addl (%rdx), %eax
xorl %ecx, %ecx
movq 0x20(%rsp), %rbx
testl %eax, %eax
js 0x10d13
cmpq %r15, %rcx
jge 0x10d13
movq 0x50(%rdx), %rsi
movl %eax, %edi
movb $0x4, %r8b
subb (%rsi,%rdi), %r8b
movb %r8b, (%rbp,%rcx)
incq %rcx
decl %eax
jmp 0x10cf1
subq $0x8, %rsp
leaq 0x74(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %rbp, %r8
pushq $0x4
popq %r9
leaq 0x38(%rsp), %rax
pushq %rax
pushq $0x0
pushq $0x2
pushq $0x5
leaq 0x78(%rsp), %rax
pushq %rax
callq 0x11975
addq $0x30, %rsp
movl 0x6c(%rsp), %eax
cmpl 0x8(%rsp), %eax
jl 0x10d8a
cvtsi2sd %eax, %xmm1
movsd (%rsp), %xmm0
mulsd 0x4083(%rip), %xmm0     # 0x14de0
subsd %xmm1, %xmm0
divsd 0x407f(%rip), %xmm0     # 0x14de8
movsd 0x407f(%rip), %xmm1     # 0x14df0
ucomisd %xmm0, %xmm1
ja 0x10d92
divsd (%rsp), %xmm0
movsd 0x362c(%rip), %xmm1     # 0x143b0
ucomisd %xmm0, %xmm1
ja 0x10d92
incq %r13
jmp 0x10c54
movq (%r14), %rax
cmpq %r13, %rax
movq 0x38(%rsp), %r12
movq 0x48(%rsp), %r13
jne 0x10dbe
movq %rbp, %rdi
callq 0x2040
movq 0x30(%rsp), %rdi
callq 0x2040
incq %r13
jmp 0x10b1a
movq 0x10(%r12), %rcx
movq 0x40(%rsp), %rdx
movq $-0x2, (%rcx,%rdx)
andq $0x0, 0x8(%rcx,%rdx)
xorl %ecx, %ecx
subq $0x1, %rax
jb 0x10da4
movq 0x10(%r14), %rdx
movq (%rdx,%rcx), %rsi
movq 0x28(%rsp), %rdi
cmpq 0x10(%rbx,%rdi,8), %rsi
jne 0x10e00
movq $-0x2, (%rdx,%rcx)
andq $0x0, 0x8(%rdx,%rcx)
addq $0x10, %rcx
jmp 0x10dd8
pushq $0x8
popq %rcx
movq %rax, %rdx
subq $0x1, %rdx
jb 0x10e34
movq 0x10(%r12), %rsi
cmpq $-0x2, -0x8(%rsi,%rcx)
je 0x10e26
cmpq $0x0, (%rsi,%rcx)
jne 0x10e2f
addq $0x10, %rcx
movq %rdx, %rax
jmp 0x10e0c
testq %rax, %rax
jne 0x10e41
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0xdbb0
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq