// File: /workspace/github_cpp_makefile_star2_O2/melop[P]admixem/Individual.cpp
// Function: Individual::GiveBirth(std::vector<Individual*, std::allocator<Individual*>>&, int, bool)
void Individual::GiveBirth(vector<Individual *> &vOffSprings, int nNum, bool bIgnoreGlobalRules) {
	
		#ifdef _OPENMP
		int nCPU = omp_get_thread_num();
			#ifdef DEBUG
			printf("CPU %d: %s : %d\n", nCPU, "SexSelFormulae # ", nCPU);
			#endif
		#else
			int nCPU = 0;
		#endif

	if (this->_nAvailableGametes <= 0) {
		//printf("Not enough gametes left.\n");
		return; //no more offsprings.
	}

	int nCurrGen = SimulConfig.GetCurrGen();
	//Go through the parenthood of each inseminated gamete:
	if (this->_bSex == Male) {
		throw "Males cannot give birth, r u nuts??";
	}

	nNum = nNum==-1? this->_arrOtherParentsForOffsprings.size():nNum;
	nNum = (nNum <= this->_nAvailableGametes)? nNum : this->_nAvailableGametes;
	//printf("Female %d: %d offsprings\n", this->GetID(), nNum);
	// do frequency-independent natural selection here!

	string sPop = ((Population*)(this->_pPop))->GetPopName();

	list< pair< Parser *, int> > * pqParsers;
	//#pragma omp critical 
	//{
		pqParsers = SimulConfig.pNaturalSelConfig->GetFormulae(sPop);
	

	//list< vector<string> > * pqvCourterSymbols = SimulConfig.pNaturalSelConfig->GetFormulaSymbolStringsCourter( sPop);
	list< vector<string> > * pqvSelfSymbols = SimulConfig.pNaturalSelConfig->GetFormulaSymbolStringsSelf( sPop);

	
	for(int i=0; i<nNum;i++) { 
		if (this->_arrOtherParentsForOffsprings.size()==0) return;

		
		Individual * pOffSpring = NULL;
		Individual * pDad = NULL;

		do {
			//#pragma omp critical 
			//{
				int nRandDad = fnGetRandIndex(this->_arrOtherParentsForOffsprings.size() );
				pDad = this->_arrOtherParentsForOffsprings.at(nRandDad);
			//}
		}
		while(pDad==NULL);

		bool bSuccess = false;
	
		pOffSpring = new Individual( pDad, this , bSuccess); // create a new kid
		
		if (!bSuccess) {
			printf("Something wrong when creating new individual. return false\n");
			i--;
			continue;
		}

		if (!pOffSpring) {
			printf("Something wrong when creating new individual. return NULL\n");
			i--;
			continue;
		}

		this->_nAvailableGametes--; // one less gamete!

		// See if it's lucky enough to survive the cruel nature!!
		// Go through each selection rule:
		
		//list< vector<string> >::iterator itCourterSymbols= pqvCourterSymbols->begin();
		list< vector<string> >::iterator itSelfSymbols= pqvSelfSymbols->begin();
		int nParserCount = -1;
		for (list< pair< Parser *, int> >::iterator itParser= pqParsers->begin(); itParser != pqParsers->end() ; ++itParser) {
			nParserCount++;
			vector<string> vSymbolsSelf = *itSelfSymbols;
			Parser * pParser = itParser->first;
			int nGen = itParser->second;

			if ((nGen ==-1 && !bIgnoreGlobalRules) || (bIgnoreGlobalRules && nGen == nCurrGen) ) {
				//Set self symbol values
				bool bLive = false;

				for(vector<string>::iterator itSymbol=vSymbolsSelf.begin();itSymbol!=vSymbolsSelf.end();++itSymbol)
				{
					pParser->symbols_[string("My_"+(*itSymbol))] = pOffSpring->GetPhenotype(*itSymbol);
					pParser->symbols_[*itSymbol] = pOffSpring->GetPhenotype(*itSymbol); //set both variables
				}

				double nSurvivalProb = pParser->Evaluate();
				bLive = (vUniformGen[nCPU]->Next() <= nSurvivalProb)? true : false;

				((Population*)this->_pPop)->fnAddNatSelProb(nParserCount, pOffSpring->GetID(), pDad->GetID(), ((Population*)pDad->GetPrevPop())->GetPopId(),
															this->GetID(), ((Population*)this->GetPrevPop())->GetPopId(),
															nSurvivalProb , bLive);

				/*if (SimulConfig.GetConfig("DumpNaturalSelProb") == "On") {
					printf("%f\n",nSurvivalProb);
				}*/

				if (!bLive) {
					delete pOffSpring; // uhoh, dead!!
					pOffSpring = NULL;
					break;
				}
			}

			++itSelfSymbols;
		}
		
		

		if (!pOffSpring) {
			
		}
		else {

				vOffSprings.push_back(pOffSpring);
				// just for now, inseminate one egg:			

		}

		
		//_arrOtherParentsForOffsprings.erase(_arrOtherParentsForOffsprings.begin() + nRandDad);
	}

	//} //end critical

	// now clear all gametes
	//this->_arrOtherParentsForOffsprings.clear();

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, 0x50(%rsp)
cmpl $0x0, 0x40(%rdi)
je 0x22b5c
movl %ecx, %ebx
movl %edx, %ebp
movq %rdi, %r13
leaq 0x2324e(%rip), %r14     # 0x45938
movq %r14, %rdi
callq 0x16ac0
movl %eax, 0x10(%rsp)
cmpl $0x0, 0x28(%r13)
je 0x22b6e
movq 0x148(%r13), %rcx
subq 0x140(%r13), %rcx
shrq $0x3, %rcx
cmpl $-0x1, %ebp
cmovnel %ebp, %ecx
movl 0x40(%r13), %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movq %rcx, 0x60(%rsp)
movq 0x18(%r13), %rsi
leaq 0xf8(%rsp), %r12
movq %r12, %rdi
callq 0x27b7e
movq 0x20(%r14), %r15
leaq 0xd8(%rsp), %rdi
movq %r12, %rsi
callq 0x7550
leaq 0xd8(%rsp), %rsi
movq %r15, %rdi
callq 0x13a18
movq %rax, 0x48(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x71a0
movq 0x20(%r14), %r14
leaq 0xb8(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x7550
leaq 0xb8(%rsp), %rsi
movq %r14, %rdi
callq 0x139b6
movq %rax, 0x58(%rsp)
leaq 0x140(%r13), %rax
movq %rax, 0x70(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x71a0
testb %bl, %bl
pushq $-0x1
popq %rcx
movl 0x10(%rsp), %eax
cmovel %ecx, %eax
movl %eax, 0x10(%rsp)
xorl %eax, %eax
leaq 0x158(%rsp), %r12
leaq 0x138(%rsp), %r15
leaq 0x118(%rsp), %rbx
movq %r13, 0x38(%rsp)
movl %eax, 0x24(%rsp)
cmpl 0x60(%rsp), %eax
jge 0x22b4f
movq 0x148(%r13), %rax
cmpq 0x140(%r13), %rax
je 0x22b4f
andq $0x0, 0x8(%rsp)
movq 0x148(%r13), %rdi
subq 0x140(%r13), %rdi
shrq $0x3, %rdi
callq 0x25c49
movslq %eax, %rsi
movq 0x70(%rsp), %rdi
callq 0x232b2
movq (%rax), %rbp
testq %rbp, %rbp
je 0x2280f
movb $0x0, 0x7(%rsp)
movl $0x158, %edi            # imm = 0x158
callq 0x73f0
movq %rax, %r14
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
leaq 0x7(%rsp), %rcx
callq 0x208a0
movq %r14, 0x8(%rsp)
cmpb $0x0, 0x7(%rsp)
je 0x22b3a
movq %rbp, 0x68(%rsp)
decl 0x40(%r13)
movq 0x58(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
pushq $-0x1
popq %r14
movq 0x30(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x30(%rsp)
cmpq 0x48(%rsp), %rax
je 0x22b13
movq 0x40(%rsp), %rax
leaq 0x10(%rax), %rsi
leaq 0x78(%rsp), %rdi
callq 0x1dc64
incl %r14d
movl 0x10(%rsp), %eax
movq 0x30(%rsp), %rcx
cmpl %eax, 0x18(%rcx)
jne 0x22ac2
movl %r14d, 0x14(%rsp)
movq 0x30(%rsp), %rax
movq 0x10(%rax), %rax
movq 0x78(%rsp), %rbp
movq %rax, 0x28(%rsp)
leaq 0x68(%rax), %r14
cmpq 0x80(%rsp), %rbp
je 0x2299d
movq 0x8(%rsp), %r13
movq %r12, %rdi
movq %rbp, %rsi
callq 0x7550
movq %r13, %rdi
movq %r12, %rsi
callq 0x2242e
movsd %xmm0, 0x18(%rsp)
movq %r15, %rdi
leaq 0xb79f(%rip), %rsi      # 0x2e0c8
movq %rbp, %rdx
callq 0x9325
movq %r14, %rdi
movq %r15, %rsi
callq 0x18432
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r15, %rdi
callq 0x71a0
movq %r12, %rdi
callq 0x71a0
movq 0x8(%rsp), %r13
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x7550
movq %r13, %rdi
movq %rbx, %rsi
callq 0x2242e
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
callq 0x94ac
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq %rbx, %rdi
callq 0x71a0
addq $0x20, %rbp
jmp 0x228f0
movq 0x28(%rsp), %rdi
callq 0x920c
movsd %xmm0, 0x18(%rsp)
leaq 0x2310c(%rip), %rax     # 0x45ac0
movq (%rax), %rax
movq (%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movsd %xmm0, 0x28(%rsp)
movq 0x68(%rsp), %rcx
movq 0x20(%rcx), %rdi
testq %rdi, %rdi
jne 0x229d7
movq 0x18(%rcx), %rdi
movq 0x38(%rsp), %rax
movq 0x18(%rax), %r14
movq 0x8(%rsp), %rax
movsd 0x10(%rax), %xmm0
movaps %xmm0, 0x90(%rsp)
movsd 0x10(%rcx), %xmm0
movapd %xmm0, 0xa0(%rsp)
callq 0x292bc
movl %eax, %ebp
movq 0x38(%rsp), %rax
movq 0x20(%rax), %rdi
testq %rdi, %rdi
jne 0x22a19
movq 0x18(%rax), %rdi
movq 0x10(%rax), %r13
callq 0x292bc
cvtsi2sdl 0x14(%rsp), %xmm0
movsd 0xc310(%rip), %xmm4     # 0x2ed40
movapd 0x90(%rsp), %xmm3
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
movapd 0xc30c(%rip), %xmm2     # 0x2ed50
subpd %xmm2, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm3, %xmm1
movapd 0xa0(%rsp), %xmm3
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
subpd %xmm2, %xmm3
movapd %xmm3, %xmm2
unpckhpd %xmm3, %xmm2            # xmm2 = xmm2[1],xmm3[1]
xorl %esi, %esi
movsd 0x18(%rsp), %xmm6
ucomisd 0x28(%rsp), %xmm6
addsd %xmm3, %xmm2
setae %sil
movq %r13, %xmm3
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
subpd 0xc2bd(%rip), %xmm3     # 0x2ed50
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
cvtsi2sd %ebp, %xmm3
cvtsi2sd %eax, %xmm5
movq %r14, %rdi
callq 0x29446
movsd 0x18(%rsp), %xmm0
ucomisd 0x28(%rsp), %xmm0
movl 0x14(%rsp), %r14d
jb 0x22ad4
movq 0x40(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x40(%rsp)
movb $0x1, %bpl
jmp 0x22b00
movq 0x8(%rsp), %r14
testq %r14, %r14
je 0x22ae6
movq %r14, %rdi
callq 0x1fc0e
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7400
andq $0x0, 0x8(%rsp)
xorl %ebp, %ebp
movl 0x14(%rsp), %r14d
leaq 0x78(%rsp), %rdi
callq 0x17632
testb %bpl, %bpl
jne 0x22894
cmpq $0x0, 0x8(%rsp)
je 0x22b2a
movq 0x50(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x2320e
movl 0x24(%rsp), %eax
incl %eax
movq 0x38(%rsp), %r13
jmp 0x227e7
leaq 0xc3ef(%rip), %rdi      # 0x2ef30
callq 0x75c0
movl 0x24(%rsp), %eax
jmp 0x227e7
leaq 0xf8(%rsp), %rdi
callq 0x71a0
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x8
popq %rdi
callq 0x71d0
leaq 0xc341(%rip), %rcx      # 0x2eebe
movq %rcx, (%rax)
movq 0x223c9(%rip), %rsi     # 0x44f50
movq %rax, %rdi
xorl %edx, %edx
callq 0x76a0
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x22bab
jmp 0x22bce
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x71a0
jmp 0x22c13
jmp 0x22bce
jmp 0x22bce
jmp 0x22bce
movq %rax, %rbx
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7400
jmp 0x22c13
jmp 0x22bce
jmp 0x22c06
movq %rax, %rbx
jmp 0x22c13
jmp 0x22bd5
movq %rax, %rbx
jmp 0x22bea
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x71a0
leaq 0x158(%rsp), %rdi
jmp 0x22bff
movq %rax, %rbx
leaq 0x118(%rsp), %rdi
callq 0x71a0
jmp 0x22c09
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x17632
leaq 0xf8(%rsp), %rdi
callq 0x71a0
movq %rbx, %rdi
callq 0x76d0