// File: /workspace/github_cpp_makefile_star2_O3/tplinderoth[P]introDist/Dxyz.cpp
// Function: dxyComponents(std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>> const&, std::vector<int, std::allocator<int>> const&, Matrix<double>&, int)
void dxyComponents(const std::vector<int> &alleles, const std::vector<int> &source_idx, const std::vector<int> &sink_idx,
		const std::vector<int> &outgroup_idx, Matrix<double> &dxy, int rown) {

	double ntotal = 0; // total number of sequences with data
	double nalt = 0; // number of sequences with alternate allele
	unsigned int popn = 0; // number of sequences with data in subgroup

	// calculate source population alt allele frequency
	double altx = altCount(alleles, source_idx, popn);
	double fx = (popn > 0) ? altx/popn : -9;
	ntotal += popn;
	nalt += altx;

	// calculate sink population alt allele frequency
	double alty = altCount(alleles, sink_idx, popn);
	double fy = (popn > 0) ? alty/popn : -9;
	ntotal += popn;
	nalt += alty;

	// calculate outgroup alt allele frequency
	double altz = altCount(alleles, outgroup_idx, popn);
	double fz = (popn > 0) ? altz/popn : -9;
	ntotal += popn;
	nalt += altz;

	// calculate source-sink dxy
	dxy(rown, 0) = (fx != -9 && fy != -9) ? fx*(1.0-fy) + (1.0-fx)*fy : -9;

	// calculate source-outgroup dxy
	dxy(rown, 1) = (fx != -9 && fy != -9) ? fx*(1.0-fz) + (1.0-fx)*fz : -9;

	// log number of sequences with data
	dxy(rown, 3) = ntotal;

	// calculate maf for subset of analyzed sequences
	dxy(rown, 4) = (ntotal > 0) ? nalt/ntotal : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x60, %rsp
movl %r9d, %ebx
movq %r8, %r14
movq (%rsi), %r8
movq 0x8(%rsi), %r9
xorpd %xmm1, %xmm1
xorl %eax, %eax
movl $0x0, %esi
xorpd %xmm0, %xmm0
cmpq %r9, %r8
je 0x81b4
movq (%rdi), %r10
xorl %esi, %esi
xorl %r11d, %r11d
xorl %r15d, %r15d
movslq (%r8), %r12
movl (%r10,%r12,4), %ebp
xorl %r12d, %r12d
cmpl $0x1, %ebp
sete %r12b
addl %r12d, %r11d
leal 0x1(%r15), %r12d
cmpl $-0x9, %ebp
cmovnel %r12d, %esi
cmovel %r15d, %r12d
addq $0x4, %r8
movl %r12d, %r15d
cmpq %r9, %r8
jne 0x817a
movl %r11d, %r8d
xorps %xmm0, %xmm0
cvtsi2sd %r8, %xmm0
movl %esi, %r8d
movq (%rdx), %r9
movq 0x8(%rdx), %rdx
cmpq %rdx, %r9
je 0x8208
movq (%rdi), %r10
xorl %eax, %eax
xorl %r11d, %r11d
xorl %r15d, %r15d
movslq (%r9), %r12
movl (%r10,%r12,4), %ebp
xorl %r12d, %r12d
cmpl $0x1, %ebp
sete %r12b
addl %r12d, %r11d
leal 0x1(%r15), %r12d
cmpl $-0x9, %ebp
cmovnel %r12d, %eax
cmovel %r15d, %r12d
addq $0x4, %r9
movl %r12d, %r15d
cmpq %rdx, %r9
jne 0x81ce
movl %r11d, %edx
xorps %xmm1, %xmm1
cvtsi2sd %rdx, %xmm1
movl %eax, %edx
movq (%rcx), %r9
movq 0x8(%rcx), %r10
cmpq %r10, %r9
je 0x8274
movq (%rdi), %rdi
xorl %ecx, %ecx
xorl %r11d, %r11d
xorl %r15d, %r15d
movslq (%r9), %r12
movl (%rdi,%r12,4), %ebp
xorl %r12d, %r12d
cmpl $0x1, %ebp
sete %r12b
addl %r12d, %r11d
leal 0x1(%r15), %r12d
cmpl $-0x9, %ebp
cmovnel %r12d, %ecx
cmovel %r15d, %r12d
addq $0x4, %r9
movl %r12d, %r15d
cmpq %r10, %r9
jne 0x8221
movq %r9, 0xf129(%rip)       # 0x17380
movl %r11d, %edi
cvtsi2sd %rdi, %xmm3
movl %ecx, %edi
cvtsi2sd %rdi, %xmm2
testl %ecx, %ecx
je 0x828d
movapd %xmm3, %xmm4
divsd %xmm2, %xmm4
jmp 0x8295
movq %r9, 0xf105(%rip)       # 0x17380
movsd 0x6d95(%rip), %xmm4     # 0xf018
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
jmp 0x8295
movsd 0x6d83(%rip), %xmm4     # 0xf018
movsd %xmm3, 0x50(%rsp)
movsd %xmm2, 0x58(%rsp)
xorps %xmm2, %xmm2
cvtsi2sd %r8, %xmm2
xorps %xmm3, %xmm3
cvtsi2sd %rdx, %xmm3
testl %esi, %esi
movapd %xmm0, 0x10(%rsp)
movsd %xmm2, 0x8(%rsp)
movsd %xmm3, 0x48(%rsp)
movsd %xmm1, 0x40(%rsp)
je 0x835d
testl %eax, %eax
je 0x835d
movapd %xmm0, %xmm5
divsd %xmm2, %xmm5
movsd %xmm5, (%rsp)
movapd %xmm1, %xmm0
divsd %xmm3, %xmm0
movsd 0x6d2a(%rip), %xmm6     # 0xf020
movapd %xmm6, %xmm7
subsd %xmm0, %xmm7
subsd %xmm5, %xmm6
movsd %xmm6, 0x38(%rsp)
mulsd %xmm6, %xmm0
mulsd %xmm5, %xmm7
addsd %xmm0, %xmm7
movsd %xmm7, 0x30(%rsp)
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
movsd %xmm4, 0x28(%rsp)
callq 0x8af6
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x6ce2(%rip), %xmm0     # 0xf020
movsd 0x28(%rsp), %xmm2
subsd %xmm2, %xmm0
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm1
mulsd (%rsp), %xmm0
addsd %xmm1, %xmm0
jmp 0x837e
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x8af6
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax)
movsd 0x6c9a(%rip), %xmm0     # 0xf018
movsd %xmm0, (%rsp)
movapd 0x10(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movsd 0x8(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
addsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x8af6
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r14, %rdi
movl %ebx, %esi
movl $0x3, %edx
callq 0x8af6
movsd 0x8(%rsp), %xmm1
movsd %xmm1, (%rax)
movapd 0x10(%rsp), %xmm0
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movapd %xmm2, 0x10(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl $0x4, %edx
callq 0x8af6
movaps 0x10(%rsp), %xmm0
movlps %xmm0, (%rax)
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq