// File: j-faria[P]kima/examples/trends/../../DNest4/code/SamplerImpl.h
// Function: DNest4::Sampler<RVmodel>::kill_lagging_particles()
void Sampler<ModelType>::kill_lagging_particles()
{
	// Count the number of deletions
	static unsigned int deletions = 0;

	// Flag each particle as good or bad
	std::vector<bool> good(num_threads*options.num_particles, true);

	// How good is the best particle?
	double max_log_push = -std::numeric_limits<double>::max();

	unsigned int num_bad = 0;
    double kill_probability = 0.0;
	for(size_t i=0; i<particles.size(); ++i)
	{
		if(log_push(level_assignments[i]) > max_log_push)
			max_log_push = log_push(level_assignments[i]);
        kill_probability = pow(1.0 - 1.0/(1.0 + exp(-log_push(level_assignments[i]) - 4.0)), 3);

		if(rngs[0].rand() <= kill_probability)
		{
			good[i] = false;
			++num_bad;
		}
	}

	if(num_bad < num_threads*options.num_particles)
	{
		// Replace bad particles with copies of good ones
		for(size_t i=0; i<particles.size(); ++i)
		{
			if(!good[i])
			{
				// Choose a replacement particle. Higher prob
				// of selecting better particles.
				int i_copy;
				do
				{
					i_copy = rngs[0].rand_int(num_threads*options.num_particles);
				}while(!good[i_copy] ||
			        rngs[0].rand() >= exp(log_push(level_assignments[i_copy]) - max_log_push));

				particles[i] = particles[i_copy];
				log_likelihoods[i] = log_likelihoods[i_copy];
				level_assignments[i] = level_assignments[i_copy];
				++deletions;

				std::cout<<"# Replacing lagging particle.";
				std::cout<<" This has happened "<<deletions;
				std::cout<<" times."<<std::endl;
			}
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x180, %rsp            # imm = 0x180
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rcx
movq %rcx, -0xc0(%rbp)
movl 0x28(%rcx), %eax
movl 0x38(%rcx), %ecx
imull %ecx, %eax
movq %rax, -0xb8(%rbp)
movb $0x1, -0x31(%rbp)
leaq -0x32(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x32b80
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rcx
leaq -0x30(%rbp), %rdi
leaq -0x31(%rbp), %rdx
callq 0x32ba0
jmp 0xad40b
leaq -0x32(%rbp), %rdi
callq 0x32c20
callq 0xaf880
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x68(%rbp), %rax
movq %rax, -0xc8(%rbp)
addq $0xc8, %rdi
callq 0xa8a50
movq %rax, %rcx
movq -0xc8(%rbp), %rax
cmpq %rcx, %rax
jae 0xad637
movq -0xc0(%rbp), %rdi
addq $0xf8, %rdi
movq -0x68(%rbp), %rsi
callq 0xabd30
movq -0xc0(%rbp), %rdi
movl (%rax), %esi
callq 0xacd30
movsd %xmm0, -0xd0(%rbp)
jmp 0xad4ac
movsd -0xd0(%rbp), %xmm0
ucomisd -0x50(%rbp), %xmm0
jbe 0xad52d
movq -0xc0(%rbp), %rdi
addq $0xf8, %rdi
movq -0x68(%rbp), %rsi
callq 0xabd30
movq -0xc0(%rbp), %rdi
movl (%rax), %esi
callq 0xacd30
movsd %xmm0, -0xd8(%rbp)
jmp 0xad4ea
movsd -0xd8(%rbp), %xmm0
movsd %xmm0, -0x50(%rbp)
jmp 0xad52d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x32(%rbp), %rdi
callq 0x32c20
jmp 0xad989
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x32d20
jmp 0xad989
movq -0xc0(%rbp), %rdi
addq $0xf8, %rdi
movq -0x68(%rbp), %rsi
callq 0xabd30
movq -0xc0(%rbp), %rdi
movl (%rax), %esi
callq 0xacd30
movsd %xmm0, -0xe0(%rbp)
jmp 0xad55c
movsd -0xe0(%rbp), %xmm1
movsd 0x183a4(%rip), %xmm0    # 0xc5910
subsd %xmm1, %xmm0
callq 0x6870
movaps %xmm0, %xmm2
movsd 0x9ab0(%rip), %xmm0     # 0xb7030
addsd %xmm0, %xmm2
movaps %xmm0, %xmm1
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0xa169(%rip), %xmm1     # 0xb7700
callq 0x6820
movq -0xc0(%rbp), %rdi
movsd %xmm0, -0x60(%rbp)
addq $0x678, %rdi            # imm = 0x678
xorl %eax, %eax
movl %eax, %esi
callq 0xa8770
movq %rax, %rdi
callq 0xb26f0
movsd %xmm0, -0xe8(%rbp)
jmp 0xad5ca
movsd -0xe8(%rbp), %xmm1
movsd -0x60(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jb 0xad624
movq -0x68(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x32c40
movq %rdx, -0xf8(%rbp)
movq %rax, -0xf0(%rbp)
jmp 0xad5fa
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movq %rax, -0x70(%rbp)
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
callq 0x32ca0
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0xad626
movq -0x68(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x68(%rbp)
jmp 0xad44c
movq -0xc0(%rbp), %rdx
movl -0x54(%rbp), %eax
movl 0x28(%rdx), %ecx
imull 0x38(%rdx), %ecx
cmpl %ecx, %eax
jae 0xad977
movq $0x0, -0x80(%rbp)
movq -0xc0(%rbp), %rdi
movq -0x80(%rbp), %rax
movq %rax, -0x100(%rbp)
addq $0xc8, %rdi
callq 0xa8a50
movq %rax, %rcx
movq -0x100(%rbp), %rax
cmpq %rcx, %rax
jae 0xad975
movq -0x80(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x32c40
movq %rdx, -0x110(%rbp)
movq %rax, -0x108(%rbp)
jmp 0xad6a6
movq -0x110(%rbp), %rax
movq -0x108(%rbp), %rcx
movq %rcx, -0x90(%rbp)
movq %rax, -0x88(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x32cf0
xorb $-0x1, %al
testb $0x1, %al
jne 0xad6d9
jmp 0xad962
jmp 0xad6db
movq -0xc0(%rbp), %rdi
addq $0x678, %rdi            # imm = 0x678
xorl %eax, %eax
movl %eax, %esi
callq 0xa8770
movq %rax, %rdi
movq -0xc0(%rbp), %rax
movl 0x28(%rax), %esi
movl 0x38(%rax), %eax
imull %eax, %esi
callq 0xb28c0
movl %eax, -0x114(%rbp)
jmp 0xad712
movl -0x114(%rbp), %eax
movl %eax, -0x94(%rbp)
movslq -0x94(%rbp), %rsi
leaq -0x30(%rbp), %rdi
callq 0x32c40
movq %rdx, -0x128(%rbp)
movq %rax, -0x120(%rbp)
jmp 0xad73e
movq -0x128(%rbp), %rax
movq -0x120(%rbp), %rcx
movq %rcx, -0xa8(%rbp)
movq %rax, -0xa0(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x32cf0
movb %al, %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x129(%rbp)
jne 0xad77a
jmp 0xad7ff
movq -0xc0(%rbp), %rdi
addq $0x678, %rdi            # imm = 0x678
xorl %eax, %eax
movl %eax, %esi
callq 0xa8770
movq %rax, %rdi
callq 0xb26f0
movsd %xmm0, -0x138(%rbp)
jmp 0xad7a3
movq -0xc0(%rbp), %rdi
addq $0xf8, %rdi
movslq -0x94(%rbp), %rsi
callq 0xabd30
movq -0xc0(%rbp), %rdi
movl (%rax), %esi
callq 0xacd30
movsd %xmm0, -0x140(%rbp)
jmp 0xad7d5
movsd -0x140(%rbp), %xmm0
subsd -0x50(%rbp), %xmm0
callq 0x6870
movaps %xmm0, %xmm1
movsd -0x138(%rbp), %xmm0
ucomisd %xmm1, %xmm0
setae %al
movb %al, -0x129(%rbp)
movb -0x129(%rbp), %al
testb $0x1, %al
jne 0xad6db
movq -0xc0(%rbp), %rdi
addq $0xc8, %rdi
movq %rdi, -0x150(%rbp)
movslq -0x94(%rbp), %rsi
callq 0xa1e50
movq -0x150(%rbp), %rdi
movq %rax, -0x148(%rbp)
movq -0x80(%rbp), %rsi
callq 0xa1e50
movq -0x148(%rbp), %rsi
movq %rax, %rdi
callq 0xa1eb0
jmp 0xad856
movq -0xc0(%rbp), %rdi
addq $0xe0, %rdi
movq %rdi, -0x170(%rbp)
movslq -0x94(%rbp), %rsi
callq 0xa2260
movq -0x170(%rbp), %rdi
movq %rax, -0x168(%rbp)
movq -0x80(%rbp), %rsi
callq 0xa2260
movq -0x168(%rbp), %rcx
movq -0xc0(%rbp), %rdi
movups (%rcx), %xmm0
movups %xmm0, (%rax)
addq $0xf8, %rdi
movq %rdi, -0x160(%rbp)
movslq -0x94(%rbp), %rsi
callq 0xabd30
movq -0x160(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x154(%rbp)
movq -0x80(%rbp), %rsi
callq 0xabd30
movl -0x154(%rbp), %ecx
movl %ecx, (%rax)
movl 0x6cf26(%rip), %eax     # 0x11a808
incl %eax
movl %eax, 0x6cf1e(%rip)     # 0x11a808
movq 0x6c6a7(%rip), %rdi     # 0x119f98
leaq 0x1de48(%rip), %rsi     # 0xcb740
callq 0x64f0
jmp 0xad8ff
movq 0x6c692(%rip), %rdi     # 0x119f98
leaq 0x1de51(%rip), %rsi     # 0xcb75e
callq 0x64f0
movq %rax, -0x178(%rbp)
jmp 0xad91b
movq -0x178(%rbp), %rdi
movl 0x6cee0(%rip), %esi     # 0x11a808
callq 0x6650
jmp 0xad92f
movq 0x6c662(%rip), %rdi     # 0x119f98
leaq 0x1de35(%rip), %rsi     # 0xcb772
callq 0x64f0
movq %rax, -0x180(%rbp)
jmp 0xad94b
movq -0x180(%rbp), %rdi
movq 0x6c5bf(%rip), %rsi     # 0x119f18
callq 0x6530
jmp 0xad960
jmp 0xad962
jmp 0xad964
movq -0x80(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x80(%rbp)
jmp 0xad658
jmp 0xad977
leaq -0x30(%rbp), %rdi
callq 0x32d20
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x6830
nopw %cs:(%rax,%rax)
nopl (%rax)