// File: /workspace/github_cpp_makefile_star2_O3/melop[P]admixem/config.cpp
// Function: PhenotypeConfigurations::LoadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void PhenotypeConfigurations::LoadFromFile(string szConfigFile) 
{
	FILE *pConfigFile;
	char szBuffer[2048];
	char szPhenotypeName[100];
	char szFormula[2048];
	
	#ifdef _OPENMP
		int nTotalCPUCore =  omp_get_max_threads();//omp_get_num_threads();
		//printf("OpenMP enabled\n");
	#else
		int nTotalCPUCore = 1;
		//printf("OpenMP disabled \n");
	#endif
	
	printf("Start loading phenotype file %s ...\n", szConfigFile.c_str());

	_szConfigFilename = szConfigFile; // save name of config file

	pConfigFile = fopen(szConfigFile.c_str(), "r");

	if (!pConfigFile) {
		printf("%s\n", "Cannot open phenotype file ...");
		throw "Cannot open phenotype file!";
	}

	if (fgets(szBuffer, 2048, pConfigFile) == NULL) { // skip the header line
		throw "Cannot read phenotype file!";
	}

	while(fgets(szBuffer, 2048, pConfigFile) != NULL) 
	{
		string sPhenotypeName, sFormula;

		sscanf(szBuffer, "%[^\t\n]	%[^\t\n]", szPhenotypeName, szFormula);
		sPhenotypeName = szPhenotypeName;
		sFormula = szFormula;
		if (sPhenotypeName.length() != 0 && sFormula.length() !=0) {
			this->_mpPhenotypes[sPhenotypeName] = sFormula;
			
			Parser * pF = new Parser("");
			try {
				pF->Evaluate(sFormula);
			}
			catch(std::runtime_error e) {
				if ( string(e.what()) == "Divide by zero" ) {
					
					printf("Warning in formula: %s \nErrMsg:%s\nThis could simply be caused by uninitialized values", sFormula.c_str(), e.what());
					
					
				}
				else {
					printf("Error occured when trying to parse formula: %s \nErrMsg:%s\nPlease check formulae grammar!", sFormula.c_str(), e.what());
					exit(100);
				}
			}

			vector< pair<int, double> > vSymbols;
			vector< string > vDadPheSymbols;
			vector< string > vMomPheSymbols;
			vector<string> vSymbolStrings;
			//list all the symbols used in this formula
			for(map<string,double>::iterator it = pF->symbols_.begin(); it != pF->symbols_.end(); ++it) 
			{	
				//Parse chromosome number and position from the symbol
				//cout << (it->first) << "\n"; //list all keys

				int nSymbolChr;
				double nSymbolPos;
				
				if (strlen(it->first.c_str()) < 6) { // no need to add symbol
					continue;
				}

				string sSymbolLabel = it->first;
				size_t nFirstUS = sSymbolLabel.find_first_of("_");
				size_t nLastUS = sSymbolLabel.find_last_of("_");

				if (sSymbolLabel.find_first_of("Phe") == 0) { //if referring to the phenotype of dad or mum
					string sWhichParent = sSymbolLabel.substr(  nFirstUS+1, 3);
					string sPhenotype = sSymbolLabel.substr(  nLastUS+1 );

					if (sWhichParent == "Dad" ) {
						vDadPheSymbols.push_back(sPhenotype);
						//vSymbolStrings.push_back(sSymbolLabel); //don't add this
						this->_mpIsReferToParentPhe[sPhenotypeName] = true;
					}

					if (sWhichParent == "Mom" ) {
						vMomPheSymbols.push_back(sPhenotype);
						//vSymbolStrings.push_back(sSymbolLabel); //don't add this.
						this->_mpIsReferToParentPhe[sPhenotypeName] = true;

					}

					continue;
				}

				string sTemp = it->first.substr(3);	
				nFirstUS = sTemp.find_first_of("_");
				nLastUS = sTemp.find_last_of("_");

				if (nFirstUS != nLastUS) { // there is _a1 _a2 suffixes, delete the suffix
					sTemp = sTemp.substr(0,nLastUS);
					sSymbolLabel = "chr" + sTemp;				
				}

				sTemp = sTemp.replace(sTemp.find_first_of("_"), 1, " ");
				if (sscanf(sTemp.c_str(), "%d %lf", &nSymbolChr, &nSymbolPos)!=2) 
				{ // see if it's another format with _a1, a2 suffixes
					continue;
				};
				pair<int,double> oSymbolPair( nSymbolChr, nSymbolPos);
				vSymbols.push_back(oSymbolPair);
				vSymbolStrings.push_back(sSymbolLabel); // save label too
			}

			for (int nCPU=0;nCPU<nTotalCPUCore;nCPU++) {
				//map<string , Parser *> _mpPhenotypeFormulae;
				this->_mpmpPhenotypeFormulae[nCPU][sPhenotypeName] = new Parser(sFormula);//pF;
			}
			this->_mpPhenotypeFormulaSymbols[sPhenotypeName] = vSymbols;
			this->_mpPhenotypeFormulaDadSymbols[sPhenotypeName] = vDadPheSymbols;
			this->_mpPhenotypeFormulaMomSymbols[sPhenotypeName] = vMomPheSymbols;
			this->_mpPhenotypeFormulaSymbolStrings[sPhenotypeName] = vSymbolStrings;
		}
	}

	for (std::map<string,string>::iterator it = _mpPhenotypes.begin(); it != _mpPhenotypes.end(); ++it) {
		printf("%s %s\n", it->first.c_str(), it->second.c_str());
	}

	printf("%d phenotype(s) loaded. \n", _mpPhenotypes.size());

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11f8, %rsp           # imm = 0x11F8
movq %rsi, %rbx
movq %rdi, %r14
movq (%rsi), %rsi
leaq 0x216b7(%rip), %rdi     # 0x34e28
xorl %eax, %eax
callq 0x7060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x71d0
movq (%rbx), %rdi
leaq 0x2353d(%rip), %rsi     # 0x36cca
callq 0x7440
testq %rax, %rax
je 0x13ffc
movq %rax, %rbx
movq %r14, 0xd0(%rsp)
leaq 0x1f0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rax, %rdx
callq 0x7590
testq %rax, %rax
je 0x1401b
leaq 0x1f0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x7590
testq %rax, %rax
je 0x13f94
movq 0xd0(%rsp), %rax
leaq 0x58(%rax), %rcx
movq %rcx, 0x168(%rsp)
leaq 0x38(%rsp), %rbp
leaq 0x20(%rax), %rcx
movq %rcx, 0xd8(%rsp)
leaq 0x88(%rax), %r15
leaq 0xb8(%rax), %rcx
movq %rcx, 0x158(%rsp)
leaq 0xe8(%rax), %rcx
movq %rcx, 0x150(%rsp)
leaq 0x118(%rax), %rcx
movq %rcx, 0x148(%rsp)
addq $0x148, %rax            # imm = 0x148
movq %rax, 0x140(%rsp)
leaq 0x1f0(%rsp), %r14
leaq 0x28(%rsp), %r12
movq %rbx, 0x170(%rsp)
movq %r15, 0x160(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
xorl %ecx, %ecx
movq %rcx, 0x90(%rsp)
movb %cl, 0x98(%rsp)
leaq 0x78(%rsp), %rax
movq %rax, 0x68(%rsp)
movq %rcx, 0x70(%rsp)
movb %cl, 0x78(%rsp)
movq %r14, %rdi
leaq 0x21600(%rip), %rsi     # 0x34ea4
leaq 0x180(%rsp), %r14
movq %r14, %rdx
leaq 0x9f0(%rsp), %rcx
xorl %eax, %eax
callq 0x7360
movq 0x90(%rsp), %rbx
movq %r14, %rdi
callq 0x7170
leaq 0x88(%rsp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x7570
movq 0x70(%rsp), %rbx
leaq 0x9f0(%rsp), %r14
movq %r14, %rdi
callq 0x7170
leaq 0x68(%rsp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x7570
cmpq $0x0, 0x90(%rsp)
je 0x13e70
cmpq $0x0, 0x70(%rsp)
je 0x13e70
movq 0x168(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x19f5e
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0x71d0
movl $0x98, %edi
callq 0x7380
movq %rax, %rbx
movq %rbp, 0x28(%rsp)
movq %r12, %rdi
leaq 0x216d0(%rip), %rdx     # 0x35039
movq %rdx, %rsi
callq 0xa91c
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19fda
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13993
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
callq 0x99c0
xorps %xmm0, %xmm0
movaps %xmm0, 0xb0(%rsp)
xorl %eax, %eax
movq %rax, 0xc0(%rsp)
movq %rax, 0x130(%rsp)
movaps %xmm0, 0x120(%rsp)
movq %rax, 0x110(%rsp)
movaps %xmm0, 0x100(%rsp)
movq %rax, 0xf0(%rsp)
movaps %xmm0, 0xe0(%rsp)
movq 0x80(%rbx), %r15
addq $0x70, %rbx
cmpq %rbx, %r15
je 0x13d4d
movq %rbx, %r12
movq 0x20(%r15), %rbx
movq %rbx, %rdi
callq 0x7170
cmpq $0x6, %rax
jb 0x13d36
movq %rbp, %r13
movq %rbp, 0x28(%rsp)
movq 0x28(%r15), %rdx
addq %rbx, %rdx
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xa9b2
movl $0x1, %ecx
movq %r14, %rdi
leaq 0x217e6(%rip), %rbx     # 0x35226
movq %rbx, %rsi
xorl %edx, %edx
callq 0x7540
movq %rax, %rbp
movl $0x1, %ecx
movq %r14, %rdi
movq %rbx, %rsi
movq $-0x1, %rdx
callq 0x7700
movq %rax, %rbx
movl $0x3, %ecx
movq %r14, %rdi
leaq 0x214eb(%rip), %rsi     # 0x34f61
xorl %edx, %edx
callq 0x7540
testq %rax, %rax
je 0x13c0c
leaq 0x20(%r15), %rsi
movl $0x3, %edx
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq $-0x1, %rcx
callq 0x73c0
movl $0x1, %ecx
movq %rbx, %rdi
leaq 0x21774(%rip), %rbp     # 0x35226
movq %rbp, %rsi
xorl %edx, %edx
callq 0x7540
movq %rax, %rbx
movl $0x1, %ecx
leaq 0x8(%rsp), %rdi
movq %rbp, %rsi
movq $-0x1, %rdx
callq 0x7700
cmpq %rax, %rbx
leaq 0x8(%rsp), %rbx
movq %r13, %rbp
leaq 0x48(%rsp), %r14
je 0x13b5c
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x73c0
movq %rbx, %rdi
movq %r14, %rsi
callq 0x7460
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x13b21
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r14, %rdi
leaq 0x21442(%rip), %rsi     # 0x34f6d
movq %rbx, %rdx
callq 0x9a1b
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x7460
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x13b5c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movl $0x1, %ecx
movq %rbx, %rdi
leaq 0x216bb(%rip), %rsi     # 0x35226
xorl %edx, %edx
callq 0x7540
movl $0x1, %edx
movl $0x1, %r8d
movq %rbx, %rdi
movq %rax, %rsi
leaq 0x233d4(%rip), %rcx     # 0x36f5e
callq 0x73e0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x71d0
movq 0x8(%rsp), %rdi
leaq 0x213cb(%rip), %rsi     # 0x34f71
leaq 0xac(%rsp), %rdx
leaq 0x178(%rsp), %rcx
xorl %eax, %eax
callq 0x7360
cmpl $0x2, %eax
jne 0x13d03
movl 0xac(%rsp), %eax
movl %eax, 0x48(%rsp)
movsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xb8(%rsp), %rsi
cmpq 0xc0(%rsp), %rsi
je 0x13ce1
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0xb8(%rsp)
jmp 0x13cf1
incq %rbp
movl $0x3, %ecx
leaq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x73c0
incq %rbx
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
movq $-0x1, %rcx
movq %r13, %rbp
callq 0x73c0
leaq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0x21315(%rip), %rsi     # 0x34f65
callq 0x7100
testl %eax, %eax
leaq 0x88(%rsp), %rbx
jne 0x13c8b
leaq 0x120(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x1a172
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a1b0
movb $0x1, (%rax)
leaq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0x212d4(%rip), %rsi     # 0x34f69
callq 0x7100
testl %eax, %eax
jne 0x13cc3
leaq 0x100(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x1a172
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a1b0
movb $0x1, (%rax)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x13d03
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x13d03
leaq 0xb0(%rsp), %rdi
movq %r14, %rdx
callq 0x1dfa0
leaq 0xe0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1a172
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x13d1f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13d36
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r15, %rdi
callq 0x7270
movq %rax, %r15
movq %r12, %rbx
cmpq %r12, %rax
jne 0x139f9
movl $0x0, 0x28(%rsp)
movq 0x160(%rsp), %r15
leaq 0x88(%rsp), %rbx
leaq 0x28(%rsp), %r12
movl $0x98, %edi
callq 0x7380
movq %rax, %r14
movq %rax, %rdi
leaq 0x68(%rsp), %rsi
callq 0x19fda
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a22c
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1a292
movq %r14, (%rax)
movl 0x28(%rsp), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x28(%rsp)
testl %eax, %eax
js 0x13d6a
movq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a30e
movq %rax, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1a38a
movq 0x150(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a4b4
movq %rax, %rdi
leaq 0x120(%rsp), %rsi
callq 0x1a530
movq 0x148(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a4b4
movq %rax, %rdi
leaq 0x100(%rsp), %rsi
callq 0x1a530
movq 0x140(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a4b4
movq %rax, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1a530
leaq 0xe0(%rsp), %rdi
callq 0x1a6c6
leaq 0x100(%rsp), %rdi
callq 0x1a6c6
leaq 0x120(%rsp), %rdi
callq 0x1a6c6
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x13e70
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x13e8c
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
movq 0x170(%rsp), %rbx
je 0x13eb9
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x7390
leaq 0x1f0(%rsp), %r14
movq %r14, %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x7590
testq %rax, %rax
jne 0x13866
jmp 0x13f94
movq %rax, %r15
cmpl $0x1, %edx
jne 0x1419a
movq %rbx, %r14
movq %r15, %rdi
callq 0x7470
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x7160
movq %r15, %rdi
callq 0x7150
movq %rbx, %rdi
callq 0x74d0
leaq 0x28(%rsp), %rdi
movq %rax, %rsi
leaq 0x48(%rsp), %rdx
callq 0x9c98
leaq 0x28(%rsp), %rdi
leaq 0x2021e(%rip), %rsi     # 0x34152
callq 0x7100
movl %eax, %ebx
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13f52
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x68(%rsp), %r15
leaq 0x8(%rsp), %rdi
callq 0x74d0
testl %ebx, %ebx
jne 0x14040
leaq 0x20f42(%rip), %rdi     # 0x34eb2
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x7060
leaq 0x8(%rsp), %rdi
callq 0x7738
callq 0x7610
movq %r14, %rbx
jmp 0x139a0
movq 0xd0(%rsp), %r12
movq 0x70(%r12), %rbx
leaq 0x60(%r12), %r15
cmpq %r15, %rbx
je 0x13fd4
leaq 0x20fc6(%rip), %r14     # 0x34f78
movq 0x20(%rbx), %rsi
movq 0x40(%rbx), %rdx
movq %r14, %rdi
xorl %eax, %eax
callq 0x7060
movq %rbx, %rdi
callq 0x7270
movq %rax, %rbx
cmpq %r15, %rax
jne 0x13fb2
movq 0x80(%r12), %rsi
leaq 0x20f9c(%rip), %rdi     # 0x34f7f
xorl %eax, %eax
callq 0x7060
addq $0x11f8, %rsp           # imm = 0x11F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20e4a(%rip), %rdi     # 0x34e4d
callq 0x7560
movl $0x8, %edi
callq 0x7180
leaq 0x20e53(%rip), %rcx     # 0x34e6c
jmp 0x1402c
movl $0x8, %edi
callq 0x7180
leaq 0x20e5c(%rip), %rcx     # 0x34e88
movq %rcx, (%rax)
movq 0x33f02(%rip), %rsi     # 0x47f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
leaq 0x20ec1(%rip), %rdi     # 0x34f08
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x7060
movl $0x64, %edi
callq 0x7450
jmp 0x140fa
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x7738
callq 0x7610
jmp 0x1419a
movq %rax, %rdi
callq 0x9cd5
movq %rax, %r15
jmp 0x140a3
jmp 0x140fa
movq %rax, %r15
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x140a3
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movl $0x98, %esi
movq %rbx, %rdi
jmp 0x14195
jmp 0x1410e
jmp 0x1410e
movq %rax, %r15
jmp 0x140dc
jmp 0x14102
jmp 0x1410e
movq %rax, %r15
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x140dc
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x14105
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x14105
movq %rax, %r15
jmp 0x1419a
movq %rax, %r15
movq %r13, %rbp
jmp 0x1412d
jmp 0x14153
jmp 0x14153
movq %rax, %r15
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x1412d
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x14156
movq 0x38(%rsp), %rsi
incq %rsi
jmp 0x1414c
movq %rax, %r15
movl $0x98, %esi
movq %r14, %rdi
callq 0x7390
jmp 0x14156
movq %rax, %r15
leaq 0xe0(%rsp), %rdi
callq 0x1a6c6
leaq 0x100(%rsp), %rdi
callq 0x1a6c6
leaq 0x120(%rsp), %rdi
callq 0x1a6c6
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x1419a
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x141b6
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x141db
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r15, %rdi
callq 0x76a0
nop