// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, 0x18(%rsp)
movq %r8, %r14
movq %rcx, %r12
movq %rdx, %rbp
movq %rdi, %r13
leaq 0x64(%rsp), %r15
andl $0x0, (%r15)
movq %r15, %rdx
callq 0x8180
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movapd %xmm0, 0x30(%rsp)
movl (%r15), %eax
testl %eax, %eax
setne %bl
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x8180
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x28(%rsp)
movl (%r15), %eax
testl %eax, %eax
setne %bpl
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r13, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x8180
movl %eax, %eax
cvtsi2sd %rax, %xmm1
movl (%r15), %eax
cvtsi2sd %rax, %xmm0
andb %bl, %bpl
cmpb $0x1, %bpl
movsd %xmm0, 0x58(%rsp)
movsd %xmm1, 0x50(%rsp)
jne 0x7c50
movl 0x18(%rsp), %ebx
testl %eax, %eax
je 0x7c8c
divsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
jmp 0x7c9a
movq %r14, %rdi
movl 0x18(%rsp), %ebx
movl %ebx, %esi
xorl %edx, %edx
callq 0x8402
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax)
movsd 0x63a3(%rip), %xmm0     # 0xe018
movsd %xmm0, 0x8(%rsp)
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
jmp 0x7d3e
movsd 0x6384(%rip), %xmm0     # 0xe018
movsd %xmm0, 0x8(%rsp)
movapd 0x30(%rsp), %xmm4
movsd 0x10(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
divsd %xmm1, %xmm4
movsd %xmm4, 0x18(%rsp)
movapd %xmm2, %xmm0
divsd %xmm3, %xmm0
movsd 0x6354(%rip), %xmm1     # 0xe020
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
subsd %xmm4, %xmm1
movsd %xmm1, 0x48(%rsp)
mulsd %xmm1, %xmm0
mulsd %xmm4, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x40(%rsp)
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x8402
movsd 0x20(%rsp), %xmm2
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x8(%rsp), %xmm0
movsd 0x6306(%rip), %xmm1     # 0xe020
subsd %xmm0, %xmm1
movsd 0x48(%rsp), %xmm3
mulsd %xmm0, %xmm3
mulsd 0x18(%rsp), %xmm1
addsd %xmm3, %xmm1
movsd %xmm1, 0x8(%rsp)
movsd 0x28(%rsp), %xmm1
movapd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
addsd 0x50(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
movsd 0x10(%rsp), %xmm0
addsd %xmm2, %xmm0
addsd 0x58(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
pushq $0x1
popq %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x8402
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
pushq $0x3
popq %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x8402
movsd 0x10(%rsp), %xmm1
movsd %xmm1, (%rax)
movapd 0x30(%rsp), %xmm0
divsd %xmm1, %xmm0
xorpd %xmm2, %xmm2
cmpltsd %xmm1, %xmm2
andpd %xmm0, %xmm2
movapd %xmm2, 0x30(%rsp)
pushq $0x4
popq %rdx
movq %r14, %rdi
movl %ebx, %esi
callq 0x8402
movaps 0x30(%rsp), %xmm0
movlps %xmm0, (%rax)
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq