// File: /workspace/github_cpp_makefile_star2_O1/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::BedFileI::_ld(char const*, char const*, sampFiles::PopIndex const&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&)
void BedFileI::_ld(const char *snp1, const char *snp2, const PopIndex &popID, vector<double> &rSq, vector<double> &Dprime, vector<double> &dcnt1, vector<double> &dcnt2){

	// The data in .bed files are strictly biallelic by definition of the format, so that makes things easier.
	// Using derived alleles because they are mostly at low frequency so the expected number of calculations is low

	// constants
	const char homDerv = static_cast<char>(0x00); // homozygous derived for all bit-pair positions

	// modifiable parameters
	double D;
	double p1;     // frequency of the derived allele at snp1
	double q1;     // frequency of the derived allele at snp2
	double f11;    // frequency of the (hom. maj. allele @1, hom. maj.allel @2) haplotype; to be estimated; D = f11 - p1*q1
	double n11;    // # of 1,1 homozygote haplotypes
	double n12;    // # of hom. 1, heterozygous 2 haplotypes
	double n21;    // # of het. 1, hom. 2 haplotypes
	double n22;    // # of het. 1, het. 2 haplotypes
	double Npres;  // # of present genotypes (present at both loci)
	double p1q1;   // p1*q1
	double p2q2;   // p2*q2
	// f11 min and max
	double f11Min;
	double f11Max;
	// re-used intermediate variables
	double opq;       // 1-2p-2q
	double smn;       // 2n11+n12+n21
	double ppq;       // p1+q1
	// qubic equation coefficients
	double a;
	double b;
	double c;
	double d;
	// decision variables
	double bda;    // b/a
	double xN;
	double deltaSq;
	double hSq;
	double gammaN;
	double Delta3;

	char curBitPair1;
	char curBitPair2;

	for (size_t iPop = 0; iPop < popID.popNumber(); iPop++) {
		// reset everything
		p1    = 0.0;
		q1    = 0.0;
		f11   = 0.0;
		n11   = 0.0;
		n12   = 0.0;
		n21   = 0.0;
		n22   = 0.0;
		Npres = 0.0;

		for (auto popIt = popID[iPop].begin(); popIt != popID[iPop].end(); ++popIt) {
			size_t byteInd    = (*popIt)/4;
			size_t bitPairInd = (*popIt) % 4; // will automatically index from the end of the byte
			curBitPair1 = snp1[byteInd] & _masks[bitPairInd];
			curBitPair2 = snp2[byteInd] & _masks[bitPairInd];

			// Test miss
			if ( (curBitPair1 == _tests.at('M')[bitPairInd]) || (curBitPair2 == _tests.at('M')[bitPairInd]) ) { // must use .at() because _tests is const
				continue;
			}
			Npres += 1.0;
			// Go through all the pairwise possibilities; adding up gametes (one per diploid)
			if (curBitPair1 == homDerv) {
				p1 += 1.0;
				if (curBitPair2 == homDerv) {
					q1  += 1.0;
					n11 += 1.0;
				} else if (curBitPair2 == _tests.at('H')[bitPairInd]){
					q1  += 0.5;
					n12 += 1.0;
				}
			} else if (curBitPair1 == _tests.at('H')[bitPairInd]){
				p1 += 0.5;
				if (curBitPair2 == homDerv) {
					q1  += 1.0;
					n21 += 1.0;
				} else if (curBitPair2 == _tests.at('H')[bitPairInd]){
					q1  += 0.5;
					n22 += 1.0;
				}
			} else {
				if (curBitPair2 == homDerv) {
					q1 += 1.0;
				} else if (curBitPair2 == _tests.at('H')[bitPairInd]){
					q1 += 0.5;
				}
			}
		}
		dcnt1[iPop]  = (p1 <= Npres - p1 ? 2.0*p1 : 2.0*(Npres - p1));
		dcnt2[iPop]  = (q1 <= Npres - q1 ? 2.0*q1 : 2.0*(Npres - q1));

		if (Npres <= 1.0) {
			rSq[iPop]    = -9.0;
			Dprime[iPop] = -9.0;
			continue;
		}
		if ( (dcnt1[iPop] == 0.0) || (dcnt2[iPop] == 0.0) ) { // even if the SNPs are pre-screened for polymorphism, the alternatives may correspond to missing at the other locus
			rSq[iPop]    = -9.0;
			Dprime[iPop] = -9.0;
			continue;
		}
		if (n22 == Npres) { // if everyone is het, cannot determine LD because we don't know the phase
			rSq[iPop]    = -9.0;
			Dprime[iPop] = -9.0;
			continue;
		}
		// Finish calculating statistics if everything is sane
		p1   = p1/Npres;
		q1   = q1/Npres;
		// intermediate values
		p1q1 = p1*q1;

		if (n22 == 0.0) { // no het/het match-ups; means I can directly enumerate haplotypes
			f11        = (n11 + 0.5*(n12 + n21))/Npres;
			D          = f11 - p1q1;
			p2q2       = (1.0-p1)*(1.0-q1);
			rSq[iPop]  = (D*D)/(p1q1*p2q2);

			if (D < -10.0*EPS) {
				double Dmax  = ( p1q1 <= p2q2 ? -p1q1 : -p2q2 ); // (9) of Gaunt et al.
				Dprime[iPop] = D/Dmax;
			} else if (D > 10.0*EPS) {
				p1q1 = p1*(1.0 - q1);
				p2q2 = (1.0 - p1)*q1;

				double Dmax  = ( p1q1 <= p2q2 ? p1q1 : p2q2 ); // (9) of Gaunt et al.
				Dprime[iPop] = D/Dmax;
			} else {
				Dprime[iPop] = 0.0;
			}

		} else {
			// There is n22, so need to estimate f11 via ML (using Gaunt et al. cubic equation); they say f11 is between major alleles, but that is not necessary. Results will be the same for minor or any other combination
			// Calculate the bracketing values for f11:
			f11Min = n11 + 0.5*(n12 + n21); // i.e., all the het/het haplotypes are crossovers
			f11Max = f11Min + 0.5*n22;      // i.e., all het/het haplotypes are in phase
			f11Min = f11Min/Npres - 100.0*EPS; // some padding to take into account round-off errors
			f11Max = f11Max/Npres + 100.0*EPS;
			// re-used auxiliary variables
			ppq = p1 + q1;
			opq = 1.0 - 2.0*ppq;
			smn = 2.0*n11 + n12 + n21;
			// coefficients
			a   = 2.0*Npres; // will need to multiply again later to make 4N; doing this to re-use the 2N
			b   = a*opq - 2.0*smn - n22;
			c   = a*p1q1 - smn*opq - n22*(1.0 - ppq);
			d   = -smn*p1q1;
			a  *= 2.0;
			// decision variables
			bda     = b/a;
			xN      = -bda/3.0;
			deltaSq = bda*bda/9.0 - c/(3.0*a);
			hSq     = 4.0*a*a*deltaSq*deltaSq*deltaSq;
			gammaN  = xN*( xN*( a*xN + b ) + c ) + d;
			Delta3  = gammaN*gammaN - hSq;

			// now decide how many roots we have
			if (Delta3 > 100.0*EPS) { // everything is cool, only one distinct root
				f11 = xN + cbrt((sqrt(Delta3) - gammaN)/(2.0*a)) + cbrt(-(sqrt(Delta3) + gammaN)/(2.0*a));
			} else if (Delta3 < -100.0*EPS){ // Three roots; worst case scenario
				double theta  = acos(-gammaN/sqrt(hSq))/3.0;
				double ddelta = 2.0*sqrt(deltaSq);
				double alpha  = xN + ddelta*cos(theta);
				double beta   = xN + ddelta*cos(2.0*PI/3.0 + theta);
				double gamma  = xN + ddelta*cos(4.0*PI/3.0 + theta);

				if ( (alpha < f11Min) || (alpha > f11Max) ) {
					if ( (beta < f11Min) || (beta > f11Max) ) {
						if ( (gamma < f11Min) || (gamma > f11Max) ) {
							rSq[iPop]    = -9.0;
							Dprime[iPop] = -9.0;
							continue;
						} else {
							f11 = gamma;
						}
					} else if ( (gamma < f11Min) || (gamma > f11Max) ) {
						f11 = beta;
					} else {
						// two plausible roots; choose the one with smallest |D|
						f11 = (fabs(beta - p1q1) <= fabs(gamma - p1q1) ? beta : gamma);
					}
				} else {
					if ( (beta < f11Min) || (beta > f11Max) ) {
						if ( (gamma < f11Min) || (gamma > f11Max) ) {
							f11 = alpha;
						} else {
							// two plausible roots; choose the one with smallest |D|
							f11 = (fabs(alpha - p1q1) <= fabs(gamma - p1q1) ? alpha : gamma);
						}
					} else if ( (gamma < f11Min) || (gamma > f11Max) ) {
						// two plausible roots; choose the one with smallest |D|
						f11 = (fabs(beta - p1q1) <= fabs(alpha - p1q1) ? beta : alpha);
					} else {
						// three plausible roots; choose the one with smallest |D|
						double minAB = (fabs(beta - p1q1) <= fabs(alpha - p1q1) ? beta : alpha);
						f11 = (fabs(minAB - p1q1) <= fabs(gamma - p1q1) ? minAB : gamma);
					}

				}
			} else { // Delta3 == 0; two different roots
				double mu    = cbrt(gammaN/(2.0*a));
				double alpha = xN + mu;
				double gamma = xN - 2.0*mu;
				if ( (alpha < f11Min) || (alpha > f11Max) ) { // f11 cannot be greater than min(p1, q1)
					if ( (gamma < f11Min) || (gamma > f11Max) ) {
						rSq[iPop]    = -9.0;
						Dprime[iPop] = -9.0;
						continue;
					} else {
						f11 = gamma;
					}
				} else {
					if ( (gamma < f11Min) || (gamma > f11Max) ) { // impossible gamma; going with alpha
						f11 = alpha;
					} else { // both doable; conservatively choose the one minimzing |D|
						f11 = (fabs(alpha - p1q1) <= fabs(gamma - p1q1) ? alpha : gamma);
					}
				}

			}

			D         = f11 - p1q1;
			p2q2      = (1.0-p1)*(1.0-q1);
			rSq[iPop] = (D*D)/(p1q1*p2q2);

			if (D < -10.0*EPS) {
				double Dmax  = ( p1q1 <= p2q2 ? -p1q1 : -p2q2 ); // (9) of Gaunt et al.
				Dprime[iPop] = D/Dmax;
			} else if (D > 10.0*EPS) {
				p1q1 = p1*(1.0 - q1);
				p2q2 = (1.0 - p1)*q1;

				double Dmax  = ( p1q1 <= p2q2 ? p1q1 : p2q2 ); // (9) of Gaunt et al.
				Dprime[iPop] = D/Dmax;
			} else {
				Dprime[iPop] = 0.0;
			}

		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %r9, 0x90(%rsp)
movq %r8, 0x38(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq (%rcx), %rax
cmpq %rax, 0x8(%rcx)
je 0xb296
movq %rcx, %r15
xorl %ebx, %ebx
leaq 0x15cb1(%rip), %r14     # 0x20300
movq %rcx, 0x98(%rsp)
movq %rbx, 0xc8(%rsp)
leaq (,%rbx,8), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx), %rbx
movq (%r15), %rax
xorpd %xmm0, %xmm0
movapd %xmm0, 0x70(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
xorpd %xmm7, %xmm7
xorpd %xmm6, %xmm6
cmpq 0x8(%rax,%rcx), %rbx
je 0xa963
movq 0xc8(%rsp), %rax
leaq (%rax,%rax,2), %rax
movq %rax, 0x80(%rsp)
movapd %xmm0, 0x40(%rsp)
movapd %xmm0, 0x50(%rsp)
movapd %xmm0, 0x60(%rsp)
movapd %xmm0, 0xa0(%rsp)
movapd %xmm0, 0x70(%rsp)
movapd %xmm7, 0x20(%rsp)
movapd %xmm6, 0x10(%rsp)
movq (%rbx), %rbp
movq %rbp, %rax
shrq $0x2, %rax
andl $0x3, %ebp
movq 0x15be9(%rip), %rcx     # 0x202e8
movb (%rcx,%rbp), %r15b
movq 0xd0(%rsp), %rcx
movb (%rcx,%rax), %r13b
andb %r15b, %r13b
movq 0xd8(%rsp), %rcx
movb (%rcx,%rax), %r12b
movb $0x4d, 0xf(%rsp)
movq %r14, %rdi
leaq 0xf(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r13b
jne 0xa743
movq 0x98(%rsp), %r15
jmp 0xa770
andb %r15b, %r12b
movb $0x4d, 0xe(%rsp)
movq %r14, %rdi
leaq 0xe(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r12b
jne 0xa79b
movq 0x98(%rsp), %r15
leaq 0x15b90(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
addq $0x8, %rbx
movq (%r15), %rax
movq 0x80(%rsp), %rcx
cmpq 0x8(%rax,%rcx,8), %rbx
jne 0xa6df
jmp 0xa963
movsd 0xdd3d(%rip), %xmm0     # 0x184e0
movapd 0x70(%rsp), %xmm1
addsd %xmm0, %xmm1
testb %r13b, %r13b
movq 0x98(%rsp), %r15
movapd %xmm1, 0x70(%rsp)
je 0xa854
movb $0x48, 0xc(%rsp)
leaq 0x15b30(%rip), %rdi     # 0x20300
leaq 0xc(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r13b
jne 0xa8ba
movapd 0x10(%rsp), %xmm6
addsd 0xdcf3(%rip), %xmm6     # 0x184e8
testb %r12b, %r12b
je 0xa915
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0xb(%rsp)
leaq 0x15af0(%rip), %rdi     # 0x20300
leaq 0xb(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r12b
jne 0xa769
movapd 0x20(%rsp), %xmm7
addsd 0xdcb3(%rip), %xmm7     # 0x184e8
movapd 0xa0(%rsp), %xmm0
addsd 0xdc9a(%rip), %xmm0     # 0x184e0
movapd %xmm0, 0xa0(%rsp)
jmp 0xa951
movapd 0x10(%rsp), %xmm6
addsd %xmm0, %xmm6
testb %r12b, %r12b
leaq 0x15a98(%rip), %r14     # 0x20300
je 0xa8f6
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0xd(%rsp)
movq %r14, %rdi
leaq 0xd(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r12b
jne 0xa769
movapd 0x20(%rsp), %xmm7
addsd 0xdc47(%rip), %xmm7     # 0x184e8
movapd 0x50(%rsp), %xmm0
addsd 0xdc31(%rip), %xmm0     # 0x184e0
movapd %xmm0, 0x50(%rsp)
jmp 0xa951
testb %r12b, %r12b
je 0xa943
movb $0x48, 0xa(%rsp)
leaq 0x15a31(%rip), %rdi     # 0x20300
leaq 0xa(%rsp), %rsi
callq 0x16d8a
movq (%rax), %rax
cmpb (%rax,%rbp), %r12b
jne 0xa769
movapd 0x20(%rsp), %xmm7
addsd 0xdbf4(%rip), %xmm7     # 0x184e8
jmp 0xa951
movapd 0x20(%rsp), %xmm7
addsd %xmm0, %xmm7
movapd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
jmp 0xa77c
movsd 0xdbc3(%rip), %xmm0     # 0x184e0
movapd 0x20(%rsp), %xmm7
addsd %xmm0, %xmm7
movapd 0x60(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x60(%rsp)
leaq 0x159c2(%rip), %r14     # 0x20300
jmp 0xa77c
movapd 0x20(%rsp), %xmm7
addsd 0xdb8f(%rip), %xmm7     # 0x184e0
leaq 0x159a8(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
jmp 0xa77c
movapd 0x70(%rsp), %xmm3
movapd %xmm3, %xmm0
subsd %xmm6, %xmm0
movapd %xmm6, %xmm1
cmplesd %xmm0, %xmm1
movapd %xmm1, %xmm2
andnpd %xmm0, %xmm2
andpd %xmm6, %xmm1
orpd %xmm2, %xmm1
addsd %xmm1, %xmm1
movq 0x130(%rsp), %rax
movq (%rax), %rax
movq 0xc8(%rsp), %rbx
movsd %xmm1, (%rax,%rbx,8)
movapd %xmm3, %xmm1
subsd %xmm7, %xmm1
movapd %xmm7, %xmm0
cmplesd %xmm1, %xmm0
movapd %xmm0, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm7, %xmm0
orpd %xmm2, %xmm0
addsd %xmm0, %xmm0
movq 0x138(%rsp), %rcx
movq (%rcx), %rcx
movsd %xmm0, (%rcx,%rbx,8)
movsd 0xdafd(%rip), %xmm1     # 0x184e0
ucomisd %xmm3, %xmm1
jae 0xaa20
movsd (%rax,%rbx,8), %xmm1
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm1
xorpd %xmm1, %xmm1
jne 0xa9fe
jnp 0xaa20
ucomisd %xmm1, %xmm0
jne 0xaa06
jnp 0xaa20
movapd 0x70(%rsp), %xmm9
movapd 0xa0(%rsp), %xmm11
ucomisd %xmm9, %xmm11
jne 0xaa7b
jp 0xaa7b
movq 0x38(%rsp), %rax
movq (%rax), %rax
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax,%rbx,8)
movsd 0xdab2(%rip), %xmm2     # 0x184f0
movq 0x90(%rsp), %rax
movq (%rax), %rax
movsd %xmm2, (%rax,%rbx,8)
incq %rbx
movq (%r15), %rax
movq 0x8(%r15), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
movabsq $-0x5555555555555555, %rdx # imm = 0xAAAAAAAAAAAAAAAB
imulq %rdx, %rcx
cmpq %rcx, %rbx
jb 0xa657
jmp 0xb296
divsd %xmm9, %xmm6
divsd %xmm9, %xmm7
movapd %xmm7, %xmm8
mulsd %xmm6, %xmm8
ucomisd %xmm1, %xmm11
movapd 0x60(%rsp), %xmm10
movapd %xmm10, %xmm1
movapd 0x50(%rsp), %xmm12
addsd %xmm12, %xmm1
movsd 0xda34(%rip), %xmm5     # 0x184e8
mulsd %xmm5, %xmm1
movapd 0x40(%rsp), %xmm2
addsd %xmm2, %xmm1
movapd %xmm1, %xmm13
divsd %xmm9, %xmm13
movsd 0xda0c(%rip), %xmm4     # 0x184e0
jne 0xab51
jp 0xab51
subsd %xmm8, %xmm13
movapd %xmm4, %xmm1
movapd %xmm4, %xmm0
subsd %xmm6, %xmm0
subsd %xmm7, %xmm1
movapd %xmm1, %xmm2
mulsd %xmm0, %xmm2
movapd %xmm13, %xmm3
mulsd %xmm13, %xmm3
movapd %xmm8, %xmm4
mulsd %xmm2, %xmm4
divsd %xmm4, %xmm3
movq 0x38(%rsp), %rax
movq (%rax), %rax
movsd %xmm3, (%rax,%rbx,8)
movsd 0xda17(%rip), %xmm3     # 0x18538
ucomisd %xmm13, %xmm3
jbe 0xad7e
movapd %xmm8, %xmm0
cmplesd %xmm2, %xmm0
andpd %xmm0, %xmm8
andnpd %xmm2, %xmm0
orpd %xmm8, %xmm0
xorpd 0xd984(%rip), %xmm0     # 0x184d0
jmp 0xadae
movapd %xmm7, %xmm0
addsd %xmm6, %xmm0
movapd %xmm4, %xmm3
subsd %xmm0, %xmm3
addsd %xmm0, %xmm0
subsd %xmm0, %xmm4
addsd %xmm2, %xmm2
addsd %xmm12, %xmm2
addsd %xmm2, %xmm10
movapd %xmm9, %xmm14
addsd %xmm9, %xmm14
movapd %xmm14, %xmm0
mulsd %xmm4, %xmm0
movapd %xmm10, %xmm2
addsd %xmm10, %xmm2
subsd %xmm2, %xmm0
subsd %xmm11, %xmm0
mulsd %xmm10, %xmm4
movapd %xmm14, %xmm2
mulsd %xmm8, %xmm2
subsd %xmm4, %xmm2
mulsd %xmm11, %xmm3
subsd %xmm3, %xmm2
addsd %xmm14, %xmm14
movapd %xmm0, %xmm3
divsd %xmm14, %xmm3
movapd %xmm3, %xmm12
movsd 0xd934(%rip), %xmm4     # 0x18508
divsd %xmm4, %xmm12
movapd %xmm14, %xmm15
mulsd %xmm12, %xmm15
addsd %xmm0, %xmm15
movapd %xmm14, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm12, %xmm15
addsd %xmm2, %xmm15
movapd %xmm2, %xmm4
divsd %xmm0, %xmm4
mulsd %xmm5, %xmm11
movsd 0xd8e8(%rip), %xmm2     # 0x184f8
addsd %xmm2, %xmm13
mulsd %xmm8, %xmm10
mulsd %xmm3, %xmm3
divsd 0xd8ea(%rip), %xmm3     # 0x18510
addsd %xmm3, %xmm4
movapd %xmm14, %xmm0
mulsd 0xd8e1(%rip), %xmm0     # 0x18518
mulsd %xmm14, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
movapd %xmm12, 0x80(%rsp)
mulsd %xmm12, %xmm15
subsd %xmm10, %xmm15
movapd %xmm15, %xmm5
mulsd %xmm15, %xmm5
subsd %xmm0, %xmm5
movsd 0xd88e(%rip), %xmm3     # 0x18500
ucomisd %xmm3, %xmm5
movapd %xmm6, 0x10(%rsp)
movapd %xmm7, 0x20(%rsp)
movapd %xmm8, 0xb0(%rsp)
jbe 0xadbd
xorps %xmm0, %xmm0
sqrtsd %xmm5, %xmm0
movapd %xmm5, 0x60(%rsp)
ucomisd 0xd8a1(%rip), %xmm5     # 0x18548
movsd %xmm0, 0x50(%rsp)
movapd %xmm15, 0x40(%rsp)
jae 0xacd6
movapd 0x60(%rsp), %xmm0
movsd %xmm14, 0x70(%rsp)
callq 0x4220
movapd 0x40(%rsp), %xmm15
movsd 0x70(%rsp), %xmm14
subsd %xmm15, %xmm0
addsd %xmm14, %xmm14
movsd %xmm14, 0x70(%rsp)
divsd %xmm14, %xmm0
callq 0x4070
movapd %xmm0, %xmm2
movapd 0x60(%rsp), %xmm0
ucomisd 0xd845(%rip), %xmm0     # 0x18548
movsd 0x50(%rsp), %xmm1
jae 0xad20
movsd %xmm2, 0x60(%rsp)
callq 0x4220
movsd 0x60(%rsp), %xmm2
movapd %xmm0, %xmm1
movapd 0x80(%rsp), %xmm0
addsd %xmm2, %xmm0
movapd %xmm0, 0x80(%rsp)
movapd 0x40(%rsp), %xmm0
addsd %xmm1, %xmm0
xorpd 0xd788(%rip), %xmm0     # 0x184d0
divsd 0x70(%rsp), %xmm0
callq 0x4070
addsd 0x80(%rsp), %xmm0
leaq 0x1559d(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
movapd 0xb0(%rsp), %xmm8
jmp 0xb1e7
xorpd %xmm2, %xmm2
ucomisd 0xd7b5(%rip), %xmm13    # 0x18540
jbe 0xaa3e
mulsd %xmm1, %xmm6
mulsd %xmm0, %xmm7
movapd %xmm6, %xmm0
cmplesd %xmm7, %xmm0
andpd %xmm0, %xmm6
andnpd %xmm7, %xmm0
orpd %xmm6, %xmm0
divsd %xmm0, %xmm13
movapd %xmm13, %xmm2
jmp 0xaa3e
addsd %xmm1, %xmm11
divsd %xmm9, %xmm11
addsd %xmm3, %xmm11
ucomisd %xmm5, %xmm2
movapd %xmm13, 0x60(%rsp)
movapd %xmm11, 0x70(%rsp)
jbe 0xadfd
movsd %xmm4, 0x50(%rsp)
ucomisd 0xd75a(%rip), %xmm0     # 0x18548
jb 0xaed9
sqrtsd %xmm0, %xmm0
jmp 0xaeec
addsd %xmm14, %xmm14
divsd %xmm14, %xmm15
movapd %xmm15, %xmm0
callq 0x4070
movapd 0x70(%rsp), %xmm3
movapd 0x80(%rsp), %xmm4
movapd %xmm4, %xmm1
addsd %xmm0, %xmm1
addsd %xmm0, %xmm0
subsd %xmm0, %xmm4
movapd 0x60(%rsp), %xmm5
ucomisd %xmm1, %xmm5
movapd %xmm3, %xmm0
cmpltpd %xmm4, %xmm0
movapd %xmm4, %xmm2
cmpltpd %xmm5, %xmm2
orpd %xmm0, %xmm2
movd %xmm2, %ecx
ja 0xae81
ucomisd %xmm3, %xmm1
ja 0xae81
movb $0x1, %al
testb $0x1, %cl
leaq 0x15498(%rip), %r14     # 0x20300
movapd 0xb0(%rsp), %xmm8
je 0xb0b8
movapd %xmm1, %xmm4
jmp 0xb0ef
movb $0x1, %al
testb $0x1, %cl
leaq 0x15473(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
movapd 0xb0(%rsp), %xmm8
je 0xb0fb
movq 0x38(%rsp), %rax
movq (%rax), %rax
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax,%rbx,8)
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rcx, (%rax,%rbx,8)
xorpd %xmm4, %xmm4
xorl %eax, %eax
jmp 0xb0fb
movapd %xmm15, 0x40(%rsp)
callq 0x4220
movapd 0x40(%rsp), %xmm15
xorpd 0xd5db(%rip), %xmm15    # 0x184d0
divsd %xmm0, %xmm15
movapd %xmm15, %xmm0
callq 0x4280
divsd 0xd614(%rip), %xmm0     # 0x18520
movsd %xmm0, 0x40(%rsp)
xorpd %xmm1, %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0xaf28
sqrtsd %xmm0, %xmm0
jmp 0xaf2d
callq 0x4220
addsd %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x40(%rsp), %xmm0
callq 0x4520
mulsd 0x50(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movapd %xmm0, 0xa0(%rsp)
movsd 0x40(%rsp), %xmm0
addsd 0xd5c0(%rip), %xmm0     # 0x18528
callq 0x4520
mulsd 0x50(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movapd %xmm0, 0xe0(%rsp)
movsd 0x40(%rsp), %xmm0
addsd 0xd59d(%rip), %xmm0     # 0x18530
callq 0x4520
movapd 0xe0(%rsp), %xmm9
movapd 0xa0(%rsp), %xmm5
mulsd 0x50(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
movapd 0x60(%rsp), %xmm3
ucomisd %xmm5, %xmm3
movapd 0x70(%rsp), %xmm4
movapd %xmm4, %xmm1
cmpltpd %xmm9, %xmm1
movapd %xmm9, %xmm2
cmpltpd %xmm3, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %eax
movapd %xmm4, %xmm1
cmpltpd %xmm0, %xmm1
movapd %xmm0, %xmm2
cmpltpd %xmm3, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
ja 0xb049
ucomisd %xmm4, %xmm5
ja 0xb049
testb $0x1, %al
movapd 0xd4ae(%rip), %xmm4     # 0x184c0
leaq 0x152e7(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
movapd 0xb0(%rsp), %xmm8
je 0xb104
movb $0x1, %al
testb $0x1, %cl
je 0xb1b0
movapd %xmm5, %xmm0
jmp 0xb1df
testb $0x1, %al
leaq 0x152ae(%rip), %r14     # 0x20300
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
movapd 0xb0(%rsp), %xmm8
movb $0x1, %al
je 0xb0a5
testb $0x1, %cl
je 0xb1df
movq 0x38(%rsp), %rax
movq (%rax), %rax
movabsq $-0x3fde000000000000, %rcx # imm = 0xC022000000000000
movq %rcx, (%rax,%rbx,8)
movq 0x90(%rsp), %rax
movq (%rax), %rax
movq %rcx, (%rax,%rbx,8)
xorpd %xmm0, %xmm0
xorl %eax, %eax
jmp 0xb1df
testb $0x1, %cl
je 0xb175
movapd %xmm9, %xmm0
jmp 0xb1df
movapd %xmm1, %xmm0
subsd %xmm8, %xmm0
movapd 0xd3f7(%rip), %xmm3     # 0x184c0
andpd %xmm3, %xmm0
movapd %xmm4, %xmm2
subsd %xmm8, %xmm2
andpd %xmm3, %xmm2
cmplesd %xmm2, %xmm0
andpd %xmm0, %xmm1
andnpd %xmm4, %xmm0
orpd %xmm1, %xmm0
movapd %xmm0, %xmm4
movapd 0x10(%rsp), %xmm6
movapd 0x20(%rsp), %xmm7
movapd %xmm4, %xmm0
jmp 0xb1df
movapd %xmm9, %xmm1
subsd %xmm8, %xmm1
andpd %xmm4, %xmm1
movapd %xmm5, %xmm2
subsd %xmm8, %xmm2
andpd %xmm4, %xmm2
cmplesd %xmm2, %xmm1
movapd %xmm1, %xmm2
andpd %xmm9, %xmm2
andnpd %xmm5, %xmm1
orpd %xmm2, %xmm1
movb $0x1, %al
testb $0x1, %cl
jne 0xb1db
movapd %xmm1, %xmm2
subsd %xmm8, %xmm2
andpd %xmm4, %xmm2
movapd %xmm0, %xmm3
subsd %xmm8, %xmm3
andpd %xmm4, %xmm3
cmplesd %xmm3, %xmm2
movapd %xmm2, %xmm3
andpd %xmm1, %xmm3
andnpd %xmm0, %xmm2
orpd %xmm3, %xmm2
movapd %xmm2, %xmm0
jmp 0xb1df
movapd %xmm9, %xmm1
subsd %xmm8, %xmm1
movapd 0xd339(%rip), %xmm3     # 0x184c0
andpd %xmm3, %xmm1
movapd %xmm0, %xmm2
subsd %xmm8, %xmm2
andpd %xmm3, %xmm2
cmplesd %xmm2, %xmm1
movapd %xmm1, %xmm2
andpd %xmm9, %xmm2
andnpd %xmm0, %xmm1
orpd %xmm2, %xmm1
jmp 0xb1db
movapd %xmm5, %xmm1
subsd %xmm8, %xmm1
andpd %xmm4, %xmm1
movapd %xmm0, %xmm2
subsd %xmm8, %xmm2
andpd %xmm4, %xmm2
cmplesd %xmm2, %xmm1
andpd %xmm1, %xmm5
andnpd %xmm0, %xmm1
orpd %xmm5, %xmm1
movapd %xmm1, %xmm0
testb %al, %al
je 0xaa4e
subsd %xmm8, %xmm0
movsd 0xd2ec(%rip), %xmm3     # 0x184e0
movapd %xmm3, %xmm1
subsd %xmm6, %xmm1
subsd %xmm7, %xmm3
movapd %xmm3, %xmm2
mulsd %xmm1, %xmm2
movapd %xmm0, %xmm4
mulsd %xmm0, %xmm4
movapd %xmm8, %xmm5
mulsd %xmm2, %xmm5
divsd %xmm5, %xmm4
movq 0x38(%rsp), %rax
movq (%rax), %rax
movsd %xmm4, (%rax,%rbx,8)
movsd 0xd306(%rip), %xmm4     # 0x18538
ucomisd %xmm0, %xmm4
jbe 0xb25a
movapd %xmm8, %xmm1
cmplesd %xmm2, %xmm1
andpd %xmm1, %xmm8
andnpd %xmm2, %xmm1
orpd %xmm8, %xmm1
xorpd 0xd278(%rip), %xmm1     # 0x184d0
jmp 0xb289
xorpd %xmm2, %xmm2
ucomisd 0xd2da(%rip), %xmm0     # 0x18540
jbe 0xaa3e
mulsd %xmm3, %xmm6
mulsd %xmm1, %xmm7
movapd %xmm6, %xmm1
cmplesd %xmm7, %xmm1
andpd %xmm1, %xmm6
andnpd %xmm7, %xmm1
orpd %xmm6, %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, %xmm2
jmp 0xaa3e
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq