// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %r8, %rbx
movq %rcx, %r14
movl %edx, %ebp
movq %rsi, 0x10(%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, 0x90(%rsp)
movapd %xmm0, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
leaq 0x90ef(%rip), %rdi      # 0x245c0
callq 0x3590
sarl %ebp
xorl %r13d, %r13d
testl %ebp, %ebp
cmovgl %ebp, %r13d
addq $0x20, %r14
subq $0x1, %r13
jb 0x1b602
cmpq $0x0, -0x20(%r14)
je 0x1b5f9
cmpq $0x0, (%r14)
je 0x1b5f9
leaq -0x20(%r14), %rsi
movq %r12, %rdi
callq 0x1bab9
cvtsi2sd %eax, %xmm0
movq -0x8(%r14), %r15
cvtsi2sdl 0x1c(%r15), %xmm1
mulsd 0x8d2a(%rip), %xmm1     # 0x24250
ucomisd %xmm1, %xmm0
ja 0x1b5f9
movq %r12, %rdi
movq %r14, %rsi
callq 0x1bab9
cvtsi2sd %eax, %xmm0
movq 0x18(%r14), %rax
cvtsi2sdl 0x1c(%rax), %xmm1
mulsd 0x8d00(%rip), %xmm1     # 0x24250
ucomisd %xmm1, %xmm0
ja 0x1b5f9
movl 0x18(%r15), %ecx
cmpl 0x18(%rax), %ecx
jne 0x1b5f9
movq (%r15), %rsi
movq (%rax), %rdx
movq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rcx
callq 0x1bb45
movq 0x38(%rsp), %rbp
testq %rbp, %rbp
je 0x1b5f9
movslq 0xb4(%r12), %rcx
cmpq %rcx, %rbp
jg 0x1b5f9
movl %eax, %eax
imulq $0x18, %rax, %rax
leaq (%rsp,%rax), %r15
addq $0x40, %r15
movq (%r15), %rdx
cmpq 0x8(%r15), %rdx
jne 0x1b5ea
leaq (%rdx,%rdx), %rsi
testq %rdx, %rdx
pushq $0x2
popq %rax
cmoveq %rax, %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
movq %rdx, 0x20(%rsp)
leaq 0x8d20(%rip), %rdx      # 0x242f0
pushq $0x40
popq %rcx
leaq 0x8d24(%rip), %r8       # 0x242fe
callq 0x1e0b4
movq 0x20(%rsp), %rdx
movq %rax, 0x10(%r15)
jmp 0x1b5ee
movq 0x10(%r15), %rax
leaq 0x1(%rdx), %rcx
movq %rcx, (%r15)
movq %rbp, (%rax,%rdx,8)
addq $0x40, %r14
jmp 0x1b4e5
leaq 0x11837(%rip), %rax     # 0x2ce40
cmpl $0x3, (%rax)
jge 0x1ba7a
movq 0x109b7(%rip), %r15     # 0x2bfd0
xorl %r12d, %r12d
cmpq $0x4, %r12
je 0x1b99d
movq %r12, %r14
shlq $0x5, %r14
addq %rbx, %r14
imulq $0x18, %r12, %rax
leaq (%rsp,%rax), %r13
addq $0x40, %r13
movq (%r13), %rbp
movl %r12d, %eax
shrl %eax
movl %r12d, %edx
andl $0x1, %edx
movq (%r15), %rdi
leaq 0x8d3e(%rip), %rsi      # 0x24393
movsbl (%rax,%rsi), %ecx
movsbl (%rdx,%rsi), %r8d
cmpq $0x9, %rbp
ja 0x1b68a
leaq 0x8ceb(%rip), %rsi      # 0x24356
leaq 0x8c8c(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
movl $0x1, 0x8(%r14)
movq 0x10(%r13), %rdi
jmp 0x1b990
movq %r12, 0x30(%rsp)
movq %r15, %r12
xorl %r15d, %r15d
leaq 0x8cfa(%rip), %rsi      # 0x24396
leaq 0x8c5b(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
movq 0x10(%r13), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xbee3
movq %rbp, %xmm0
punpckldq 0x78aa(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x78b2(%rip), %xmm0     # 0x22f80
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addpd %xmm0, %xmm1
movapd %xmm1, %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
mulpd 0x8ba6(%rip), %xmm0     # 0x24290
addpd 0x8bae(%rip), %xmm0     # 0x242a0
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %eax
cltq
movl (%r13,%rax,8), %ecx
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %eax
mulsd 0x8b46(%rip), %xmm1     # 0x24258
movsd 0x5086(%rip), %xmm2     # 0x207a0
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %edx
cltq
movslq %edx, %rdx
movl (%r13,%rdx,8), %r9d
movd %r9d, %xmm0
movd %ecx, %xmm1
punpckldq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0],xmm1[1],xmm0[1]
movl %r9d, %edx
subl %ecx, %edx
cvtsi2sd %edx, %xmm0
cvtdq2pd %xmm1, %xmm3
movl (%r13,%rax,8), %r8d
movapd %xmm0, 0x10(%rsp)
addsd %xmm0, %xmm0
movapd %xmm3, %xmm1
subsd %xmm0, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %eax
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovll %edx, %eax
movl %eax, (%r14)
movapd %xmm3, 0x20(%rsp)
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm0, %xmm1
addsd %xmm2, %xmm1
cvttsd2si %xmm1, %eax
movl %eax, 0x4(%r14)
movq (%r12), %rdi
leaq 0x8c40(%rip), %rsi      # 0x243da
leaq 0x8b5d(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
movq (%r12), %rdi
movl (%r14), %ecx
movl 0x4(%r14), %r8d
leaq 0x8c4f(%rip), %rsi      # 0x24409
leaq 0x8b3d(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
movslq (%r14), %rax
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
cmpq %r15, %rbp
je 0x1b81b
movq (%r13,%r15,8), %rdx
cmpq %rax, %rdx
jb 0x1b816
movslq 0x4(%r14), %rsi
cmpq %rsi, %rdx
ja 0x1b816
movq %rdx, %xmm0
punpckldq 0x777a(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7782(%rip), %xmm0     # 0x22f80
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd %xmm1, 0x10(%r14)
incl %ecx
incq %r15
jmp 0x1b7d1
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, 0x10(%r14)
movslq (%r14), %rax
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x10797(%rip), %r15     # 0x2bfd0
movq 0x30(%rsp), %r12
cmpq %rcx, %rbp
je 0x1b88e
movq (%r13,%rcx,8), %rdx
cmpq %rax, %rdx
jb 0x1b889
movslq 0x4(%r14), %rsi
cmpq %rsi, %rdx
ja 0x1b889
movq %rdx, %xmm3
punpckldq 0x770d(%rip), %xmm3 # xmm3 = xmm3[0],mem[0],xmm3[1],mem[1]
subpd 0x7715(%rip), %xmm3     # 0x22f80
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
subsd %xmm1, %xmm4
mulsd %xmm4, %xmm4
addsd %xmm4, %xmm0
movsd %xmm0, 0x18(%r14)
incq %rcx
jmp 0x1b83e
divsd %xmm2, %xmm0
ucomisd 0x89ce(%rip), %xmm0     # 0x24268
jb 0x1b8a2
sqrtsd %xmm0, %xmm1
jmp 0x1b8ab
callq 0x3230
movapd %xmm0, %xmm1
movsd %xmm1, 0x18(%r14)
movq (%r15), %rdi
movsd 0x10(%r14), %xmm0
leaq 0x8b92(%rip), %rsi      # 0x24453
leaq 0x8a36(%rip), %rdx      # 0x242fe
movb $0x2, %al
callq 0x3500
movapd 0x10(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x89cf(%rip), %xmm0     # 0x242b0
addpd 0x20(%rsp), %xmm0
addpd 0x89b1(%rip), %xmm0     # 0x242a0
cvttpd2dq %xmm0, %xmm2
movd %xmm2, %ecx
cvtsi2sd %ecx, %xmm4
movq %xmm2, (%r14)
movsd 0x10(%r14), %xmm1
movsd 0x18(%r14), %xmm0
movapd %xmm0, %xmm3
mulsd 0x4e90(%rip), %xmm3     # 0x207a8
addsd %xmm1, %xmm3
ucomisd %xmm3, %xmm4
jbe 0x1b935
movapd %xmm3, %xmm4
addsd 0x4e72(%rip), %xmm4     # 0x207a0
cvttsd2si %xmm4, %ecx
movl %ecx, (%r14)
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movd %xmm2, %r8d
cvtsi2sd %r8d, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x1b967
mulsd 0x4e46(%rip), %xmm0     # 0x20798
addsd %xmm1, %xmm0
addsd 0x4e42(%rip), %xmm0     # 0x207a0
cvttsd2si %xmm0, %r8d
movl %r8d, 0x4(%r14)
testl %ecx, %ecx
jg 0x1b975
movl $0x1, (%r14)
pushq $0x1
popq %rcx
movq (%r15), %rdi
leaq 0x8afc(%rip), %rsi      # 0x2447b
leaq 0x8978(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
movq %r13, %rdi
callq 0x3390
incq %r12
jmp 0x1b61c
xorl %eax, %eax
xorl %ecx, %ecx
cmpq $0x60, %rax
je 0x1b9bd
movslq %ecx, %rdx
movq 0x40(%rsp,%rax), %rsi
cmpq %rdx, %rsi
cmovbl %ecx, %esi
addq $0x18, %rax
movl %esi, %ecx
jmp 0x1b9a1
cvtsi2sd %ecx, %xmm2
mulsd 0x8897(%rip), %xmm2     # 0x24260
addq $0x8, %rbx
leaq 0x40(%rsp), %r12
leaq 0x8ade(%rip), %r14      # 0x244b7
leaq 0x891e(%rip), %r15      # 0x242fe
xorl %r13d, %r13d
cmpq $0x4, %r13
je 0x1ba68
cmpl $0x0, (%rbx)
jne 0x1ba15
movsd (%r12), %xmm0
unpcklps 0x7575(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x757d(%rip), %xmm0     # 0x22f80
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
ja 0x1ba22
incq %r13
addq $0x18, %r12
addq $0x20, %rbx
jmp 0x1b9e3
movl $0x1, (%rbx)
movq 0x105a1(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
movl %r13d, %eax
shrl %eax
leaq 0x8955(%rip), %rdx      # 0x24393
movsbl (%rax,%rdx), %ecx
movl %r13d, %eax
andl $0x1, %eax
movsbl (%rax,%rdx), %r8d
movq %r14, %rsi
movq %r15, %rdx
xorl %eax, %eax
movsd %xmm2, 0x10(%rsp)
callq 0x3500
movsd 0x10(%rsp), %xmm2
jmp 0x1ba15
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x1054f(%rip), %rax     # 0x2bfd0
movq (%rax), %rdi
movq 0x40(%rsp), %rcx
movq 0x58(%rsp), %r8
movq 0x70(%rsp), %r9
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
leaq 0x8863(%rip), %rsi      # 0x24309
leaq 0x8851(%rip), %rdx      # 0x242fe
xorl %eax, %eax
callq 0x3500
jmp 0x1b612