// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movss %xmm0, -0x1c(%rbp)
movss %xmm1, -0x20(%rbp)
movl %edx, -0x24(%rbp)
movl %ecx, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq -0x18(%rbp), %rcx
shrq %rcx
imulq $0x68, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
movl $0xffffffff, -0x68(%rbp) # imm = 0xFFFFFFFF
movl -0x28(%rbp), %eax
movsd 0x86c9(%rip), %xmm0     # 0x25cc8
movsd 0x86c9(%rip), %xmm1     # 0x25cd0
movsd %xmm1, -0x118(%rbp)
cmpl $0x0, %eax
movsd %xmm0, -0x110(%rbp)
jne 0x1d62c
movsd -0x118(%rbp), %xmm0
movsd %xmm0, -0x110(%rbp)
movsd -0x110(%rbp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0xa4(%rbp)
movq -0x30(%rbp), %rax
cmpl $0x0, (%rax)
jl 0x1d666
movq -0x30(%rbp), %rax
addq $0x20, %rax
movq -0x18(%rbp), %rcx
andq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
cmpq $0x2, (%rax)
je 0x1d671
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1ddfb
movq -0x30(%rbp), %rax
addq $0x20, %rax
movq -0x18(%rbp), %rcx
andq $0x1, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x48(%rbp)
movl $0x0, -0x50(%rbp)
cmpl $0x2, -0x50(%rbp)
jge 0x1d792
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
cmpq $0x0, (%rax)
jge 0x1d6c1
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1ddfb
movq -0x10(%rbp), %rax
movq 0x20(%rax), %rdi
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x50(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x16e30
movq %rax, -0xb0(%rbp)
movq -0xb0(%rbp), %rax
andq $0x1, %rax
movl %eax, %ecx
movslq -0x50(%rbp), %rax
movl %ecx, -0x5c(%rbp,%rax,4)
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0xb0(%rbp), %rax
shrq %rax
imulq $0x68, %rax, %rax
addq %rax, %rcx
movslq -0x50(%rbp), %rax
movq %rcx, -0x40(%rbp,%rax,8)
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
cmpq $0x1, 0x20(%rax)
jne 0x1d742
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
cmpq $0x1, 0x38(%rax)
je 0x1d74d
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1ddfb
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movl (%rax), %ecx
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rdx
movq -0x40(%rbp,%rdx,8), %rdx
movq 0x48(%rdx), %rdx
addq 0x8(%rdx), %rax
subl %eax, %ecx
movslq -0x50(%rbp), %rax
movl %ecx, -0x64(%rbp,%rax,4)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1d693
movq -0x40(%rbp), %rax
addq $0x20, %rax
movl -0x5c(%rbp), %ecx
xorl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
addq $0x20, %rcx
movl -0x58(%rbp), %edx
xorl $0x1, %edx
movslq %edx, %rdx
imulq $0x18, %rdx, %rdx
addq %rdx, %rcx
movq 0x10(%rcx), %rcx
cmpq (%rcx), %rax
je 0x1d7dd
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x1ddfb
movl -0x64(%rbp), %eax
subl -0x60(%rbp), %eax
cmpl -0x24(%rbp), %eax
jg 0x1d7f3
movl -0x60(%rbp), %eax
subl -0x64(%rbp), %eax
cmpl -0x24(%rbp), %eax
jle 0x1d7ff
movl $0x1, -0x4(%rbp)
jmp 0x1ddfb
movl $0x0, -0x50(%rbp)
cmpl $0x2, -0x50(%rbp)
jge 0x1db3e
movslq -0x50(%rbp), %rax
cmpl $0x0, -0x64(%rbp,%rax,4)
jle 0x1d9c6
movslq -0x50(%rbp), %rax
movl -0x64(%rbp,%rax,4), %eax
shll %eax
movslq %eax, %rdi
callq 0x21c0
movq %rax, %rcx
movslq -0x50(%rbp), %rax
movq %rcx, -0x80(%rbp,%rax,8)
movslq -0x50(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rcx
movslq -0x50(%rbp), %rax
movslq -0x64(%rbp,%rax,4), %rax
addq %rax, %rcx
movslq -0x50(%rbp), %rax
movq %rcx, -0x90(%rbp,%rax,8)
movl $0x0, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movslq -0x50(%rbp), %rcx
cmpl -0x64(%rbp,%rcx,4), %eax
jge 0x1d8ed
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x50(%rax), %rax
movslq -0x4c(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movq -0x40(%rbp,%rdx,8), %rdx
movq 0x30(%rdx), %rdx
addq 0x8(%rdx), %rcx
movb (%rax,%rcx), %dl
movslq -0x50(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x58(%rax), %rax
movslq -0x4c(%rbp), %rcx
movslq -0x50(%rbp), %rdx
movq -0x40(%rbp,%rdx,8), %rdx
movq 0x30(%rdx), %rdx
addq 0x8(%rdx), %rcx
movb (%rax,%rcx), %dl
movslq -0x50(%rbp), %rax
movq -0x90(%rbp,%rax,8), %rax
movslq -0x4c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1d865
movslq -0x50(%rbp), %rax
cmpl $0x0, -0x5c(%rbp,%rax,4)
je 0x1d927
movslq -0x50(%rbp), %rax
movl -0x64(%rbp,%rax,4), %edi
movslq -0x50(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rsi
callq 0x4690
movslq -0x50(%rbp), %rax
movl -0x64(%rbp,%rax,4), %edi
movslq -0x50(%rbp), %rax
movq -0x90(%rbp,%rax,8), %rsi
callq 0x4620
movl $0x0, -0x4c(%rbp)
movslq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
movl -0x4c(%rbp), %eax
movslq -0x50(%rbp), %rcx
cmpl -0x64(%rbp,%rcx,4), %eax
jge 0x1d99d
movslq -0x50(%rbp), %rax
movq -0x80(%rbp,%rax,8), %rax
movslq -0x4c(%rbp), %rcx
movb (%rax,%rcx), %dl
addb $-0x1, %dl
movb %dl, (%rax,%rcx)
movslq -0x50(%rbp), %rax
movq -0x90(%rbp,%rax,8), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
subl $0x21, %eax
cvtsi2ss %eax, %xmm0
movslq -0x50(%rbp), %rax
addss -0xa0(%rbp,%rax,4), %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1d93e
movslq -0x50(%rbp), %rax
cvtsi2ssl -0x64(%rbp,%rax,4), %xmm1
movslq -0x50(%rbp), %rax
movss -0xa0(%rbp,%rax,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
jmp 0x1db2e
movslq -0x50(%rbp), %rax
movq $0x0, -0x90(%rbp,%rax,8)
movslq -0x50(%rbp), %rax
movq $0x0, -0x80(%rbp,%rax,8)
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x30(%rax), %rax
movq 0x8(%rax), %rax
movl %eax, -0xb4(%rbp)
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movslq (%rax), %rax
movslq -0x50(%rbp), %rcx
movq -0x40(%rbp,%rcx,8), %rcx
movq 0x48(%rcx), %rcx
subq 0x8(%rcx), %rax
movl %eax, -0xb8(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jle 0x1da61
movl -0xb8(%rbp), %eax
xorl -0xb4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
xorl -0xb8(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
xorl -0xb4(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x1db00
movl -0xb4(%rbp), %eax
movl %eax, -0x4c(%rbp)
movslq -0x50(%rbp), %rax
xorps %xmm0, %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
movl -0x4c(%rbp), %eax
cmpl -0xb8(%rbp), %eax
jge 0x1dad4
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x58(%rax), %rax
movslq -0x4c(%rbp), %rcx
movsbl (%rax,%rcx), %eax
subl $0x21, %eax
cvtsi2ss %eax, %xmm0
movslq -0x50(%rbp), %rax
addss -0xa0(%rbp,%rax,4), %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1da8c
movl -0xb8(%rbp), %eax
subl -0xb4(%rbp), %eax
cvtsi2ss %eax, %xmm1
movslq -0x50(%rbp), %rax
movss -0xa0(%rbp,%rax,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0xa0(%rbp,%rax,4)
jmp 0x1db2c
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rax
movq 0x58(%rax), %rax
movslq -0xb4(%rbp), %rcx
movsbl (%rax,%rcx), %eax
subl $0x21, %eax
cvtsi2ss %eax, %xmm0
movslq -0x50(%rbp), %rax
movss %xmm0, -0xa0(%rbp,%rax,4)
jmp 0x1db2e
jmp 0x1db30
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1d806
movl $0x1, -0x68(%rbp)
cmpl $0x0, -0x64(%rbp)
jle 0x1dce6
cmpl $0x0, -0x60(%rbp)
jle 0x1dce6
movl $0x0, -0x54(%rbp)
movl $0x0, -0x4c(%rbp)
cmpl $0x4, -0x4c(%rbp)
jge 0x1dbbd
movl $0x0, -0x50(%rbp)
cmpl $0x4, -0x50(%rbp)
jge 0x1dbb0
movl -0x4c(%rbp), %edx
movl -0x50(%rbp), %esi
movl $0xfffffffc, %eax       # imm = 0xFFFFFFFC
movl $0x5, %ecx
cmpl %esi, %edx
cmovel %ecx, %eax
movb %al, %cl
movl -0x54(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x54(%rbp)
cltq
movb %cl, -0xd0(%rbp,%rax)
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1db74
jmp 0x1dbb2
movl -0x4c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4c(%rbp)
jmp 0x1db67
movl -0x64(%rbp), %esi
movq -0x80(%rbp), %rdx
movl -0x60(%rbp), %ecx
movq -0x78(%rbp), %r8
leaq -0xd0(%rbp), %rax
leaq -0x108(%rbp), %rdi
movl $0x4, %r9d
xorl %r10d, %r10d
xorl %r10d, %r10d
movq %rax, (%rsp)
movl $0x5, 0x8(%rsp)
movl $0x2, 0x10(%rsp)
movl $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
callq 0x20b50
movq -0x108(%rbp), %rax
movq %rax, -0xec(%rbp)
movq -0x100(%rbp), %rax
movq %rax, -0xe4(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0xdc(%rbp)
movl -0xf0(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0x64(%rbp), %eax
cmpl -0x60(%rbp), %eax
jge 0x1dc58
movl -0x64(%rbp), %eax
movl %eax, -0x11c(%rbp)
jmp 0x1dc61
movl -0x60(%rbp), %eax
movl %eax, -0x11c(%rbp)
movl -0x11c(%rbp), %eax
cvtsi2sd %eax, %xmm0
cvtsi2sdl -0xec(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
movsd 0x805e(%rip), %xmm2     # 0x25cf0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x8046(%rip), %xmm1     # 0x25ce8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
movss -0x94(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movl -0x64(%rbp), %eax
addl -0x60(%rbp), %eax
cvtsi2sd %eax, %xmm1
movsd 0x8010(%rip), %xmm2     # 0x25ce0
divsd %xmm2, %xmm1
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x98(%rbp)
jmp 0x1dd1f
movl -0x64(%rbp), %eax
subl -0x60(%rbp), %eax
movl %eax, %ecx
negl %ecx
cmovnsl %ecx, %eax
cvtsi2sd %eax, %xmm0
movsd 0x7fd9(%rip), %xmm1     # 0x25cd8
mulsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, -0x94(%rbp)
movss 0x7fe1(%rip), %xmm0     # 0x25cf8
movss %xmm0, -0x98(%rbp)
movss -0x94(%rbp), %xmm1
movss -0xa4(%rbp), %xmm0
ucomiss %xmm1, %xmm0
ja 0x1dd52
movss -0x98(%rbp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x75e0(%rip), %xmm0     # 0x25328
ucomisd %xmm1, %xmm0
jbe 0x1dde3
movss -0xa0(%rbp), %xmm1
movss -0x9c(%rbp), %xmm0
movl $0x1, %eax
xorl %ecx, %ecx
ucomiss %xmm1, %xmm0
cmoval %ecx, %eax
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x28(%rbp)
jne 0x1ddc8
movslq -0x50(%rbp), %rax
movss -0xa0(%rbp,%rax,4), %xmm1
movss -0x1c(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1dde1
movslq -0x50(%rbp), %rax
movss -0xa0(%rbp,%rax,4), %xmm1
movl -0x50(%rbp), %eax
xorl $0x1, %eax
cltq
movss -0xa0(%rbp,%rax,4), %xmm0
movslq -0x50(%rbp), %rax
addss -0xa0(%rbp,%rax,4), %xmm0
divss %xmm0, %xmm1
movss -0x20(%rbp), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x1dde1
movq -0x10(%rbp), %rdi
movslq -0x50(%rbp), %rax
movq -0x40(%rbp,%rax,8), %rsi
callq 0x18490
movl $0x2, -0x68(%rbp)
jmp 0x1dde3
movq -0x80(%rbp), %rdi
callq 0x2040
movq -0x78(%rbp), %rdi
callq 0x2040
movl -0x68(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)