// File: /workspace/github_c_makefile_star2_O1/ToniWestbrook[P]paladin/bwamem_pair.c
// Function: mem_pestat
void mem_pestat(const mem_opt_t *opt, int64_t l_pac, int n, const mem_alnreg_v *regs, mem_pestat_t pes[4])
{
	int i, d, max;
	uint64_v isize[4];
	memset(pes, 0, 4 * sizeof(mem_pestat_t));
	memset(isize, 0, sizeof(kvec_t(int)) * 4);
	printf("WARNING\n");
	for (i = 0; i < n>>1; ++i) {
		int dir;
		int64_t is;
		mem_alnreg_v *r[2];
		r[0] = (mem_alnreg_v*)&regs[i<<1|0];
		r[1] = (mem_alnreg_v*)&regs[i<<1|1];
		if (r[0]->n == 0 || r[1]->n == 0) continue;
		if (cal_sub(opt, r[0]) > MIN_RATIO * r[0]->a[0].score) continue;
		if (cal_sub(opt, r[1]) > MIN_RATIO * r[1]->a[0].score) continue;
		if (r[0]->a[0].rid != r[1]->a[0].rid) continue; // not on the same chr
		dir = mem_infer_dir(l_pac, r[0]->a[0].rb, r[1]->a[0].rb, &is);
		if (is && is <= opt->max_ins) kv_push(uint64_t, isize[dir], is);
	}
	if (bwa_verbose >= 3) fprintf(stderr, "[M::%s] # candidate unique pairs for (FF, FR, RF, RR): (%ld, %ld, %ld, %ld)\n", __func__, isize[0].n, isize[1].n, isize[2].n, isize[3].n);
	for (d = 0; d < 4; ++d) { // TODO: this block is nearly identical to the one in bwtsw2_pair.c. It would be better to merge these two.
		mem_pestat_t *r = &pes[d];
		uint64_v *q = &isize[d];
		int p25, p50, p75, x;
		if (q->n < MIN_DIR_CNT) {
			fprintf(stderr, "[M::%s] skip orientation %c%c as there are not enough pairs\n", __func__, "FR"[d>>1&1], "FR"[d&1]);
			r->failed = 1;
			free(q->a);
			continue;
		} else fprintf(stderr, "[M::%s] analyzing insert size distribution for orientation %c%c...\n", __func__, "FR"[d>>1&1], "FR"[d&1]);
		ks_introsort_64(q->n, q->a);
		p25 = q->a[(int)(.25 * q->n + .499)];
		p50 = q->a[(int)(.50 * q->n + .499)];
		p75 = q->a[(int)(.75 * q->n + .499)];
		r->low  = (int)(p25 - OUTLIER_BOUND * (p75 - p25) + .499);
		if (r->low < 1) r->low = 1;
		r->high = (int)(p75 + OUTLIER_BOUND * (p75 - p25) + .499);
		fprintf(stderr, "[M::%s] (25, 50, 75) percentile: (%d, %d, %d)\n", __func__, p25, p50, p75);
		fprintf(stderr, "[M::%s] low and high boundaries for computing mean and std.dev: (%d, %d)\n", __func__, r->low, r->high);
		for (i = x = 0, r->avg = 0; i < q->n; ++i)
			if (q->a[i] >= r->low && q->a[i] <= r->high)
				r->avg += q->a[i], ++x;
		r->avg /= x;
		for (i = 0, r->std = 0; i < q->n; ++i)
			if (q->a[i] >= r->low && q->a[i] <= r->high)
				r->std += (q->a[i] - r->avg) * (q->a[i] - r->avg);
		r->std = sqrt(r->std / x);
		fprintf(stderr, "[M::%s] mean and std.dev: (%.2f, %.2f)\n", __func__, r->avg, r->std);
		r->low  = (int)(p25 - MAPPING_BOUND * (p75 - p25) + .499);
		r->high = (int)(p75 + MAPPING_BOUND * (p75 - p25) + .499);
		if (r->low  > r->avg - MAX_STDDEV * r->std) r->low  = (int)(r->avg - MAX_STDDEV * r->std + .499);
		if (r->high < r->avg - MAX_STDDEV * r->std) r->high = (int)(r->avg + MAX_STDDEV * r->std + .499);
		if (r->low < 1) r->low = 1;
		fprintf(stderr, "[M::%s] low and high boundaries for proper pairs: (%d, %d)\n", __func__, r->low, r->high);
		free(q->a);
	}
	for (d = 0, max = 0; d < 4; ++d)
		max = max > isize[d].n? max : isize[d].n;
	for (d = 0; d < 4; ++d)
		if (pes[d].failed == 0 && isize[d].n < max * MIN_DIR_RATIO) {
			pes[d].failed = 1;
			fprintf(stderr, "[M::%s] skip orientation %c%c\n", __func__, "FR"[d>>1&1], "FR"[d&1]);
		}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, %rbx
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, (%rsp)
movq %rdi, %r12
xorpd %xmm0, %xmm0
movupd %xmm0, 0x70(%r8)
movupd %xmm0, 0x60(%r8)
movupd %xmm0, 0x50(%r8)
movupd %xmm0, 0x40(%r8)
movupd %xmm0, 0x30(%r8)
movupd %xmm0, 0x20(%r8)
movupd %xmm0, 0x10(%r8)
movupd %xmm0, (%r8)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
leaq 0xa016(%rip), %rdi      # 0x29510
callq 0x3590
sarl %ebp
testl %ebp, %ebp
jle 0x1f677
movq (%rsp), %rax
addq %rax, %rax
movq %rax, 0x10(%rsp)
movl %ebp, %ebp
addq $0x20, %r14
cmpq $0x0, -0x20(%r14)
je 0x1f66a
cmpq $0x0, (%r14)
je 0x1f66a
leaq -0x20(%r14), %rsi
movq %r12, %rdi
callq 0x1fb20
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq -0x8(%r14), %r15
xorps %xmm1, %xmm1
cvtsi2sdl 0x1c(%r15), %xmm1
mulsd 0x9c78(%rip), %xmm1     # 0x291d0
ucomisd %xmm1, %xmm0
ja 0x1f66a
movq %r12, %rdi
movq %r14, %rsi
callq 0x1fb20
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x18(%r14), %rax
xorps %xmm1, %xmm1
cvtsi2sdl 0x1c(%rax), %xmm1
mulsd 0x9c48(%rip), %xmm1     # 0x291d0
ucomisd %xmm1, %xmm0
ja 0x1f66a
movl 0x18(%r15), %ecx
cmpl 0x18(%rax), %ecx
jne 0x1f66a
movq (%r15), %rsi
movq (%rax), %rcx
movq (%rsp), %rax
cmpq %rax, %rsi
setge %dl
cmpq %rax, %rcx
setl %al
xorb %dl, %al
movq %rcx, %rdi
notq %rdi
addq 0x10(%rsp), %rdi
testb %al, %al
cmovneq %rcx, %rdi
xorl %ecx, %ecx
movq %rdi, %r8
subq %rsi, %r8
setle %dl
movq %r8, %r13
negq %r13
cmovsq %r8, %r13
cmpq %rsi, %rdi
je 0x1f66a
movslq 0xb4(%r12), %rsi
cmpq %rsi, %r13
jg 0x1f66a
movb %dl, %cl
leaq (%rcx,%rcx,2), %rcx
xorb $0x1, %al
movzbl %al, %eax
xorq %rcx, %rax
leaq (%rax,%rax,2), %rax
leaq (%rsp,%rax,8), %r15
addq $0x20, %r15
movq (%r15), %rdx
movq 0x8(%r15), %rax
cmpq %rax, %rdx
jne 0x1f65b
movq %rdx, 0x8(%rsp)
movl $0x2, %esi
testq %rax, %rax
je 0x1f62e
addq %rax, %rax
movq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
leaq 0x9bff(%rip), %rdx      # 0x29240
movl $0x40, %ecx
leaq 0x9c01(%rip), %r8       # 0x2924e
callq 0x22d64
movq %rax, 0x10(%r15)
movq 0x8(%rsp), %rdx
movq 0x10(%r15), %rax
leaq 0x1(%rdx), %rcx
movq %rcx, (%r15)
movq %r13, (%rax,%rdx,8)
addq $0x40, %r14
decq %rbp
jne 0x1f51b
leaq 0x117c2(%rip), %rax     # 0x30e40
cmpl $0x3, (%rax)
jge 0x1fb11
movq 0x10942(%rip), %r15     # 0x2ffd0
xorl %r14d, %r14d
movq %r14, %r12
shlq $0x5, %r12
addq %rbx, %r12
leaq (%r14,%r14,2), %rax
leaq (%rsp,%rax,8), %r13
addq $0x20, %r13
movq (%r13), %rbp
movl %r14d, %eax
shrl %eax
movl %r14d, %edx
andl $0x1, %edx
movq (%r15), %rdi
leaq 0x9c23(%rip), %rsi      # 0x292e3
movsbl (%rax,%rsi), %ecx
movsbl (%rdx,%rsi), %r8d
cmpq $0x9, %rbp
ja 0x1f6f6
leaq 0x9bd0(%rip), %rsi      # 0x292a6
leaq 0x9b71(%rip), %rdx      # 0x2924e
xorl %eax, %eax
callq 0x3500
movl $0x1, 0x8(%r12)
movq 0x10(%r13), %r13
jmp 0x1fa49
movq %r14, 0x18(%rsp)
movq %r15, %r14
xorl %r15d, %r15d
leaq 0x9bde(%rip), %rsi      # 0x292e6
leaq 0x9b3f(%rip), %rdx      # 0x2924e
xorl %eax, %eax
callq 0x3500
movq 0x10(%r13), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd241
movq %rbp, %xmm0
punpckldq 0x880e(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x8816(%rip), %xmm0     # 0x27f50
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
mulsd 0x9a86(%rip), %xmm0     # 0x291d8
movsd 0x6046(%rip), %xmm2     # 0x257a0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
cltq
movl (%r13,%rax,8), %ecx
movapd %xmm1, %xmm0
mulsd 0x8a9b(%rip), %xmm0     # 0x28210
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
cltq
mulsd 0x9a59(%rip), %xmm1     # 0x291e0
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %edx
movslq %edx, %rdx
movl (%r13,%rdx,8), %r9d
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movl %r9d, %edx
subl %ecx, %edx
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
movl (%r13,%rax,8), %r8d
movsd %xmm0, (%rsp)
addsd %xmm0, %xmm0
movsd %xmm1, 0x10(%rsp)
subsd %xmm0, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
movl $0x1, %edx
cmovll %edx, %eax
movl %eax, (%r12)
xorps %xmm1, %xmm1
cvtsi2sd %r9d, %xmm1
movsd %xmm1, 0x8(%rsp)
addsd %xmm1, %xmm0
addsd %xmm2, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x4(%r12)
movq (%r14), %rdi
leaq 0x9b28(%rip), %rsi      # 0x2932a
leaq 0x9a45(%rip), %rdx      # 0x2924e
xorl %eax, %eax
callq 0x3500
movq (%r14), %rdi
movl (%r12), %ecx
movl 0x4(%r12), %r8d
leaq 0x9b36(%rip), %rsi      # 0x29359
leaq 0x9a24(%rip), %rdx      # 0x2924e
xorl %eax, %eax
callq 0x3500
movq $0x0, 0x10(%r12)
movslq (%r12), %rax
xorl %ecx, %ecx
movq (%r13,%r15,8), %rdx
cmpq %rax, %rdx
jb 0x1f885
movslq 0x4(%r12), %rsi
cmpq %rsi, %rdx
ja 0x1f885
movq %rdx, %xmm0
punpckldq 0x86df(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x86e7(%rip), %xmm0     # 0x27f50
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
addsd 0x10(%r12), %xmm1
movsd %xmm1, 0x10(%r12)
incl %ecx
incq %r15
cmpq %r15, %rbp
jne 0x1f840
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
movsd 0x10(%r12), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%r12)
movq $0x0, 0x18(%r12)
movslq (%r12), %rax
xorl %ecx, %ecx
movq 0x8683(%rip), %xmm4     # 0x27f40
movapd 0x868b(%rip), %xmm5     # 0x27f50
movq 0x10704(%rip), %r15     # 0x2ffd0
movq 0x18(%rsp), %r14
movq (%r13,%rcx,8), %rdx
cmpq %rax, %rdx
jb 0x1f914
movslq 0x4(%r12), %rsi
cmpq %rsi, %rdx
ja 0x1f914
movq %rdx, %xmm2
punpckldq %xmm4, %xmm2    # xmm2 = xmm2[0],xmm4[0],xmm2[1],xmm4[1]
subpd %xmm5, %xmm2
movapd %xmm2, %xmm3
unpckhpd %xmm2, %xmm3            # xmm3 = xmm3[1],xmm2[1]
addsd %xmm2, %xmm3
subsd %xmm0, %xmm3
mulsd %xmm3, %xmm3
addsd 0x18(%r12), %xmm3
movsd %xmm3, 0x18(%r12)
incq %rcx
cmpq %rcx, %rbp
jne 0x1f8d1
movsd 0x18(%r12), %xmm0
divsd %xmm1, %xmm0
ucomisd 0x98d1(%rip), %xmm0     # 0x29200
jb 0x1f93a
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x1f943
callq 0x3230
movapd %xmm0, %xmm1
movsd %xmm1, 0x18(%r12)
movq (%r15), %rdi
movsd 0x10(%r12), %xmm0
leaq 0x9a48(%rip), %rsi      # 0x293a3
leaq 0x98ec(%rip), %rdx      # 0x2924e
movb $0x2, %al
callq 0x3500
movsd (%rsp), %xmm2
movapd %xmm2, %xmm0
mulsd 0x986e(%rip), %xmm0     # 0x291e8
addsd 0x10(%rsp), %xmm0
movsd 0x5e18(%rip), %xmm1     # 0x257a0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, (%r12)
mulsd 0x9854(%rip), %xmm2     # 0x291f0
addsd 0x8(%rsp), %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %eax
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm3
movl %eax, 0x4(%r12)
movsd 0x10(%r12), %xmm1
movsd 0x18(%r12), %xmm0
movapd %xmm0, %xmm2
mulsd 0x5dd7(%rip), %xmm2     # 0x257a8
addsd %xmm1, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x1f9ef
movapd %xmm2, %xmm3
addsd 0x5db9(%rip), %xmm3     # 0x257a0
cvttsd2si %xmm3, %ecx
movl %ecx, (%r12)
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
ucomisd %xmm3, %xmm2
jbe 0x1fa19
mulsd 0x5d94(%rip), %xmm0     # 0x25798
addsd %xmm1, %xmm0
addsd 0x5d90(%rip), %xmm0     # 0x257a0
cvttsd2si %xmm0, %eax
movl %eax, 0x4(%r12)
cmpl $0x0, (%r12)
jg 0x1fa28
movl $0x1, (%r12)
movq (%r15), %rdi
movl (%r12), %ecx
movl 0x4(%r12), %r8d
leaq 0x9990(%rip), %rsi      # 0x293cb
leaq 0x980c(%rip), %rdx      # 0x2924e
xorl %eax, %eax
callq 0x3500
movq %r13, %rdi
callq 0x3390
incq %r14
cmpq $0x4, %r14
jne 0x1f691
xorl %eax, %eax
xorl %ecx, %ecx
movslq %ecx, %rcx
movq 0x20(%rsp,%rax), %rdx
cmpq %rcx, %rdx
cmovael %edx, %ecx
addq $0x18, %rax
cmpq $0x60, %rax
jne 0x1fa62
cvtsi2sd %ecx, %xmm4
mulsd 0x9772(%rip), %xmm4     # 0x291f8
addq $0x8, %rbx
leaq 0x20(%rsp), %r15
xorl %r14d, %r14d
movsd 0x84a6(%rip), %xmm2     # 0x27f40
movapd 0x84ae(%rip), %xmm3     # 0x27f50
cmpl $0x0, (%rbx)
jne 0x1fac5
movsd (%r15), %xmm0
unpcklps %xmm2, %xmm0            # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm3, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm4
ja 0x1fad8
incq %r14
addq $0x18, %r15
addq $0x20, %rbx
cmpq $0x4, %r14
jne 0x1faa2
jmp 0x1faff
movq %rbx, %rdi
movq %r14, %rsi
movsd %xmm4, (%rsp)
callq 0x43a2
movsd (%rsp), %xmm4
movapd 0x845b(%rip), %xmm3     # 0x27f50
movsd 0x8443(%rip), %xmm2     # 0x27f40
jmp 0x1fac5
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
callq 0x436a
jmp 0x1f687