// File: /workspace/github_c_makefile_star2_O3/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, 0x38(%rsp)
movq %rcx, 0x68(%rsp)
movq %rsi, %r9
movq %rdi, 0x60(%rsp)
movq (%rsi), %r10
leaq (%r10,%r10), %r11
movb $0x1, %dl
xorl %edi, %edi
xorl %eax, %eax
xorl %r8d, %r8d
movq $0x0, 0x10(%rsp)
movq %rsi, 0x8(%rsp)
movq %r10, 0x48(%rsp)
movq %r11, 0x18(%rsp)
movq 0xe0(%rsp), %rcx
movl (%rcx,%rdi,4), %ecx
testl %ecx, %ecx
jle 0x2097f
movl %edx, 0x40(%rsp)
movq %rdi, %rdx
shlq $0x5, %rdx
movq 0x38(%rsp), %rsi
addq %rsi, %rdx
addq $0x18, %rdx
movq %rdx, 0x58(%rsp)
movq 0x10(%rsp), %rdx
shlq $0x4, %rdx
orq $0x8, %rdx
movq %rdx, 0x20(%rsp)
movl $0x7, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rdi, 0x28(%rsp)
movq 0x58(%rsp), %rdx
movq (%rdx), %rdx
movq -0x1c(%rdx,%rbp,4), %rsi
movq %rsi, %r13
notq %r13
addq %r11, %r13
cmpq %r10, %rsi
setge 0x30(%rsp)
cmovlq %rsi, %r13
movslq -0x4(%rdx,%rbp,4), %r14
movq 0x10(%r9), %rsi
leaq (%r14,%r14,4), %rdi
subq (%rsi,%rdi,8), %r13
movq 0x10(%rsp), %rsi
addq %r15, %rsi
movl (%rdx,%rbp,4), %ebx
cmpq %r8, %rsi
jne 0x20927
leaq (%r8,%r8), %rsi
testq %r8, %r8
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x50(%rsp)
shlq $0x4, %rsi
movq %rax, %rdi
leaq 0x8a80(%rip), %rdx      # 0x29370
movl $0xc4, %ecx
leaq 0x8c65(%rip), %r8       # 0x29561
callq 0x2324c
movq 0x18(%rsp), %r11
movq 0x48(%rsp), %r10
movq 0x8(%rsp), %r9
movq 0xe0(%rsp), %rcx
movq 0x28(%rsp), %rdi
movl (%rcx,%rdi,4), %ecx
movq 0x50(%rsp), %r8
jmp 0x2092c
movq 0x28(%rsp), %rdi
shlq $0x20, %r14
orq %r14, %r13
shlq $0x20, %rbx
movslq %r12d, %rdx
xorl %esi, %esi
movb 0x30(%rsp), %r14b
movb %r14b, %sil
leaq (%rdx,%rsi,2), %rdx
orq %rdi, %rbx
orq %rdx, %rbx
movq 0x20(%rsp), %rdx
addq %rax, %rdx
movq %r13, -0x8(%rdx,%r12,4)
movq %rbx, (%rdx,%r12,4)
incq %r15
movslq %ecx, %rdx
addq $0x4, %r12
addq $0x18, %rbp
cmpq %rdx, %r15
jl 0x2088a
addq %r15, 0x10(%rsp)
movl 0x40(%rsp), %edx
movl $0x1, %edi
xorl %r15d, %r15d
testb $0x1, %dl
movl $0x0, %edx
jne 0x20839
movq 0x10(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq %rax, %rbx
callq 0xcc4b
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x70(%rsp)
testq %r14, %r14
movq %rbx, 0x8(%rsp)
je 0x20d27
movl 0xc0(%rsp), %eax
shll $0x8, %eax
cltq
movq %rax, 0x48(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x40(%rsp)
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
xorl %eax, %eax
xorl %r13d, %r13d
xorl %r9d, %r9d
xorl %edi, %edi
movq %rbx, %rsi
movq %rax, 0x18(%rsp)
shlq $0x4, %rax
movq %rax, 0x38(%rsp)
leaq (%rsi,%rax), %r10
movb $0x1, %dl
xorl %eax, %eax
movq %r10, 0x50(%rsp)
movl 0x8(%r10), %r11d
movl %r11d, %r14d
shrl %r14d
andl $0x1, %r14d
orl %eax, %r14d
shlq $0x5, %r14
movq 0x68(%rsp), %rcx
cmpl $0x0, 0x8(%rcx,%r14)
movl %edx, 0x58(%rsp)
je 0x20a48
movl $0x2, %eax
testb $0x1, 0x58(%rsp)
movl $0x0, %edx
jne 0x20a0c
jmp 0x20c1f
andl $0x1, %r11d
orl %eax, %r11d
xorl $0x1, %r11d
movslq 0x70(%rsp,%r11,4), %r15
testq %r15, %r15
js 0x20a32
addq 0x68(%rsp), %r14
leaq 0x1(%r15), %r12
movq %r15, %rbx
shlq $0x20, %rbx
addq 0x18(%rsp), %rbx
shlq $0x4, %r15
addq 0x40(%rsp), %r15
movq %r11, 0x20(%rsp)
movq (%r15), %rax
movl %eax, %ecx
andl $0x3, %ecx
cmpl %r11d, %ecx
jne 0x20c0a
movq (%r10), %rcx
subq -0x8(%r15), %rcx
movslq 0x4(%r14), %rdx
cmpq %rdx, %rcx
jg 0x20a32
movslq (%r14), %rdx
cmpq %rdx, %rcx
jl 0x20c0a
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
subsd 0x10(%r14), %xmm0
divsd 0x18(%r14), %xmm0
andpd 0x8877(%rip), %xmm0     # 0x29340
movl 0xc(%r10), %ecx
shrq $0x20, %rax
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd %xmm1, 0x28(%rsp)
mulsd 0x87f9(%rip), %xmm0     # 0x292e0
movq %rdi, 0x30(%rsp)
movq %r9, %rbp
callq 0x3680
addsd %xmm0, %xmm0
callq 0x3250
movq %rbp, %r9
movq 0x30(%rsp), %rdi
mulsd 0x87db(%rip), %xmm0     # 0x292e8
movq 0x60(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdl (%rax), %xmm1
mulsd %xmm0, %xmm1
addsd 0x28(%rsp), %xmm1
addsd 0x4c75(%rip), %xmm1     # 0x257a0
cvttsd2si %xmm1, %ebp
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
cmpq %r9, %rdi
jne 0x20b7b
movq %r13, %rdi
leaq (%r9,%r9), %r13
testq %r9, %r9
movl $0x2, %eax
cmoveq %rax, %r13
movq %r13, %rsi
shlq $0x4, %rsi
leaq 0x8811(%rip), %rdx      # 0x29370
movl $0xdd, %ecx
leaq 0x89f6(%rip), %r8       # 0x29561
callq 0x2324c
movq 0x30(%rsp), %rdi
movq %r13, %r9
movq %rax, %r13
movq 0x8(%rsp), %rsi
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
movq 0x20(%rsp), %r11
leaq 0x1(%rdi), %rax
shlq $0x4, %rdi
movq %rbx, 0x8(%r13,%rdi)
shlq $0x20, %rbp
movq %rbx, %rcx
xorq 0x48(%rsp), %rcx
movq %rcx, %rdx
shlq $0x20, %rdx
notq %rdx
addq %rcx, %rdx
movq %rdx, %rcx
shrq $0x16, %rcx
xorq %rdx, %rcx
movq %rcx, %rdx
shlq $0xd, %rdx
notq %rdx
addq %rcx, %rdx
movq %rdx, %rcx
shrq $0x8, %rcx
xorq %rdx, %rcx
leaq (%rcx,%rcx,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 %rbp, %rdx
movq %rdx, (%r13,%rdi)
movq %rax, %rdi
movq 0x50(%rsp), %r10
addq %r8, %rbx
addq $-0x10, %r15
decq %r12
jg 0x20a80
jmp 0x20a32
movq 0x38(%rsp), %rax
movl 0x8(%rsi,%rax), %eax
andl $0x3, %eax
movq 0x18(%rsp), %rcx
movl %ecx, 0x70(%rsp,%rax,4)
movq %rcx, %rax
incq %rax
cmpq 0x10(%rsp), %rax
jne 0x209f1
testq %rdi, %rdi
je 0x20d2c
movq 0x60(%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), %ebx
addl 0x10(%rdx), %ebx
cmpl %ebx, %ecx
cmovgl %ecx, %ebx
movq %rdi, %r14
movq %r13, %rsi
callq 0xcc4b
movq %r14, %rax
shlq $0x4, %rax
movq -0x8(%rax,%r13), %rcx
movslq %ecx, %rdx
sarq $0x20, %rcx
shlq $0x4, %rcx
movq 0x8(%rsp), %rdi
movl 0x8(%rdi,%rcx), %ecx
movl %ecx, %esi
shrl $0x2, %esi
andl $0x1, %ecx
movq 0xd8(%rsp), %r8
movl %esi, (%r8,%rcx,4)
shlq $0x4, %rdx
movl 0x8(%rdi,%rdx), %ecx
movl %ecx, %edx
shrl $0x2, %edx
andl $0x1, %ecx
movl %edx, (%r8,%rcx,4)
movl -0xc(%rax,%r13), %r15d
cmpq $0x1, %r14
je 0x20d2f
movl -0x1c(%rax,%r13), %eax
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
addl $-0x2, %r14d
movq 0xd0(%rsp), %rax
movl $0x0, (%rax)
js 0x20d45
movl %r14d, %eax
leaq 0x1(%rax), %rcx
shlq $0x4, %rax
addq %r13, %rax
xorl %edx, %edx
movq 0xc8(%rsp), %rsi
movl (%rsi), %esi
subl 0x4(%rax), %esi
cmpl %ebx, %esi
jg 0x20d1c
incl %edx
movq 0xd0(%rsp), %rsi
movl %edx, (%rsi)
addq $-0x10, %rax
decq %rcx
jg 0x20cff
jmp 0x20d45
xorl %r13d, %r13d
jmp 0x20d2f
xorl %r15d, %r15d
xorl %eax, %eax
movq 0xc8(%rsp), %rcx
movl %eax, (%rcx)
movq 0xd0(%rsp), %rcx
movl %eax, (%rcx)
movq %r13, %rdi
callq 0x3390
movq 0x8(%rsp), %rdi
callq 0x3390
movl %r15d, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq