// File: /workspace/github_cpp_makefile_star2_O1/melop[P]admixem/Population.cpp
// Function: Population::ImmigrateConfirm(bool)
bool Population::ImmigrateConfirm(bool bForceExistingDie) {
	int nCacheMaleSize = _mpImCacheMales.size();
	int nCacheFemaleSize = _mpImCacheFemales.size();
	int nTotalCached = nCacheMaleSize + nCacheFemaleSize;
	int nTotalToAdd;
	int nCurrPopSize = this->GetPopSize(4);
	int nRoomLeft = _nPopMaxSize-nCurrPopSize >=0? _nPopMaxSize-nCurrPopSize : 0;
	double nProbToAdd;

	if (bForceExistingDie) {
		nTotalToAdd = nTotalCached < _nPopMaxSize? nTotalCached : _nPopMaxSize;
		nProbToAdd = (double)nTotalToAdd / (double)nTotalCached;
	} else {
		if (nRoomLeft == 0) {
			return false; //no room to add any immigrants
		}
		nTotalToAdd = nTotalCached < nRoomLeft? nTotalCached : nRoomLeft;
		nProbToAdd = (double)nTotalToAdd / (double)nTotalCached;
	}
		//printf("MaxPopSize: %d \n", _nPopMaxSize );
	printf("Candidate immigrating males %d\n", nCacheMaleSize);
	printf("Candidate immigrating females %d\n", nCacheFemaleSize);

	int nAddedMales=0;
	int nAddedFemales=0;

	if (bForceExistingDie && nRoomLeft < nTotalToAdd) {
		int nMakeMoreRoom = nTotalToAdd - nRoomLeft;
		for(int i=0;i< nMakeMoreRoom;i++) {
			Individual * pVictim = this->Emigrate();
			if (!pVictim) {
				
			} else {
				delete pVictim;
			}
		}
	}

	for(vector< Individual * >::iterator it=this->_mpImCacheMales.begin(); it!=this->_mpImCacheMales.end();++it) {
		if (UniformGen.Next() < nProbToAdd) {
			//add

			_mpMales.push_back( *it );
			nAddedMales++;
		}
	}

	for(vector< Individual * >::iterator it=this->_mpImCacheFemales.begin(); it!=this->_mpImCacheFemales.end();++it) {
		if (UniformGen.Next() < nProbToAdd) {
			//add
			_mpFemales.push_back( *it );
			nAddedFemales++;
		}
	}

	printf("Added immigrating males %d\n", nAddedMales);
	printf("Added immigrating females %d\n", nAddedFemales);
	//do final check on pop size:
	nCurrPopSize = this->GetPopSize(4);
	if (nCurrPopSize > _nPopMaxSize) {
		//randomly remove some individuals
		while(this->GetPopSize(4) > _nPopMaxSize) {
			Individual * pVictim = this->Emigrate();
			if (!pVictim) {
				
			} else {
				delete pVictim;
			}
		}
	}

	return ((nAddedMales+nAddedFemales)>0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %esi, %r14d
movq %rdi, %rbx
movq 0x80(%rdi), %rsi
subq 0x78(%rdi), %rsi
movq 0x98(%rdi), %r15
subq 0x90(%rdi), %r15
movq 0x68(%rdi), %rax
subq 0x60(%rdi), %rax
shrq $0x3, %rax
movq 0x50(%rdi), %rcx
subq 0x48(%rdi), %rcx
shrq $0x3, %rcx
addl %eax, %ecx
movl 0x2c(%rdi), %r12d
movl %r12d, %eax
subl %ecx, %eax
xorl %r13d, %r13d
testl %eax, %eax
cmovgl %eax, %r13d
testl %r14d, %r14d
jne 0x2c8bd
movl %r13d, %r12d
testl %eax, %eax
jle 0x2caba
shrq $0x3, %rsi
shrq $0x3, %r15
leal (%r15,%rsi), %eax
cmpl %r12d, %eax
cmovll %eax, %r12d
cvtsi2sd %r12d, %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0xb1c2(%rip), %rdi      # 0x37ab1
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x7060
leaq 0xb1d2(%rip), %rdi      # 0x37ad1
movl %r15d, %esi
xorl %eax, %eax
callq 0x7060
testb %r14b, %r14b
je 0x2c945
subl %r13d, %r12d
jle 0x2c945
jle 0x2c945
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2c716
testq %rax, %rax
je 0x2c93d
movq %rax, %r14
movq %rax, %rdi
callq 0x21dfc
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7390
incl %r15d
cmpl %r12d, %r15d
jl 0x2c918
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x78(%rbx), %r14
cmpq 0x80(%rbx), %r14
je 0x2c9b6
leaq 0x48(%rbx), %r15
xorl %ebp, %ebp
leaq 0x1d0cf(%rip), %r12     # 0x49a40
movq %r12, %rdi
callq 0xbaa8
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2c9a9
movq 0x50(%rbx), %rsi
cmpq 0x58(%rbx), %rsi
je 0x2c99c
movq (%r14), %rax
movq %rax, (%rsi)
addq $0x8, 0x50(%rbx)
jmp 0x2c9a7
movq %r15, %rdi
movq %r14, %rdx
callq 0x275b2
incl %ebp
addq $0x8, %r14
cmpq 0x80(%rbx), %r14
jne 0x2c971
movq 0x90(%rbx), %r15
cmpq 0x98(%rbx), %r15
je 0x2ca1c
leaq 0x60(%rbx), %r12
xorl %r14d, %r14d
leaq 0x1d06c(%rip), %r13     # 0x49a40
movq %r13, %rdi
callq 0xbaa8
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2ca0d
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x2c9ff
movq (%r15), %rax
movq %rax, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x2ca0a
movq %r12, %rdi
movq %r15, %rdx
callq 0x275b2
incl %r14d
addq $0x8, %r15
cmpq 0x98(%rbx), %r15
jne 0x2c9d4
jmp 0x2ca1f
xorl %r14d, %r14d
leaq 0xb0cd(%rip), %rdi      # 0x37af3
movl %ebp, %esi
xorl %eax, %eax
callq 0x7060
leaq 0xb0d9(%rip), %rdi      # 0x37b0f
movl %r14d, %esi
xorl %eax, %eax
callq 0x7060
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
shrq $0x3, %rax
movq 0x50(%rbx), %rcx
subq 0x48(%rbx), %rcx
shrq $0x3, %rcx
addl %eax, %ecx
cmpl %ecx, 0x2c(%rbx)
jge 0x2caa5
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
shrq $0x3, %rax
movq 0x50(%rbx), %rcx
subq 0x48(%rbx), %rcx
shrq $0x3, %rcx
addl %eax, %ecx
cmpl %ecx, 0x2c(%rbx)
jge 0x2caa5
movq %rbx, %rdi
callq 0x2c716
testq %rax, %rax
je 0x2ca5f
movq %rax, %r15
movq %rax, %rdi
callq 0x21dfc
movl $0x158, %esi            # imm = 0x158
movq %r15, %rdi
callq 0x7390
jmp 0x2ca5f
addl %ebp, %r14d
setg %al
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x2caab