// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %rsp
movq %rdi, 0x20(%rsp)
movq %rsi, 0x8(%rsp)
movl (%rsi), %eax
testl %eax, %eax
sets %cl
cmpl %edx, %eax
setge %al
orb %cl, %al
jne 0x13899
movq 0x8(%rsp), %rax
movq 0x20(%rax), %rcx
movq 0x38(%rax), %rax
movq %rcx, 0x38(%rsp)
addq %rcx, %rax
cmpq $0x1, %rax
jne 0x13899
movq 0x8(%rsp), %rax
addq $0x20, %rax
xorl %ecx, %ecx
movdqa 0x49a9(%rip), %xmm0     # 0x17e50
movdqa 0x49b1(%rip), %xmm1     # 0x17e60
movdqa 0x49b9(%rip), %xmm2     # 0x17e70
xorl %edx, %edx
movslq %edx, %rdx
movd %ecx, %xmm3
pshufd $0x0, %xmm3, %xmm3      # xmm3 = xmm3[0,0,0,0]
pcmpeqd %xmm0, %xmm3
packssdw %xmm3, %xmm3
packsswb %xmm3, %xmm3
movdqa %xmm3, %xmm4
pandn %xmm1, %xmm4
pand %xmm2, %xmm3
por %xmm4, %xmm3
movd %xmm3, 0x60(%rsp,%rdx)
addq $0x4, %rdx
incl %ecx
cmpl $0x4, %ecx
jne 0x134b9
xorl %r15d, %r15d
cmpq $0x0, 0x38(%rsp)
sete %r15b
leaq (%r15,%r15,2), %rcx
cmpq $0x0, (%rax,%rcx,8)
je 0x1388a
leaq (%rax,%rcx,8), %r12
xorl %r13d, %r13d
movq %r12, 0x30(%rsp)
movq %r15, 0x18(%rsp)
movq 0x10(%r12), %rax
movq %r13, %rbp
shlq $0x4, %rbp
movq (%rax,%rbp), %rsi
testq %rsi, %rsi
js 0x13856
movq 0x20(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xf0c7
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %rbx
addq 0x10(%r14), %rbx
cmpq 0x8(%rsp), %rbx
je 0x13856
andl $0x1, %eax
leaq (%rax,%rax,2), %r14
cmpq $0x1, 0x20(%rbx,%r14,8)
je 0x13856
movq %r13, 0x50(%rsp)
movq 0x8(%rsp), %rax
movq %rbp, %rcx
movl (%rax), %ebp
movq 0x10(%r12), %rax
movq %rcx, 0x48(%rsp)
movq 0x8(%rax,%rcx), %r12
movl %ebp, %eax
subl %r12d, %eax
movq %rax, 0x10(%rsp)
leal 0x1(,%rax,2), %eax
movslq %eax, %rdi
callq 0x21a0
movq %rax, %r13
cmpq $0x0, 0x38(%rsp)
je 0x135e1
cmpl %r12d, %ebp
jle 0x13617
movslq %r12d, %rax
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %rdx
addq 0x50(%rdx), %rax
xorl %edx, %edx
movb (%rax,%rdx), %sil
decb %sil
movb %sil, (%r13,%rdx)
incq %rdx
cmpq %rdx, %rcx
jne 0x135cb
jmp 0x13610
notl %r12d
addl %r12d, %ebp
js 0x13617
movl %ebp, %eax
movl 0x10(%rsp), %ecx
movq 0x8(%rsp), %rdx
addq 0x50(%rdx), %rax
xorl %edx, %edx
movb $0x4, %sil
subb (%rax), %sil
movb %sil, (%r13,%rdx)
incq %rdx
decq %rax
cmpq %rdx, %rcx
jne 0x135fa
movq 0x10(%rsp), %rbp
jmp 0x13619
xorl %ebp, %ebp
leaq (%rbx,%r14,8), %r14
addq $0x20, %r14
movl $0x2, %edi
movl %ebp, %esi
movq %r13, %rdx
movl $0x4, %ecx
leaq 0x60(%rsp), %r8
callq 0x13910
movq %rax, 0x40(%rsp)
movq (%r14), %rax
testq %rax, %rax
je 0x1383a
movq 0x10(%rsp), %rcx
addl %ecx, %ecx
leal (%rbp,%rbp,4), %edx
shrl %edx
movl %edx, 0x2c(%rsp)
cvtsi2sd %ebp, %xmm2
movq %rcx, 0x10(%rsp)
movslq %ecx, %rbx
xorl %r12d, %r12d
movsd %xmm2, 0x58(%rsp)
movq 0x10(%r14), %rcx
movq %r12, %rbp
shlq $0x4, %rbp
movq (%rcx,%rbp), %rsi
testq %rsi, %rsi
js 0x137da
movq 0x8(%rsp), %rcx
cmpq 0x10(%rcx,%r15,8), %rsi
je 0x137da
movq 0x20(%rsp), %r15
movq 0x20(%r15), %rdi
callq 0xf0c7
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %rdx
addq 0x10(%r15), %rdx
testb $0x1, %al
jne 0x13706
movq 0x10(%r14), %rax
movq 0x8(%rax,%rbp), %rax
xorl %ecx, %ecx
cmpl %eax, (%rdx)
jle 0x13747
cmpl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r15
jle 0x1374c
cltq
movl $0x1, %esi
movq 0x50(%rdx), %rcx
leaq (%rax,%rsi), %rdi
movb -0x1(%rcx,%rdi), %cl
decb %cl
movb %cl, -0x1(%r13,%rsi)
movslq (%rdx), %r8
leaq 0x1(%rsi), %rcx
cmpq %r8, %rdi
jge 0x13702
cmpq %rbx, %rsi
movq %rcx, %rsi
jl 0x136db
decl %ecx
jmp 0x1374c
movq 0x10(%r14), %rax
movl 0x8(%rax,%rbp), %eax
notl %eax
xorl %ecx, %ecx
addl (%rdx), %eax
js 0x13747
cmpl $0x0, 0x10(%rsp)
movq 0x18(%rsp), %r15
jle 0x1374c
movl %eax, %eax
xorl %ecx, %ecx
movq 0x50(%rdx), %rsi
movb $0x4, %dil
subb (%rsi,%rax), %dil
movb %dil, (%r13,%rcx)
incq %rcx
testl %eax, %eax
jle 0x1374c
decq %rax
cmpq %rbx, %rcx
jl 0x13726
jmp 0x1374c
movq 0x18(%rsp), %r15
subq $0x8, %rsp
leaq 0x84(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r13, %r8
movl $0x4, %r9d
leaq 0x48(%rsp), %rax
pushq %rax
pushq $0x0
pushq $0x2
pushq $0x5
leaq 0x88(%rsp), %rax
pushq %rax
callq 0x1442d
addq $0x30, %rsp
movl 0x7c(%rsp), %eax
cmpl 0x2c(%rsp), %eax
jge 0x13792
movq (%r14), %rax
jmp 0x137da
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
movsd 0x58(%rsp), %xmm2
movapd %xmm2, %xmm0
mulsd 0x4685(%rip), %xmm0     # 0x17e30
subsd %xmm1, %xmm0
divsd 0x4681(%rip), %xmm0     # 0x17e38
movsd 0x4681(%rip), %xmm1     # 0x17e40
ucomisd %xmm0, %xmm1
movq (%r14), %rax
ja 0x137e6
divsd %xmm2, %xmm0
movsd 0x3bdc(%rip), %xmm1     # 0x173b0
ucomisd %xmm0, %xmm1
ja 0x137e6
incq %r12
cmpq %r12, %rax
ja 0x13671
cmpq %r12, %rax
je 0x1383a
movq 0x30(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x48(%rsp), %rdx
movq $-0x2, (%rcx,%rdx)
movq $0x0, 0x8(%rcx,%rdx)
testq %rax, %rax
je 0x1383a
movq 0x10(%r14), %rcx
movq (%rcx), %rdx
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsi,%r15,8), %rdx
jne 0x13831
movq $-0x2, (%rcx)
movq $0x0, 0x8(%rcx)
addq $0x10, %rcx
decq %rax
jne 0x13813
movq %r13, %rdi
callq 0x2040
movq 0x40(%rsp), %rdi
callq 0x2040
movq 0x30(%rsp), %r12
movq 0x50(%rsp), %r13
incq %r13
movq (%r12), %rax
cmpq %r13, %rax
ja 0x1351f
testq %rax, %rax
je 0x1388a
movq 0x10(%r12), %rcx
addq $0x8, %rcx
cmpq $-0x2, -0x8(%rcx)
je 0x13881
cmpq $0x0, (%rcx)
jne 0x138ab
addq $0x10, %rcx
decq %rax
jne 0x13874
movq 0x20(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x100b2
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %rax, %rax
jne 0x13899
jmp 0x1388a