// File: /workspace/github_c_makefile_star2_O3/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 $0xa8, %rsp
movss %xmm1, 0x38(%rsp)
movss %xmm0, 0x3c(%rsp)
xorl %r8d, %r8d
testl %ecx, %ecx
sete %r9b
movq %rdi, 0x60(%rsp)
movq 0x10(%rdi), %rax
movq %rsi, %rdi
shrq %rdi
imulq $0x68, %rdi, %rdi
cmpl $0x0, (%rax,%rdi)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
js 0x13251
addq %rdi, %rax
andl $0x1, %esi
leaq (%rsi,%rsi,2), %rsi
cmpq $0x2, 0x20(%rax,%rsi,8)
jne 0x13251
movl %edx, 0x8(%rsp)
movl %ecx, 0xc(%rsp)
movb %r9b, %r8b
leaq 0x4fa1(%rip), %rdx      # 0x17e20
movss (%rdx,%r8,4), %xmm0
movss %xmm0, 0x34(%rsp)
leaq 0x20(%rax,%rsi,8), %r14
leaq 0x40(%rsp), %rbx
leaq 0x24(%rsp), %r15
leaq 0x1c(%rsp), %r13
movb $0x1, %bpl
xorl %eax, %eax
movq 0x60(%rsp), %r12
movq 0x10(%r14), %rcx
shlq $0x4, %rax
movq (%rcx,%rax), %rsi
testq %rsi, %rsi
js 0x1324c
movq 0x20(%r12), %rdi
callq 0xf0c7
movl %eax, %ecx
andl $0x1, %ecx
movl %ecx, (%r15)
movq 0x10(%r12), %rcx
shrq %rax
imulq $0x68, %rax, %rdx
leaq (%rcx,%rdx), %rax
movq %rax, (%rbx)
cmpq $0x1, 0x20(%rcx,%rdx)
jne 0x1324c
cmpq $0x1, 0x38(%rax)
jne 0x1324c
movl (%rax), %ecx
movq 0x30(%rax), %rdx
movq 0x48(%rax), %rax
movl 0x8(%rax), %eax
addl 0x8(%rdx), %eax
subl %eax, %ecx
movl %ecx, (%r13)
movl $0x1, %eax
leaq 0x18(%rsp), %r13
leaq 0x20(%rsp), %r15
leaq 0x48(%rsp), %rbx
testb $0x1, %bpl
movl $0x0, %ebp
jne 0x12ea9
movslq 0x24(%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 0x20(%rsp), %rcx
xorq $0x1, %rcx
leaq (%rcx,%rcx,2), %rcx
movq 0x30(%rdx,%rcx,8), %rcx
cmpq (%rcx), %rax
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jne 0x13251
movl 0x1c(%rsp), %edx
movl 0x18(%rsp), %esi
movl %edx, %edi
subl %esi, %edi
movl 0x8(%rsp), %r8d
cmpl %r8d, %edi
setg %al
movl %esi, %ecx
subl %edx, %ecx
cmpl %r8d, %ecx
setg %cl
orb %al, %cl
movl $0x1, %ebp
jne 0x13251
movl %edi, 0x28(%rsp)
movl %esi, 0x2c(%rsp)
movl %edx, 0x30(%rsp)
leaq 0x40(%rsp), %r15
leaq 0x24(%rsp), %rbp
leaq 0x1c(%rsp), %rax
leaq 0x50(%rsp), %r13
leaq 0x10(%rsp), %r9
movb $0x1, %r8b
movslq (%rax), %r12
testq %r12, %r12
jle 0x13084
movq %r9, %rbx
movl %r8d, 0x8(%rsp)
leal (%r12,%r12), %eax
movslq %eax, %rdi
callq 0x21a0
movq %rax, %r14
movq %rax, (%r13)
movq %rax, %r13
addq %r12, %r13
movq (%r15), %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 0x12ffa
cmpl $0x0, (%rbp)
je 0x13045
movl %r12d, %edi
movq %r14, %rsi
callq 0x468c
movl %r12d, %edi
movq %r13, %rsi
callq 0x4662
xorps %xmm0, %xmm0
xorl %eax, %eax
decb (%r14,%rax)
movsbl (%r13,%rax), %ecx
addl $-0x21, %ecx
xorps %xmm1, %xmm1
cvtsi2ss %ecx, %xmm1
addss %xmm1, %xmm0
incq %rax
cmpq %rax, %r12
jne 0x1304a
xorps %xmm1, %xmm1
cvtsi2ss %r12d, %xmm1
divss %xmm1, %xmm0
movss %xmm0, (%rbx)
movl 0x8(%rsp), %r8d
jmp 0x13106
movq $0x0, (%r13)
movq (%r15), %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 0x130ec
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 0x130c6
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
jmp 0x13101
movq 0x58(%rdx), %rax
movslq %edi, %rcx
movsbl (%rax,%rcx), %eax
addl $-0x21, %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, (%r9)
leaq 0x14(%rsp), %r9
leaq 0x68(%rsp), %r13
leaq 0x18(%rsp), %rax
leaq 0x20(%rsp), %rbp
leaq 0x48(%rsp), %r15
testb $0x1, %r8b
movl $0x0, %r8d
jne 0x12fc8
movl 0x30(%rsp), %ebp
testl %ebp, %ebp
setle %al
movl 0x2c(%rsp), %r15d
testl %r15d, %r15d
setle %cl
orb %al, %cl
jne 0x13265
xorl %eax, %eax
movdqa 0x4cfb(%rip), %xmm0     # 0x17e50
movdqa 0x4d03(%rip), %xmm1     # 0x17e60
movdqa 0x4d0b(%rip), %xmm2     # 0x17e70
xorl %ecx, %ecx
movl 0xc(%rsp), %ebx
movslq %ecx, %rcx
movd %eax, %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, 0x70(%rsp,%rcx)
addq $0x4, %rcx
incl %eax
cmpl $0x4, %eax
jne 0x1316b
movq 0x50(%rsp), %rdx
movq 0x68(%rsp), %r8
subq $0x8, %rsp
leaq 0x78(%rsp), %rax
leaq 0x94(%rsp), %r14
movq %r14, %rdi
movl %ebp, %esi
movl %r15d, %ecx
movl $0x4, %r9d
pushq $0x0
pushq $0x0
pushq $0x2
pushq $0x5
pushq %rax
callq 0x1442d
addq $0x30, %rsp
xorps %xmm0, %xmm0
cvtsi2sdl (%r14), %xmm0
cmpl %r15d, %ebp
movl %r15d, %eax
cmovll %ebp, %eax
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x4c31(%rip), %xmm1     # 0x17e30
subsd %xmm0, %xmm1
divsd 0x4c2d(%rip), %xmm1     # 0x17e38
xorps %xmm0, %xmm0
cvtsd2ss %xmm1, %xmm0
addl %ebp, %r15d
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
xorps %xmm2, %xmm2
cvtss2sd %xmm0, %xmm2
mulsd 0x48e4(%rip), %xmm1     # 0x17b10
divsd %xmm1, %xmm2
xorps %xmm1, %xmm1
cvtsd2ss %xmm2, %xmm1
cvtss2sd %xmm1, %xmm1
movsd 0x416d(%rip), %xmm2     # 0x173b0
ucomisd %xmm1, %xmm2
seta %al
jmp 0x13289
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl 0x28(%rsp), %ecx
movl %ecx, %eax
negl %eax
cmovsl %ecx, %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x4ba9(%rip), %xmm0     # 0x17e28
cvtsd2ss %xmm0, %xmm0
xorl %eax, %eax
movl 0xc(%rsp), %ebx
movss 0x34(%rsp), %xmm1
ucomiss %xmm0, %xmm1
movl $0x1, %ebp
ja 0x1329d
testb %al, %al
je 0x132fd
movss 0x10(%rsp), %xmm0
movss 0x14(%rsp), %xmm1
xorl %eax, %eax
ucomiss %xmm0, %xmm1
setbe %al
testl %ebx, %ebx
je 0x132cb
movq 0x40(%rsp,%rax,8), %rsi
movq 0x60(%rsp), %rdi
callq 0x100b2
movl $0x2, %ebp
jmp 0x132fd
movss 0x10(%rsp,%rax,4), %xmm2
movss 0x3c(%rsp), %xmm3
ucomiss %xmm2, %xmm3
jbe 0x132fd
xorl %ecx, %ecx
ucomiss %xmm0, %xmm1
seta %cl
movss 0x10(%rsp,%rcx,4), %xmm0
addss %xmm2, %xmm0
divss %xmm0, %xmm2
movss 0x38(%rsp), %xmm0
ucomiss %xmm2, %xmm0
ja 0x132b5
movq 0x50(%rsp), %rdi
callq 0x2040
movq 0x68(%rsp), %rdi
callq 0x2040
jmp 0x13251