// File: /workspace/github_c_makefile_star2_O3/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 $0x98, %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, 0x80(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
leaq 0x9c12(%rip), %rdi      # 0x29640
callq 0x3590
sarl %ebp
testl %ebp, %ebp
jle 0x1fbaa
movq 0x10(%rsp), %rax
addq %rax, %rax
movq %rax, 0x20(%rsp)
movl %ebp, %ebp
addq $0x20, %r14
cmpq $0x0, -0x20(%r14)
je 0x1fb9d
cmpq $0x0, (%r14)
je 0x1fb9d
leaq -0x20(%r14), %rsi
movq %r12, %rdi
callq 0x2002c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq -0x8(%r14), %r15
xorps %xmm1, %xmm1
cvtsi2sdl 0x1c(%r15), %xmm1
mulsd 0x9833(%rip), %xmm1     # 0x292c0
ucomisd %xmm1, %xmm0
ja 0x1fb9d
movq %r12, %rdi
movq %r14, %rsi
callq 0x2002c
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq 0x18(%r14), %rax
xorps %xmm1, %xmm1
cvtsi2sdl 0x1c(%rax), %xmm1
mulsd 0x9803(%rip), %xmm1     # 0x292c0
ucomisd %xmm1, %xmm0
ja 0x1fb9d
movl 0x18(%r15), %ecx
cmpl 0x18(%rax), %ecx
jne 0x1fb9d
movq (%r15), %rsi
movq (%rax), %rcx
movq 0x10(%rsp), %rax
cmpq %rax, %rsi
setge %dl
cmpq %rax, %rcx
setl %al
xorb %dl, %al
movq %rcx, %rdi
notq %rdi
addq 0x20(%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 0x1fb9d
movslq 0xb4(%r12), %rsi
cmpq %rsi, %r13
jg 0x1fb9d
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 $0x30, %r15
movq (%r15), %rdx
cmpq 0x8(%r15), %rdx
jne 0x1fb8e
movl $0x2, %esi
testq %rdx, %rdx
je 0x1fb5a
leaq (%rdx,%rdx), %rsi
movq %rsi, 0x8(%r15)
movq 0x10(%r15), %rdi
shlq $0x3, %rsi
movq %rdx, 0x8(%rsp)
leaq 0x97fe(%rip), %rdx      # 0x29370
movl $0x40, %ecx
leaq 0x9800(%rip), %r8       # 0x2937e
callq 0x2324c
movq 0x8(%rsp), %rdx
movq %rax, 0x10(%r15)
jmp 0x1fb92
movq 0x10(%r15), %rax
leaq 0x1(%rdx), %rcx
movq %rcx, (%r15)
movq %r13, (%rax,%rdx,8)
addq $0x40, %r14
decq %rbp
jne 0x1fa50
leaq 0x1128f(%rip), %rax     # 0x30e40
cmpl $0x3, (%rax)
jge 0x2001d
movq 0x1040f(%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 $0x30, %r13
movq (%r13), %rbp
movl %r14d, %eax
shrl %eax
movl %r14d, %edx
andl $0x1, %edx
movq (%r15), %rdi
leaq 0x9820(%rip), %rsi      # 0x29413
movsbl (%rax,%rsi), %ecx
movsbl (%rdx,%rsi), %r8d
cmpq $0x9, %rbp
ja 0x1fc29
leaq 0x97cd(%rip), %rsi      # 0x293d6
leaq 0x976e(%rip), %rdx      # 0x2937e
xorl %eax, %eax
callq 0x3500
movl $0x1, 0x8(%r12)
movq 0x10(%r13), %rdi
jmp 0x1ff53
movq %r14, 0x8(%rsp)
movq %r15, %r14
xorl %r15d, %r15d
leaq 0x97db(%rip), %rsi      # 0x29416
leaq 0x973c(%rip), %rdx      # 0x2937e
xorl %eax, %eax
callq 0x3500
movq 0x10(%r13), %r13
movq %rbp, %rdi
movq %r13, %rsi
callq 0xd4c1
movq %rbp, %xmm0
punpckldq 0x835b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x8363(%rip), %xmm0     # 0x27fd0
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 0x9677(%rip), %xmm0     # 0x29300
addpd 0x967f(%rip), %xmm0     # 0x29310
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 0x9617(%rip), %xmm1     # 0x292c8
movsd 0x5ae7(%rip), %xmm2     # 0x257a0
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
xorps %xmm0, %xmm0
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
movl $0x1, %edx
cmovll %edx, %eax
movl %eax, (%r12)
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(%r12)
movq (%r14), %rdi
leaq 0x971b(%rip), %rsi      # 0x2945a
leaq 0x9638(%rip), %rdx      # 0x2937e
xorl %eax, %eax
callq 0x3500
movq (%r14), %rdi
movl (%r12), %ecx
movl 0x4(%r12), %r8d
leaq 0x9729(%rip), %rsi      # 0x29489
leaq 0x9617(%rip), %rdx      # 0x2937e
xorl %eax, %eax
callq 0x3500
movslq (%r12), %rax
xorpd %xmm1, %xmm1
xorl %ecx, %ecx
movq (%r13,%r15,8), %rdx
cmpq %rax, %rdx
jb 0x1fdba
movslq 0x4(%r12), %rsi
cmpq %rsi, %rdx
ja 0x1fdba
movq %rdx, %xmm0
punpckldq 0x8227(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x822f(%rip), %xmm0     # 0x27fd0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
addsd %xmm2, %xmm1
movsd %xmm1, 0x10(%r12)
incl %ecx
incq %r15
cmpq %r15, %rbp
jne 0x1fd78
xorps %xmm2, %xmm2
cvtsi2sd %ecx, %xmm2
divsd %xmm2, %xmm1
movsd %xmm1, 0x10(%r12)
xorpd %xmm0, %xmm0
xorl %ecx, %ecx
movq 0x81de(%rip), %xmm5     # 0x27fc0
movapd 0x81e6(%rip), %xmm6     # 0x27fd0
movq 0x101df(%rip), %r15     # 0x2ffd0
movq 0x8(%rsp), %r14
movq (%r13,%rcx,8), %rdx
cmpq %rax, %rdx
jb 0x1fe36
movslq 0x4(%r12), %rsi
cmpq %rsi, %rdx
ja 0x1fe36
movq %rdx, %xmm3
punpckldq %xmm5, %xmm3    # xmm3 = xmm3[0],xmm5[0],xmm3[1],xmm5[1]
subpd %xmm6, %xmm3
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(%r12)
incq %rcx
cmpq %rcx, %rbp
jne 0x1fdf6
divsd %xmm2, %xmm0
ucomisd 0x948e(%rip), %xmm0     # 0x292d8
jb 0x1fe55
xorps %xmm1, %xmm1
sqrtsd %xmm0, %xmm1
jmp 0x1fe5e
callq 0x3230
movapd %xmm0, %xmm1
movsd %xmm1, 0x18(%r12)
movq (%r15), %rdi
movsd 0x10(%r12), %xmm0
leaq 0x965d(%rip), %rsi      # 0x294d3
leaq 0x9501(%rip), %rdx      # 0x2937e
movb $0x2, %al
callq 0x3500
movapd 0x10(%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
mulpd 0x948a(%rip), %xmm0     # 0x29320
addpd 0x20(%rsp), %xmm0
addpd 0x946c(%rip), %xmm0     # 0x29310
cvttpd2dq %xmm0, %xmm2
movd %xmm2, %ecx
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
movq %xmm2, (%r12)
movsd 0x10(%r12), %xmm1
movsd 0x18(%r12), %xmm0
movapd %xmm0, %xmm3
mulsd 0x58d5(%rip), %xmm3     # 0x257a8
addsd %xmm1, %xmm3
ucomisd %xmm3, %xmm4
jbe 0x1fef1
movapd %xmm3, %xmm4
addsd 0x58b7(%rip), %xmm4     # 0x257a0
cvttsd2si %xmm4, %ecx
movl %ecx, (%r12)
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movd %xmm2, %r8d
xorps %xmm2, %xmm2
cvtsi2sd %r8d, %xmm2
ucomisd %xmm2, %xmm3
jbe 0x1ff27
mulsd 0x5887(%rip), %xmm0     # 0x25798
addsd %xmm1, %xmm0
addsd 0x5883(%rip), %xmm0     # 0x257a0
cvttsd2si %xmm0, %r8d
movl %r8d, 0x4(%r12)
testl %ecx, %ecx
jg 0x1ff38
movl $0x1, (%r12)
movl $0x1, %ecx
movq (%r15), %rdi
leaq 0x95b9(%rip), %rsi      # 0x294fb
leaq 0x9435(%rip), %rdx      # 0x2937e
xorl %eax, %eax
callq 0x3500
movq %r13, %rdi
callq 0x3390
incq %r14
cmpq $0x4, %r14
movsd 0x8059(%rip), %xmm2     # 0x27fc0
movapd 0x8061(%rip), %xmm3     # 0x27fd0
jne 0x1fbc4
xorl %eax, %eax
xorl %ecx, %ecx
movslq %ecx, %rcx
movq 0x30(%rsp,%rax), %rdx
cmpq %rcx, %rdx
cmovael %edx, %ecx
addq $0x18, %rax
cmpq $0x60, %rax
jne 0x1ff79
xorps %xmm4, %xmm4
cvtsi2sd %ecx, %xmm4
mulsd 0x9330(%rip), %xmm4     # 0x292d0
addq $0x8, %rbx
leaq 0x30(%rsp), %r15
xorl %r14d, %r14d
cmpl $0x0, (%rbx)
jne 0x1ffcf
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 0x1ffe2
incq %r14
addq $0x18, %r15
addq $0x20, %rbx
cmpq $0x4, %r14
jne 0x1ffac
jmp 0x2000b
movq %rbx, %rdi
movq %r14, %rsi
movsd %xmm4, 0x10(%rsp)
callq 0x439a
movsd 0x10(%rsp), %xmm4
movapd 0x7fcf(%rip), %xmm3     # 0x27fd0
movsd 0x7fb7(%rip), %xmm2     # 0x27fc0
jmp 0x1ffcf
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x30(%rsp), %rdi
callq 0x4362
jmp 0x1fbba