// File: 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
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
movq $0x0, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0xb2c0
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x50(%rbp)
cmpl $0x0, -0x44(%rbp)
jbe 0xa9c5
movsd -0x50(%rbp), %xmm0
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x80(%rbp)
jmp 0xa9d4
movsd 0x13643(%rip), %xmm0    # 0x1e010
movsd %xmm0, -0x80(%rbp)
jmp 0xa9d4
movsd -0x80(%rbp), %xmm0
movsd %xmm0, -0x58(%rbp)
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
movsd -0x38(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x50(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x18(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0xb2c0
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x60(%rbp)
cmpl $0x0, -0x44(%rbp)
jbe 0xaa45
movsd -0x60(%rbp), %xmm0
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x88(%rbp)
jmp 0xaa57
movsd 0x135c3(%rip), %xmm0    # 0x1e010
movsd %xmm0, -0x88(%rbp)
jmp 0xaa57
movsd -0x88(%rbp), %xmm0
movsd %xmm0, -0x68(%rbp)
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
movsd -0x38(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x60(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
movq -0x20(%rbp), %rsi
leaq -0x44(%rbp), %rdx
callq 0xb2c0
movl %eax, %eax
cvtsi2sd %rax, %xmm0
movsd %xmm0, -0x70(%rbp)
cmpl $0x0, -0x44(%rbp)
jbe 0xaacb
movsd -0x70(%rbp), %xmm0
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x90(%rbp)
jmp 0xaadd
movsd 0x1353d(%rip), %xmm0    # 0x1e010
movsd %xmm0, -0x90(%rbp)
jmp 0xaadd
movsd -0x90(%rbp), %xmm0
movsd %xmm0, -0x78(%rbp)
movl -0x44(%rbp), %eax
cvtsi2sd %rax, %xmm0
addsd -0x38(%rbp), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x70(%rbp), %xmm0
addsd -0x40(%rbp), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x58(%rbp), %xmm0
movsd 0x134f8(%rip), %xmm1    # 0x1e010
ucomisd %xmm1, %xmm0
jne 0xab22
jp 0xab22
jmp 0xab6f
movsd -0x68(%rbp), %xmm0
movsd 0x134e1(%rip), %xmm1    # 0x1e010
ucomisd %xmm1, %xmm0
jne 0xab39
jp 0xab39
jmp 0xab6f
movsd -0x58(%rbp), %xmm0
movsd 0x134d2(%rip), %xmm2    # 0x1e018
subsd -0x68(%rbp), %xmm2
movsd 0x134c5(%rip), %xmm1    # 0x1e018
subsd -0x58(%rbp), %xmm1
mulsd -0x68(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x98(%rbp)
jmp 0xab81
movsd 0x13499(%rip), %xmm0    # 0x1e010
movsd %xmm0, -0x98(%rbp)
jmp 0xab81
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0xa0(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
xorl %edx, %edx
callq 0xba10
movsd -0xa0(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x58(%rbp), %xmm0
movsd 0x13458(%rip), %xmm1    # 0x1e010
ucomisd %xmm1, %xmm0
jne 0xabc2
jp 0xabc2
jmp 0xac0f
movsd -0x68(%rbp), %xmm0
movsd 0x13441(%rip), %xmm1    # 0x1e010
ucomisd %xmm1, %xmm0
jne 0xabd9
jp 0xabd9
jmp 0xac0f
movsd -0x58(%rbp), %xmm0
movsd 0x13432(%rip), %xmm2    # 0x1e018
subsd -0x78(%rbp), %xmm2
movsd 0x13425(%rip), %xmm1    # 0x1e018
subsd -0x58(%rbp), %xmm1
mulsd -0x78(%rbp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0xac21
movsd 0x133f9(%rip), %xmm0    # 0x1e010
movsd %xmm0, -0xa8(%rbp)
jmp 0xac21
movsd -0xa8(%rbp), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x1, %edx
callq 0xba10
movsd -0xb8(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xb0(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x3, %edx
callq 0xba10
movsd -0xb0(%rbp), %xmm0
movsd %xmm0, (%rax)
movsd -0x38(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xac9a
movsd -0x40(%rbp), %xmm0
divsd -0x38(%rbp), %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0xaca7
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
jmp 0xaca7
movsd -0xc0(%rbp), %xmm0
movsd %xmm0, -0xc8(%rbp)
movq -0x28(%rbp), %rdi
movl -0x2c(%rbp), %esi
movl $0x4, %edx
callq 0xba10
movsd -0xc8(%rbp), %xmm0
movsd %xmm0, (%rax)
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)