// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl 0x38(%rdi), %esi
imull 0x28(%rdi), %esi
leaq 0xf(%rsp), %rdx
movb $0x1, (%rdx)
leaq 0x20(%rsp), %r14
leaq 0xe(%rsp), %rcx
movq %r14, %rdi
callq 0x15c56
xorl %r12d, %r12d
movsd 0x1020b(%rip), %xmm0    # 0x46f80
movsd %xmm0, 0x10(%rsp)
movl $0x510, %r13d           # imm = 0x510
xorl %r15d, %r15d
movq 0xc8(%rbx), %rsi
movq 0xd0(%rbx), %rcx
movq %rcx, %rax
subq %rsi, %rax
cqto
idivq %r13
cmpq %rax, %r15
jae 0x36e65
movq 0xf8(%rbx), %rax
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x36ba0
ucomisd 0x10(%rsp), %xmm0
jbe 0x36dda
movq 0xf8(%rbx), %rax
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x36ba0
movsd %xmm0, 0x10(%rsp)
movq 0xf8(%rbx), %rax
movl (%rax,%r15,4), %esi
movq %rbx, %rdi
callq 0x36ba0
movsd 0x10193(%rip), %xmm1    # 0x46f88
subsd %xmm0, %xmm1
movapd %xmm1, %xmm0
callq 0x6700
movsd 0x5226(%rip), %xmm2     # 0x3c030
addsd %xmm2, %xmm0
movapd %xmm2, %xmm1
divsd %xmm0, %xmm1
movapd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd 0x5842(%rip), %xmm1     # 0x3c668
callq 0x66c0
movsd %xmm0, 0x18(%rsp)
movq 0x678(%rbx), %rdi
callq 0x38b70
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x36e5d
movq %r14, %rdi
movq %r15, %rsi
callq 0x15cba
notq %rdx
andq %rdx, (%rax)
incl %r12d
incq %r15
jmp 0x36d84
movl 0x38(%rbx), %eax
imull 0x28(%rbx), %eax
cmpl %eax, %r12d
jae 0x36fbf
xorl %r14d, %r14d
movl $0x510, %r13d           # imm = 0x510
leaq 0x20(%rsp), %r15
movq 0x2d10e(%rip), %r12     # 0x63f98
subq %rsi, %rcx
movq %rcx, %rax
cqto
idivq %r13
cmpq %rax, %r14
jae 0x36fbf
movq %r15, %rdi
movq %r14, %rsi
callq 0x15cba
testq %rdx, (%rax)
jne 0x36fa9
movq 0x678(%rbx), %rdi
movl 0x38(%rbx), %esi
imull 0x28(%rbx), %esi
callq 0x38cc4
movslq %eax, %rbp
movq %r15, %rdi
movq %rbp, %rsi
callq 0x15cba
testq %rdx, (%rax)
je 0x36eb2
movq 0x678(%rbx), %rdi
callq 0x38b70
movsd %xmm0, 0x18(%rsp)
movq 0xf8(%rbx), %rax
movl (%rax,%rbp,4), %esi
movq %rbx, %rdi
callq 0x36ba0
subsd 0x10(%rsp), %xmm0
callq 0x6700
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jae 0x36eb2
movq 0xc8(%rbx), %rax
imulq $0x510, %rbp, %rsi      # imm = 0x510
addq %rax, %rsi
imulq $0x510, %r14, %rdi      # imm = 0x510
addq %rax, %rdi
callq 0x33b6a
movq 0xe0(%rbx), %rax
movq %rbp, %rcx
shlq $0x4, %rcx
movq %r14, %rdx
shlq $0x4, %rdx
movupd (%rax,%rcx), %xmm0
movupd %xmm0, (%rax,%rdx)
movq 0xf8(%rbx), %rax
movl (%rax,%rbp,4), %ecx
movl %ecx, (%rax,%r14,4)
incl 0x2d7aa(%rip)           # 0x64710
movq %r12, %rdi
leaq 0x14ab0(%rip), %rsi     # 0x4ba20
callq 0x6430
movq %r12, %rdi
leaq 0x14abf(%rip), %rsi     # 0x4ba3e
callq 0x6430
movl 0x2d786(%rip), %esi     # 0x64710
movq %rax, %rdi
callq 0x62f0
movq %r12, %rdi
leaq 0x14ab6(%rip), %rsi     # 0x4ba52
callq 0x6430
movq %rax, %rdi
callq 0x61c0
incq %r14
movq 0xc8(%rbx), %rsi
movq 0xd0(%rbx), %rcx
jmp 0x36e8a
leaq 0x20(%rsp), %rdi
callq 0x15cd8
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x36fe2
jmp 0x36fe2
jmp 0x36fe2
jmp 0x36fe2
jmp 0x36fe2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x15cd8
movq %rbx, %rdi
callq 0x66e0
nop