// File: /workspace/github_c_makefile_star2_O1/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)
movl (%rsi), %eax
testl %eax, %eax
sets %cl
cmpl %edx, %eax
setge %al
orb %cl, %al
jne 0x1319f
movq %rsi, %rbp
movq 0x20(%rsi), %rcx
movq 0x38(%rsi), %rax
movq %rcx, 0x30(%rsp)
addq %rcx, %rax
cmpq $0x1, %rax
jne 0x1319f
leaq 0x20(%rbp), %rax
xorl %ecx, %ecx
movl $0x5, %edx
xorl %esi, %esi
movslq %esi, %rsi
xorl %edi, %edi
cmpl %edi, %ecx
movl $0xfc, %r8d
cmovel %edx, %r8d
movb %r8b, 0x60(%rsp,%rsi)
incq %rsi
incl %edi
cmpl $0x4, %edi
jne 0x12da9
incl %ecx
cmpl $0x4, %ecx
jne 0x12da4
xorl %r15d, %r15d
cmpq $0x0, 0x30(%rsp)
sete %r15b
leaq (%r15,%r15,2), %rcx
leaq (%rax,%rcx,8), %r13
movq (%r13), %rax
testq %rax, %rax
je 0x1315e
xorl %r12d, %r12d
movq %rbp, 0x40(%rsp)
movq %r13, 0x28(%rsp)
movq %r15, 0x10(%rsp)
movq 0x10(%r13), %rax
movq %r12, %rcx
shlq $0x4, %rcx
movq %rcx, 0x38(%rsp)
movq (%rax,%rcx), %rsi
testq %rsi, %rsi
js 0x1314e
movq 0x20(%rsp), %r14
movq 0x20(%r14), %rdi
callq 0xeb55
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %rbx
addq 0x10(%r14), %rbx
cmpq %rbp, %rbx
je 0x1314e
andl $0x1, %eax
leaq (%rax,%rax,2), %rax
cmpq $0x1, 0x20(%rbx,%rax,8)
je 0x1314e
movq %rax, 0x18(%rsp)
movq %r12, 0x50(%rsp)
movl (%rbp), %r12d
movq 0x10(%r13), %rax
movq 0x38(%rsp), %rcx
movq 0x8(%rax,%rcx), %r14
movl %r12d, %eax
subl %r14d, %eax
movq %rax, 0x8(%rsp)
leal 0x1(,%rax,2), %eax
movslq %eax, %rdi
callq 0x21b0
movq %rax, %r13
cmpq $0x0, 0x30(%rsp)
je 0x12eb7
subl %r14d, %r12d
jle 0x12ee3
movslq %r14d, %rax
addq 0x50(%rbp), %rax
xorl %ebp, %ebp
movb (%rax,%rbp), %cl
decb %cl
movb %cl, (%r13,%rbp)
incq %rbp
cmpq %rbp, %r12
jne 0x12ea3
jmp 0x12ee5
notl %r14d
addl %r14d, %r12d
js 0x12ee3
movl %r12d, %eax
addq 0x50(%rbp), %rax
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movb $0x4, %cl
subb (%rax), %cl
movb %cl, (%r13,%rbp)
incq %rbp
decq %rax
cmpq %rbp, %r14
jne 0x12ecd
jmp 0x12eea
xorl %ebp, %ebp
movq 0x8(%rsp), %r14
movq 0x18(%rsp), %rax
leaq (%rbx,%rax,8), %rbx
addq $0x20, %rbx
movl $0x2, %edi
movl %ebp, %esi
movq %r13, %rdx
movl $0x4, %ecx
leaq 0x60(%rsp), %r8
callq 0x13210
movq %rax, 0x48(%rsp)
cmpq $0x0, (%rbx)
je 0x130d8
addl %r14d, %r14d
leal (%rbp,%rbp,4), %eax
shrl %eax
movl %eax, 0x18(%rsp)
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x58(%rsp)
movq %r14, 0x8(%rsp)
movslq %r14d, %r12
xorl %r14d, %r14d
movq 0x10(%rbx), %rax
movq %r14, %rbp
shlq $0x4, %rbp
movq (%rax,%rbp), %rsi
movl $0x13, %eax
testq %rsi, %rsi
js 0x130c5
movq 0x40(%rsp), %rcx
cmpq 0x10(%rcx,%r15,8), %rsi
je 0x130c5
movq 0x20(%rsp), %r15
movq 0x20(%r15), %rdi
callq 0xeb55
movq %rax, %rcx
shrq %rcx
imulq $0x68, %rcx, %rdx
addq 0x10(%r15), %rdx
testb $0x1, %al
jne 0x12fdb
movq 0x10(%rbx), %rax
movq 0x8(%rax,%rbp), %rax
xorl %ecx, %ecx
cmpl %eax, (%rdx)
jle 0x1301c
cmpl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r15
jle 0x13021
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 0x12fd7
cmpq %r12, %rsi
movq %rcx, %rsi
jl 0x12fb0
decl %ecx
jmp 0x13021
movq 0x10(%rbx), %rax
movl 0x8(%rax,%rbp), %eax
notl %eax
xorl %ecx, %ecx
addl (%rdx), %eax
js 0x1301c
cmpl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %r15
jle 0x13021
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 0x13021
decq %rax
cmpq %r12, %rcx
jl 0x12ffb
jmp 0x13021
movq 0x10(%rsp), %r15
subq $0x8, %rsp
leaq 0x84(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r13, %r8
movl $0x4, %r9d
leaq 0x50(%rsp), %rax
pushq %rax
pushq $0x0
pushq $0x2
pushq $0x5
leaq 0x88(%rsp), %rax
pushq %rax
callq 0x13d08
addq $0x30, %rsp
movl 0x7c(%rsp), %eax
cmpl 0x18(%rsp), %eax
jl 0x130c3
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd 0x58(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd 0x4d25(%rip), %xmm1     # 0x17da0
subsd %xmm0, %xmm1
divsd 0x4d21(%rip), %xmm1     # 0x17da8
movapd %xmm1, %xmm0
divsd %xmm2, %xmm0
movsd 0x4d19(%rip), %xmm2     # 0x17db0
cmpnltpd %xmm2, %xmm1
movsd 0x430c(%rip), %xmm2     # 0x173b0
cmpnltpd %xmm2, %xmm0
andpd %xmm1, %xmm0
movd %xmm0, %ecx
movl $0x11, %eax
testb $0x1, %cl
movl $0x0, %ecx
cmovnel %ecx, %eax
je 0x130c5
xorl %eax, %eax
cmpl $0x11, %eax
je 0x130db
incq %r14
cmpq %r14, (%rbx)
ja 0x12f41
jmp 0x130db
xorl %r14d, %r14d
movq (%rbx), %rax
cmpq %r14, %rax
movq 0x40(%rsp), %rbp
movq 0x50(%rsp), %r12
je 0x13137
movq 0x28(%rsp), %rcx
movq 0x10(%rcx), %rcx
movq 0x38(%rsp), %rdx
movq $-0x2, (%rcx,%rdx)
movq $0x0, 0x8(%rcx,%rdx)
testq %rax, %rax
je 0x13137
movq 0x10(%rbx), %rcx
movq (%rcx), %rdx
cmpq 0x10(%rbp,%r15,8), %rdx
jne 0x1312e
movq $-0x2, (%rcx)
movq $0x0, 0x8(%rcx)
addq $0x10, %rcx
decq %rax
jne 0x13115
movq %r13, %rdi
callq 0x2040
movq 0x48(%rsp), %rdi
callq 0x2040
movq 0x28(%rsp), %r13
incq %r12
movq (%r13), %rax
cmpq %r12, %rax
ja 0x12dff
testq %rax, %rax
je 0x1318b
movq 0x10(%r13), %rdx
addq $0x8, %rdx
xorl %ecx, %ecx
cmpq $-0x2, -0x8(%rdx)
je 0x1317a
cmpq $0x0, (%rdx)
jne 0x1318d
incq %rcx
addq $0x10, %rdx
cmpq %rcx, %rax
jne 0x1316d
movq %rax, %rcx
jmp 0x1318d
xorl %ecx, %ecx
cmpq %rcx, %rax
jne 0x1319f
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0xfaf0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq