// File: /workspace/github_c_makefile_star2_O2/ToniWestbrook[P]paladin/bwamem_pair.c
// Function: mem_pair
int mem_pair(const mem_opt_t *opt, const bntseq_t *bns, const uint8_t *pac, const mem_pestat_t pes[4], bseq1_t s[2], mem_alnreg_v a[2], int id, int *sub, int *n_sub, int z[2], int n_pri[2])
{
	pair64_v v, u;
	int r, i, k, y[4], ret; // y[] keeps the last hit
	int64_t l_pac = bns->l_pac;
	kv_init(v); kv_init(u);
	for (r = 0; r < 2; ++r) { // loop through read number
		for (i = 0; i < n_pri[r]; ++i) {
			pair64_t key;
			mem_alnreg_t *e = &a[r].a[i];
			key.x = e->rb < l_pac? e->rb : (l_pac<<1) - 1 - e->rb; // forward position
			key.x = (uint64_t)e->rid<<32 | (key.x - bns->anns[e->rid].offset);
			key.y = (uint64_t)e->score << 32 | i << 2 | (e->rb >= l_pac)<<1 | r;
			kv_push(pair64_t, v, key);
		}
	}
	ks_introsort_128(v.n, v.a);
	y[0] = y[1] = y[2] = y[3] = -1;
	//for (i = 0; i < v.n; ++i) printf("[%d]\t%d\t%c%ld\n", i, (int)(v.a[i].y&1)+1, "+-"[v.a[i].y>>1&1], (long)v.a[i].x);
	for (i = 0; i < v.n; ++i) {
		for (r = 0; r < 2; ++r) { // loop through direction
			int dir = r<<1 | (v.a[i].y>>1&1), which;
			if (pes[dir].failed) continue; // invalid orientation
			which = r<<1 | ((v.a[i].y&1)^1);
			if (y[which] < 0) continue; // no previous hits
			for (k = y[which]; k >= 0; --k) { // TODO: this is a O(n^2) solution in the worst case; remember to check if this loop takes a lot of time (I doubt)
				int64_t dist;
				int q;
				double ns;
				pair64_t *p;
				if ((v.a[k].y&3) != which) continue;
				dist = (int64_t)v.a[i].x - v.a[k].x;
				//printf("%d: %lld\n", k, dist);
				if (dist > pes[dir].high) break;
				if (dist < pes[dir].low)  continue;
				ns = (dist - pes[dir].avg) / pes[dir].std;
				q = (int)((v.a[i].y>>32) + (v.a[k].y>>32) + .721 * log(2. * erfc(fabs(ns) * M_SQRT1_2)) * opt->a + .499); // .721 = 1/log(4)
				if (q < 0) q = 0;
				p = kv_pushp(pair64_t, u);
				p->y = (uint64_t)k<<32 | i;
				p->x = (uint64_t)q<<32 | (hash_64(p->y ^ id<<8) & 0xffffffffU);
				//printf("[%lld,%lld]\t%d\tdist=%ld\n", v.a[k].x, v.a[i].x, q, (long)dist);
			}
		}
		y[v.a[i].y&3] = i;
	}
	if (u.n) { // found at least one proper pair
		int tmp = opt->a + opt->b;
		tmp = tmp > opt->o_del + opt->e_del? tmp : opt->o_del + opt->e_del;
		tmp = tmp > opt->o_ins + opt->e_ins? tmp : opt->o_ins + opt->e_ins;
		ks_introsort_128(u.n, u.a);
		i = u.a[u.n-1].y >> 32; k = u.a[u.n-1].y << 32 >> 32;
		z[v.a[i].y&1] = v.a[i].y<<32>>34; // index of the best pair
		z[v.a[k].y&1] = v.a[k].y<<32>>34;
		ret = u.a[u.n-1].x >> 32;
		*sub = u.n > 1? u.a[u.n-2].x>>32 : 0;
		for (i = (long)u.n - 2, *n_sub = 0; i >= 0; --i)
			if (*sub - (int)(u.a[i].x>>32) <= tmp) ++*n_sub;
	} else ret = 0, *sub = 0, *n_sub = 0;
	free(u.a); free(v.a);
	return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r9, 0x68(%rsp)
movq %rcx, 0x60(%rsp)
movq %rsi, %r8
movq %rdi, 0x58(%rsp)
movq 0xe0(%rsp), %r9
movq (%rsi), %r10
leaq (%r10,%r10), %r11
xorl %esi, %esi
movq $0x0, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
xorl %eax, %eax
movq %r8, 0x40(%rsp)
movq %r10, 0x10(%rsp)
movq %r11, 0x38(%rsp)
cmpq $0x2, %rsi
je 0x1c39e
movq %rsi, %rcx
shlq $0x5, %rcx
movq 0x68(%rsp), %rdx
addq %rdx, %rcx
addq $0x18, %rcx
movq %rcx, 0x8(%rsp)
movq 0x18(%rsp), %rcx
leaq (%rcx,%rcx), %r14
xorl %edx, %edx
pushq $0xe
popq %rbp
xorl %ebx, %ebx
movq %rsi, 0x48(%rsp)
movq 0x18(%rsp), %rcx
leaq (%rcx,%rbx), %rdi
movslq (%r9,%rsi,4), %rcx
cmpq %rcx, %rbx
jge 0x1c391
movq %rbx, 0x50(%rsp)
movq %rdx, 0x28(%rsp)
movq 0x8(%rsp), %rcx
movq (%rcx), %rcx
movq -0x1c(%rcx,%rbp,2), %rdx
movq %rdx, %rbx
notq %rbx
addq %r11, %rbx
xorl %r12d, %r12d
cmpq %r10, %rdx
cmovlq %rdx, %rbx
setge 0x30(%rsp)
movslq -0x4(%rcx,%rbp,2), %r15
movq 0x10(%r8), %rdx
imulq $0x28, %r15, %rsi
subq (%rdx,%rsi), %rbx
movl (%rcx,%rbp,2), %r13d
cmpq 0x20(%rsp), %rdi
jne 0x1c347
testq %rdi, %rdi
movq %r14, %rsi
pushq $0x2
popq %rcx
cmoveq %rcx, %rsi
movq %rsi, 0x20(%rsp)
shlq $0x4, %rsi
movq %rax, %rdi
leaq 0x7fd1(%rip), %rdx      # 0x242f0
movl $0xc4, %ecx
leaq 0x81b6(%rip), %r8       # 0x244e1
callq 0x1e0b4
movq 0x38(%rsp), %r11
movq 0x10(%rsp), %r10
movq 0xe0(%rsp), %r9
movq 0x40(%rsp), %r8
movq 0x28(%rsp), %rdx
shlq $0x20, %r15
orq %r15, %rbx
shlq $0x20, %r13
movslq %edx, %rdx
movb 0x30(%rsp), %cl
movb %cl, %r12b
leaq (%rdx,%r12,2), %rcx
movq 0x48(%rsp), %rsi
orq %rsi, %r13
orq %rcx, %r13
movq %rbx, (%rax,%r14,8)
movq %r13, 0x8(%rax,%r14,8)
movq 0x50(%rsp), %rbx
incq %rbx
addq $0x30, %rbp
addl $0x4, %edx
addq $0x2, %r14
jmp 0x1c29e
incq %rsi
movq %rdi, 0x18(%rsp)
jmp 0x1c267
movq 0x18(%rsp), %rdi
movq %rax, %rsi
movq %rax, 0x8(%rsp)
callq 0xb6cf
movq 0x8(%rsp), %rsi
movl 0xc0(%rsp), %eax
shll $0x8, %eax
cltq
movq %rax, 0x40(%rsp)
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x70(%rsp)
xorl %r8d, %r8d
xorl %ebp, %ebp
movq $0x0, 0x30(%rsp)
xorl %r9d, %r9d
cmpq 0x18(%rsp), %r8
je 0x1c61f
movq %r8, %rax
shlq $0x4, %rax
movq %rax, 0x38(%rsp)
leaq (%rsi,%rax), %r10
xorl %ecx, %ecx
movq %r8, 0x20(%rsp)
movq %r10, 0x48(%rsp)
cmpl $0x2, %ecx
je 0x1c606
movl 0x8(%r10), %r13d
movl %r13d, %eax
shrl %eax
andl $0x1, %eax
leal (%rax,%rcx,2), %r15d
shlq $0x5, %r15
movq 0x60(%rsp), %rax
cmpl $0x0, 0x8(%rax,%r15)
jne 0x1c5ff
movq %rcx, 0x10(%rsp)
leal (%rcx,%rcx), %eax
andl $0x1, %r13d
orl %eax, %r13d
xorl $0x1, %r13d
movl 0x70(%rsp,%r13,4), %r12d
testl %r12d, %r12d
js 0x1c5fa
addq 0x60(%rsp), %r15
testl %r12d, %r12d
js 0x1c5fa
movl %r12d, %r14d
movq %r14, %rcx
shlq $0x4, %rcx
movq 0x8(%rsi,%rcx), %rax
movl %eax, %edx
andl $0x3, %edx
cmpl %r13d, %edx
jne 0x1c5f2
addq %rsi, %rcx
movq (%r10), %rdx
subq (%rcx), %rdx
movslq 0x4(%r15), %rcx
cmpq %rcx, %rdx
jg 0x1c5fa
movslq (%r15), %rcx
cmpq %rcx, %rdx
jl 0x1c5f2
movq %r9, 0x28(%rsp)
cvtsi2sd %rdx, %xmm0
subsd 0x10(%r15), %xmm0
divsd 0x18(%r15), %xmm0
andpd 0x7dfe(%rip), %xmm0     # 0x242c0
movl 0xc(%r10), %ecx
shrq $0x20, %rax
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd %xmm1, 0x50(%rsp)
mulsd 0x7d90(%rip), %xmm0     # 0x24270
callq 0x3680
addsd %xmm0, %xmm0
callq 0x3250
movq %rbp, %rsi
mulsd 0x7d7f(%rip), %xmm0     # 0x24278
movq 0x58(%rsp), %rax
cvtsi2sdl (%rax), %xmm1
mulsd %xmm0, %xmm1
addsd 0x50(%rsp), %xmm1
addsd 0x428c(%rip), %xmm1     # 0x207a0
cvttsd2si %xmm1, %ebx
testl %ebx, %ebx
movl $0x0, %eax
cmovlel %eax, %ebx
cmpq 0x30(%rsp), %rbp
jne 0x1c568
leaq (%rsi,%rsi), %rcx
testq %rsi, %rsi
pushq $0x2
popq %rax
cmoveq %rax, %rcx
movq %rcx, 0x30(%rsp)
movq %rcx, %rsi
shlq $0x4, %rsi
movq 0x28(%rsp), %rdi
leaq 0x7da1(%rip), %rdx      # 0x242f0
movl $0xdd, %ecx
leaq 0x7f86(%rip), %r8       # 0x244e1
callq 0x1e0b4
movq %rbp, %rsi
movq %rax, %r9
jmp 0x1c56d
movq 0x28(%rsp), %r9
movq 0x20(%rsp), %r8
leaq 0x1(%rsi), %rbp
shlq $0x4, %rsi
shlq $0x20, %r14
orq %r8, %r14
movq %r14, 0x8(%r9,%rsi)
shlq $0x20, %rbx
xorq 0x40(%rsp), %r14
movq %r14, %rcx
shlq $0x20, %rcx
notq %rcx
addq %r14, %rcx
movq %rcx, %rdx
shrq $0x16, %rdx
xorq %rcx, %rdx
movq %rdx, %rcx
shlq $0xd, %rcx
notq %rcx
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x8, %rdx
xorq %rcx, %rdx
leaq (%rdx,%rdx,8), %rcx
movq %rcx, %rdx
shrq $0xf, %rdx
xorq %rcx, %rdx
movq %rdx, %rcx
shlq $0x1b, %rcx
notq %rcx
addq %rdx, %rcx
movq %rcx, %rdx
shrq $0x1f, %rdx
xorl %ecx, %edx
orq %rbx, %rdx
movq %rdx, (%r9,%rsi)
movq 0x8(%rsp), %rsi
movq 0x48(%rsp), %r10
decl %r12d
jmp 0x1c45c
movq 0x10(%rsp), %rcx
incl %ecx
jmp 0x1c408
movq 0x38(%rsp), %rax
movl 0x8(%rsi,%rax), %eax
andl $0x3, %eax
movl %r8d, 0x70(%rsp,%rax,4)
incq %r8
jmp 0x1c3e1
testq %rbp, %rbp
je 0x1c702
movq 0x58(%rsp), %rdx
movl 0x4(%rdx), %eax
movl 0xc(%rdx), %ecx
addl (%rdx), %eax
addl 0x8(%rdx), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl 0x14(%rdx), %r15d
addl 0x10(%rdx), %r15d
cmpl %r15d, %ecx
cmovgl %ecx, %r15d
movq %rbp, %rdi
movq %r9, %rsi
movq %r9, %rbx
callq 0xb6cf
movq %rbx, %r9
movq %rbp, %rcx
shlq $0x4, %rcx
movq -0x8(%rcx,%rbx), %rax
movslq %eax, %rdx
sarq $0x20, %rax
shlq $0x4, %rax
movq 0x8(%rsp), %rdi
movl 0x8(%rdi,%rax), %eax
movl %eax, %esi
shrl $0x2, %esi
andl $0x1, %eax
movq 0xd8(%rsp), %r8
movl %esi, (%r8,%rax,4)
shlq $0x4, %rdx
movl 0x8(%rdi,%rdx), %eax
movl %eax, %edx
shrl $0x2, %edx
andl $0x1, %eax
movl %edx, (%r8,%rax,4)
xorl %eax, %eax
movl $0x0, %edx
cmpq $0x1, %rbp
je 0x1c6b7
movl -0x1c(%rcx,%r9), %edx
movl -0xc(%rcx,%r9), %r14d
movq 0xc8(%rsp), %rcx
movl %edx, (%rcx)
addl $-0x2, %ebp
movq 0xd0(%rsp), %rcx
andl $0x0, (%rcx)
testl %ebp, %ebp
js 0x1c71b
movq 0xc8(%rsp), %rcx
movl (%rcx), %ecx
movl %ebp, %edx
shlq $0x4, %rdx
subl 0x4(%r9,%rdx), %ecx
cmpl %r15d, %ecx
jg 0x1c6fe
incl %eax
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
decl %ebp
jmp 0x1c6d4
movq 0xc8(%rsp), %rax
andl $0x0, (%rax)
movq 0xd0(%rsp), %rax
andl $0x0, (%rax)
xorl %r14d, %r14d
movq %r9, %rdi
callq 0x3390
movq 0x8(%rsp), %rdi
callq 0x3390
movl %r14d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq