// File: /workspace/github_cpp_makefile_star2_O2/tonymugen[P]sampleSNPs/varfiles.cpp
// Function: sampFiles::BedFileI::sampleLD(sampFiles::PopIndex const&, unsigned long const&)
void BedFileI::sampleLD(const PopIndex &popID, const uint64_t &n){
	if (n == 0) {
		cerr << "WARNING: zero SNPs requested. Nothing to be done." << endl;
		return;
	}
    if (_varFile.is_open()) {
        _varFile.close();
    }
	if (!_bimFile.is_open()) {
		try {
			string bimName = _fileStub + ".bim";
			_bimFile.open(bimName.c_str(), ios::in);
			if (!_bimFile.is_open()) {
				cerr << "ERROR: failed to open .bim file " << bimName << " for input" << endl;
				exit(2);
			}

		} catch (system_error &error) {
			cerr << "ERROR: cannot open BIM file for input: " << error.code().message() << flush;
			perror(" ");
			exit(1);
		}
	}

	uint64_t N = _numLines(); // have to do this first to set _nCols properly
	if (_nCols != popID.size()) {
		cerr << "Sample size the population index (" << popID.size() << ") not equal to sample size (" << _nCols << ") in the .fam file" << endl;
		exit(5);
	}
	const uint64_t Nbed = (_nCols/4UL) + static_cast<uint64_t>( (_nCols%4UL) > 0UL ); // 4 SNPs/byte plus padding in BED
    uint64_t nSamp      = 2 * n; // n is the number of pairs

    try { // has to be here, because _numLines() closes the file
        _varFile.open(_fileName.c_str(), ios::in | ios::binary);
		if (!_varFile.is_open()) {
			cerr << "ERROR: failed to open .bed file " << _fileName << " for input" << endl;
			exit(2);
		}

        // check the magic bytes and SNP-major status
        char magic[3];
        _varFile.read(magic, 3);
        if ( (magic[0] != static_cast<char>(0x6C)) || (magic[1] != static_cast<char>(0x1B)) ) {
            cerr << "ERROR: binary file " << _fileName << " not recognized as BED" << endl;
            exit(2);
        } else if (magic[2] != static_cast<char>(0x01)){
            cerr << "ERROR: binary file " << _fileName << " not SNP-major. Run a newer version of plink to fix it." << endl;
            exit(3);
        }
    } catch (system_error &error) {
        cerr << "ERROR: cannot open BED file " << _fileName << ": " << error.code().message() << flush;
        perror(" ");
        exit(1);
    }

	const string outName = _fileStub + "_LD.tsv";
	if (nSamp > N) {
		cerr << "ERROR: requested a sample of " << nSamp << " SNPs that is greater than the number of SNPs (" << N << ") in the input file." << endl;
		return;
	} else if (nSamp == N) {
		cerr << "WARNING: the number of sampled SNPs the same as the total number of SNPs in the file. LD will be between neighboring loci." << endl;
		// use neighboring loci
		char *locus1  = new char[Nbed];
		char *locus2  = new char[Nbed];
		uint64_t cumS = 0; // cumulative position in the file
		vector<double> rSq(popID.popNumber());
		vector<double> Dprime(popID.popNumber());
		vector<double> cnt1(popID.popNumber());
		vector<double> cnt2(popID.popNumber());
		string chrID;   // chromosome ID
		uint64_t pos1;  // position of locus1
		uint64_t pos2;  // position of locus2
		string bimLine; // line in the .bim file
		string field;   // single field in the .bim file
		stringstream lineStream;

		ofstream outFile(outName);
		outFile << "ChrID\tpos1\tpos2\tDistance"  << flush;
		for (unsigned short iPop = 1; iPop <= popID.popNumber(); iPop++) {
			outFile << "\trSq_" << iPop << "\tDprime_" << iPop << "\tn1_" << iPop << "\tn2_" << iPop << flush;
		}
		outFile << endl;

		while (nSamp) {
			// first locus in the pair
			_varFile.seekg(cumS*Nbed + 3); // seekg() index is base-0
			_varFile.read(locus1, Nbed);
			getline(_bimFile, bimLine);
			cumS++;
			nSamp--;

			lineStream.str(bimLine); // replaces whatever was there from before
			lineStream >> field;
			chrID = field;
			lineStream >> field >> field >> field;
			pos1 = atoi(field.c_str());

			// second locus
			_varFile.seekg(cumS*Nbed + 3);
			_varFile.read(locus2, Nbed);
			getline(_bimFile, bimLine);
			cumS++;
			nSamp--;

			lineStream.str(bimLine);
			lineStream >> field;
			if (chrID != field) { // if the chromosome number changes, we discard this pair and continue
				continue;
			}
			lineStream >> field >> field >> field;
			pos2 = atoi(field.c_str());
			if (pos2 <= pos1) {
				cerr << "WARNING: second SNP position (" << pos2 << ") no larger than the first (" << pos1 << ") on chromosome " << chrID << ". Skipping this pair without re-trying." << endl;
				continue;
			}
			_ld(locus1, locus2, popID, rSq, Dprime, cnt1, cnt2);
			outFile << chrID << "\t" << pos1 << "\t" << pos2 << "\t" << pos2 - pos1 << flush;
			for (size_t iPop = 0; iPop < popID.popNumber(); iPop++) {
				outFile <<  "\t" << rSq[iPop] << "\t" << Dprime[iPop] << "\t" << cnt1[iPop] << "\t" << cnt2[iPop] << flush;
			}
			outFile << endl;
		}

		delete [] locus1;
		delete [] locus2;
		_varFile.close();
		_bimFile.close();
		outFile.close();
        return;
	}
	// sample loci and calculate LD
	char *locus1  = new char[Nbed];
	char *locus2  = new char[Nbed];
	uint64_t cumS = 0; // cumulative position in the file
	uint64_t S;
	vector<double> rSq(popID.popNumber());
	vector<double> Dprime(popID.popNumber());
	vector<double> cnt1(popID.popNumber());
	vector<double> cnt2(popID.popNumber());
	string chrID;   // chromosome ID
	uint64_t pos1;  // position of locus1
	uint64_t pos2;  // position of locus2
	string bimLine; // line in the .bim file
	string field;   // single field in the .bim file
	stringstream lineStream;

	ofstream outFile(outName);
	outFile << "ChrID\tpos1\tpos2\tDistance"  << flush;
	for (unsigned short iPop = 1; iPop <= popID.popNumber(); iPop++) {
		outFile << "\trSq_" << iPop << "\tDprime_" << iPop << "\tn1_" << iPop << "\tn2_" << iPop << flush;
	}
	outFile << endl;

	RanDraw *snpSamp;  // so that I can catch RanDraw constructor exceptions
	try {
		snpSamp = new RanDraw();
	} catch (string error) {
		cerr << "ERROR: " << error << endl;
		exit(5);
	}

	while (nSamp) {
		// first locus in the pair
		try {
			S = snpSamp->vitter(nSamp, N); // sample the number of SNPs to skip; keep track of the running total
		} catch (string error) {
			cerr << "ERROR: " << error << endl;
			exit(5);
		}
		cumS += S;
		N    -= S + 1;
		_varFile.seekg(cumS*Nbed + 3); // seekg() index is base-0
		_varFile.read(locus1, Nbed);
		cumS++;                        // step up cumS because seekg() will not change it (unlike the getline() that automatially advances)
		while (S) { // skipping S lines in the bim file
			_bimFile.ignore(numeric_limits<streamsize>::max(), '\n');
 			S--;
		}
		getline(_bimFile, bimLine);
		lineStream.str(bimLine); // replaces whatever was there from before
		lineStream >> field;
		chrID = field;
		lineStream >> field >> field >> field;
		pos1 = atoi(field.c_str());
		nSamp--;

		// second locus
		try {
			S = snpSamp->vitter(nSamp, N);
		} catch (string error) {
			cerr << "ERROR: " << error << endl;
			exit(5);
		}
		cumS += S;
		N    -= S + 1;
		_varFile.seekg(cumS*Nbed + 3);
		_varFile.read(locus2, Nbed);
		cumS++;
		while (S) {
			_bimFile.ignore(numeric_limits<streamsize>::max(), '\n');
			S--;
		}
		getline(_bimFile, bimLine);

		lineStream.str(bimLine);
		lineStream >> field;
		if (chrID != field) { // if the chromosome number changes, we discard this pair and start over
			nSamp++;          // back up so that we end up with the correct number of pairs regardless of how many chromosome boundaries we cross
			continue;
		}
		lineStream >> field >> field >> field;
		pos2 = atoi(field.c_str());
		if (pos2 <= pos1) {
			cerr << "WARNING: second SNP position (" << pos2 << ") no larger than the first (" << pos1 << ") on chromosome " << chrID << ". Skipping this pair without re-trying." << endl;
			nSamp--; // will not re-try in this case
			continue;
		}
		_ld(locus1, locus2, popID, rSq, Dprime, cnt1, cnt2);
		outFile << chrID << "\t" << pos1 << "\t" << pos2 << "\t" << pos2 - pos1 << flush;
		for (size_t iPop = 0; iPop < popID.popNumber(); iPop++) {
			outFile <<  "\t" << rSq[iPop] << "\t" << Dprime[iPop] << "\t" << cnt1[iPop] << "\t" << cnt2[iPop] << flush;
		}
		outFile << endl;
		nSamp--;
	}

	delete [] locus1;
	delete [] locus2;
	_varFile.close();
	_bimFile.close();
	outFile.close();

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4f8, %rsp            # imm = 0x4F8
movq %rsi, 0x8(%rsp)
cmpq $0x0, (%rdx)
je 0xc217
movq %rdx, %r15
movq %rdi, %rbx
leaq 0x8(%rdi), %rax
movq %rax, (%rsp)
leaq 0x88(%rdi), %r13
movq %r13, %rdi
callq 0x40b0
testb %al, %al
je 0xc084
movq (%rsp), %rdi
callq 0x4050
leaq 0x458(%rbx), %r14
leaq 0x4d8(%rbx), %r12
movq %r12, %rdi
callq 0x40b0
testb %al, %al
jne 0xc0e9
leaq 0x668(%rbx), %rsi
leaq 0x8788(%rip), %rdx      # 0x14834
leaq 0x170(%rsp), %rdi
callq 0x5e08
movq 0x170(%rsp), %rsi
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0x4280
movq %r12, %rdi
callq 0x40b0
testb %al, %al
je 0xd0e1
leaq 0x170(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x75fe
movq %rax, 0x18(%rsp)
movq 0x238(%rbx), %rax
movq 0x8(%rsp), %rcx
cmpq 0x18(%rcx), %rax
jne 0xcfd6
movq %rax, %rcx
shrq $0x2, %rcx
andl $0x3, %eax
cmpq $0x1, %rax
sbbq $-0x1, %rcx
movq %rcx, 0x10(%rsp)
movq (%r15), %r12
leaq 0x218(%rbx), %r15
movq 0x218(%rbx), %rsi
pushq $0xc
popq %rdx
movq (%rsp), %rdi
callq 0x4280
movq %r13, %rdi
callq 0x40b0
testb %al, %al
je 0xd034
leaq 0x170(%rsp), %rsi
pushq $0x3
popq %rdx
movq (%rsp), %rdi
callq 0x4340
cmpb $0x6c, 0x170(%rsp)
jne 0xd06e
cmpb $0x1b, 0x171(%rsp)
jne 0xd06e
cmpb $0x1, 0x172(%rsp)
jne 0xd0a5
addq %r12, %r12
addq $0x668, %rbx            # imm = 0x668
leaq 0x886b(%rip), %rdx      # 0x14a0a
leaq 0x150(%rsp), %rdi
movq %rbx, %rsi
callq 0x5e08
movq %r12, 0x48(%rsp)
cmpq 0x18(%rsp), %r12
jbe 0xc243
movq 0xfe2a(%rip), %rdi      # 0x1bff0
leaq 0x8429(%rip), %rsi      # 0x145f6
callq 0x42a0
movq %rax, %rdi
movq 0x48(%rsp), %rsi
callq 0x41b0
leaq 0x8712(%rip), %rsi      # 0x148f8
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
movq 0x18(%rsp), %rsi
callq 0x41b0
leaq 0x8442(%rip), %rsi      # 0x14644
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xcfb7
movq 0xfdd2(%rip), %rdi      # 0x1bff0
leaq 0x839f(%rip), %rsi      # 0x145c4
callq 0x42a0
movq %rax, %rdi
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x4140
pushq $0x18
popq %r12
jne 0xc444
movq 0xfd9c(%rip), %rdi      # 0x1bff0
leaq 0x87b7(%rip), %rsi      # 0x14a12
callq 0x42a0
movq 0x8(%rsp), %rbx
movq %rax, %rdi
callq 0x4140
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x4030
movq %rax, 0x120(%rsp)
movq %r15, %rdi
callq 0x4030
movq %rax, 0x110(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0x50(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xf8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xe0(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xc8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
leaq 0xb8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0x98(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x370(%rsp), %rdi
callq 0x41d0
leaq 0x170(%rsp), %rdi
leaq 0x150(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4530
leaq 0x87fe(%rip), %rsi      # 0x14b81
leaq 0x170(%rsp), %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x41c0
movw $0x1, %r12w
leaq 0x87fc(%rip), %r13      # 0x14ba0
leaq 0x87fe(%rip), %rbp      # 0x14ba9
leaq 0x87fc(%rip), %rbx      # 0x14bae
movzwl %r12w, %r15d
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r15, %rax
jb 0xcb09
leaq 0x170(%rsp), %rdi
leaq 0x87b8(%rip), %rsi      # 0x14b9a
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %r13, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
callq 0x41c0
incl %r12d
jmp 0xc3b2
movq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0x4030
movq %rax, 0x78(%rsp)
movq 0x8(%rsp), %rbx
movq %r15, %rdi
callq 0x4030
movq %rax, 0x70(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xf8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xe0(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0xc8(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %r12
leaq 0x138(%rsp), %rdi
leaq 0x170(%rsp), %rdx
movq %rax, %rsi
callq 0x12114
leaq 0xb8(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
leaq 0x98(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x38(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x370(%rsp), %rdi
callq 0x41d0
leaq 0x170(%rsp), %rdi
leaq 0x150(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x4530
leaq 0x8625(%rip), %rsi      # 0x14b81
leaq 0x170(%rsp), %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x41c0
movw $0x1, %r12w
leaq 0x8623(%rip), %r13      # 0x14ba0
leaq 0x8625(%rip), %rbp      # 0x14ba9
leaq 0x8623(%rip), %rbx      # 0x14bae
movzwl %r12w, %r15d
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %r15, %rax
jb 0xc619
leaq 0x170(%rsp), %rdi
leaq 0x85e3(%rip), %rsi      # 0x14b9a
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %r13, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x42a0
movq %rax, %rdi
movl %r15d, %esi
callq 0x4480
movq %rax, %rdi
callq 0x41c0
incl %r12d
jmp 0xc58b
leaq 0x170(%rsp), %rdi
callq 0x4140
pushq $0x8
popq %rdi
callq 0x42c0
movq %rax, 0x20(%rsp)
movq %rax, %rdi
callq 0x12bf2
xorl %r13d, %r13d
movabsq $0x7fffffffffffffff, %rbx # imm = 0x7FFFFFFFFFFFFFFF
pushq $0xa
popq %rbp
movq 0x48(%rsp), %rcx
movq %rcx, %rax
subq $0x1, %rax
jb 0xcf13
movq %rcx, 0x48(%rsp)
movq %rcx, %xmm0
movq 0x7e31(%rip), %xmm1     # 0x144a0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x7e35(%rip), %xmm2     # 0x144b0
subpd %xmm2, %xmm0
movapd %xmm0, %xmm3
unpckhpd %xmm0, %xmm3            # xmm3 = xmm3[1],xmm0[1]
addsd %xmm0, %xmm3
movapd %xmm3, 0x120(%rsp)
movq %rax, %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
subpd %xmm2, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movapd %xmm1, 0x110(%rsp)
movaps 0x120(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x18(%rsp), %xmm0
unpcklps 0x7dcf(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7dd7(%rip), %xmm0     # 0x144b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x12edc
movq %rax, %r15
addq %rax, %r13
movq %r13, %rsi
imulq 0x10(%rsp), %rsi
addq $0x3, %rsi
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x4350
movq (%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x4340
movq %r15, %rax
notq %rax
addq %rax, 0x18(%rsp)
incq %r13
subq $0x1, %r15
jb 0xc759
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x41a0
jmp 0xc744
movq %r14, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4060
leaq 0x388(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x4400
leaq 0x370(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4170
leaq 0x370(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movq 0x28(%rsp), %rdi
callq 0x4420
movl %eax, %r15d
movaps 0x110(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x18(%rsp), %xmm0
unpcklps 0x7caa(%rip), %xmm0     # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x7cb2(%rip), %xmm0     # 0x144b0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd %xmm1, 0x80(%rsp)
movq 0x20(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x12edc
movq %rax, %r12
addq %rax, %r13
movq %r13, %rsi
imulq 0x10(%rsp), %rsi
addq $0x3, %rsi
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x4350
movq (%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x4340
movq %r12, %rax
notq %rax
addq %rax, 0x18(%rsp)
incq %r13
subq $0x1, %r12
jb 0xc87e
movq %r14, %rdi
movq %rbx, %rsi
movl %ebp, %edx
callq 0x41a0
jmp 0xc869
movq %r14, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4060
leaq 0x388(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x4400
leaq 0x370(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
leaq 0xa8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x12109
testb %al, %al
jne 0xc6b6
leaq 0x370(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movq %rax, %rdi
leaq 0x28(%rsp), %rsi
callq 0x4360
movslq %r15d, %r12
movq 0x28(%rsp), %rdi
callq 0x4420
movslq %eax, %r15
cmpl %r12d, %r15d
jbe 0xca83
movq 0x78(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x8(%rsp), %rcx
leaq 0xf8(%rsp), %r8
leaq 0xe0(%rsp), %r9
leaq 0x138(%rsp), %rax
pushq %rax
leaq 0xd0(%rsp), %rax
pushq %rax
callq 0x8b2e
popq %rax
popq %rcx
leaq 0x170(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x81c1(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x81a7(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x818d(%rip), %rsi      # 0x14b2c
callq 0x42a0
subq %r12, %r15
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
callq 0x41c0
xorl %r15d, %r15d
movq 0x8(%rsp), %rcx
movq 0x8(%rcx), %rax
subq (%rcx), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rax, %r15
jae 0xcaeb
leaq 0x170(%rsp), %rdi
leaq 0x8143(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq 0xf8(%rsp), %rcx
movsd (%rcx,%r15,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x811e(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq 0xe0(%rsp), %rcx
movsd (%rcx,%r15,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x80f9(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq 0xc8(%rsp), %rcx
movsd (%rcx,%r15,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
leaq 0x80d4(%rip), %rsi      # 0x14b2c
callq 0x42a0
movq 0x138(%rsp), %rcx
movsd (%rcx,%r15,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
callq 0x41c0
incq %r15
jmp 0xc9bd
movq 0xf566(%rip), %rdi      # 0x1bff0
leaq 0x8026(%rip), %rsi      # 0x14ab7
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x802b(%rip), %rsi      # 0x14ad6
callq 0x42a0
movq %rax, %rdi
movq %r12, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x802e(%rip), %rsi      # 0x14af3
callq 0x42a0
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x8020(%rip), %rsi      # 0x14b04
callq 0x42a0
jmp 0xcaf3
leaq 0x170(%rsp), %rax
movq %rax, %rdi
callq 0x4140
movq 0x48(%rsp), %rcx
addq $-0x2, %rcx
jmp 0xc650
leaq 0x170(%rsp), %rdi
movq %rdi, %r12
callq 0x4140
movq $0x0, 0x20(%rsp)
leaq 0x28(%rsp), %r13
leaq 0x7ffe(%rip), %rbp      # 0x14b2c
cmpq $0x0, 0x18(%rsp)
je 0xce6c
movq 0x20(%rsp), %rsi
imulq 0x10(%rsp), %rsi
addq $0x3, %rsi
movq (%rsp), %rdi
xorl %edx, %edx
callq 0x4350
movq (%rsp), %rdi
movq 0x120(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x4340
movq %r14, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4060
leaq 0x388(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x4400
leaq 0x370(%rsp), %rdi
movq %r13, %rsi
callq 0x4360
leaq 0xa8(%rsp), %rdi
movq %r13, %rsi
callq 0x4170
leaq 0x370(%rsp), %rdi
movq %r13, %rsi
callq 0x4360
movq %rax, %rdi
movq %r13, %rsi
callq 0x4360
movq %rax, %rdi
movq %r13, %rsi
callq 0x4360
movq 0x20(%rsp), %r15
orq $0x1, %r15
movq 0x28(%rsp), %rdi
callq 0x4420
movl %eax, %ebx
imulq 0x10(%rsp), %r15
addq $0x3, %r15
movq (%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4350
movq (%rsp), %rdi
movq 0x110(%rsp), %rsi
movq 0x10(%rsp), %rdx
callq 0x4340
movq %r14, %rdi
leaq 0x88(%rsp), %rsi
callq 0x4060
leaq 0x388(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x4400
leaq 0x370(%rsp), %rdi
movq %r13, %rsi
callq 0x4360
addq $0x2, 0x20(%rsp)
addq $-0x2, 0x18(%rsp)
leaq 0xa8(%rsp), %rdi
movq %r13, %rsi
callq 0x12109
testb %al, %al
jne 0xcb2e
leaq 0x370(%rsp), %rdi
movq %r13, %rsi
callq 0x4360
movq %rax, %rdi
movq %r13, %rsi
callq 0x4360
movq %rax, %rdi
movq %r13, %rsi
callq 0x4360
movslq %ebx, %r15
movq 0x28(%rsp), %rdi
callq 0x4420
movslq %eax, %rbx
cmpl %r15d, %ebx
jbe 0xcdf4
movq 0x120(%rsp), %rsi
movq 0x110(%rsp), %rdx
movq 0x8(%rsp), %rcx
leaq 0x50(%rsp), %r8
leaq 0xf8(%rsp), %r9
leaq 0xc8(%rsp), %rax
pushq %rax
leaq 0xe8(%rsp), %rax
pushq %rax
callq 0x8b2e
popq %rax
popq %rcx
movq %r12, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x41b0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
subq %r15, %rbx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x41b0
movq %rax, %rdi
callq 0x41c0
xorl %ebx, %ebx
movq 0x8(%rsp), %r15
movq 0x8(%r15), %rax
subq (%r15), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
cmpq %rax, %rbx
jae 0xce5c
movq %r12, %rdi
movq %rbp, %rsi
callq 0x42a0
movq 0x50(%rsp), %rcx
movsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq 0xf8(%rsp), %rcx
movsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq 0xe0(%rsp), %rcx
movsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
movq %rbp, %rsi
callq 0x42a0
movq 0xc8(%rsp), %rcx
movsd (%rcx,%rbx,8), %xmm0
movq %rax, %rdi
callq 0x4450
movq %rax, %rdi
callq 0x41c0
incq %rbx
jmp 0xcd4f
movq 0xf1f5(%rip), %rdi      # 0x1bff0
leaq 0x7cb5(%rip), %rsi      # 0x14ab7
callq 0x42a0
movq %rax, %rdi
movq %rbx, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x7cba(%rip), %rsi      # 0x14ad6
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x41b0
movq %rax, %rdi
leaq 0x7cbd(%rip), %rsi      # 0x14af3
callq 0x42a0
movq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x4270
movq %rax, %rdi
leaq 0x7caf(%rip), %rsi      # 0x14b04
callq 0x42a0
jmp 0xce5f
movq %r12, %rax
movq %rax, %rdi
callq 0x4140
jmp 0xcb2e
movq 0x120(%rsp), %rdi
callq 0x4370
movq 0x110(%rsp), %rdi
callq 0x4370
movq (%rsp), %rdi
callq 0x4050
movq %r14, %rdi
callq 0x4050
leaq 0x170(%rsp), %rdi
callq 0x43c0
leaq 0x170(%rsp), %rdi
callq 0x43f0
leaq 0x370(%rsp), %rdi
callq 0x4210
leaq 0x28(%rsp), %rdi
callq 0x4558
leaq 0x88(%rsp), %rdi
callq 0x4558
leaq 0xa8(%rsp), %rdi
callq 0x4558
leaq 0xc8(%rsp), %rdi
callq 0x1251e
leaq 0xe0(%rsp), %rdi
callq 0x1251e
leaq 0xf8(%rsp), %rdi
callq 0x1251e
leaq 0x50(%rsp), %rdi
jmp 0xcfb2
movq 0x78(%rsp), %rdi
callq 0x4370
movq 0x70(%rsp), %rdi
callq 0x4370
movq (%rsp), %rdi
callq 0x4050
movq %r14, %rdi
callq 0x4050
leaq 0x170(%rsp), %rdi
callq 0x43c0
leaq 0x170(%rsp), %rdi
callq 0x43f0
leaq 0x370(%rsp), %rdi
callq 0x4210
leaq 0x28(%rsp), %rdi
callq 0x4558
leaq 0x88(%rsp), %rdi
callq 0x4558
leaq 0xa8(%rsp), %rdi
callq 0x4558
leaq 0x138(%rsp), %rdi
callq 0x1251e
leaq 0xc8(%rsp), %rdi
callq 0x1251e
leaq 0xe0(%rsp), %rdi
callq 0x1251e
leaq 0xf8(%rsp), %rdi
callq 0x1251e
leaq 0x150(%rsp), %rdi
callq 0x4558
addq $0x4f8, %rsp            # imm = 0x4F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xf013(%rip), %rdi      # 0x1bff0
leaq 0x7b4a(%rip), %rsi      # 0x14b2e
callq 0x42a0
movq 0x8(%rsp), %rcx
movq 0x18(%rcx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0x7b50(%rip), %rsi      # 0x14b51
movq %rax, %rdi
callq 0x42a0
movq 0x238(%rbx), %rsi
movq %rax, %rdi
callq 0x40c0
leaq 0x7b4f(%rip), %rsi      # 0x14b6e
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xd44d
movq 0xefb5(%rip), %rdi      # 0x1bff0
leaq 0x7938(%rip), %rsi      # 0x1497a
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x7513(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xd11b
movq 0xef7b(%rip), %rdi      # 0x1bff0
leaq 0x77da(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x77d7(%rip), %rsi      # 0x1486a
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
jmp 0xd11b
movq 0xef44(%rip), %rdi      # 0x1bff0
leaq 0x77a3(%rip), %rsi      # 0x14856
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x77b7(%rip), %rsi      # 0x14881
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x3
jmp 0xd44f
movq 0xef08(%rip), %rdi      # 0x1bff0
leaq 0x77ca(%rip), %rsi      # 0x148b9
callq 0x42a0
leaq 0x170(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
leaq 0x7461(%rip), %rsi      # 0x1456c
movq %rax, %rdi
callq 0x42a0
movq %rax, %rdi
callq 0x4140
pushq $0x2
jmp 0xd44f
movq %rdx, %r14
movq %rax, %rbx
pushq $0x8
popq %rsi
movq 0x20(%rsp), %rdi
callq 0x42d0
jmp 0xd13d
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x2, %r14d
jne 0xd4ec
movq %rbx, %rdi
callq 0x4330
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xee85(%rip), %rdi      # 0x1bff0
leaq 0x75b3(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0xd44d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xd4ec
movq %rax, %rbx
jmp 0xd484
movq %rax, %rbx
jmp 0xd491
movq %rax, %rbx
jmp 0xd4f9
movq %rax, %rbx
jmp 0xd4c2
movq %rax, %rbx
jmp 0xd506
movq %rax, %rbx
jmp 0xd537
movq %rax, %rbx
jmp 0xd4cf
movq %rax, %rbx
jmp 0xd4dc
movq %rax, %rbx
jmp 0xd544
movq %rax, %rbx
jmp 0xd551
jmp 0xd376
jmp 0xd376
jmp 0xd376
jmp 0xd376
jmp 0xd376
jmp 0xd376
movq %rdx, %r14
movq %rax, %rbx
jmp 0xd23b
jmp 0xd4e9
jmp 0xd4e9
movq %rdx, %r14
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x4558
cmpl $0x1, %r14d
jne 0xd56b
movq %rbx, %rdi
callq 0x4110
movq %rax, %r14
movq 0xed99(%rip), %rdi      # 0x1bff0
leaq 0x7784(%rip), %rsi      # 0x149e2
callq 0x42a0
movq %rax, %rbx
movq 0x18(%r14), %rsi
movl 0x10(%r14), %edx
movq (%rsi), %rax
leaq 0x170(%rsp), %rdi
callq *0x20(%rax)
leaq 0x170(%rsp), %rsi
movq %rbx, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
jmp 0xd335
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x4558
jmp 0xd2b0
jmp 0xd2ad
movq %rax, %rbx
callq 0x4470
jmp 0xd56b
jmp 0xd474
jmp 0xd2c1
movq %rax, %rbx
cmpl $0x1, %edx
jne 0xd56b
movq %rbx, %rdi
callq 0x4110
movq %rax, %rbx
movq 0xed11(%rip), %rdi      # 0x1bff0
leaq 0x7553(%rip), %rsi      # 0x14839
callq 0x42a0
movq %rax, %rdi
movq %r15, %rsi
callq 0x4270
leaq 0x7514(%rip), %rsi      # 0x14811
movq %rax, %rdi
callq 0x42a0
movq %rax, %r14
movq 0x18(%rbx), %rsi
movl 0x10(%rbx), %edx
movq (%rsi), %rax
leaq 0x170(%rsp), %rdi
callq *0x20(%rax)
leaq 0x170(%rsp), %rsi
movq %r14, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x41c0
leaq 0x170(%rsp), %rdi
callq 0x4558
leaq 0x74c9(%rip), %rdi      # 0x14812
callq 0x4250
pushq $0x1
jmp 0xd44f
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x4558
jmp 0xd36c
jmp 0xd369
movq %rax, %rbx
callq 0x4470
jmp 0xd56b
movq %rax, %rbx
jmp 0xd55e
jmp 0xd474
jmp 0xd4e9
jmp 0xd4e9
jmp 0xd474
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xd4ec
movq %rbx, %rdi
callq 0x4330
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xec2e(%rip), %rdi      # 0x1bff0
leaq 0x735c(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
jmp 0xd44d
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xd4ec
movq %rax, %rbx
cmpl $0x2, %edx
jne 0xd4ec
movq %rbx, %rdi
callq 0x4330
leaq 0x50(%rsp), %rdi
movq %rax, %rsi
callq 0x43b0
movq %rbx, %rdi
callq 0x4110
movq 0xebc4(%rip), %rdi      # 0x1bff0
leaq 0x72f2(%rip), %rsi      # 0x14725
callq 0x42a0
leaq 0x50(%rsp), %rsi
movq %rax, %rdi
callq 0x4270
movq %rax, %rdi
callq 0x4140
pushq $0x5
popq %rdi
callq 0x4310
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x4558
callq 0x4470
jmp 0xd4ec
movq %rax, %rdi
callq 0x6194
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x43f0
leaq 0x370(%rsp), %rdi
callq 0x4210
leaq 0x28(%rsp), %rdi
callq 0x4558
leaq 0x88(%rsp), %rdi
callq 0x4558
leaq 0xa8(%rsp), %rdi
callq 0x4558
leaq 0xc8(%rsp), %rdi
callq 0x1251e
leaq 0xe0(%rsp), %rdi
callq 0x1251e
leaq 0xf8(%rsp), %rdi
callq 0x1251e
leaq 0x50(%rsp), %rdi
jmp 0xd559
jmp 0xd4e9
jmp 0xd4e9
jmp 0xd4e9
movq %rax, %rbx
leaq 0x170(%rsp), %rdi
callq 0x43f0
leaq 0x370(%rsp), %rdi
callq 0x4210
leaq 0x28(%rsp), %rdi
callq 0x4558
leaq 0x88(%rsp), %rdi
callq 0x4558
leaq 0xa8(%rsp), %rdi
callq 0x4558
leaq 0x138(%rsp), %rdi
callq 0x1251e
leaq 0xc8(%rsp), %rdi
callq 0x1251e
leaq 0xe0(%rsp), %rdi
callq 0x1251e
leaq 0xf8(%rsp), %rdi
callq 0x1251e
leaq 0x150(%rsp), %rdi
callq 0x4558
movq %rbx, %rdi
callq 0x44d0
nop