// File: /workspace/github_cpp_makefile_star2_O1/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), %rsi
cmpq %rsi, %r8
je 0x81af
movq (%rdi), %r9
xorl %eax, %eax
xorl %r10d, %r10d
xorl %r11d, %r11d
movslq (%r8), %r15
movl (%r9,%r15,4), %ebp
xorl %r15d, %r15d
cmpl $0x1, %ebp
sete %r15b
addl %r15d, %r10d
leal 0x1(%r11), %r15d
cmpl $-0x9, %ebp
cmovnel %r15d, %eax
cmovel %r11d, %r15d
addq $0x4, %r8
movl %r15d, %r11d
cmpq %rsi, %r8
jne 0x8170
movl %r10d, %esi
cvtsi2sd %rsi, %xmm0
movapd %xmm0, 0x10(%rsp)
jmp 0x81bb
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
xorl %eax, %eax
movq %r8, 0xf1be(%rip)       # 0x17380
movl %eax, %esi
cvtsi2sd %rsi, %xmm1
testl %eax, %eax
jne 0x81d7
movsd 0x6e43(%rip), %xmm2     # 0xf018
jmp 0x81e1
movapd 0x10(%rsp), %xmm2
divsd %xmm1, %xmm2
movq (%rdx), %r8
movq 0x8(%rdx), %r9
xorpd %xmm0, %xmm0
xorl %edx, %edx
cmpq %r9, %r8
je 0x8237
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 0x81fe
movl %r11d, %r9d
cvtsi2sd %r9, %xmm3
jmp 0x823d
xorl %esi, %esi
xorpd %xmm3, %xmm3
movq %r8, 0xf13c(%rip)       # 0x17380
movl %esi, %r8d
movq (%rcx), %r9
movq 0x8(%rcx), %rcx
cmpq %rcx, %r9
je 0x8298
movq (%rdi), %rdi
xorl %edx, %edx
xorl %r10d, %r10d
xorl %r11d, %r11d
movslq (%r9), %r15
movl (%rdi,%r15,4), %ebp
xorl %r15d, %r15d
cmpl $0x1, %ebp
sete %r15b
addl %r15d, %r10d
leal 0x1(%r11), %r15d
cmpl $-0x9, %ebp
cmovnel %r15d, %edx
cmovel %r11d, %r15d
addq $0x4, %r9
movl %r15d, %r11d
cmpq %rcx, %r9
jne 0x825e
movl %r10d, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %rcx, %xmm0
cvtsi2sd %r8, %xmm4
movq %r9, 0xf0dc(%rip)       # 0x17380
movl %edx, %ecx
cvtsi2sd %rcx, %xmm5
testl %eax, %eax
movsd %xmm1, 0x20(%rsp)
movsd %xmm0, 0x58(%rsp)
movsd %xmm5, 0x50(%rsp)
movsd %xmm4, 0x48(%rsp)
movsd %xmm3, 0x40(%rsp)
je 0x82db
testl %esi, %esi
je 0x82db
testl %edx, %edx
je 0x8307
divsd %xmm5, %xmm0
jmp 0x830f
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
callq 0x8b0a
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax)
movsd 0x6d1c(%rip), %xmm0     # 0xf018
movsd %xmm0, 0x8(%rsp)
jmp 0x8391
movsd 0x6d09(%rip), %xmm0     # 0xf018
movsd %xmm0, 0x8(%rsp)
movapd %xmm3, %xmm0
divsd %xmm4, %xmm0
movsd 0x6cfb(%rip), %xmm1     # 0xf020
movapd %xmm1, %xmm5
subsd %xmm0, %xmm5
subsd %xmm2, %xmm1
movsd %xmm1, 0x38(%rsp)
mulsd %xmm1, %xmm0
mulsd %xmm2, %xmm5
addsd %xmm0, %xmm5
movsd %xmm5, 0x28(%rsp)
movq %r14, %rdi
movl %ebx, %esi
xorl %edx, %edx
movsd %xmm2, 0x30(%rsp)
callq 0x8b0a
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movsd 0x8(%rsp), %xmm0
movsd 0x6cad(%rip), %xmm1     # 0xf020
subsd %xmm0, %xmm1
movsd 0x38(%rsp), %xmm2
mulsd %xmm0, %xmm2
mulsd 0x30(%rsp), %xmm1
addsd %xmm2, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd 0x10(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
addsd 0x58(%rsp), %xmm0
movapd %xmm0, 0x10(%rsp)
movsd 0x20(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq %r14, %rdi
movl %ebx, %esi
movl $0x1, %edx
callq 0x8b0a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r14, %rdi
movl %ebx, %esi
movl $0x3, %edx
callq 0x8b0a
movsd 0x20(%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 0x8b0a
movaps 0x10(%rsp), %xmm0
movlps %xmm0, (%rax)
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq