// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x50(%rbp)
movl $0x0, -0x64(%rbp)
cmpl $0x2, -0x64(%rbp)
jge 0x2ec41
movl $0x0, -0x68(%rbp)
movl -0x68(%rbp), %eax
movq 0x30(%rbp), %rcx
movslq -0x64(%rbp), %rdx
cmpl (%rcx,%rdx,4), %eax
jge 0x2ec31
movq -0x30(%rbp), %rax
movslq -0x64(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x18(%rax), %rax
movslq -0x68(%rbp), %rcx
imulq $0x60, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movq (%rax), %rax
cmpq -0x90(%rbp), %rax
jge 0x2eadb
movq -0xa8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xe0(%rbp)
jmp 0x2eafa
movq -0x90(%rbp), %rax
shlq %rax
subq $0x1, %rax
movq -0xa8(%rbp), %rcx
subq (%rcx), %rax
movq %rax, -0xe0(%rbp)
movq -0xe0(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
movslq 0x18(%rax), %rax
shlq $0x20, %rax
movq -0xa0(%rbp), %rcx
movq -0x10(%rbp), %rdx
movq 0x10(%rdx), %rdx
movq -0xa8(%rbp), %rsi
movslq 0x18(%rsi), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
subq (%rdx), %rcx
orq %rcx, %rax
movq %rax, -0xa0(%rbp)
movq -0xa8(%rbp), %rax
movslq 0x1c(%rax), %rax
shlq $0x20, %rax
movl -0x68(%rbp), %ecx
shll $0x2, %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movq -0xa8(%rbp), %rcx
movq (%rcx), %rcx
cmpq -0x90(%rbp), %rcx
setge %cl
andb $0x1, %cl
movzbl %cl, %ecx
shll %ecx
movslq %ecx, %rcx
orq %rcx, %rax
movslq -0x64(%rbp), %rcx
orq %rcx, %rax
movq %rax, -0x98(%rbp)
movq -0x48(%rbp), %rax
cmpq -0x40(%rbp), %rax
jne 0x2ebf2
cmpq $0x0, -0x40(%rbp)
je 0x2ebb1
movq -0x40(%rbp), %rax
shlq %rax
movq %rax, -0xe8(%rbp)
jmp 0x2ebbf
movl $0x2, %eax
movq %rax, -0xe8(%rbp)
jmp 0x2ebbf
movq -0xe8(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x38(%rbp), %rdi
movq -0x40(%rbp), %rsi
shlq $0x4, %rsi
leaq 0xc54c(%rip), %rdx      # 0x3b129
movl $0xc4, %ecx
leaq 0xc731(%rip), %r8       # 0x3b31a
callq 0x31d40
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq -0x48(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x48(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq -0xa0(%rbp), %rcx
movq %rcx, (%rax)
movq -0x98(%rbp), %rcx
movq %rcx, 0x8(%rax)
jmp 0x2ec23
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x2ea7c
jmp 0x2ec33
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2ea6b
movq -0x48(%rbp), %rdi
movq -0x38(%rbp), %rsi
callq 0x100b0
movl $0xffffffff, -0x74(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x78(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x7c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x80(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x68(%rbp)
movslq -0x68(%rbp), %rax
cmpq -0x48(%rbp), %rax
jae 0x2efee
movl $0x0, -0x64(%rbp)
cmpl $0x2, -0x64(%rbp)
jge 0x2efc2
movl -0x64(%rbp), %eax
shll %eax
cltq
movq -0x38(%rbp), %rcx
movslq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
shrq %rcx
andq $0x1, %rcx
orq %rcx, %rax
movl %eax, -0xac(%rbp)
movq -0x20(%rbp), %rax
movslq -0xac(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
cmpl $0x0, 0x8(%rax)
je 0x2ecd7
jmp 0x2efb4
movl -0x64(%rbp), %eax
shll %eax
cltq
movq -0x38(%rbp), %rcx
movslq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq 0x8(%rcx), %rcx
andq $0x1, %rcx
xorq $0x1, %rcx
orq %rcx, %rax
movl %eax, -0xb0(%rbp)
movslq -0xb0(%rbp), %rax
cmpl $0x0, -0x80(%rbp,%rax,4)
jge 0x2ed15
jmp 0x2efb4
movslq -0xb0(%rbp), %rax
movl -0x80(%rbp,%rax,4), %eax
movl %eax, -0x6c(%rbp)
cmpl $0x0, -0x6c(%rbp)
jl 0x2efb2
movq -0x38(%rbp), %rax
movslq -0x6c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
andq $0x3, %rax
movslq -0xb0(%rbp), %rcx
cmpq %rcx, %rax
je 0x2ed55
jmp 0x2efa4
movq -0x38(%rbp), %rax
movslq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
movq -0x38(%rbp), %rcx
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
subq (%rcx), %rax
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0xac(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq 0x4(%rcx), %rcx
cmpq %rcx, %rax
jle 0x2eda7
jmp 0x2efb2
movq -0xb8(%rbp), %rax
movq -0x20(%rbp), %rcx
movslq -0xac(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movslq (%rcx), %rcx
cmpq %rcx, %rax
jge 0x2edcd
jmp 0x2efa4
cvtsi2sdq -0xb8(%rbp), %xmm0
movq -0x20(%rbp), %rax
movslq -0xac(%rbp), %rcx
shlq $0x5, %rcx
movsd 0x10(%rax,%rcx), %xmm2
movsd 0x18(%rax,%rcx), %xmm1
subsd %xmm2, %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq -0x38(%rbp), %rcx
movslq -0x68(%rbp), %rax
shlq $0x4, %rax
movl 0xc(%rcx,%rax), %eax
movslq -0x6c(%rbp), %rdx
shlq $0x4, %rdx
movl 0xc(%rcx,%rdx), %ecx
addq %rcx, %rax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0xf0(%rbp)
movsd -0xc8(%rbp), %xmm0
movaps 0xc2d4(%rip), %xmm1     # 0x3b110
pand %xmm1, %xmm0
movsd 0xc2a8(%rip), %xmm1     # 0x3b0f0
mulsd %xmm1, %xmm0
callq 0x3660
movaps %xmm0, %xmm1
movsd 0xb854(%rip), %xmm0     # 0x3a6b0
mulsd %xmm1, %xmm0
callq 0x3240
movsd -0xf0(%rbp), %xmm1
movaps %xmm0, %xmm2
movsd 0xc270(%rip), %xmm0     # 0x3b0e8
mulsd %xmm2, %xmm0
movq -0x8(%rbp), %rax
cvtsi2sdl (%rax), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x8914(%rip), %xmm1     # 0x377a8
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0xbc(%rbp)
cmpl $0x0, -0xbc(%rbp)
jge 0x2eeb5
movl $0x0, -0xbc(%rbp)
movq -0x60(%rbp), %rax
cmpq -0x58(%rbp), %rax
jne 0x2ef19
cmpq $0x0, -0x58(%rbp)
je 0x2eed6
movq -0x58(%rbp), %rax
shlq %rax
movq %rax, -0xf8(%rbp)
jmp 0x2eee4
movl $0x2, %eax
movq %rax, -0xf8(%rbp)
jmp 0x2eee4
movq -0xf8(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
shlq $0x4, %rsi
leaq 0xc227(%rip), %rdx      # 0x3b129
movl $0xdd, %ecx
leaq 0xc40c(%rip), %r8       # 0x3b31a
callq 0x31d40
movq %rax, -0x50(%rbp)
jmp 0x2ef1b
jmp 0x2ef1b
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
movq %rcx, %rdx
addq $0x1, %rdx
movq %rdx, -0x60(%rbp)
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movslq -0x6c(%rbp), %rcx
shlq $0x20, %rcx
movslq -0x68(%rbp), %rax
orq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, 0x8(%rax)
movslq -0xbc(%rbp), %rax
shlq $0x20, %rax
movq %rax, -0x100(%rbp)
movq -0xd0(%rbp), %rax
movq 0x8(%rax), %rdi
movl 0x10(%rbp), %eax
shll $0x8, %eax
cltq
xorq %rax, %rdi
callq 0x2f260
movq -0x100(%rbp), %rcx
movabsq $0xffffffff, %rdx       # imm = 0xFFFFFFFF
andq %rdx, %rax
orq %rax, %rcx
movq -0xd0(%rbp), %rax
movq %rcx, (%rax)
movl -0x6c(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x2ed23
jmp 0x2efb4
movl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x64(%rbp)
jmp 0x2ec86
movl -0x68(%rbp), %ecx
movq -0x38(%rbp), %rax
movslq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rax
movq 0x8(%rax), %rax
andq $0x3, %rax
movl %ecx, -0x80(%rbp,%rax,4)
movl -0x68(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x2ec71
cmpq $0x0, -0x60(%rbp)
je 0x2f21a
movq -0x8(%rbp), %rax
movl (%rax), %eax
movq -0x8(%rbp), %rcx
addl 0x4(%rcx), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x8(%rcx), %ecx
movq -0x8(%rbp), %rdx
addl 0xc(%rdx), %ecx
cmpl %ecx, %eax
jle 0x2f032
movl -0xd4(%rbp), %eax
movl %eax, -0x104(%rbp)
jmp 0x2f046
movq -0x8(%rbp), %rax
movl 0x8(%rax), %eax
movq -0x8(%rbp), %rcx
addl 0xc(%rcx), %eax
movl %eax, -0x104(%rbp)
movl -0x104(%rbp), %eax
movl %eax, -0xd4(%rbp)
movl -0xd4(%rbp), %eax
movq -0x8(%rbp), %rcx
movl 0x10(%rcx), %ecx
movq -0x8(%rbp), %rdx
addl 0x14(%rdx), %ecx
cmpl %ecx, %eax
jle 0x2f078
movl -0xd4(%rbp), %eax
movl %eax, -0x108(%rbp)
jmp 0x2f08c
movq -0x8(%rbp), %rax
movl 0x10(%rax), %eax
movq -0x8(%rbp), %rcx
addl 0x14(%rcx), %eax
movl %eax, -0x108(%rbp)
movl -0x108(%rbp), %eax
movl %eax, -0xd4(%rbp)
movq -0x60(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0x100b0
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shrq $0x20, %rax
movl %eax, -0x68(%rbp)
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq $0x20, %rax
shrq $0x20, %rax
movl %eax, -0x6c(%rbp)
movq -0x38(%rbp), %rax
movslq -0x68(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq $0x20, %rax
shrq $0x22, %rax
movl %eax, %edx
movq 0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x68(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movq 0x8(%rcx), %rcx
andq $0x1, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x38(%rbp), %rax
movslq -0x6c(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rax
shlq $0x20, %rax
shrq $0x22, %rax
movl %eax, %edx
movq 0x28(%rbp), %rax
movq -0x38(%rbp), %rcx
movslq -0x6c(%rbp), %rsi
shlq $0x4, %rsi
addq %rsi, %rcx
movq 0x8(%rcx), %rcx
andq $0x1, %rcx
movl %edx, (%rax,%rcx,4)
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
subq $0x1, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movl %eax, -0x84(%rbp)
cmpq $0x1, -0x60(%rbp)
jbe 0x2f1a5
movq -0x50(%rbp), %rax
movq -0x60(%rbp), %rcx
subq $0x2, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq (%rax), %rax
shrq $0x20, %rax
movq %rax, -0x110(%rbp)
jmp 0x2f1b0
xorl %eax, %eax
movq %rax, -0x110(%rbp)
jmp 0x2f1b0
movq -0x110(%rbp), %rax
movl %eax, %ecx
movq 0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x60(%rbp), %rax
subq $0x2, %rax
movl %eax, -0x68(%rbp)
movq 0x20(%rbp), %rax
movl $0x0, (%rax)
cmpl $0x0, -0x68(%rbp)
jl 0x2f218
movq 0x18(%rbp), %rax
movl (%rax), %eax
movq -0x50(%rbp), %rcx
movslq -0x68(%rbp), %rdx
shlq $0x4, %rdx
addq %rdx, %rcx
movq (%rcx), %rcx
shrq $0x20, %rcx
subl %ecx, %eax
cmpl -0xd4(%rbp), %eax
jg 0x2f20b
movq 0x20(%rbp), %rax
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x2f20d
movl -0x68(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x68(%rbp)
jmp 0x2f1d4
jmp 0x2f238
movl $0x0, -0x84(%rbp)
movq 0x18(%rbp), %rax
movl $0x0, (%rax)
movq 0x20(%rbp), %rax
movl $0x0, (%rax)
movq -0x50(%rbp), %rdi
callq 0x3380
movq -0x38(%rbp), %rdi
callq 0x3380
movl -0x84(%rbp), %eax
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)