// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movl %ecx, %ebp
movl %edx, %r13d
movss %xmm1, 0x18(%rsp)
movss %xmm0, 0x1c(%rsp)
movq %rdi, %rbx
xorl %eax, %eax
testl %ecx, %ecx
sete %dl
movq 0x10(%rdi), %rcx
movq %rsi, %rdi
shrq %rdi
imulq $0x68, %rdi, %rdi
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
cmpl $0x0, (%rcx,%rdi)
js 0x12c0b
addq %rdi, %rcx
andl $0x1, %esi
leaq (%rsi,%rsi,2), %rsi
cmpq $0x2, 0x20(%rcx,%rsi,8)
jne 0x12c0b
movb %dl, %al
leaq 0x5579(%rip), %rdx      # 0x17d90
movss (%rdx,%rax,4), %xmm0
movss %xmm0, 0x14(%rsp)
leaq (%rcx,%rsi,8), %r14
addq $0x20, %r14
xorl %r15d, %r15d
movq 0x10(%r14), %rax
movq (%rax,%r15,8), %rsi
testq %rsi, %rsi
js 0x12c0b
movq 0x20(%rbx), %rdi
callq 0xeb55
movl %eax, %ecx
andl $0x1, %ecx
movl %ecx, 0x28(%rsp,%r15,2)
movq 0x10(%rbx), %rcx
shrq %rax
imulq $0x68, %rax, %rdx
leaq (%rcx,%rdx), %rax
movq %rax, 0x40(%rsp,%r15,4)
cmpq $0x1, 0x20(%rcx,%rdx)
jne 0x12c0b
cmpq $0x1, 0x38(%rax)
jne 0x12c0b
movl (%rax), %ecx
movq 0x30(%rax), %rdx
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
addl 0x8(%rdx), %eax
subl %eax, %ecx
movl %ecx, 0x20(%rsp,%r15,2)
addq $0x2, %r15
cmpq $0x2, %r15
je 0x1282d
movslq 0x28(%rsp), %rax
xorq $0x1, %rax
leaq (%rax,%rax,2), %rax
movq 0x40(%rsp), %rcx
movq 0x48(%rsp), %rdx
movq 0x30(%rcx,%rax,8), %rax
movq (%rax), %rax
movslq 0x2c(%rsp), %rcx
xorq $0x1, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x30(%rdx,%rcx,8), %rcx
cmpq (%rcx), %rax
jne 0x12c0b
movl 0x20(%rsp), %edx
movl 0x24(%rsp), %ecx
movl %edx, %esi
subl %ecx, %esi
cmpl %r13d, %esi
setg %al
movl %ecx, 0x10(%rsp)
subl %edx, %ecx
cmpl %r13d, %ecx
setg %cl
orb %al, %cl
movl $0x1, %r12d
jne 0x12c0b
movl %esi, 0x8(%rsp)
movl %edx, 0xc(%rsp)
xorl %r15d, %r15d
movslq 0x20(%rsp,%r15,4), %r12
testq %r12, %r12
jle 0x129ce
leal (%r12,%r12), %eax
movslq %eax, %rdi
callq 0x21b0
movq %rax, %r14
movq %rax, 0x30(%rsp,%r15,8)
movq %rax, %r13
addq %r12, %r13
movq 0x40(%rsp,%r15,8), %rax
xorl %ecx, %ecx
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
cmpq %rcx, %r12
jne 0x1293d
cmpl $0x0, 0x28(%rsp,%r15,4)
je 0x1298a
movl %r12d, %edi
movq %r14, %rsi
callq 0x4205
movl %r12d, %edi
movq %r13, %rsi
callq 0x41db
movl $0x0, (%rsp,%r15,4)
cmpl $0x2, %r12d
movl $0x1, %eax
cmovgel %r12d, %eax
xorps %xmm0, %xmm0
xorl %ecx, %ecx
decb (%r14,%rcx)
movsbl (%r13,%rcx), %edx
addl $-0x21, %edx
xorps %xmm1, %xmm1
cvtsi2ss %edx, %xmm1
addss %xmm1, %xmm0
incq %rcx
cmpq %rcx, %rax
jne 0x129a4
xorps %xmm1, %xmm1
cvtsi2ss %r12d, %xmm1
jmp 0x12a3b
movq $0x0, 0x30(%rsp,%r15,8)
movq 0x40(%rsp,%r15,8), %rdx
movq 0x30(%rdx), %rax
movq 0x8(%rax), %rcx
movl (%rdx), %esi
movq 0x48(%rdx), %rax
subl 0x8(%rax), %esi
cmpl %ecx, %esi
movl %ecx, %edi
cmovll %esi, %edi
movl %ecx, %eax
cmovgl %esi, %eax
subl %edi, %eax
jle 0x12a57
movl $0x0, (%rsp,%r15,4)
movslq %esi, %rsi
movslq %ecx, %rcx
cmpq %rcx, %rsi
cmovlq %rsi, %rcx
addq 0x58(%rdx), %rcx
xorps %xmm0, %xmm0
xorl %edx, %edx
movsbl (%rcx,%rdx), %esi
addl $-0x21, %esi
xorps %xmm1, %xmm1
cvtsi2ss %esi, %xmm1
addss %xmm1, %xmm0
incq %rdx
cmpl %edx, %eax
jne 0x12a1b
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
movss %xmm0, (%rsp,%r15,4)
leaq 0x1(%r15), %rax
testq %r15, %r15
movq %rax, %r15
je 0x1290e
jmp 0x12a6e
movq 0x58(%rdx), %rax
movslq %edi, %rcx
movsbl (%rax,%rcx), %eax
addl $-0x21, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
jmp 0x12a3f
movl 0xc(%rsp), %r15d
testl %r15d, %r15d
setle %al
movl 0x10(%rsp), %r12d
testl %r12d, %r12d
setle %cl
orb %al, %cl
jne 0x12b5e
xorl %eax, %eax
movl $0x5, %ecx
xorl %edx, %edx
movslq %edx, %rdx
xorl %esi, %esi
cmpl %esi, %eax
movl $0xfc, %edi
cmovel %ecx, %edi
movb %dil, 0x50(%rsp,%rdx)
incq %rdx
incl %esi
cmpl $0x4, %esi
jne 0x12a9a
incl %eax
cmpl $0x4, %eax
jne 0x12a95
movq 0x30(%rsp), %rdx
movq 0x38(%rsp), %r8
subq $0x8, %rsp
leaq 0x58(%rsp), %rax
leaq 0x74(%rsp), %r14
movq %r14, %rdi
movl %r15d, %esi
movl %r12d, %ecx
movl $0x4, %r9d
pushq $0x0
pushq $0x0
pushq $0x2
pushq $0x5
pushq %rax
callq 0x13d08
addq $0x30, %rsp
xorps %xmm0, %xmm0
cvtsi2sdl (%r14), %xmm0
cmpl %r12d, %r15d
movl %r12d, %eax
cmovll %r15d, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x528c(%rip), %xmm1     # 0x17da0
subsd %xmm0, %xmm1
divsd 0x5288(%rip), %xmm1     # 0x17da8
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
addl %r15d, %r12d
xorps %xmm1, %xmm1
cvtsi2sd %r12d, %xmm1
cvtss2sd %xmm0, %xmm2
mulsd 0x4f42(%rip), %xmm1     # 0x17a80
divsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x485b(%rip), %xmm2     # 0x173b0
ucomisd %xmm1, %xmm2
seta %al
jmp 0x12b7e
movl 0x8(%rsp), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x5220(%rip), %xmm0     # 0x17d98
cvtsd2ss %xmm0, %xmm0
xorl %eax, %eax
movss 0x14(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movl $0x1, %r12d
ja 0x12b93
testb %al, %al
je 0x12bf7
movss (%rsp), %xmm0
movss 0x4(%rsp), %xmm1
testl %ebp, %ebp
je 0x12bbf
xorl %eax, %eax
ucomiss %xmm0, %xmm1
setbe %al
movq 0x40(%rsp,%rax,8), %rsi
movq %rbx, %rdi
callq 0xfaf0
movl $0x2, %r12d
jmp 0x12bf7
xorl %eax, %eax
ucomiss %xmm0, %xmm1
setbe %al
movss (%rsp,%rax,4), %xmm2
movss 0x1c(%rsp), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x12bf7
xorl %eax, %eax
ucomiss %xmm0, %xmm1
seta %al
movss (%rsp,%rax,4), %xmm3
addss %xmm2, %xmm3
divss %xmm3, %xmm2
movss 0x18(%rsp), %xmm3
ucomiss %xmm2, %xmm3
ja 0x12ba2
movq 0x30(%rsp), %rdi
callq 0x2040
movq 0x38(%rsp), %rdi
callq 0x2040
movl %r12d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq