// File: /workspace/github_cpp_makefile_star2_O1/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 $0x158, %rsp            # imm = 0x158
cmpl $0x0, 0x40(%rdi)
je 0x26279
movl %ecx, %ebx
movl %edx, %r12d
movq %rdi, %r14
movq %rsi, 0x80(%rsp)
leaq 0x23c0a(%rip), %r15     # 0x49908
movq %r15, %rdi
callq 0x198ee
movl %eax, 0x8(%rsp)
cmpl $0x0, 0x28(%r14)
je 0x26299
movq 0x148(%r14), %rcx
subq 0x140(%r14), %rcx
shrq $0x3, %rcx
cmpl $-0x1, %r12d
cmovnel %r12d, %ecx
movl 0x40(%r14), %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movq %rcx, 0x38(%rsp)
movq 0x18(%r14), %rsi
leaq 0x110(%rsp), %r13
movq %r13, %rdi
callq 0x2c3c8
movq 0x20(%r15), %r12
leaq 0x100(%rsp), %rbp
movq %rbp, -0x10(%rbp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0xf0(%rsp), %rdi
callq 0xa916
leaq 0xf0(%rsp), %rsi
movq %r12, %rdi
callq 0x15b64
movq %rax, %r13
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x25da9
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x20(%r15), %r15
leaq 0xe0(%rsp), %r12
movq %r12, -0x10(%r12)
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdx
addq %rsi, %rdx
leaq 0xd0(%rsp), %rdi
callq 0xa916
leaq 0xd0(%rsp), %rsi
movq %r15, %rdi
callq 0x15aec
movq %rax, 0x88(%rsp)
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x25e0f
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x7390
cmpl $0x0, 0x38(%rsp)
jle 0x26258
testb %bl, %bl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnel 0x8(%rsp), %eax
movl %eax, 0x34(%rsp)
xorl %ebx, %ebx
leaq 0x7(%rsp), %r15
movq %r13, 0x90(%rsp)
movq 0x148(%r14), %rax
cmpq 0x140(%r14), %rax
je 0x26258
movq $0x0, 0x10(%rsp)
movq 0x148(%r14), %rdi
subq 0x140(%r14), %rdi
shrq $0x3, %rdi
callq 0x29a86
movslq %eax, %rsi
movq 0x140(%r14), %rax
movq 0x148(%r14), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x2628b
movq (%rax,%rsi,8), %rbp
testq %rbp, %rbp
je 0x25e56
movb $0x0, 0x7(%rsp)
movl $0x158, %edi            # imm = 0x158
callq 0x7380
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq %r14, %rdx
movq %r15, %rcx
callq 0x231de
movq %r12, 0x10(%rsp)
cmpb $0x0, 0x7(%rsp)
je 0x26234
movl %ebx, 0x30(%rsp)
decl 0x40(%r14)
movq 0x88(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
movq %r13, %r15
movq (%r15), %r15
cmpq %r13, %r15
je 0x26205
movq 0x28(%rsp), %rax
leaq 0x10(%rax), %rsi
leaq 0x98(%rsp), %rdi
callq 0x2021c
incl %r12d
movl 0x34(%rsp), %eax
cmpl %eax, 0x18(%r15)
jne 0x261e1
movl %r12d, 0x24(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x18(%rsp)
movq 0x98(%rsp), %r12
cmpq 0xa0(%rsp), %r12
je 0x2607e
movq 0x18(%rsp), %rax
leaq 0x68(%rax), %r13
movq 0x10(%rsp), %rbx
leaq 0x70(%rsp), %rax
movq %rax, 0x60(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x60(%rsp), %rdi
callq 0xa916
addq $0x50, %rbx
movq %rbx, %rdi
leaq 0x60(%rsp), %rsi
callq 0x9b86
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
leaq 0x1015b(%rip), %rsi     # 0x360f8
movq %r12, %rdx
callq 0x9981
movq %r13, %rdi
movq %rbx, %rsi
callq 0x1b39a
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x25fdf
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x25ffb
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x10(%rsp), %rbx
leaq 0x50(%rsp), %rax
movq %rax, 0x40(%rsp)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x40(%rsp), %rdi
callq 0xa916
addq $0x50, %rbx
movq %rbx, %rdi
leaq 0x40(%rsp), %rsi
callq 0x9b86
movsd (%rax), %xmm0
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x9b86
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2606c
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x7390
addq $0x20, %r12
cmpq 0xa0(%rsp), %r12
jne 0x25f4b
movq 0x18(%rsp), %rdi
callq 0x9836
movsd %xmm0, 0x8(%rsp)
leaq 0x239fb(%rip), %rax     # 0x49a90
movq (%rax), %rax
movq (%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movsd %xmm0, 0x18(%rsp)
movq 0x20(%rbp), %rdi
testq %rdi, %rdi
jne 0x260b3
movq 0x18(%rbp), %rdi
movq 0x18(%r14), %r12
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm0
movaps %xmm0, 0x130(%rsp)
movsd 0x10(%rbp), %xmm0
movapd %xmm0, 0x140(%rsp)
callq 0x2e34a
movl %eax, %r13d
movq 0x20(%r14), %rdi
testq %rdi, %rdi
jne 0x260ec
movq 0x18(%r14), %rdi
movq 0x10(%r14), %rbx
callq 0x2e34a
xorps %xmm0, %xmm0
cvtsi2sdl 0x24(%rsp), %xmm0
movsd 0x10cba(%rip), %xmm4    # 0x36dc0
movapd 0x130(%rsp), %xmm3
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
movapd 0x10cb6(%rip), %xmm2    # 0x36dd0
subpd %xmm2, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm3, %xmm1
movapd 0x140(%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 0x8(%rsp), %xmm6
ucomisd 0x18(%rsp), %xmm6
addsd %xmm3, %xmm2
setae %sil
movq %rbx, %xmm3
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
subpd 0x10c67(%rip), %xmm3    # 0x36dd0
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtsi2sd %r13d, %xmm3
cvtsi2sd %eax, %xmm5
movq %r12, %rdi
callq 0x2e5b0
xorl %ebx, %ebx
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
movq 0x90(%rsp), %r13
movl 0x24(%rsp), %r12d
jae 0x261d3
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x261c5
movq %rbx, %rdi
callq 0x21dfc
movl $0x158, %esi            # imm = 0x158
movq %rbx, %rdi
callq 0x7390
movq $0x0, 0x10(%rsp)
movl $0x7, %ebx
movsd 0x8(%rsp), %xmm0
ucomisd 0x18(%rsp), %xmm0
jb 0x261f0
movq 0x28(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x28(%rsp)
xorl %ebx, %ebx
leaq 0x98(%rsp), %rdi
callq 0x1a61a
testl %ebx, %ebx
je 0x25eeb
movq 0x10(%rsp), %rax
testq %rax, %rax
movl 0x30(%rsp), %ebx
leaq 0x7(%rsp), %r15
je 0x2624c
movq 0x80(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x26242
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x2624c
leaq 0x10d75(%rip), %rdi     # 0x36fb0
callq 0x7560
jmp 0x2624e
leaq 0x10(%rsp), %rdx
callq 0x275b2
incl %ebx
cmpl 0x38(%rsp), %ebx
jl 0x25e39
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26279
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x7390
addq $0x158, %rsp            # imm = 0x158
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1098f(%rip), %rdi     # 0x36c21
xorl %eax, %eax
callq 0x7530
movl $0x8, %edi
callq 0x7180
leaq 0x10c94(%rip), %rcx     # 0x36f3e
movq %rcx, (%rax)
movq 0x22c84(%rip), %rsi     # 0x48f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r12, %rdi
je 0x2639b
movq 0xe0(%rsp), %rsi
jmp 0x262fa
jmp 0x26320
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %rbp, %rdi
je 0x2639b
movq 0x100(%rsp), %rsi
incq %rsi
jmp 0x2630e
jmp 0x26320
jmp 0x26320
movq %rax, %rbx
movl $0x158, %esi            # imm = 0x158
movq %r12, %rdi
callq 0x7390
jmp 0x2639b
jmp 0x26320
jmp 0x26320
jmp 0x2636a
jmp 0x26320
movq %rax, %rbx
jmp 0x2639b
jmp 0x26351
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
leaq 0xc0(%rsp), %rax
cmpq %rax, %rdi
je 0x26354
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x26354
movq %rax, %rbx
movq 0x60(%rsp), %rdi
leaq 0x70(%rsp), %rax
cmpq %rax, %rdi
je 0x2638e
movq 0x70(%rsp), %rsi
jmp 0x26386
movq %rax, %rbx
jmp 0x2638e
movq %rax, %rbx
movq 0x40(%rsp), %rdi
leaq 0x50(%rsp), %rax
cmpq %rax, %rdi
je 0x2638e
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x7390
leaq 0x98(%rsp), %rdi
callq 0x1a61a
leaq 0x120(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x263bc
movq 0x120(%rsp), %rsi
incq %rsi
callq 0x7390
movq %rbx, %rdi
callq 0x76a0