// File: 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
movq %rsp, %rbp
subq $0xb0, %rsp
movb %sil, %al
movq %rdi, -0x10(%rbp)
andb $0x1, %al
movb %al, -0x11(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x80(%rbp)
addq $0x78, %rdi
callq 0x5c320
movq -0x80(%rbp), %rdi
movl %eax, -0x18(%rbp)
addq $0x90, %rdi
callq 0x5c320
movq -0x80(%rbp), %rdi
movl %eax, -0x1c(%rbp)
movl -0x18(%rbp), %eax
addl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movl $0x4, %esi
callq 0x69aa0
movl %eax, %ecx
movq -0x80(%rbp), %rax
movl %ecx, -0x28(%rbp)
movl 0x2c(%rax), %eax
subl -0x28(%rbp), %eax
cmpl $0x0, %eax
jl 0x69ecb
movq -0x80(%rbp), %rax
movl 0x2c(%rax), %eax
subl -0x28(%rbp), %eax
movl %eax, -0x84(%rbp)
jmp 0x69ed5
xorl %eax, %eax
movl %eax, -0x84(%rbp)
jmp 0x69ed5
movl -0x84(%rbp), %eax
movl %eax, -0x2c(%rbp)
testb $0x1, -0x11(%rbp)
je 0x69f26
movq -0x80(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jge 0x69efb
movl -0x20(%rbp), %eax
movl %eax, -0x88(%rbp)
jmp 0x69f08
movq -0x80(%rbp), %rax
movl 0x2c(%rax), %eax
movl %eax, -0x88(%rbp)
movl -0x88(%rbp), %eax
movl %eax, -0x24(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
jmp 0x69f6d
cmpl $0x0, -0x2c(%rbp)
jne 0x69f35
movb $0x0, -0x1(%rbp)
jmp 0x6a1e9
movl -0x20(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x69f48
movl -0x20(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x69f51
movl -0x2c(%rbp), %eax
movl %eax, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
movl %eax, -0x24(%rbp)
cvtsi2sdl -0x24(%rbp), %xmm0
cvtsi2sdl -0x20(%rbp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x18(%rbp), %esi
leaq 0xee2d(%rip), %rdi      # 0x78da4
movb $0x0, %al
callq 0x8080
movl -0x1c(%rbp), %esi
leaq 0xee3c(%rip), %rdi      # 0x78dc4
movb $0x0, %al
callq 0x8080
movl $0x0, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
testb $0x1, -0x11(%rbp)
je 0x6a018
movl -0x2c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jge 0x6a018
movl -0x24(%rbp), %eax
subl -0x2c(%rbp), %eax
movl %eax, -0x44(%rbp)
movl $0x0, -0x48(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x44(%rbp), %eax
jge 0x6a016
movq -0x80(%rbp), %rdi
callq 0x69c80
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x69fd9
jmp 0x6a009
movq -0x50(%rbp), %rax
movq %rax, -0x98(%rbp)
cmpq $0x0, %rax
je 0x6a007
movq -0x98(%rbp), %rdi
callq 0x553c0
movq -0x98(%rbp), %rdi
movl $0x158, %esi            # imm = 0x158
callq 0x84d0
jmp 0x6a009
jmp 0x6a00b
movl -0x48(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x69fbb
jmp 0x6a018
movq -0x80(%rbp), %rdi
addq $0x78, %rdi
callq 0x60a70
movq %rax, -0x58(%rbp)
movq -0x80(%rbp), %rdi
addq $0x78, %rdi
callq 0x60900
movq %rax, -0x60(%rbp)
leaq -0x58(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x6ca70
testb $0x1, %al
jne 0x6a04d
jmp 0x6a0a4
leaq 0x40b3c(%rip), %rdi     # 0xaab90
callq 0x19e20
movaps %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6a097
movq -0x80(%rbp), %rax
addq $0x48, %rax
movq %rax, -0xa0(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x6cab0
movq -0xa0(%rbp), %rdi
movq %rax, %rsi
callq 0x5c0f0
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x6a099
leaq -0x58(%rbp), %rdi
callq 0x6cad0
jmp 0x6a029
movq -0x80(%rbp), %rdi
addq $0x90, %rdi
callq 0x60a70
movq %rax, -0x68(%rbp)
movq -0x80(%rbp), %rdi
addq $0x90, %rdi
callq 0x60900
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x6ca70
testb $0x1, %al
jne 0x6a0df
jmp 0x6a136
leaq 0x40aaa(%rip), %rdi     # 0xaab90
callq 0x19e20
movaps %xmm0, %xmm1
movsd -0x38(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6a129
movq -0x80(%rbp), %rax
addq $0x60, %rax
movq %rax, -0xa8(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x6cab0
movq -0xa8(%rbp), %rdi
movq %rax, %rsi
callq 0x5c0f0
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x6a12b
leaq -0x68(%rbp), %rdi
callq 0x6cad0
jmp 0x6a0b8
movl -0x3c(%rbp), %esi
leaq 0xeca6(%rip), %rdi      # 0x78de6
movb $0x0, %al
callq 0x8080
movl -0x40(%rbp), %esi
leaq 0xecb1(%rip), %rdi      # 0x78e02
movb $0x0, %al
callq 0x8080
movq -0x80(%rbp), %rdi
movl $0x4, %esi
callq 0x69aa0
movq -0x80(%rbp), %rcx
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl 0x2c(%rcx), %eax
jle 0x6a1d8
jmp 0x6a177
movq -0x80(%rbp), %rdi
movl $0x4, %esi
callq 0x69aa0
movq -0x80(%rbp), %rcx
cmpl 0x2c(%rcx), %eax
jle 0x6a1d6
movq -0x80(%rbp), %rdi
callq 0x69c80
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x6a1a4
jmp 0x6a1d4
movq -0x78(%rbp), %rax
movq %rax, -0xb0(%rbp)
cmpq $0x0, %rax
je 0x6a1d2
movq -0xb0(%rbp), %rdi
callq 0x553c0
movq -0xb0(%rbp), %rdi
movl $0x158, %esi            # imm = 0x158
callq 0x84d0
jmp 0x6a1d4
jmp 0x6a177
jmp 0x6a1d8
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
cmpl $0x0, %eax
setg %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xb0, %rsp
popq %rbp
retq
nopw (%rax,%rax)