// File: /workspace/github_c_makefile_star2_O2/lh3[P]fermi-lite/bubble.c
// Function: mag_vh_pop_simple
int mag_vh_pop_simple(mag_t *g, uint64_t idd, float max_cov, float max_frac, int max_bdiff, int aggressive)
{
	magv_t *p = &g->v.a[idd>>1], *q[2];
	ku128_v *r;
	int i, j, k, dir[2], l[2], ret = -1;
	char *seq[2], *cov[2];
	float n_diff, r_diff, avg[2], max_n_diff = aggressive? MAX_N_DIFF * 2. : MAX_N_DIFF;

	if (p->len < 0 || p->nei[idd&1].n != 2) return ret; // deleted or no bubble
	r = &p->nei[idd&1];
	for (j = 0; j < 2; ++j) {
		uint64_t x;
		if ((int64_t)r->a[j].x < 0) return ret;
		x = mag_tid2idd(g->h, r->a[j].x);
		dir[j] = x&1;
		q[j] = &g->v.a[x>>1];
		if (q[j]->nei[0].n != 1 || q[j]->nei[1].n != 1) return ret; // no bubble
		l[j] = q[j]->len - (int)(q[j]->nei[0].a->y + q[j]->nei[1].a->y); // bubble length, excluding overlaps
	}
	if (q[0]->nei[dir[0]^1].a->x != q[1]->nei[dir[1]^1].a->x) return ret; // no bubble
	if (l[0] - l[1] > max_bdiff || l[1] - l[0] > max_bdiff) return 1; // huge bubble differences
	for (j = 0; j < 2; ++j) { // set seq[] and cov[], and compute avg[]
		if (l[j] > 0) {
			seq[j] = malloc(l[j]<<1);
			cov[j] = seq[j] + l[j];
			for (i = 0; i < l[j]; ++i) {
				seq[j][i] = q[j]->seq[i + q[j]->nei[0].a->y];
				cov[j][i] = q[j]->cov[i + q[j]->nei[0].a->y];
			}
			if (dir[j]) {
				seq_revcomp6(l[j], (uint8_t*)seq[j]);
				seq_reverse(l[j], (uint8_t*)cov[j]);
			}
			for (i = 0, avg[j] = 0.; i < l[j]; ++i) {
				--seq[j][i]; // change DNA6 encoding to DNA4 for SW below
				avg[j] += cov[j][i] - 33;
			}
			avg[j] /= l[j];
		} else { // l[j] <= 0; this may happen around a tandem repeat
			int beg, end;
			seq[j] = cov[j] = 0;
			beg = q[j]->nei[0].a->y; end = q[j]->len - q[j]->nei[1].a->y;
			if (beg > end) beg ^= end, end ^= beg, beg ^= end; // swap
			if (beg < end) {
				for (i = beg, avg[j] = 0.; i < end; ++i)
					avg[j] += q[j]->cov[i] - 33;
				avg[j] /= end - beg;
			} else avg[j] = q[j]->cov[beg] - 33; // FIXME: when q[j] is contained, weird thing may happen
		}
	}
	ret = 1;
	if (l[0] > 0 && l[1] > 0) { // then do SW to compute n_diff and r_diff
		int8_t mat[16];
		kswr_t aln;
		for (i = k = 0; i < 4; ++i)
			for (j = 0; j < 4; ++j)
				mat[k++] = i == j? 5 : -4;
		aln = ksw_align(l[0], (uint8_t*)seq[0], l[1], (uint8_t*)seq[1], 4, mat, 5, 2, 0, 0);
		n_diff = ((l[0] < l[1]? l[0] : l[1]) * 5. - aln.score) / (5. + 4.); // 5: matching score; -4: mismatchig score
		r_diff = n_diff / ((l[0] + l[1]) / 2.);
		//fprintf(stderr, "===> %f %f <===\n", n_diff, r_diff); for (j = 0; j < 2; ++j) { for (i = 0; i < l[j]; ++i) fputc("ACGTN"[(int)seq[j][i]], stderr); fputc('\n', stderr); }
	} else {
		n_diff = abs(l[0] - l[1]) * L_DIFF_COEF;
		r_diff = 1.;
		//fprintf(stderr, "---> (%d,%d) <---\n", l[0], l[1]);
	}
	if (n_diff < max_n_diff || r_diff < MAX_R_DIFF) {
		j = avg[0] < avg[1]? 0 : 1;
		if (aggressive || (avg[j] < max_cov && avg[j] / (avg[j^1] + avg[j]) < max_frac)) {
			mag_v_del(g, q[j]);
			ret = 2;
		}
	}
	free(seq[0]); free(seq[1]);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %r12d
movss %xmm1, 0x28(%rsp)
movss %xmm0, 0x2c(%rsp)
movq %rdi, %rbx
xorl %eax, %eax
testl %ecx, %ecx
sete %dl
movq 0x10(%rdi), %r8
movq %rsi, %rdi
shrq %rdi
imulq $0x68, %rdi, %rdi
pushq $-0x1
popq %r14
cmpl $0x0, (%r8,%rdi)
js 0x1093e
addq %rdi, %r8
andl $0x1, %esi
imulq $0x18, %rsi, %rsi
cmpq $0x2, 0x20(%r8,%rsi)
jne 0x1093e
movl %ecx, 0x18(%rsp)
movb %dl, %al
leaq 0x4867(%rip), %rdx      # 0x14dd0
movss (%rdx,%rax,4), %xmm0
movss %xmm0, 0x24(%rsp)
leaq (%r8,%rsi), %r15
addq $0x20, %r15
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x105f1
movq 0x10(%r15), %rax
movq (%rax,%r13,8), %rsi
testq %rsi, %rsi
js 0x1093e
movq 0x20(%rbx), %rdi
callq 0xd0f4
movl %eax, %ecx
andl $0x1, %ecx
movl %ecx, 0x38(%rsp,%r13,2)
movq 0x10(%rbx), %rcx
shrq %rax
imulq $0x68, %rax, %rdx
leaq (%rcx,%rdx), %rax
movq %rax, 0x50(%rsp,%r13,4)
cmpq $0x1, 0x20(%rcx,%rdx)
jne 0x1093e
cmpq $0x1, 0x38(%rax)
jne 0x1093e
movq 0x30(%rax), %rcx
movq 0x48(%rax), %rdx
movl 0x8(%rdx), %edx
addl 0x8(%rcx), %edx
movl (%rax), %eax
subl %edx, %eax
movl %eax, 0x30(%rsp,%r13,2)
addq $0x2, %r13
jmp 0x1057f
movslq 0x38(%rsp), %rax
xorq $0x1, %rax
imulq $0x18, %rax, %rax
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
movq 0x30(%rcx,%rax), %rax
movq (%rax), %rax
movslq 0x3c(%rsp), %rcx
xorq $0x1, %rcx
imulq $0x18, %rcx, %rcx
movq 0x30(%rdx,%rcx), %rcx
cmpq (%rcx), %rax
jne 0x1093e
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %ecx
movl %edx, %esi
subl %ecx, %esi
cmpl %r12d, %esi
setg %al
movl %ecx, 0x14(%rsp)
subl %edx, %ecx
cmpl %r12d, %ecx
setg %cl
orb %al, %cl
je 0x10656
pushq $0x1
popq %r14
jmp 0x1093e
movl %esi, 0x1c(%rsp)
movl %edx, 0x20(%rsp)
xorl %r15d, %r15d
cmpq $0x2, %r15
je 0x107b1
movslq 0x30(%rsp,%r15,4), %rbp
testq %rbp, %rbp
jle 0x10720
leal (,%rbp,2), %eax
movslq %eax, %rdi
callq 0x21a0
movq %rax, %r14
movq %rax, 0x40(%rsp,%r15,8)
movq %rax, %r13
addq %rbp, %r13
movq 0x50(%rsp,%r15,8), %rax
movq %rbp, %r12
negq %r12
xorl %ecx, %ecx
movq %r12, %rdx
addq %rcx, %rdx
je 0x106d7
movq 0x30(%rax), %rdx
movq 0x50(%rax), %rsi
addq 0x8(%rdx), %rsi
movb (%rcx,%rsi), %dl
movb %dl, (%r14,%rcx)
movq 0x30(%rax), %rdx
movq 0x58(%rax), %rsi
addq 0x8(%rdx), %rsi
movb (%rcx,%rsi), %dl
movb %dl, (%r13,%rcx)
incq %rcx
jmp 0x106a3
cmpl $0x0, 0x38(%rsp,%r15,4)
je 0x106f3
movl %ebp, %edi
movq %r14, %rsi
callq 0x37be
movl %ebp, %edi
movq %r13, %rsi
callq 0x3796
xorps %xmm0, %xmm0
xorl %eax, %eax
movq %r12, %rcx
addq %rax, %rcx
je 0x1071a
decb (%r14,%rax)
movsbl (%r13,%rax), %ecx
addl $-0x21, %ecx
cvtsi2ss %ecx, %xmm1
addss %xmm1, %xmm0
incq %rax
jmp 0x106f8
cvtsi2ss %ebp, %xmm1
jmp 0x1079e
andq $0x0, 0x40(%rsp,%r15,8)
movq 0x50(%rsp,%r15,8), %rax
movq 0x30(%rax), %rcx
movq 0x8(%rcx), %rdx
movl (%rax), %esi
movq 0x48(%rax), %rcx
subl 0x8(%rcx), %esi
cmpl %edx, %esi
movl %edx, %edi
cmovll %esi, %edi
movl %edx, %ecx
cmovgl %esi, %ecx
subl %edi, %ecx
jle 0x10786
movslq %esi, %rdi
movslq %edx, %rdx
cmpq %rdx, %rdi
movq %rdx, %rsi
cmovlq %rdi, %rsi
cmovgq %rdi, %rdx
xorps %xmm0, %xmm0
cmpl %esi, %edx
je 0x1079a
movq 0x58(%rax), %rdi
movsbl (%rdi,%rsi), %edi
addl $-0x21, %edi
cvtsi2ss %edi, %xmm1
addss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp,%r15,4)
incq %rsi
jmp 0x10763
movq 0x58(%rax), %rax
movslq %edi, %rcx
movsbl (%rax,%rcx), %eax
addl $-0x21, %eax
cvtsi2ss %eax, %xmm0
jmp 0x107a2
cvtsi2ss %ecx, %xmm1
divss %xmm1, %xmm0
movss %xmm0, 0xc(%rsp,%r15,4)
incq %r15
jmp 0x10661
movl 0x20(%rsp), %r15d
testl %r15d, %r15d
setle %al
cmpl $0x0, 0x14(%rsp)
setle %cl
orb %al, %cl
jne 0x107fb
xorl %eax, %eax
pushq $0x5
popq %rcx
xorl %edx, %edx
movl 0x18(%rsp), %ebp
cmpl $0x4, %edx
je 0x10821
cltq
xorl %esi, %esi
cmpl $0x4, %esi
je 0x107f7
cmpl %esi, %edx
movl $0xfc, %edi
cmovel %ecx, %edi
movb %dil, 0x60(%rsp,%rax)
incq %rax
incl %esi
jmp 0x107dc
incl %edx
jmp 0x107d3
movl 0x1c(%rsp), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
cvtsi2sd %eax, %xmm0
mulsd 0x45c6(%rip), %xmm0     # 0x14dd8
cvtsd2ss %xmm0, %xmm0
xorl %eax, %eax
movl 0x18(%rsp), %ebp
jmp 0x108ba
movq 0x40(%rsp), %rdx
movq 0x48(%rsp), %r8
subq $0x8, %rsp
leaq 0x68(%rsp), %rax
leaq 0x84(%rsp), %r14
pushq $0x4
popq %r9
movq %r14, %rdi
movl %r15d, %esi
movl 0x1c(%rsp), %r12d
movl %r12d, %ecx
pushq $0x0
pushq $0x0
pushq $0x2
pushq $0x5
pushq %rax
callq 0x11975
addq $0x30, %rsp
cvtsi2sdl (%r14), %xmm0
cmpl %r12d, %r15d
movl %r12d, %eax
cmovll %r15d, %eax
cvtsi2sd %eax, %xmm1
mulsd 0x4565(%rip), %xmm1     # 0x14de0
subsd %xmm0, %xmm1
divsd 0x4561(%rip), %xmm1     # 0x14de8
cvtsd2ss %xmm1, %xmm0
addl %r15d, %r12d
cvtsi2sd %r12d, %xmm1
cvtss2sd %xmm0, %xmm2
mulsd 0x4221(%rip), %xmm1     # 0x14ac0
divsd %xmm1, %xmm2
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x3afd(%rip), %xmm2     # 0x143b0
ucomisd %xmm1, %xmm2
seta %al
movss 0x24(%rsp), %xmm1
ucomiss %xmm0, %xmm1
pushq $0x1
popq %r14
ja 0x108cd
testb %al, %al
je 0x1092a
movss 0xc(%rsp), %xmm0
movss 0x10(%rsp), %xmm1
xorl %eax, %eax
ucomiss %xmm0, %xmm1
setbe %al
testl %ebp, %ebp
je 0x108f8
movq 0x50(%rsp,%rax,8), %rsi
movq %rbx, %rdi
callq 0xdbb0
pushq $0x2
popq %r14
jmp 0x1092a
movss 0xc(%rsp,%rax,4), %xmm2
movss 0x2c(%rsp), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x1092a
xorl %ecx, %ecx
ucomiss %xmm0, %xmm1
seta %cl
movss 0xc(%rsp,%rcx,4), %xmm0
addss %xmm2, %xmm0
divss %xmm0, %xmm2
movss 0x28(%rsp), %xmm0
ucomiss %xmm2, %xmm0
ja 0x108e5
movq 0x40(%rsp), %rdi
callq 0x2040
movq 0x48(%rsp), %rdi
callq 0x2040
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq