// File: /workspace/github_cpp_makefile_star2_O3/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 $0x168, %rsp            # imm = 0x168
cmpl $0x0, 0x40(%rdi)
je 0x25ffb
movl %ecx, %ebx
movl %edx, %r15d
movq %rdi, %r12
movq %rsi, 0xa8(%rsp)
leaq 0x22e86(%rip), %r14     # 0x48908
movq %r14, %rdi
callq 0x199ca
cmpl $0x0, 0x28(%r12)
je 0x2601b
movl %eax, %ebp
movq 0x148(%r12), %rcx
subq 0x140(%r12), %rcx
shrq $0x3, %rcx
cmpl $-0x1, %r15d
cmovnel %r15d, %ecx
movl 0x40(%r12), %eax
cmpl %eax, %ecx
cmovael %eax, %ecx
movq %rcx, 0x40(%rsp)
movq %r12, 0x30(%rsp)
movq 0x18(%r12), %rsi
leaq 0x120(%rsp), %r12
movq %r12, %rdi
callq 0x2bf56
movq 0x20(%r14), %r15
leaq 0x110(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%r12), %rsi
movq 0x8(%r12), %rdx
addq %rsi, %rdx
leaq 0x100(%rsp), %rdi
callq 0xa9b2
leaq 0x100(%rsp), %rsi
movq %r15, %rdi
callq 0x15cb6
movq %rax, 0x48(%rsp)
movq 0x100(%rsp), %rdi
cmpq %r13, %rdi
movq 0x30(%rsp), %r12
je 0x25b3d
movq 0x110(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x20(%r14), %r14
leaq 0xf0(%rsp), %r15
movq %r15, -0x10(%r15)
movq 0x120(%rsp), %rsi
movq 0x128(%rsp), %rdx
addq %rsi, %rdx
leaq 0xe0(%rsp), %rdi
callq 0xa9b2
leaq 0xe0(%rsp), %rsi
movq %r14, %rdi
callq 0x15c3e
movq %rax, 0xb0(%rsp)
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x25ba2
movq 0xf0(%rsp), %rsi
incq %rsi
callq 0x7390
cmpl $0x0, 0x40(%rsp)
jle 0x25fda
testb %bl, %bl
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmovnel %ebp, %eax
movl %eax, 0x3c(%rsp)
xorl %ebx, %ebx
leaq 0x88(%rsp), %r15
movq 0x140(%r12), %rax
movq 0x148(%r12), %rdi
cmpq %rax, %rdi
je 0x25fda
movq $0x0, 0x10(%rsp)
subq %rax, %rdi
shrq $0x3, %rdi
callq 0x296fa
movslq %eax, %rsi
movq 0x140(%r12), %rax
movq 0x148(%r12), %rdi
movq %rdi, %rdx
subq %rax, %rdx
sarq $0x3, %rdx
cmpq %rsi, %rdx
jbe 0x2600d
movq (%rax,%rsi,8), %rbp
testq %rbp, %rbp
je 0x25be7
movb $0x0, 0xf(%rsp)
movl $0x158, %edi            # imm = 0x158
callq 0x7380
movq %rax, %r14
movq %rax, %rdi
movq %rbp, %rsi
movq %r12, %rdx
leaq 0xf(%rsp), %rcx
callq 0x231f4
movq %r14, 0x10(%rsp)
cmpb $0x0, 0xf(%rsp)
je 0x25f5a
movl %ebx, 0x38(%rsp)
decl 0x40(%r12)
movq 0x48(%rsp), %rax
movq (%rax), %rbx
cmpq %rax, %rbx
leaq 0x70(%rsp), %r14
je 0x25f9a
movq 0xb0(%rsp), %rax
movq (%rax), %r13
movl $0xffffffff, 0x2c(%rsp) # imm = 0xFFFFFFFF
movq %r13, 0xb8(%rsp)
leaq 0x10(%r13), %rsi
movq %r14, %rdi
callq 0x2024e
incl 0x2c(%rsp)
movl 0x3c(%rsp), %eax
cmpl %eax, 0x18(%rbx)
jne 0x25f31
movq 0x10(%rbx), %rax
movq %rax, 0x20(%rsp)
movq 0x70(%rsp), %r13
cmpq 0x78(%rsp), %r13
je 0x25e09
movq 0x20(%rsp), %rax
leaq 0x68(%rax), %r14
movq 0x10(%rsp), %r12
leaq 0x60(%rsp), %rax
movq %rax, 0x50(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
leaq 0x50(%rsp), %rdi
callq 0xa9b2
addq $0x50, %r12
movq %r12, %rdi
leaq 0x50(%rsp), %rsi
callq 0x9c1c
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
leaq 0xf3f7(%rip), %rsi      # 0x35118
movq %r13, %rdx
callq 0x9a1b
movq %r14, %rdi
movq %r12, %rsi
callq 0x1b47a
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x25d63
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x25d7f
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x7390
movq 0x10(%rsp), %r12
leaq 0x98(%rsp), %rax
movq %rax, 0x88(%rsp)
movq (%r13), %rsi
movq 0x8(%r13), %rdx
addq %rsi, %rdx
movq %r15, %rdi
callq 0xa9b2
addq $0x50, %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x9c1c
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r14, %rdi
movq %r13, %rsi
callq 0x9c1c
movsd 0x18(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x25dfa
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x7390
addq $0x20, %r13
cmpq 0x78(%rsp), %r13
jne 0x25cd0
movq 0x20(%rsp), %rdi
callq 0x98bc
movsd %xmm0, 0x18(%rsp)
leaq 0x22c70(%rip), %rax     # 0x48a90
movq (%rax), %rax
movq (%rax), %rdi
movq (%rdi), %rax
callq *(%rax)
movsd %xmm0, 0x20(%rsp)
movq 0x20(%rbp), %rdi
testq %rdi, %rdi
jne 0x25e3e
movq 0x18(%rbp), %rdi
movq 0x30(%rsp), %rax
movq 0x18(%rax), %r14
movq 0x10(%rsp), %rax
movsd 0x10(%rax), %xmm0
movaps %xmm0, 0x140(%rsp)
movsd 0x10(%rbp), %xmm0
movapd %xmm0, 0x150(%rsp)
callq 0x2deb0
movl %eax, %r13d
movq 0x30(%rsp), %rax
movq 0x20(%rax), %rdi
testq %rdi, %rdi
jne 0x25e81
movq 0x18(%rax), %rdi
movq 0x10(%rax), %r12
callq 0x2deb0
xorps %xmm0, %xmm0
cvtsi2sdl 0x2c(%rsp), %xmm0
movsd 0xfec5(%rip), %xmm4     # 0x35d60
movapd 0x140(%rsp), %xmm3
unpcklps %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
movapd 0xfec1(%rip), %xmm2     # 0x35d70
subpd %xmm2, %xmm3
movapd %xmm3, %xmm1
unpckhpd %xmm3, %xmm1            # xmm1 = xmm1[1],xmm3[1]
addsd %xmm3, %xmm1
movapd 0x150(%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 0x20(%rsp), %xmm6
addsd %xmm3, %xmm2
setae %sil
movq %r12, %xmm3
punpckldq %xmm4, %xmm3    # xmm3 = xmm3[0],xmm4[0],xmm3[1],xmm4[1]
subpd 0xfe72(%rip), %xmm3     # 0x35d70
movapd %xmm3, %xmm4
unpckhpd %xmm3, %xmm4            # xmm4 = xmm4[1],xmm3[1]
addsd %xmm3, %xmm4
xorps %xmm3, %xmm3
cvtsi2sd %r13d, %xmm3
cvtsi2sd %eax, %xmm5
movq %r14, %rdi
callq 0x2e116
movsd 0x18(%rsp), %xmm0
ucomisd 0x20(%rsp), %xmm0
movq 0x30(%rsp), %r12
jb 0x25f68
movq 0xb8(%rsp), %r13
movq (%r13), %r13
leaq 0x70(%rsp), %r14
movq %r14, %rdi
callq 0x1a6c6
movq (%rbx), %rbx
cmpq 0x48(%rsp), %rbx
jne 0x25c89
jmp 0x25f9a
leaq 0xffef(%rip), %rdi      # 0x35f50
callq 0x7560
jmp 0x25fd0
movq 0x10(%rsp), %r14
testq %r14, %r14
je 0x25f7a
movq %r14, %rdi
callq 0x21e10
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7390
movq $0x0, 0x10(%rsp)
leaq 0x70(%rsp), %rdi
callq 0x1a6c6
movq 0x10(%rsp), %rax
testq %rax, %rax
movl 0x38(%rsp), %ebx
je 0x25fce
movq 0xa8(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x25fc4
movq %rax, (%rsi)
addq $0x8, 0x8(%rdi)
jmp 0x25fce
leaq 0x10(%rsp), %rdx
callq 0x2730a
incl %ebx
cmpl 0x40(%rsp), %ebx
jl 0x25bc5
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x25ffb
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x7390
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xfbb2(%rip), %rdi      # 0x35bc6
xorl %eax, %eax
callq 0x7530
movl $0x8, %edi
callq 0x7180
leaq 0xfeb2(%rip), %rcx      # 0x35ede
movq %rcx, (%rax)
movq 0x21f02(%rip), %rsi     # 0x47f38
movq %rax, %rdi
xorl %edx, %edx
callq 0x7660
movq %rax, %rbx
movq 0xe0(%rsp), %rdi
cmpq %r15, %rdi
je 0x26123
movq 0xf0(%rsp), %rsi
jmp 0x2607c
jmp 0x260a2
movq %rax, %rbx
movq 0x100(%rsp), %rdi
cmpq %r13, %rdi
je 0x26123
movq 0x110(%rsp), %rsi
incq %rsi
jmp 0x26090
jmp 0x260a2
jmp 0x260a2
movq %rax, %rbx
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7390
jmp 0x26123
jmp 0x260a2
jmp 0x260a2
jmp 0x260ec
jmp 0x260a2
movq %rax, %rbx
jmp 0x26123
movq %rax, %rbx
movq 0xc0(%rsp), %rdi
leaq 0xd0(%rsp), %rax
cmpq %rax, %rdi
je 0x260d6
movq 0xd0(%rsp), %rsi
incq %rsi
callq 0x7390
jmp 0x260d6
jmp 0x260d3
movq %rax, %rbx
movq 0x50(%rsp), %rdi
leaq 0x60(%rsp), %rax
cmpq %rax, %rdi
je 0x26119
movq 0x60(%rsp), %rsi
jmp 0x26111
movq %rax, %rbx
jmp 0x26119
movq %rax, %rbx
movq 0x88(%rsp), %rdi
leaq 0x98(%rsp), %rax
cmpq %rax, %rdi
je 0x26119
movq 0x98(%rsp), %rsi
incq %rsi
callq 0x7390
leaq 0x70(%rsp), %rdi
callq 0x1a6c6
leaq 0x130(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x26144
movq 0x130(%rsp), %rsi
incq %rsi
callq 0x7390
movq %rbx, %rdi
callq 0x76a0