// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %r9, 0x18(%rsp)
movq %rcx, %r15
movq %rsi, %r8
movq %rdi, 0x68(%rsp)
movq 0xf0(%rsp), %r9
movq (%rsi), %r10
leaq (%r10,%r10), %r11
xorl %esi, %esi
xorl %eax, %eax
xorl %edi, %edi
movq $0x0, 0x8(%rsp)
movq %rcx, 0x60(%rsp)
movq %r8, 0x48(%rsp)
movq %r10, 0x40(%rsp)
movq %r11, 0x38(%rsp)
cmpl $0x0, (%r9,%rsi,4)
jle 0x20460
movq %rsi, %rcx
shlq $0x5, %rcx
movq 0x18(%rsp), %rdx
addq %rdx, %rcx
addq $0x18, %rcx
movq %rcx, 0x28(%rsp)
movq 0x8(%rsp), %rcx
shlq $0x4, %rcx
orq $0x8, %rcx
movq %rcx, 0x20(%rsp)
movl $0x7, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
movq %rsi, 0x58(%rsp)
movq 0x28(%rsp), %rcx
movq (%rcx), %rcx
movq -0x1c(%rcx,%rbp,4), %rdx
movq %rdx, %r13
notq %r13
addq %r11, %r13
cmpq %r10, %rdx
setge 0x10(%rsp)
cmovlq %rdx, %r13
movslq -0x4(%rcx,%rbp,4), %r14
movq 0x10(%r8), %rdx
leaq (%r14,%r14,4), %rsi
subq (%rdx,%rsi,8), %r13
movq 0x8(%rsp), %rdx
addq %r15, %rdx
movl (%rcx,%rbp,4), %ebx
cmpq %rdi, %rdx
jne 0x20406
leaq (%rdi,%rdi), %rsi
testq %rdi, %rdi
movl $0x2, %ecx
cmoveq %rcx, %rsi
movq %rsi, 0x50(%rsp)
shlq $0x4, %rsi
movq %rax, %rdi
leaq 0x8e67(%rip), %rdx      # 0x29240
movl $0xc4, %ecx
leaq 0x904c(%rip), %r8       # 0x29431
callq 0x22d64
movq 0x38(%rsp), %r11
movq 0x40(%rsp), %r10
movq 0xf0(%rsp), %r9
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %rdi
shlq $0x20, %r14
orq %r14, %r13
shlq $0x20, %rbx
movslq %r12d, %rcx
xorl %edx, %edx
movb 0x10(%rsp), %sil
movb %sil, %dl
leaq (%rcx,%rdx,2), %rcx
movq 0x58(%rsp), %rsi
orq %rsi, %rbx
orq %rcx, %rbx
movq 0x20(%rsp), %rcx
addq %rax, %rcx
movq %r13, -0x8(%rcx,%r12,4)
movq %rbx, (%rcx,%r12,4)
incq %r15
movslq (%r9,%rsi,4), %rcx
addq $0x4, %r12
addq $0x18, %rbp
cmpq %rcx, %r15
jl 0x20373
addq %r15, 0x8(%rsp)
movq 0x60(%rsp), %r15
leaq 0x1(%rsi), %rcx
testq %rsi, %rsi
movq %rcx, %rsi
je 0x2032e
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rax, %rsi
movq %rax, %rbx
callq 0xc9d8
pcmpeqd %xmm0, %xmm0
movdqa %xmm0, 0x80(%rsp)
testq %r14, %r14
movq %rbx, 0x30(%rsp)
je 0x20758
movl 0xd0(%rsp), %eax
shll $0x8, %eax
cltq
movq %rax, 0x38(%rsp)
leaq 0x8(%rbx), %rax
movq %rax, 0x78(%rsp)
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
xorl %eax, %eax
xorl %r9d, %r9d
xorl %r10d, %r10d
xorl %edi, %edi
movq %rax, 0x18(%rsp)
shlq $0x4, %rax
movq %rax, 0x70(%rsp)
leaq (%rbx,%rax), %r11
xorl %r13d, %r13d
movq %r11, 0x40(%rsp)
movl 0x8(%r11), %edx
movl %edx, %eax
shrl %eax
andl $0x1, %eax
leal (%rax,%r13,2), %ecx
shlq $0x5, %rcx
cmpl $0x0, 0x8(%r15,%rcx)
je 0x20512
leal 0x1(%r13), %eax
testl %r13d, %r13d
movl %eax, %r13d
je 0x204e6
jmp 0x20728
leal (,%r13,2), %eax
andl $0x1, %edx
orl %eax, %edx
xorl $0x1, %edx
movslq 0x80(%rsp,%rdx,4), %r14
testq %r14, %r14
js 0x20501
addq %r15, %rcx
movq %rcx, 0x58(%rsp)
movq %r14, %r12
shlq $0x20, %r12
addq 0x18(%rsp), %r12
movq %r14, %rbp
shlq $0x4, %rbp
addq 0x78(%rsp), %rbp
movq %r13, 0x48(%rsp)
movq %rdx, 0x10(%rsp)
movq (%rbp), %rax
movl %eax, %ecx
andl $0x3, %ecx
movb $0x1, %bl
cmpl %edx, %ecx
jne 0x20704
movq (%r11), %rcx
subq -0x8(%rbp), %rcx
movq 0x58(%rsp), %rsi
movslq 0x4(%rsi), %rdx
cmpq %rdx, %rcx
jle 0x20588
xorl %ebx, %ebx
jmp 0x206ff
movslq (%rsi), %rdx
cmpq %rdx, %rcx
jl 0x206ff
movq %r9, 0x20(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
subsd 0x10(%rsi), %xmm0
divsd 0x18(%rsi), %xmm0
andpd 0x8c7d(%rip), %xmm0     # 0x29230
movl 0xc(%r11), %ecx
shrq $0x20, %rax
addq %rcx, %rax
cvtsi2sd %rax, %xmm1
movsd %xmm1, 0x50(%rsp)
mulsd 0x8c37(%rip), %xmm0     # 0x29208
movq %rdi, 0x28(%rsp)
movq %r10, %r13
callq 0x3680
addsd %xmm0, %xmm0
callq 0x3250
movq %r13, %r10
movq 0x28(%rsp), %rdi
mulsd 0x8c19(%rip), %xmm0     # 0x29210
movq 0x68(%rsp), %rax
xorps %xmm1, %xmm1
cvtsi2sdl (%rax), %xmm1
mulsd %xmm0, %xmm1
addsd 0x50(%rsp), %xmm1
addsd 0x518b(%rip), %xmm1     # 0x257a0
cvttsd2si %xmm1, %r13d
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
cmpq %r10, %rdi
jne 0x20671
leaq (%r10,%r10), %r15
testq %r10, %r10
movl $0x2, %eax
cmoveq %rax, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movq 0x20(%rsp), %rdi
leaq 0x8bf2(%rip), %rdx      # 0x29240
movl $0xdd, %ecx
leaq 0x8dd7(%rip), %r8       # 0x29431
callq 0x22d64
movq 0x28(%rsp), %rdi
movq %rax, %r9
movq %r15, %r10
movq 0x60(%rsp), %r15
jmp 0x20676
movq 0x20(%rsp), %r9
movabsq $-0x100000000, %r8      # imm = 0xFFFFFFFF00000000
leaq 0x1(%rdi), %rax
shlq $0x4, %rdi
movq %r12, 0x8(%r9,%rdi)
shlq $0x20, %r13
movq %r12, %rcx
xorq 0x38(%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 %r13, %rdx
movq %rdx, (%r9,%rdi)
movq %rax, %rdi
movq 0x40(%rsp), %r11
movq 0x48(%rsp), %r13
movq 0x10(%rsp), %rdx
testb %bl, %bl
je 0x20501
leaq -0x1(%r14), %rax
addq %r8, %r12
addq $-0x10, %rbp
testq %r14, %r14
movq %rax, %r14
jg 0x20559
jmp 0x20501
movq 0x70(%rsp), %rax
movq 0x30(%rsp), %rbx
movl 0x8(%rbx,%rax), %eax
andl $0x3, %eax
movq 0x18(%rsp), %rcx
movl %ecx, 0x80(%rsp,%rax,4)
movq %rcx, %rax
incq %rax
cmpq 0x8(%rsp), %rax
jne 0x204cc
jmp 0x2075d
xorl %edi, %edi
xorl %r9d, %r9d
testq %rdi, %rdi
je 0x20843
movq 0x68(%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, %r15
movq %r9, %rsi
movq %r9, %r14
callq 0xc9d8
movq %r14, %r9
movq %r15, %rax
shlq $0x4, %rax
movq -0x8(%rax,%r14), %rcx
movslq %ecx, %rdx
sarq $0x20, %rcx
shlq $0x4, %rcx
movq 0x30(%rsp), %rdi
movl 0x8(%rdi,%rcx), %ecx
movl %ecx, %esi
shrl $0x2, %esi
andl $0x1, %ecx
movq 0xe8(%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)
xorl %ecx, %ecx
cmpq $0x1, %r15
je 0x207ec
movl -0x1c(%rax,%r9), %ecx
movl -0xc(%rax,%r9), %r14d
movq 0xd8(%rsp), %rax
movl %ecx, (%rax)
addl $-0x2, %r15d
movq 0xe0(%rsp), %rax
movl $0x0, (%rax)
js 0x2085c
movl %r15d, %eax
leaq 0x1(%rax), %rcx
shlq $0x4, %rax
addq %r9, %rax
movq 0xd8(%rsp), %rdx
movl (%rdx), %edx
subl 0x4(%rax), %edx
cmpl %ebx, %edx
jg 0x20838
movq 0xe0(%rsp), %rdx
incl (%rdx)
addq $-0x10, %rax
decq %rcx
jg 0x2081d
jmp 0x2085c
xorl %r14d, %r14d
movq 0xd8(%rsp), %rax
movl %r14d, (%rax)
movq 0xe0(%rsp), %rax
movl %r14d, (%rax)
movq %r9, %rdi
callq 0x3390
movq 0x30(%rsp), %rdi
callq 0x3390
movl %r14d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq