// File: j-faria[P]kima/examples/trends/../../DNest4/code/SamplerImpl.h
// Function: DNest4::Sampler<RVmodel>::do_bookkeeping()
void Sampler<ModelType>::do_bookkeeping()
{
	bool created_level = false;

	// Create a new level?
	if(!enough_levels(levels) &&
        (all_above.size() >= options.new_level_interval))
	{
		// Create the level
		std::sort(all_above.begin(), all_above.end());
		int index = static_cast<int>((1. - 1./compression)*all_above.size());
		std::cout<<"# Creating level "<<levels.size()<<" with log likelihood = ";
		std::cout<<std::setprecision(12);
		std::cout<<all_above[index].get_value()<<"."<<std::endl;

		levels.push_back(Level(all_above[index]));
		all_above.erase(all_above.begin(), all_above.begin() + index + 1);
		for(auto& a:above)
			a.clear();

		// If last level
		if(enough_levels(levels))
		{
            // Regularisation
            double reg = options.new_level_interval*sqrt(options.lambda);
			Level::renormalise_visits(levels, static_cast<int>(reg));
			all_above.clear();
            options.max_num_levels = levels.size();
            std::cout<<"# Done creating levels."<<std::endl;
		}
		else
		{
			// If it's not the last level, look for lagging particles
			kill_lagging_particles();
		}

		created_level = true;
	}

	// Recalculate log_X values of levels
	Level::recalculate_log_X(levels, compression,
                        options.new_level_interval*sqrt(options.lambda));

	// Save levels if one was created
	if(created_level)
		save_levels();

	if(count_mcmc_steps_since_save >= options.save_interval)
	{
		save_particle();

		// If a level was not created, save anyway because of the time
		if(!created_level)
			save_levels();
	}

    // Check for a new record holder
    auto indices = argsort(log_likelihoods);
    if(best_ever_log_likelihood < log_likelihoods[indices.back()])
    {
        best_ever_particle = particles[indices.back()];
        best_ever_log_likelihood = log_likelihoods[indices.back()];
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
movb $0x0, -0x9(%rbp)
movq %rdi, %rsi
addq $0x630, %rsi            # imm = 0x630
callq 0xabb20
testb $0x1, %al
jne 0xaaaf4
movq -0xf0(%rbp), %rdi
addq $0x660, %rdi            # imm = 0x660
callq 0xa4e70
movq -0xf0(%rbp), %rcx
movl 0x3c(%rcx), %ecx
cmpq %rcx, %rax
jb 0xaaaf4
movq -0xf0(%rbp), %rdi
addq $0x660, %rdi            # imm = 0x660
movq %rdi, -0x118(%rbp)
callq 0xaa590
movq -0x118(%rbp), %rdi
movq %rax, -0x18(%rbp)
callq 0xaa5c0
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
callq 0xad230
movq -0x118(%rbp), %rdi
movq -0xf0(%rbp), %rax
movsd 0x30(%rax), %xmm2
movsd 0xc87f(%rip), %xmm0     # 0xb7030
movaps %xmm0, %xmm1
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, -0x110(%rbp)
callq 0xa4e70
movsd -0x110(%rbp), %xmm0
movq %rax, %xmm1
movaps 0xc833(%rip), %xmm2     # 0xb7010
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
movapd 0xc837(%rip), %xmm2     # 0xb7020
subpd %xmm2, %xmm1
movaps %xmm1, %xmm2
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, -0x24(%rbp)
movq 0x6f78e(%rip), %rdi     # 0x119f98
leaq 0x20eed(%rip), %rsi     # 0xcb6fe
callq 0x64f0
movq -0xf0(%rbp), %rdi
movq %rax, -0x108(%rbp)
addq $0x630, %rdi            # imm = 0x630
callq 0xa41f0
movq -0x108(%rbp), %rdi
movq %rax, %rsi
callq 0x6110
movq %rax, %rdi
leaq 0x20ec7(%rip), %rsi     # 0xcb710
callq 0x64f0
movl $0xc, %edi
callq 0xa9350
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %esi
movq 0x6f733(%rip), %rdi     # 0x119f98
callq 0x6750
movq -0xf0(%rbp), %rdi
addq $0x660, %rdi            # imm = 0x660
movslq -0x24(%rbp), %rsi
callq 0xa2260
movq %rax, %rdi
callq 0xa93a0
movq 0x6f708(%rip), %rdi     # 0x119f98
callq 0x68f0
movq %rax, %rdi
leaq 0x21718(%rip), %rsi     # 0xcbfb7
callq 0x64f0
movq %rax, %rdi
movq 0x6f66a(%rip), %rsi     # 0x119f18
callq 0x6530
movq -0xf0(%rbp), %rdi
movq %rdi, %rax
addq $0x630, %rax            # imm = 0x630
movq %rax, -0x100(%rbp)
addq $0x660, %rdi            # imm = 0x660
movslq -0x24(%rbp), %rsi
callq 0xa2260
movq %rax, %rsi
leaq -0x60(%rbp), %rdi
callq 0xb1390
movq -0x100(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0xad270
movq -0xf0(%rbp), %rdi
movq %rdi, %rax
addq $0x660, %rax            # imm = 0x660
movq %rax, -0xf8(%rbp)
addq $0x660, %rdi            # imm = 0x660
callq 0xaa590
movq %rax, -0x70(%rbp)
leaq -0x68(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xad330
movq -0xf0(%rbp), %rdi
addq $0x660, %rdi            # imm = 0x660
callq 0xaa590
movq %rax, -0x90(%rbp)
movslq -0x24(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0xad370
movq %rax, -0x88(%rbp)
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0xad370
movq %rax, -0x80(%rbp)
leaq -0x78(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xad330
movq -0xf8(%rbp), %rdi
movq -0x68(%rbp), %rsi
movq -0x78(%rbp), %rdx
callq 0xad2a0
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq %rcx, -0x98(%rbp)
addq $0x6a0, %rax            # imm = 0x6A0
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xa8890
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0xa88c0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0xa88f0
testb $0x1, %al
jne 0xaa9ee
jmp 0xaaa1b
leaq -0xa8(%rbp), %rdi
callq 0xa8930
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rdi
callq 0xaa6e0
leaq -0xa8(%rbp), %rdi
callq 0xa8950
jmp 0xaa9d5
movq -0xf0(%rbp), %rdi
movq %rdi, %rsi
addq $0x630, %rsi            # imm = 0x630
callq 0xabb20
testb $0x1, %al
jne 0xaaa3a
jmp 0xaaae4
movq -0xf0(%rbp), %rax
movl 0x3c(%rax), %ecx
cvtsi2sd %rcx, %xmm0
movsd %xmm0, -0x120(%rbp)
movsd 0x50(%rax), %xmm0
callq 0x6410
movq -0xf0(%rbp), %rdi
movaps %xmm0, %xmm1
movsd -0x120(%rbp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
addq $0x630, %rdi            # imm = 0x630
cvttsd2si -0xc0(%rbp), %esi
callq 0xb1570
movq -0xf0(%rbp), %rdi
addq $0x660, %rdi            # imm = 0x660
callq 0xaa6e0
movq -0xf0(%rbp), %rdi
addq $0x630, %rdi            # imm = 0x630
callq 0xa41f0
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movl %ecx, 0x48(%rax)
movq 0x6f4d1(%rip), %rdi     # 0x119f98
leaq 0x20c5a(%rip), %rsi     # 0xcb728
callq 0x64f0
movq %rax, %rdi
movq 0x6f43b(%rip), %rsi     # 0x119f18
callq 0x6530
jmp 0xaaaf0
movq -0xf0(%rbp), %rdi
callq 0xad3b0
movb $0x1, -0x9(%rbp)
movq -0xf0(%rbp), %rax
movq %rax, %rcx
addq $0x630, %rcx            # imm = 0x630
movq %rcx, -0x130(%rbp)
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0x128(%rbp)
movl 0x3c(%rax), %ecx
cvtsi2sd %rcx, %xmm0
movsd %xmm0, -0x138(%rbp)
movsd 0x50(%rax), %xmm0
callq 0x6410
movsd -0x138(%rbp), %xmm1
movq -0x130(%rbp), %rdi
movaps %xmm0, %xmm2
movsd -0x128(%rbp), %xmm0
mulsd %xmm2, %xmm1
cvttsd2si %xmm1, %rax
movl %eax, %esi
callq 0xb13e0
testb $0x1, -0x9(%rbp)
je 0xaab6f
movq -0xf0(%rbp), %rdi
callq 0xa8f50
movq -0xf0(%rbp), %rcx
movl 0x694(%rcx), %eax
cmpl 0x40(%rcx), %eax
jb 0xaaba1
movq -0xf0(%rbp), %rdi
callq 0xad9a0
testb $0x1, -0x9(%rbp)
jne 0xaab9f
movq -0xf0(%rbp), %rdi
callq 0xa8f50
jmp 0xaaba1
movq -0xf0(%rbp), %rsi
addq $0xe0, %rsi
movq %rsi, -0x150(%rbp)
leaq -0xd8(%rbp), %rdi
movq %rdi, -0x158(%rbp)
callq 0xa1d10
movq -0xf0(%rbp), %rax
movq -0x158(%rbp), %rdi
addq $0x620, %rax            # imm = 0x620
movq %rax, -0x148(%rbp)
callq 0xa1e70
movq -0x150(%rbp), %rdi
movq (%rax), %rsi
callq 0xa2260
movq -0x148(%rbp), %rdi
movq %rax, %rsi
callq 0xb1a20
movb %al, -0x139(%rbp)
jmp 0xaac0f
movb -0x139(%rbp), %al
testb $0x1, %al
jne 0xaac1e
jmp 0xaacd5
movq -0xf0(%rbp), %rax
addq $0xc8, %rax
movq %rax, -0x160(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa1e70
movq -0x160(%rbp), %rdi
movq (%rax), %rsi
callq 0xa1e50
movq -0xf0(%rbp), %rdi
movq %rax, %rsi
addq $0x110, %rdi            # imm = 0x110
callq 0xa1eb0
jmp 0xaac65
movq -0xf0(%rbp), %rax
addq $0xe0, %rax
movq %rax, -0x168(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa1e70
movq -0x168(%rbp), %rdi
movq (%rax), %rsi
callq 0xa2260
movq %rax, %rcx
movq -0xf0(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, 0x620(%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x628(%rax)
jmp 0xaacd5
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xe0(%rbp)
movl %eax, -0xe4(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0xa2280
jmp 0xaacea
leaq -0xd8(%rbp), %rdi
callq 0xa2280
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0xe0(%rbp), %rdi
callq 0x6830
nopw %cs:(%rax,%rax)