// File: /workspace/github_cpp_makefile_star2_O2/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::BedFileI::_ld(char const*, char const*, unsigned long const&, unsigned short const&, double&, double&, double&, double&)
void BedFileI::_ld(const char *snp1, const char *snp2, const size_t &N, const unsigned short &pad, double &rSq, double &Dprime, double &dcnt1, double &dcnt2){
	// Using the Gaunt et al. (2007) formulation
	// 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
	// However, then convert everything to the most common allele frequencies to make things compatible with Gaunt et al.
	// Using the notation in that paper, too, to make things more clear

	// constants
	const char allMiss = static_cast<char>(0x55); // all genotypes missing in a byte
	const char homDerv = static_cast<char>(0x00); // homozygous derived for all bit-pair positions

	// modifiable parameters
	double D     = 0.0;
	double p1    = 0.0; // frequency of the derived allele at snp1
	double q1    = 0.0; // frequency of the derived allele at snp2
	double f11   = 0.0; // frequency of the (hom. maj. allele @1, hom. maj.allel @2) haplotype; to be estimated; D = f11 - p1*q1
	double n11   = 0.0; // # of 1,1 homozygote haplotypes
	double n12   = 0.0; // # of hom. 1, heterozygous 2 haplotypes
	double n21   = 0.0; // # of het. 1, hom. 2 haplotypes
	double n22   = 0.0; // # of het. 1, het. 2 haplotypes
	double Npres = 0.0; // # 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;

	// Data missing at either locus are ignored (i.e. I am using pairwise-complete observations)
	for (size_t ind = 0; ind < N-1; ind++) { // leave the last byte out for now since it has padding bits (in general)
		if ( (snp1[ind] == allMiss) || (snp2[ind] == allMiss) ) { // a byte with all data missing; ignore (NOTE the OR)
			continue;
		}

		// go through the byte from the end
		for (size_t inByte = 0; inByte < 4; inByte++) {
			curBitPair1 = snp1[ind] & _masks[inByte];
			curBitPair2 = snp2[ind] & _masks[inByte];

			// Test missingness
			if ( (curBitPair1 == _tests.at('M')[inByte]) || (curBitPair2 == _tests.at('M')[inByte]) ) { // 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')[inByte]){
					q1  += 0.5;
					n12 += 1.0;
				}
			} else if (curBitPair1 == _tests.at('H')[inByte]){
				p1 += 0.5;
				if (curBitPair2 == homDerv) {
					q1  += 1.0;
					n21 += 1.0;
				} else if (curBitPair2 == _tests.at('H')[inByte]){
					q1  += 0.5;
					n22 += 1.0;
				}
			} else {
				if (curBitPair2 == homDerv) {
					q1 += 1.0;
				} else if (curBitPair2 == _tests.at('H')[inByte]){
					q1 += 0.5;
				}
			}
		}
	}
	// finally, deal with the padded byte
	for (size_t inByte = 0; inByte < (4 - pad); inByte++) { // not testing for sane values of pad, since this is a protected function and I can expect sane values in correct class implementation
		curBitPair1 = snp1[N-1] & _masks[inByte];
		curBitPair2 = snp2[N-1] & _masks[inByte];

		// Test missingness
		if ( (curBitPair1 == _tests.at('M')[inByte]) || (curBitPair2 == _tests.at('M')[inByte]) ) { // 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')[inByte]){
				q1  += 0.5;
				n12 += 1.0;
			}
		} else if (curBitPair1 == _tests.at('H')[inByte]){
			p1 += 0.5;
			if (curBitPair2 == homDerv) {
				q1  += 1.0;
				n21 += 1.0;
			} else if (curBitPair2 == _tests.at('H')[inByte]){
				q1  += 0.5;
				n22 += 1.0;
			}
		} else {
			if (curBitPair2 == homDerv) {
				q1 += 1.0;
			} else if (curBitPair2 == _tests.at('H')[inByte]){
				q1 += 0.5;
			}
		}
	}

	// minor allele counts
	dcnt1 = (p1 <= Npres - p1 ? 2.0*p1 : 2.0*(Npres - p1));
	dcnt2 = (q1 <= Npres - q1 ? 2.0*q1 : 2.0*(Npres - q1));

	if (Npres <= 1.0) {
		rSq    = -9.0;
		Dprime = -9.0;
		return;
	}
	if ( (dcnt1 == 0.0) || (dcnt2 == 0.0) ) { // even if the SNPs are pre-screened for polymorphism, the alternatives may correspond to missing at the other locus
		rSq    = -9.0;
		Dprime = -9.0;
		return;
	}
	if (n22 == Npres) { // if everyone is het, cannot determine LD because we don't know the phase
		rSq    = -9.0;
		Dprime = -9.0;
		return;
	}
	// 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  = (D*D)/(p1q1*p2q2);

		if (D < -10.0*EPS) {
			double Dmax = ( p1q1 <= p2q2 ? -p1q1 : -p2q2 ); // (9) of Gaunt et al.
			Dprime      = 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      = D/Dmax;
		} else {
			Dprime = 0.0;
		}

		return;
	}

	// 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    = -9.0;
					Dprime = -9.0;

					return;

				} 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    = -9.0;
				Dprime = -9.0;

				return;
			} 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  = (D*D)/(p1q1*p2q2);

	if (D < -10.0*EPS) {
		double Dmax = ( p1q1 <= p2q2 ? -p1q1 : -p2q2 ); // (9) of Gaunt et al.
		Dprime      = 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      = D/Dmax;
	} else {
		Dprime = 0.0;
	}

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %r9, 0x78(%rsp)
movq %r8, 0xc0(%rsp)
movq %rcx, 0xb0(%rsp)
movq %rdx, 0x80(%rsp)
movq %rsi, 0x90(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0x145a2(%rip), %rbp     # 0x1c300
movapd %xmm0, 0xa0(%rsp)
xorpd %xmm2, %xmm2
movsd %xmm0, 0x60(%rsp)
movsd %xmm0, 0x40(%rsp)
movapd %xmm0, (%rsp)
xorpd %xmm6, %xmm6
movq 0xb0(%rsp), %rax
movq (%rax), %rax
decq %rax
cmpq %rax, %r15
jae 0x7fe1
movq 0x90(%rsp), %rax
cmpb $0x55, (%rax,%r15)
je 0x7db5
movq 0x80(%rsp), %rax
cmpb $0x55, (%rax,%r15)
jne 0x7dba
incq %r15
jmp 0x7d80
xorl %r14d, %r14d
cmpq $0x4, %r14
je 0x7db5
movsd %xmm2, 0x50(%rsp)
movapd %xmm6, 0x10(%rsp)
movq 0x14512(%rip), %rax     # 0x1c2e8
movb (%rax,%r14), %r13b
movq 0x90(%rsp), %rax
movb (%rax,%r15), %r12b
andb %r13b, %r12b
movq 0x80(%rsp), %rax
movb (%rax,%r15), %bl
movb $0x4d, 0x2f(%rsp)
movq %rbp, %rdi
leaq 0x2f(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %r12b
je 0x7e2e
andb %r13b, %bl
movb $0x4d, 0x2e(%rsp)
movq %rbp, %rdi
leaq 0x2e(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %bl
jne 0x7e42
movapd 0x10(%rsp), %xmm6
movsd 0x50(%rsp), %xmm2
incq %r14
jmp 0x7dbd
movsd 0xc696(%rip), %xmm1     # 0x144e0
movsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
testb %r12b, %r12b
movsd %xmm0, 0x30(%rsp)
je 0x7eee
movb $0x48, 0x2c(%rsp)
movq %rbp, %rdi
leaq 0x2c(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %r12b
jne 0x7f50
movapd 0x10(%rsp), %xmm6
addsd 0xc660(%rip), %xmm6     # 0x144f0
testb %bl, %bl
je 0x7fa5
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0x2b(%rsp)
movq %rbp, %rdi
leaq 0x2b(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %bl
jne 0x7e2e
movapd (%rsp), %xmm0
addsd 0xc626(%rip), %xmm0     # 0x144f0
movapd %xmm0, (%rsp)
movapd 0xa0(%rsp), %xmm0
addsd 0xc600(%rip), %xmm0     # 0x144e0
movapd %xmm0, 0xa0(%rsp)
jmp 0x7e2e
movapd 0x10(%rsp), %xmm6
addsd %xmm1, %xmm6
testb %bl, %bl
je 0x7f82
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0x2d(%rsp)
movq %rbp, %rdi
leaq 0x2d(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %bl
jne 0x7e2e
movapd (%rsp), %xmm0
addsd 0xc5be(%rip), %xmm0     # 0x144f0
movapd %xmm0, (%rsp)
movsd 0x60(%rsp), %xmm0
addsd 0xc59b(%rip), %xmm0     # 0x144e0
movsd %xmm0, 0x60(%rsp)
jmp 0x7e2e
testb %bl, %bl
je 0x7fca
movb $0x48, 0x2a(%rsp)
movq %rbp, %rdi
leaq 0x2a(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r14), %bl
jne 0x7e2e
movapd (%rsp), %xmm0
addsd 0xc570(%rip), %xmm0     # 0x144f0
jmp 0x7fd7
movapd (%rsp), %xmm0
addsd %xmm1, %xmm0
movapd %xmm0, (%rsp)
movsd 0x40(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x7e34
movapd (%rsp), %xmm0
movsd 0xc52e(%rip), %xmm1     # 0x144e0
addsd %xmm1, %xmm0
movapd %xmm0, (%rsp)
movsd 0x50(%rsp), %xmm2
addsd %xmm1, %xmm2
jmp 0x7e3a
movapd (%rsp), %xmm0
addsd 0xc509(%rip), %xmm0     # 0x144e0
movapd %xmm0, (%rsp)
jmp 0x7e2e
movsd %xmm2, 0x50(%rsp)
xorl %r15d, %r15d
pushq $0x4
popq %rbp
leaq 0x1430c(%rip), %rbx     # 0x1c300
movsd 0x30(%rsp), %xmm4
movsd 0x60(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
movsd %xmm2, 0x40(%rsp)
movsd %xmm0, 0x60(%rsp)
movq 0xc0(%rsp), %rax
movzwl (%rax), %eax
movq %rbp, %rcx
subq %rax, %rcx
cmpq %rcx, %r15
jae 0x827c
movsd %xmm4, 0x30(%rsp)
movapd %xmm6, 0x10(%rsp)
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq 0x1429e(%rip), %rcx     # 0x1c2e8
movb (%rcx,%r15), %r13b
movq 0x90(%rsp), %rcx
movb -0x1(%rcx,%rax), %r12b
andb %r13b, %r12b
movq 0x80(%rsp), %rcx
movb -0x1(%rcx,%rax), %r14b
movb $0x4d, 0x29(%rsp)
movq %rbx, %rdi
leaq 0x29(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r12b
je 0x80a4
andb %r13b, %r14b
movb $0x4d, 0x28(%rsp)
movq %rbx, %rdi
leaq 0x28(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r14b
jne 0x80c4
movapd 0x10(%rsp), %xmm6
movsd 0x30(%rsp), %xmm4
movsd 0x60(%rsp), %xmm0
movsd 0x40(%rsp), %xmm2
incq %r15
jmp 0x8006
movsd 0xc414(%rip), %xmm1     # 0x144e0
movsd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
testb %r12b, %r12b
movsd %xmm0, 0x30(%rsp)
je 0x8171
movb $0x48, 0x26(%rsp)
movq %rbx, %rdi
leaq 0x26(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r12b
jne 0x81da
movapd 0x10(%rsp), %xmm6
addsd 0xc3de(%rip), %xmm6     # 0x144f0
testb %r14b, %r14b
je 0x823a
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0x25(%rsp)
movq %rbx, %rdi
leaq 0x25(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r14b
jne 0x80a4
movapd (%rsp), %xmm0
addsd 0xc3a3(%rip), %xmm0     # 0x144f0
movapd %xmm0, (%rsp)
movapd 0xa0(%rsp), %xmm0
addsd 0xc37d(%rip), %xmm0     # 0x144e0
movapd %xmm0, 0xa0(%rsp)
jmp 0x80a4
movapd 0x10(%rsp), %xmm6
addsd %xmm1, %xmm6
testb %r14b, %r14b
je 0x8211
movapd %xmm6, 0x10(%rsp)
movb $0x48, 0x27(%rsp)
movq %rbx, %rdi
leaq 0x27(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r14b
jne 0x80a4
movapd (%rsp), %xmm0
addsd 0xc33a(%rip), %xmm0     # 0x144f0
movapd %xmm0, (%rsp)
movsd 0x60(%rsp), %xmm0
addsd 0xc317(%rip), %xmm0     # 0x144e0
movapd 0x10(%rsp), %xmm6
movsd 0x30(%rsp), %xmm4
jmp 0x80b6
testb %r14b, %r14b
je 0x8265
movb $0x48, 0x24(%rsp)
movq %rbx, %rdi
leaq 0x24(%rsp), %rsi
callq 0x12480
movq (%rax), %rax
cmpb (%rax,%r15), %r14b
jne 0x80a4
movapd (%rsp), %xmm0
addsd 0xc2e1(%rip), %xmm0     # 0x144f0
jmp 0x8272
movapd (%rsp), %xmm0
addsd %xmm1, %xmm0
movapd %xmm0, (%rsp)
movsd 0x40(%rsp), %xmm2
addsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm4
movsd 0x60(%rsp), %xmm0
jmp 0x80bc
movapd (%rsp), %xmm0
movsd 0xc299(%rip), %xmm1     # 0x144e0
addsd %xmm1, %xmm0
movapd %xmm0, (%rsp)
movsd 0x50(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
jmp 0x80aa
movapd (%rsp), %xmm0
addsd 0xc26e(%rip), %xmm0     # 0x144e0
movapd %xmm0, (%rsp)
jmp 0x80a4
movapd %xmm4, %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 0x108(%rsp), %rax
movsd %xmm1, (%rax)
movapd %xmm4, %xmm1
movapd (%rsp), %xmm3
subsd %xmm3, %xmm1
movapd %xmm3, %xmm0
cmplesd %xmm1, %xmm0
movapd %xmm0, %xmm2
andnpd %xmm1, %xmm2
andpd %xmm3, %xmm0
orpd %xmm2, %xmm0
addsd %xmm0, %xmm0
movq 0x110(%rsp), %rcx
movsd %xmm0, (%rcx)
movsd 0xc1f5(%rip), %xmm1     # 0x144e0
ucomisd %xmm4, %xmm1
jae 0x8320
movsd (%rax), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jne 0x8301
jnp 0x8320
ucomisd %xmm1, %xmm0
jne 0x8309
jnp 0x8320
movapd %xmm4, %xmm5
movapd 0xa0(%rsp), %xmm13
ucomisd %xmm5, %xmm13
jne 0x8358
jp 0x8358
movabsq $-0x3fde000000000000, %rax # imm = 0xC022000000000000
movq 0x78(%rsp), %rcx
movq %rax, (%rcx)
movsd 0xc1ae(%rip), %xmm1     # 0x144e8
movq 0x100(%rsp), %rax
movsd %xmm1, (%rax)
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
divsd %xmm5, %xmm6
movapd (%rsp), %xmm3
divsd %xmm5, %xmm3
movapd %xmm3, %xmm11
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm13
movsd 0x50(%rsp), %xmm8
movapd %xmm8, %xmm1
movsd 0x60(%rsp), %xmm9
addsd %xmm9, %xmm1
mulsd 0xc15d(%rip), %xmm1     # 0x144f0
mulsd %xmm6, %xmm11
movsd 0x40(%rsp), %xmm10
addsd %xmm10, %xmm1
movapd %xmm1, %xmm12
divsd %xmm5, %xmm12
jne 0x843f
jp 0x843f
subsd %xmm11, %xmm12
movsd 0xc119(%rip), %xmm1     # 0x144e0
movapd %xmm1, %xmm0
subsd %xmm6, %xmm0
movapd %xmm3, %xmm4
subsd %xmm3, %xmm1
movapd %xmm1, %xmm5
mulsd %xmm0, %xmm1
movapd %xmm12, %xmm2
mulsd %xmm12, %xmm2
movapd %xmm11, %xmm3
mulsd %xmm1, %xmm3
divsd %xmm3, %xmm2
movq 0x78(%rsp), %rax
movsd %xmm2, (%rax)
movsd 0xc131(%rip), %xmm2     # 0x14538
ucomisd %xmm12, %xmm2
movq 0x100(%rsp), %rax
jbe 0x86be
movapd %xmm11, %xmm0
cmplesd %xmm1, %xmm0
andpd %xmm0, %xmm11
andnpd %xmm1, %xmm0
orpd %xmm11, %xmm0
xorpd 0xc096(%rip), %xmm0     # 0x144d0
jmp 0x86ee
movsd 0xc0b9(%rip), %xmm2     # 0x14500
movapd %xmm3, (%rsp)
movapd %xmm5, %xmm7
movapd %xmm3, %xmm0
addsd %xmm6, %xmm0
movsd 0xc080(%rip), %xmm5     # 0x144e0
movapd %xmm5, %xmm4
subsd %xmm0, %xmm4
addsd %xmm0, %xmm0
subsd %xmm0, %xmm5
addsd %xmm10, %xmm10
addsd %xmm9, %xmm10
addsd %xmm10, %xmm8
movapd %xmm7, %xmm10
addsd %xmm7, %xmm10
movapd %xmm10, %xmm0
mulsd %xmm5, %xmm0
movapd %xmm8, %xmm3
addsd %xmm8, %xmm3
subsd %xmm3, %xmm0
subsd %xmm13, %xmm0
mulsd %xmm8, %xmm5
movapd %xmm10, %xmm3
mulsd %xmm11, %xmm3
subsd %xmm5, %xmm3
mulsd %xmm13, %xmm4
subsd %xmm4, %xmm3
mulsd %xmm11, %xmm8
addsd %xmm10, %xmm10
movapd %xmm0, %xmm4
divsd %xmm10, %xmm4
movsd 0xc02c(%rip), %xmm5     # 0x14508
movapd %xmm4, %xmm9
divsd %xmm5, %xmm9
mulsd %xmm4, %xmm4
divsd 0xc01e(%rip), %xmm4     # 0x14510
mulsd %xmm10, %xmm5
movapd %xmm10, %xmm14
mulsd %xmm9, %xmm14
addsd %xmm0, %xmm14
mulsd %xmm9, %xmm14
addsd %xmm3, %xmm14
divsd %xmm5, %xmm3
addsd %xmm4, %xmm3
movsd 0xbff8(%rip), %xmm0     # 0x14518
mulsd %xmm10, %xmm0
mulsd %xmm10, %xmm0
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd %xmm9, %xmm14
subsd %xmm8, %xmm14
movapd %xmm14, %xmm4
mulsd %xmm14, %xmm4
subsd %xmm0, %xmm4
ucomisd %xmm2, %xmm4
movapd %xmm6, 0x10(%rsp)
movapd %xmm11, 0x90(%rsp)
movapd %xmm9, 0x50(%rsp)
jbe 0x86fd
sqrtsd %xmm4, %xmm0
xorpd %xmm1, %xmm1
movapd %xmm4, 0x40(%rsp)
ucomisd %xmm1, %xmm4
movsd %xmm0, 0x60(%rsp)
movapd %xmm14, 0x30(%rsp)
jae 0x85b6
movapd 0x40(%rsp), %xmm0
movsd %xmm10, 0x80(%rsp)
callq 0x4230
movapd 0x30(%rsp), %xmm14
movsd 0x80(%rsp), %xmm10
subsd %xmm14, %xmm0
addsd %xmm10, %xmm10
movsd %xmm10, 0x80(%rsp)
divsd %xmm10, %xmm0
callq 0x4070
movapd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movapd 0x40(%rsp), %xmm0
ucomisd 0xbf56(%rip), %xmm0     # 0x14548
movsd 0x60(%rsp), %xmm1
jae 0x8603
callq 0x4230
movapd %xmm0, %xmm1
movapd 0x30(%rsp), %xmm0
addsd %xmm1, %xmm0
xorpd 0xbebb(%rip), %xmm0     # 0x144d0
divsd 0x80(%rsp), %xmm0
callq 0x4070
movapd 0x50(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, %xmm0
movapd 0x10(%rsp), %xmm5
movapd (%rsp), %xmm4
movsd 0xbe9c(%rip), %xmm6     # 0x144e0
movapd 0x90(%rsp), %xmm7
subsd %xmm7, %xmm0
movapd %xmm6, %xmm2
subsd %xmm5, %xmm2
subsd %xmm4, %xmm6
movapd %xmm6, %xmm1
mulsd %xmm2, %xmm1
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
movapd %xmm7, %xmm4
mulsd %xmm1, %xmm4
divsd %xmm4, %xmm3
movq 0x78(%rsp), %rax
movsd %xmm3, (%rax)
movsd 0xbeae(%rip), %xmm3     # 0x14538
ucomisd %xmm0, %xmm3
jbe 0x8766
movapd %xmm7, %xmm2
cmplesd %xmm1, %xmm2
andpd %xmm2, %xmm7
andnpd %xmm1, %xmm2
orpd %xmm7, %xmm2
xorpd 0xbe1f(%rip), %xmm2     # 0x144d0
divsd %xmm2, %xmm0
movapd %xmm0, %xmm1
jmp 0x833a
xorpd %xmm1, %xmm1
ucomisd 0xbe75(%rip), %xmm12    # 0x14540
jbe 0x8342
mulsd %xmm5, %xmm6
mulsd %xmm0, %xmm4
movapd %xmm6, %xmm0
cmplesd %xmm4, %xmm0
andpd %xmm0, %xmm6
andnpd %xmm4, %xmm0
orpd %xmm6, %xmm0
divsd %xmm0, %xmm12
movapd %xmm12, %xmm1
jmp 0x8342
mulsd 0xbdea(%rip), %xmm13    # 0x144f0
addsd %xmm1, %xmm13
addsd 0xbde4(%rip), %xmm12    # 0x144f8
divsd %xmm7, %xmm13
addsd %xmm2, %xmm13
movsd 0xbdd2(%rip), %xmm1     # 0x144f8
ucomisd %xmm4, %xmm1
movapd %xmm13, 0xa0(%rsp)
movapd %xmm12, 0x80(%rsp)
jbe 0x87af
movsd %xmm3, 0x40(%rsp)
xorpd 0xbd81(%rip), %xmm14    # 0x144d0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x8891
sqrtsd %xmm0, %xmm0
jmp 0x88a4
xorpd %xmm1, %xmm1
ucomisd 0xbdce(%rip), %xmm0     # 0x14540
movq 0x100(%rsp), %rax
jbe 0x8342
mulsd %xmm6, %xmm5
movapd (%rsp), %xmm3
mulsd %xmm2, %xmm3
movapd %xmm5, %xmm1
cmplesd %xmm3, %xmm1
andpd %xmm1, %xmm5
andnpd %xmm3, %xmm1
orpd %xmm5, %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, %xmm1
jmp 0x8342
addsd %xmm10, %xmm10
divsd %xmm10, %xmm14
movapd %xmm14, %xmm0
callq 0x4070
movapd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm1
addsd %xmm0, %xmm1
addsd %xmm0, %xmm0
subsd %xmm0, %xmm2
movapd %xmm2, %xmm0
movapd 0x80(%rsp), %xmm5
ucomisd %xmm1, %xmm5
movapd 0xa0(%rsp), %xmm4
movapd %xmm4, %xmm2
cmpltpd %xmm0, %xmm2
movapd %xmm0, %xmm3
cmpltpd %xmm5, %xmm3
orpd %xmm2, %xmm3
movd %xmm3, %eax
ja 0x8868
ucomisd %xmm4, %xmm1
ja 0x8868
testb $0x1, %al
movapd 0x10(%rsp), %xmm5
movapd (%rsp), %xmm4
movsd 0xbcb6(%rip), %xmm6     # 0x144e0
movapd 0x90(%rsp), %xmm7
jne 0x8ab5
movapd %xmm1, %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
movapd %xmm7, %xmm3
unpcklpd %xmm7, %xmm3            # xmm3 = xmm3[0],xmm7[0]
subpd %xmm3, %xmm2
andpd 0xbc6b(%rip), %xmm2     # 0x144c0
pshufd $0xee, %xmm2, %xmm3     # xmm3 = xmm2[2,3,2,3]
cmplesd %xmm3, %xmm2
andpd %xmm2, %xmm1
jmp 0x8a66
testb $0x1, %al
movapd 0x10(%rsp), %xmm5
movapd (%rsp), %xmm4
movsd 0xbc63(%rip), %xmm6     # 0x144e0
movapd 0x90(%rsp), %xmm7
jne 0x8320
jmp 0x864d
movapd %xmm14, 0x30(%rsp)
callq 0x4230
movapd 0x30(%rsp), %xmm14
divsd %xmm0, %xmm14
movapd %xmm14, %xmm0
callq 0x4290
movapd %xmm0, %xmm1
divsd 0xbc61(%rip), %xmm1     # 0x14520
movsd 0x40(%rsp), %xmm0
ucomisd 0xbc7b(%rip), %xmm0     # 0x14548
movsd %xmm1, 0x30(%rsp)
jb 0x88db
sqrtsd %xmm0, %xmm0
jmp 0x88e6
callq 0x4230
movsd 0x30(%rsp), %xmm1
addsd %xmm0, %xmm0
movapd %xmm0, 0x40(%rsp)
movapd %xmm1, %xmm0
callq 0x4500
mulsd 0x40(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
movsd 0xbc15(%rip), %xmm0     # 0x14528
addsd 0x30(%rsp), %xmm0
callq 0x4500
movapd %xmm0, 0xb0(%rsp)
movsd 0x30(%rsp), %xmm0
addsd 0xbbfb(%rip), %xmm0     # 0x14530
callq 0x4500
movapd 0x60(%rsp), %xmm9
movapd 0x50(%rsp), %xmm8
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0xb0(%rsp), %xmm2
unpcklpd %xmm0, %xmm2            # xmm2 = xmm2[0],xmm0[0]
mulpd %xmm1, %xmm2
unpcklpd %xmm8, %xmm8            # xmm8 = xmm8[0,0]
addpd %xmm2, %xmm8
movapd 0x80(%rsp), %xmm4
ucomisd %xmm9, %xmm4
movapd 0xa0(%rsp), %xmm3
movapd %xmm3, %xmm0
cmpltpd %xmm8, %xmm0
movapd %xmm8, %xmm1
cmpltpd %xmm4, %xmm1
orpd %xmm0, %xmm1
movd %xmm1, %ecx
movapd %xmm8, %xmm0
unpckhpd %xmm8, %xmm0            # xmm0 = xmm0[1],xmm8[1]
movapd %xmm3, %xmm1
cmpltpd %xmm0, %xmm1
movapd %xmm0, %xmm2
cmpltpd %xmm4, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %eax
ja 0x8a04
ucomisd %xmm3, %xmm9
ja 0x8a04
testb $0x1, %cl
movapd 0x10(%rsp), %xmm5
movapd (%rsp), %xmm4
movsd 0xbafd(%rip), %xmm6     # 0x144e0
movapd 0x90(%rsp), %xmm7
je 0x8a77
testb $0x1, %al
je 0x8abe
movapd %xmm9, %xmm0
jmp 0x864d
testb $0x1, %cl
movapd 0x10(%rsp), %xmm5
movapd (%rsp), %xmm4
movsd 0xbac6(%rip), %xmm6     # 0x144e0
movapd 0x90(%rsp), %xmm7
je 0x8a32
testb $0x1, %al
jne 0x8320
jmp 0x864d
testb $0x1, %al
jne 0x8b24
movapd %xmm7, %xmm1
unpcklpd %xmm7, %xmm1            # xmm1 = xmm1[0],xmm7[0]
movapd %xmm8, %xmm2
subpd %xmm1, %xmm2
andpd 0xba6d(%rip), %xmm2     # 0x144c0
pshufd $0xee, %xmm2, %xmm1     # xmm1 = xmm2[2,3,2,3]
cmplesd %xmm1, %xmm2
movapd %xmm2, %xmm1
andpd %xmm8, %xmm1
andnpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, %xmm0
jmp 0x864d
movapd %xmm8, %xmm1
unpcklpd %xmm9, %xmm1            # xmm1 = xmm1[0],xmm9[0]
movapd %xmm7, %xmm2
unpcklpd %xmm7, %xmm2            # xmm2 = xmm2[0],xmm7[0]
subpd %xmm2, %xmm1
andpd 0xba2b(%rip), %xmm1     # 0x144c0
pshufd $0xee, %xmm1, %xmm3     # xmm3 = xmm1[2,3,2,3]
cmplesd %xmm3, %xmm1
movapd %xmm1, %xmm3
andpd %xmm8, %xmm3
andnpd %xmm9, %xmm1
orpd %xmm3, %xmm1
testb $0x1, %al
je 0x8af6
movapd %xmm1, %xmm0
jmp 0x864d
movsd %xmm9, %xmm8            # xmm8 = xmm9[0],xmm8[1]
movapd %xmm7, %xmm1
unpcklpd %xmm7, %xmm1            # xmm1 = xmm1[0],xmm7[0]
subpd %xmm1, %xmm8
andpd 0xb9e7(%rip), %xmm8     # 0x144c0
pshufd $0xee, %xmm8, %xmm1     # xmm1 = xmm8[2,3,2,3]
cmplesd %xmm1, %xmm8
andpd %xmm8, %xmm9
andnpd %xmm0, %xmm8
orpd %xmm9, %xmm8
jmp 0x8b24
movsd %xmm1, %xmm8            # xmm8 = xmm1[0],xmm8[1]
subpd %xmm2, %xmm8
andpd 0xb9b7(%rip), %xmm8     # 0x144c0
pshufd $0xee, %xmm8, %xmm2     # xmm2 = xmm8[2,3,2,3]
cmplesd %xmm2, %xmm8
andpd %xmm8, %xmm1
andnpd %xmm0, %xmm8
orpd %xmm1, %xmm8
movapd %xmm8, %xmm0
jmp 0x864d