// File: /workspace/github_cpp_makefile_star2_O1/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 0x22855(%rip), %rdi     # 0x35e08
xorl %eax, %eax
callq 0x7060
movq %r14, %rdi
movq %rbx, %rsi
callq 0x71d0
movq (%rbx), %rdi
leaq 0x246f3(%rip), %rsi     # 0x37cc2
callq 0x7440
testq %rax, %rax
je 0x13e3e
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 0x13e5d
leaq 0x1f0(%rsp), %rdi
movl $0x800, %esi            # imm = 0x800
movq %rbx, %rdx
callq 0x7590
testq %rax, %rax
je 0x13dd6
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 0x2279e(%rip), %rsi     # 0x35e84
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 0x13cb2
cmpq $0x0, 0x70(%rsp)
je 0x13cb2
movq 0x168(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x19edc
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 0x2286e(%rip), %rdx     # 0x36019
movq %rdx, %rsi
callq 0xa886
movq %rbx, %rdi
movq %r12, %rsi
callq 0x19f58
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x137d5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq %rbx, %rdi
leaq 0x68(%rsp), %rsi
callq 0x9926
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 0x13b8f
movq %rbx, %r12
movq 0x20(%r15), %rbx
movq %rbx, %rdi
callq 0x7170
cmpq $0x6, %rax
jb 0x13b78
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 0xa916
movl $0x1, %ecx
movq %r14, %rdi
leaq 0x22984(%rip), %rbx     # 0x36206
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 0x22689(%rip), %rsi     # 0x35f41
xorl %edx, %edx
callq 0x7540
testq %rax, %rax
je 0x13a4e
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 0x22912(%rip), %rbp     # 0x36206
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 0x1399e
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 0x13963
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r14, %rdi
leaq 0x225e0(%rip), %rsi     # 0x35f4d
movq %rbx, %rdx
callq 0x9981
leaq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x7460
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x1399e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movl $0x1, %ecx
movq %rbx, %rdi
leaq 0x22859(%rip), %rsi     # 0x36206
xorl %edx, %edx
callq 0x7540
movl $0x1, %edx
movl $0x1, %r8d
movq %rbx, %rdi
movq %rax, %rsi
leaq 0x2458a(%rip), %rcx     # 0x37f56
callq 0x73e0
movq %rbx, %rdi
movq %rax, %rsi
callq 0x71d0
movq 0x8(%rsp), %rdi
leaq 0x22569(%rip), %rsi     # 0x35f51
leaq 0xac(%rsp), %rdx
leaq 0x178(%rsp), %rcx
xorl %eax, %eax
callq 0x7360
cmpl $0x2, %eax
jne 0x13b45
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 0x13b23
movups 0x48(%rsp), %xmm0
movups %xmm0, (%rsi)
addq $0x10, 0xb8(%rsp)
jmp 0x13b33
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 0x224b3(%rip), %rsi     # 0x35f45
callq 0x7100
testl %eax, %eax
leaq 0x88(%rsp), %rbx
jne 0x13acd
leaq 0x120(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x1a0f0
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a12e
movb $0x1, (%rax)
leaq 0x8(%rsp), %r14
movq %r14, %rdi
leaq 0x22472(%rip), %rsi     # 0x35f49
callq 0x7100
testl %eax, %eax
jne 0x13b05
leaq 0x100(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x1a0f0
movq 0xd8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a12e
movb $0x1, (%rax)
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x13b45
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x13b45
leaq 0xb0(%rsp), %rdi
movq %r14, %rdx
callq 0x1df48
leaq 0xe0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1a0f0
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x13b61
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13b78
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r15, %rdi
callq 0x7270
movq %rax, %r15
movq %r12, %rbx
cmpq %r12, %rax
jne 0x1383b
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 0x19f58
movq %r15, %rdi
movq %r12, %rsi
callq 0x1a1aa
movq %rax, %rdi
movq %rbx, %rsi
callq 0x1a20e
movq %r14, (%rax)
movl 0x28(%rsp), %eax
leal 0x1(%rax), %ecx
movl %ecx, 0x28(%rsp)
testl %eax, %eax
js 0x13bac
movq 0x158(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a28a
movq %rax, %rdi
leaq 0xb0(%rsp), %rsi
callq 0x1a306
movq 0x150(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a424
movq %rax, %rdi
leaq 0x120(%rsp), %rsi
callq 0x1a4a0
movq 0x148(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a424
movq %rax, %rdi
leaq 0x100(%rsp), %rsi
callq 0x1a4a0
movq 0x140(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a424
movq %rax, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1a4a0
leaq 0xe0(%rsp), %rdi
callq 0x1a61a
leaq 0x100(%rsp), %rdi
callq 0x1a61a
leaq 0x120(%rsp), %rdi
callq 0x1a61a
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x13cb2
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x13cce
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
movq 0x170(%rsp), %rbx
je 0x13cfb
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 0x136a8
jmp 0x13dd6
movq %rax, %r15
cmpl $0x1, %edx
jne 0x13fdc
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 0x9c02
leaq 0x28(%rsp), %rdi
leaq 0x213dc(%rip), %rsi     # 0x35152
callq 0x7100
movl %eax, %ebx
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13d94
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x68(%rsp), %r15
leaq 0x8(%rsp), %rdi
callq 0x74d0
testl %ebx, %ebx
jne 0x13e82
leaq 0x220e0(%rip), %rdi     # 0x35e92
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x7060
leaq 0x8(%rsp), %rdi
callq 0x7738
callq 0x7610
movq %r14, %rbx
jmp 0x137e2
movq 0xd0(%rsp), %r12
movq 0x70(%r12), %rbx
leaq 0x60(%r12), %r15
cmpq %r15, %rbx
je 0x13e16
leaq 0x22164(%rip), %r14     # 0x35f58
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 0x13df4
movq 0x80(%r12), %rsi
leaq 0x2213a(%rip), %rdi     # 0x35f5f
xorl %eax, %eax
callq 0x7060
addq $0x11f8, %rsp           # imm = 0x11F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x21fe8(%rip), %rdi     # 0x35e2d
callq 0x7560
movl $0x8, %edi
callq 0x7180
leaq 0x21ff1(%rip), %rcx     # 0x35e4c
jmp 0x13e6e
movl $0x8, %edi
callq 0x7180
leaq 0x21ffa(%rip), %rcx     # 0x35e68
movq %rcx, (%rax)
movq 0x350c0(%rip), %rsi     # 0x48f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
leaq 0x2205f(%rip), %rdi     # 0x35ee8
movq %r15, %rsi
movq %rax, %rdx
xorl %eax, %eax
callq 0x7060
movl $0x64, %edi
callq 0x7450
jmp 0x13f3c
movq %rax, %r15
leaq 0x8(%rsp), %rdi
callq 0x7738
callq 0x7610
jmp 0x13fdc
movq %rax, %rdi
callq 0x9c3f
movq %rax, %r15
jmp 0x13ee5
jmp 0x13f3c
movq %rax, %r15
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13ee5
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x7390
movl $0x98, %esi
movq %rbx, %rdi
jmp 0x13fd7
jmp 0x13f50
jmp 0x13f50
movq %rax, %r15
jmp 0x13f1e
jmp 0x13f44
jmp 0x13f50
movq %rax, %r15
movq 0x48(%rsp), %rdi
leaq 0x58(%rsp), %rax
cmpq %rax, %rdi
je 0x13f1e
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x13f47
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x13f47
movq %rax, %r15
jmp 0x13fdc
movq %rax, %r15
movq %r13, %rbp
jmp 0x13f6f
jmp 0x13f95
jmp 0x13f95
movq %rax, %r15
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x13f6f
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x28(%rsp), %rdi
cmpq %rbp, %rdi
je 0x13f98
movq 0x38(%rsp), %rsi
incq %rsi
jmp 0x13f8e
movq %rax, %r15
movl $0x98, %esi
movq %r14, %rdi
callq 0x7390
jmp 0x13f98
movq %rax, %r15
leaq 0xe0(%rsp), %rdi
callq 0x1a61a
leaq 0x100(%rsp), %rdi
callq 0x1a61a
leaq 0x120(%rsp), %rdi
callq 0x1a61a
movq 0xb0(%rsp), %rdi
testq %rdi, %rdi
je 0x13fdc
movq 0xc0(%rsp), %rsi
subq %rdi, %rsi
callq 0x7390
movq 0x68(%rsp), %rdi
leaq 0x78(%rsp), %rax
cmpq %rax, %rdi
je 0x13ff8
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x1401d
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x7390
movq %r15, %rdi
callq 0x76a0
nop