// File: /workspace/github_cpp_makefile_star2_O3/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 0x2c451
movl %r13d, %r12d
testl %eax, %eax
jle 0x2c62f
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 0xa636(%rip), %rdi      # 0x36ab9
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x7060
leaq 0xa646(%rip), %rdi      # 0x36ad9
movl %r15d, %esi
xorl %eax, %eax
callq 0x7060
testb %r14b, %r14b
je 0x2c4d9
subl %r13d, %r12d
jle 0x2c4d9
jle 0x2c4d9
xorl %r15d, %r15d
movq %rbx, %rdi
callq 0x2c2aa
testq %rax, %rax
je 0x2c4d1
movq %rax, %r14
movq %rax, %rdi
callq 0x21e10
movl $0x158, %esi            # imm = 0x158
movq %r14, %rdi
callq 0x7390
incl %r15d
cmpl %r12d, %r15d
jl 0x2c4ac
movsd 0x8(%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x78(%rbx), %r14
cmpq 0x80(%rbx), %r14
je 0x2c54a
leaq 0x48(%rbx), %r15
xorl %ebp, %ebp
leaq 0x1c53b(%rip), %r12     # 0x48a40
movq %r12, %rdi
callq 0xbba0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2c53d
movq 0x50(%rbx), %rsi
cmpq 0x58(%rbx), %rsi
je 0x2c530
movq (%r14), %rax
movq %rax, (%rsi)
addq $0x8, 0x50(%rbx)
jmp 0x2c53b
movq %r15, %rdi
movq %r14, %rdx
callq 0x2730a
incl %ebp
addq $0x8, %r14
cmpq 0x80(%rbx), %r14
jne 0x2c505
movq 0x90(%rbx), %r15
cmpq 0x98(%rbx), %r15
je 0x2c5b0
leaq 0x60(%rbx), %r12
xorl %r14d, %r14d
leaq 0x1c4d8(%rip), %r13     # 0x48a40
movq %r13, %rdi
callq 0xbba0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x2c5a1
movq 0x68(%rbx), %rsi
cmpq 0x70(%rbx), %rsi
je 0x2c593
movq (%r15), %rax
movq %rax, (%rsi)
addq $0x8, 0x68(%rbx)
jmp 0x2c59e
movq %r12, %rdi
movq %r15, %rdx
callq 0x2730a
incl %r14d
addq $0x8, %r15
cmpq 0x98(%rbx), %r15
jne 0x2c568
jmp 0x2c5b3
xorl %r14d, %r14d
leaq 0xa541(%rip), %rdi      # 0x36afb
movl %ebp, %esi
xorl %eax, %eax
callq 0x7060
leaq 0xa54d(%rip), %rdi      # 0x36b17
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 0x2c61a
movq %rbx, %rdi
callq 0x2c2aa
testq %rax, %rax
je 0x2c5d4
movq %rax, %r15
movq %rax, %rdi
callq 0x21e10
movl $0x158, %esi            # imm = 0x158
movq %r15, %rdi
callq 0x7390
jmp 0x2c5d4
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 0x2c620
nop