// File: /workspace/github_cpp_makefile_star2_O2/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, %ebp
movq %rdi, %rbx
movq 0x80(%rdi), %r12
movq 0x98(%rdi), %r15
subq 0x78(%rdi), %r12
subq 0x90(%rdi), %r15
pushq $0x4
popq %rsi
callq 0x27d3c
movl 0x2c(%rbx), %r13d
movl %r13d, %ecx
subl %eax, %ecx
xorl %r14d, %r14d
testl %ecx, %ecx
cmovgl %ecx, %r14d
testl %ebp, %ebp
jne 0x27f9d
movl %r14d, %r13d
testl %ecx, %ecx
jle 0x28150
shrq $0x3, %r12
shrq $0x3, %r15
leal (%r15,%r12), %eax
cmpl %r13d, %eax
cmovll %eax, %r13d
cvtsi2sd %r13d, %xmm0
movsd %xmm0, (%rsp)
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x7abe(%rip), %rdi      # 0x2fa89
movl %ebp, 0xc(%rsp)
xorl %ebp, %ebp
movl %r12d, %esi
xorl %eax, %eax
callq 0x7050
leaq 0x7ac7(%rip), %rdi      # 0x2faa9
movl %r15d, %esi
xorl %eax, %eax
callq 0x7050
cmpb $0x0, 0xc(%rsp)
je 0x28034
subl %r14d, %r13d
jle 0x28034
xorl %r15d, %r15d
testl %r13d, %r13d
cmovgl %r13d, %r15d
negl %r15d
testl %r15d, %r15d
je 0x28034
movq %rbx, %rdi
callq 0x27e50
testq %rax, %rax
je 0x2802f
movq %rax, %r14
movq %rax, %rdi
callq 0x1fc0e
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7400
incl %r15d
jmp 0x28005
movsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x78(%rbx), %r14
leaq 0x48(%rbx), %r15
leaq 0x1da1d(%rip), %r12     # 0x45a70
cmpq 0x80(%rbx), %r14
je 0x28082
movq %r12, %rdi
callq 0xb2e2
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2807c
movq %r15, %rdi
movq %r14, %rsi
callq 0x2320e
incl %ebp
addq $0x8, %r14
jmp 0x28053
movq 0x90(%rbx), %r15
leaq 0x60(%rbx), %r12
xorl %r14d, %r14d
leaq 0x1d9d9(%rip), %r13     # 0x45a70
cmpq 0x98(%rbx), %r15
je 0x280c7
movq %r13, %rdi
callq 0xb2e2
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x280c1
movq %r12, %rdi
movq %r15, %rsi
callq 0x2320e
incl %r14d
addq $0x8, %r15
jmp 0x28097
leaq 0x79fd(%rip), %rdi      # 0x2facb
movl %ebp, %esi
xorl %eax, %eax
callq 0x7050
leaq 0x7a09(%rip), %rdi      # 0x2fae7
movl %r14d, %esi
xorl %eax, %eax
callq 0x7050
pushq $0x4
popq %rsi
movq %rbx, %rdi
callq 0x27d3c
movl 0x2c(%rbx), %r12d
cmpl %r12d, %eax
jle 0x2813b
pushq $0x4
popq %r15
movq %rbx, %rdi
movl %r15d, %esi
callq 0x27d3c
cmpl %r12d, %eax
jle 0x2813b
movq %rbx, %rdi
callq 0x27e50
testq %rax, %rax
je 0x28135
movq %rax, %r12
movq %rax, %rdi
callq 0x1fc0e
movl $0x158, %esi            # imm = 0x158
movq %r12, %rdi
callq 0x7400
movl 0x2c(%rbx), %r12d
jmp 0x28100
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 0x28141