// File: /workspace/github_cpp_makefile_star2_O2/melop[P]admixem/simulation.cpp
// Function: PerformExport(int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, int, int, int, int, double)
void PerformExport(int nGen, int nRep, string sExportFolder, int nPop1Size, int nPop2Size, int nPop3Size, int nMarkerNum, double nRandSeed) {

	Random::Set(nRandSeed);
	string sOutFolder = SimulConfig.GetConfig("OutputFolder");
	sExportFolder = sExportFolder + "/Gen" + convertInt(nGen);
	fnMakeDir(sExportFolder.c_str());

	ofstream fPriorAlleleFreq, fOutcomeVars, fGenotypes, fEtaPriors, fLoci, fSampledMarkers;// Create priorallelefreqs.txt
	//fPriorAlleleFreq.open(sExportFolder + "/priorallelefreqs.txt");
	//fOutcomeVars.open(sExportFolder + "/outcomevars.txt");
	//fGenotypes.open(sExportFolder + "/genotypes.txt");
	//fEtaPriors.open(sExportFolder + "/etapriors.txt");
	fLoci.open((sExportFolder + "/lociChr.txt").c_str());
	fSampledMarkers.open("sampledmarkers.txt"); // this is an instruction to the php script regarding what markers to draw

	ifstream fMarkers, fPhenotypes;
	/*char szCurrGen[100];
	_itoa(nGen , szCurrGen, 10);
	*/
	string sGenHeader = sOutFolder;
	sGenHeader = (nRep==0)? sGenHeader : (sGenHeader + "_") + fnIntToString(nRep);
	sGenHeader = sGenHeader + "/Gen";
	sGenHeader = sGenHeader + fnIntToString(nGen);//szCurrGen;

	fMarkers.open((sGenHeader + "_markers.txt").c_str() , ios_base::in );

	fPhenotypes.open((sGenHeader + "_phenotypes.txt").c_str() , ios_base::in );
	//convert the phenotype file to outcomevars.txt
	string sCmd = "php export_phenotype.php -- -s \"" + sGenHeader + "_phenotypes.txt\" -o \"" + sExportFolder + "/outcomevars.txt\"";
	printf("Calling: %s ...\n", sCmd.c_str());
	system(sCmd.c_str());

	//write loci.txt file
	printf("Writing locus information...\n");
	fLoci << "\"LocusName\"\t\"NumberOfAlleles\"\t\"cM\"\t\"Chr\"" <<endl; // Header
	
	fLoci << fixed << showpoint; 
	fLoci << setprecision(6);
	

	//vector<vector<int>> vRandomMarkerSubset;
	vector<map<double, double> > * pvMarkerMapDistance = SimulConfig.pMarkerConfig->GetMpMarkerMapDistance(2); // use average map distance

	size_t nTotalChrSize = pvMarkerMapDistance->size();
	for( size_t nChr=0; nChr < nTotalChrSize ; nChr++) {
		map<double, double> * pmMarkerMapDistanceOnChr = & (pvMarkerMapDistance->at(nChr));
		size_t nCurrMarker = 1;
		size_t nSampledMarkerCount = 1;
		double nPrevMarkerPos=0.0;

		double nExpectedMarkerNumOnChr = (double)nMarkerNum * SimulConfig.pMarkerConfig->GetChrToGenomeRatio( nChr );
		double nProbToKeep = nExpectedMarkerNumOnChr / (double)pmMarkerMapDistanceOnChr->size();

		for (map<double,double>::iterator itMarker = pmMarkerMapDistanceOnChr->begin(); itMarker!=pmMarkerMapDistanceOnChr->end(); ++itMarker) {

			if (UniformGen.Next() <= nProbToKeep) {

				double nCurrPos = itMarker->second; // itMarker->first  * 10.0e-6;

				fLoci << "chr" << (nChr+1) << "_" << nCurrMarker << "\t2\t";
				fSampledMarkers << nChr << "\t" << ( nCurrMarker -1 ) << "\t" << nCurrPos << endl;
			
				if ((nSampledMarkerCount==1)) {
				
					fLoci << "NA\t"; 
				}
				else {
				
					fLoci <<  (nCurrPos - nPrevMarkerPos) << "\t";
					
				
				}
				nPrevMarkerPos = nCurrPos ;
				fLoci << (nChr+1) << endl;
				nSampledMarkerCount++;
			}
			else {
				//nPrevMarkerPos += itMarker->first; //accumulate distance
			}

			nCurrMarker++;
		}
	}

	fSampledMarkers.close();

	sCmd = "php export_genotype.php -- -s \"" + sGenHeader + "_markers.txt\" -o \"" + sExportFolder + "\" -i sampledmarkers.txt -pop1 100000 -pop2 100000 -pop3 999999";
	printf("Calling: %s ...\n", sCmd.c_str());
	system(sCmd.c_str());



	/*
	string sLn;
	int iLn = 0;
	if (std::getline(fPhenotypes, sLn)) {// Header

	}

	else {
		printf("Phenotype output error.\n");
		return;
	}

	while(std::getline(fPhenotypes, sLn)) {
		
		int nId, nPop, nSex, nFatherId, nMotherId;
		sscanf(sLn.c_str(), "%[^\t] %[^\t] %[^\t] %[^\t] %[^\t] %*[^\n]", &nId, &nPop, &nSex, &nFatherId, &nMotherId);
		string sPrelude
		iLn++;
	}
	*/
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1198, %rsp           # imm = 0x1198
movq %rdx, 0x8(%rsp)
movl %esi, %r15d
movl %edi, %ebp
callq 0xb388
leaq 0x440d(%rip), %rsi      # 0x2fc8f
leaq 0x148(%rsp), %rdi
leaq 0x188(%rsp), %rdx
callq 0x9512
leaq 0x1a09a(%rip), %rsi     # 0x45938
leaq 0x168(%rsp), %rdi
leaq 0x148(%rsp), %rdx
callq 0xf118
leaq 0x148(%rsp), %rdi
callq 0x71a0
leaq 0x47c6(%rip), %rdx      # 0x3008d
leaq 0x590(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x22ff5
leaq 0x798(%rsp), %rdi
movl %ebp, %esi
callq 0x2c21f
leaq 0x188(%rsp), %rdi
leaq 0x590(%rsp), %rsi
leaq 0x798(%rsp), %rdx
callq 0x2c2ff
leaq 0x188(%rsp), %rbx
movq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x74c0
movq %rbx, %rdi
callq 0x71a0
leaq 0x798(%rsp), %rdi
callq 0x71a0
leaq 0x590(%rsp), %rdi
callq 0x71a0
movq (%r14), %rdi
callq 0x2b571
leaq 0x798(%rsp), %rdi
callq 0x7690
leaq 0xf98(%rsp), %rdi
callq 0x7690
leaq 0xd98(%rsp), %rdi
callq 0x7690
leaq 0xb98(%rsp), %rdi
callq 0x7690
leaq 0x390(%rsp), %rdi
callq 0x7690
leaq 0x998(%rsp), %rdi
callq 0x7690
leaq 0x46f6(%rip), %rdx      # 0x30092
leaq 0x188(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x22ff5
movq 0x188(%rsp), %rsi
leaq 0x390(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x7060
leaq 0x188(%rsp), %rdi
callq 0x71a0
leaq 0x46c5(%rip), %rsi      # 0x3009f
leaq 0x998(%rsp), %rdi
pushq $0x10
popq %rdx
callq 0x7060
leaq 0x188(%rsp), %rdi
callq 0x7450
leaq 0x590(%rsp), %rdi
callq 0x7450
leaq 0x38(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x7550
testl %r15d, %r15d
je 0x2ba60
leaq 0x27b4(%rip), %rdx      # 0x2e1d6
leaq 0x128(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x22ff5
leaq 0x108(%rsp), %rdi
movl %r15d, %esi
callq 0x25cc5
leaq 0x10(%rsp), %rdi
leaq 0x128(%rsp), %rsi
leaq 0x108(%rsp), %rdx
callq 0x2c2ff
jmp 0x2ba6f
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x7550
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x74c0
movq %rbx, %rdi
callq 0x71a0
testl %r15d, %r15d
je 0x2baa8
leaq 0x108(%rsp), %rdi
callq 0x71a0
leaq 0x128(%rsp), %rdi
callq 0x71a0
leaq 0x45de(%rip), %rdx      # 0x3008d
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x22ff5
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x74c0
movq %rbx, %rdi
callq 0x71a0
leaq 0x60(%rsp), %rdi
movl %ebp, %esi
callq 0x25cc5
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x2c366
leaq 0x38(%rsp), %rbx
leaq 0x10(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x74c0
movq %r14, %rdi
callq 0x71a0
leaq 0x60(%rsp), %rdi
callq 0x71a0
leaq 0x425c(%rip), %rdx      # 0x2fd82
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x22ff5
movq 0x10(%rsp), %rsi
leaq 0x188(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x7260
leaq 0x10(%rsp), %rdi
callq 0x71a0
leaq 0x4241(%rip), %rdx      # 0x2fd9a
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x22ff5
movq 0x10(%rsp), %rsi
leaq 0x590(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x7260
leaq 0x10(%rsp), %rdi
callq 0x71a0
leaq 0x4524(%rip), %rsi      # 0x300b2
leaq 0xa8(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x9325
leaq 0x452c(%rip), %rdx      # 0x300d3
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x948c
leaq 0x60(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x23292
leaq 0x450f(%rip), %rdx      # 0x300e9
leaq 0x10(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x948c
leaq 0x60(%rsp), %rdi
callq 0x71a0
leaq 0xc8(%rsp), %rdi
callq 0x71a0
leaq 0xa8(%rsp), %rdi
callq 0x71a0
movq 0x10(%rsp), %rsi
leaq 0x44e2(%rip), %rdi      # 0x300fb
xorl %eax, %eax
callq 0x7050
movq 0x10(%rsp), %rdi
callq 0x7350
leaq 0x469c(%rip), %rdi      # 0x302cd
callq 0x75c0
leaq 0x44cf(%rip), %rsi      # 0x3010c
leaq 0x390(%rsp), %r15
movq %r15, %rdi
callq 0x73d0
movq %rax, %rdi
callq 0x71c0
movq 0x390(%rsp), %rax
movq -0x18(%rax), %rcx
movl $0xfffffefb, %edx       # imm = 0xFFFFFEFB
andl 0x3a8(%rsp,%rcx), %edx
orl $0x4, %edx
movl %edx, 0x3a8(%rsp,%rcx)
movq -0x18(%rax), %rcx
orl $0x400, 0x3a8(%rsp,%rcx) # imm = 0x400
movq -0x18(%rax), %rax
movq $0x6, 0x398(%rsp,%rax)
leaq 0x19c9b(%rip), %rax     # 0x45938
movq (%rax), %rdi
pushq $0x2
popq %rsi
callq 0xf7c6
movq %rax, %rbx
movl 0x11d0(%rsp), %ecx
movq 0x8(%rax), %rax
subq (%rbx), %rax
pushq $0x30
popq %rsi
cqto
idivq %rsi
movq %rax, 0x90(%rsp)
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x88(%rsp)
xorl %ebp, %ebp
movq %rbx, 0x80(%rsp)
cmpq 0x90(%rsp), %rbp
je 0x2bea9
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x2c38a
movq %rax, %r13
leaq 0x19c35(%rip), %rax     # 0x45938
movq (%rax), %rdi
movl %ebp, %esi
callq 0xf7e6
mulsd 0x88(%rsp), %xmm0
movsd 0x28(%r13), %xmm2
unpcklps 0x301d(%rip), %xmm2     # xmm2 = xmm2[0],mem[0],xmm2[1],mem[1]
subpd 0x3025(%rip), %xmm2     # 0x2ed50
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0x18(%r13), %rbx
addq $0x8, %r13
movq %rbp, 0x98(%rsp)
incq %rbp
xorpd %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
pushq $0x1
popq %r14
movq %r14, %r12
cmpq %r13, %rbx
je 0x2be9c
leaq 0x19cf8(%rip), %rdi     # 0x45a70
callq 0xb2e2
movsd 0xa0(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2be89
movsd 0x28(%rbx), %xmm0
movsd %xmm0, 0x30(%rsp)
movq %r15, %rdi
leaq 0x2178(%rip), %rsi      # 0x2df1d
callq 0x73d0
movq %rax, %rdi
movq %rbp, %rsi
callq 0x72b0
movq %rax, %rdi
leaq 0x2417(%rip), %rsi      # 0x2e1d6
callq 0x73d0
movq %rax, %rdi
movq %r12, %rsi
callq 0x72b0
movq %rax, %rdi
leaq 0x435c(%rip), %rsi      # 0x30135
callq 0x73d0
leaq 0x998(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x72b0
movq %rax, %rdi
leaq 0x21eb(%rip), %rsi      # 0x2dfe8
callq 0x73d0
leaq -0x1(%r12), %rsi
movq %rax, %rdi
callq 0x72b0
movq %rax, %rdi
leaq 0x21cf(%rip), %rsi      # 0x2dfe8
callq 0x73d0
movq %rax, %rdi
movsd 0x30(%rsp), %xmm0
callq 0x7620
movq %rax, %rdi
callq 0x71c0
movq %r15, %rax
leaq 0x42fb(%rip), %rsi      # 0x30139
cmpq $0x1, %r14
je 0x2be5f
movsd 0x30(%rsp), %xmm0
subsd 0x58(%rsp), %xmm0
movq %r15, %rdi
callq 0x7620
leaq 0x2189(%rip), %rsi      # 0x2dfe8
movq %rax, %rdi
callq 0x73d0
movq %r15, %rdi
movq %rbp, %rsi
callq 0x72b0
movq %rax, %rdi
callq 0x71c0
incq %r14
movsd 0x30(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
incq %r12
movq %rbx, %rdi
callq 0x72d0
movq %rax, %rbx
jmp 0x2bd68
movq 0x80(%rsp), %rbx
jmp 0x2bce0
leaq 0x998(%rsp), %rdi
callq 0x7560
leaq 0x4280(%rip), %rsi      # 0x3013d
leaq 0xe8(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0x9325
leaq 0x4287(%rip), %rdx      # 0x3015d
leaq 0xa8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x948c
leaq 0xc8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x23292
leaq 0x4264(%rip), %rdx      # 0x30170
leaq 0x60(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x948c
leaq 0x10(%rsp), %rbx
leaq 0x60(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x74c0
movq %r14, %rdi
callq 0x71a0
leaq 0xc8(%rsp), %rdi
callq 0x71a0
leaq 0xa8(%rsp), %rdi
callq 0x71a0
leaq 0xe8(%rsp), %rdi
callq 0x71a0
movq (%rbx), %rsi
leaq 0x418f(%rip), %rdi      # 0x300fb
xorl %eax, %eax
callq 0x7050
movq (%rbx), %rdi
callq 0x7350
leaq 0x10(%rsp), %rdi
callq 0x71a0
leaq 0x38(%rsp), %rdi
callq 0x71a0
leaq 0x590(%rsp), %rdi
callq 0x7100
leaq 0x188(%rsp), %rdi
callq 0x7100
leaq 0x998(%rsp), %rdi
callq 0x7590
leaq 0x390(%rsp), %rdi
callq 0x7590
leaq 0xb98(%rsp), %rdi
callq 0x7590
leaq 0xd98(%rsp), %rdi
callq 0x7590
leaq 0xf98(%rsp), %rdi
callq 0x7590
leaq 0x798(%rsp), %rdi
callq 0x7590
leaq 0x168(%rsp), %rdi
callq 0x71a0
addq $0x1198, %rsp           # imm = 0x1198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c0d5
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
callq 0x71a0
jmp 0x2c030
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
jmp 0x2c193
jmp 0x2c0d5
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x71a0
jmp 0x2c057
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x71a0
jmp 0x2c069
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x71a0
jmp 0x2c18e
jmp 0x2c18b
jmp 0x2c18b
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x71a0
jmp 0x2c097
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x71a0
jmp 0x2c0a9
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
jmp 0x2c193
jmp 0x2c0d5
jmp 0x2c18b
jmp 0x2c0d5
jmp 0x2c18b
jmp 0x2c0d5
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0x2c193
jmp 0x2c0d5
movq %rax, %rbx
jmp 0x2c198
movq %rax, %rbx
jmp 0x2c1a2
movq %rax, %rbx
jmp 0x2c1af
jmp 0x2c106
jmp 0x2c106
movq %rax, %rbx
leaq 0x188(%rsp), %rdi
callq 0x71a0
jmp 0x2c1bc
movq %rax, %rbx
jmp 0x2c1bc
movq %rax, %rbx
jmp 0x2c1c9
movq %rax, %rbx
jmp 0x2c1d6
movq %rax, %rbx
jmp 0x2c1e3
movq %rax, %rbx
jmp 0x2c1f0
movq %rax, %rbx
jmp 0x2c1fd
jmp 0x2c161
jmp 0x2c161
movq %rax, %rbx
leaq 0x798(%rsp), %rdi
callq 0x71a0
jmp 0x2c14f
movq %rax, %rbx
leaq 0x590(%rsp), %rdi
callq 0x71a0
jmp 0x2c20a
movq %rax, %rbx
jmp 0x2c20a
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x2c212
movq %rax, %rbx
jmp 0x2c217
jmp 0x2c18b
jmp 0x2c18b
jmp 0x2c18b
jmp 0x2c18b
jmp 0x2c18b
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x71a0
leaq 0x38(%rsp), %rdi
callq 0x71a0
leaq 0x590(%rsp), %rdi
callq 0x7100
leaq 0x188(%rsp), %rdi
callq 0x7100
leaq 0x998(%rsp), %rdi
callq 0x7590
leaq 0x390(%rsp), %rdi
callq 0x7590
leaq 0xb98(%rsp), %rdi
callq 0x7590
leaq 0xd98(%rsp), %rdi
callq 0x7590
leaq 0xf98(%rsp), %rdi
callq 0x7590
leaq 0x798(%rsp), %rdi
callq 0x7590
leaq 0x168(%rsp), %rdi
callq 0x71a0
movq %rbx, %rdi
callq 0x76d0