// File: j-faria[P]kima/examples/trends/../../DNest4/code/SamplerImpl.h
// Function: DNest4::Sampler<RVmodel>::run_thread(unsigned int)
void Sampler<ModelType>::run_thread(unsigned int thread)
{
	// Alternate between MCMC and bookkeeping
	while(true)
	{
		// Thread zero takes full responsibility for some tasks
		// Setting up copies of levels
		if(thread == 0)
		{
			// Each thread will write over its own copy of the levels
			for(unsigned int i=0; i<num_threads; ++i)
				copies_of_levels[i] = levels;
		}

#ifndef NO_THREADS
		// Wait for all threads to get here before proceeding
		barrier->wait();
#endif

		// Check for termination
		if(options.max_num_saves != 0 && count_saves != 0 &&
				(count_saves%options.max_num_saves == 0))
			return;

		// Do the MCMC (all threads do this!)
		mcmc_thread(thread);

#ifndef NO_THREADS
		barrier->wait();
#endif

		// Thread zero takes full responsibility for some tasks
		if(thread == 0)
		{
			// Count the MCMC steps done
			count_mcmc_steps += num_threads*options.thread_steps;
            count_mcmc_steps_since_save += num_threads*options.thread_steps;

			// Go through copies of levels and apply diffs to levels
			std::vector<Level> levels_orig = levels;
			for(const auto& _levels: copies_of_levels)
			{
				for(size_t i=0; i<levels.size(); ++i)
				{
					levels[i].increment_accepts(_levels[i].get_accepts()
														- levels_orig[i].get_accepts());
					levels[i].increment_tries(_levels[i].get_tries()
														- levels_orig[i].get_tries());
					levels[i].increment_visits(_levels[i].get_visits()
														- levels_orig[i].get_visits());
					levels[i].increment_exceeds(_levels[i].get_exceeds()
														- levels_orig[i].get_exceeds());
				}
			}

			// Combine into a single vector
			for(auto& a: above)
			{
				for(const auto& element: a)
					all_above.push_back(element);
				a.clear();
			}

			// Do the bookkeeping
			do_bookkeeping();
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xa8(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0xa966d
movl $0x0, -0x10(%rbp)
movq -0xa8(%rbp), %rcx
movl -0x10(%rbp), %eax
cmpl 0x28(%rcx), %eax
jae 0xa966b
movq -0xa8(%rbp), %rdi
movq %rdi, %rax
addq $0x630, %rax            # imm = 0x630
movq %rax, -0xb0(%rbp)
addq $0x648, %rdi            # imm = 0x648
movl -0x10(%rbp), %eax
movl %eax, %esi
callq 0xaa000
movq -0xb0(%rbp), %rsi
movq %rax, %rdi
callq 0xaa020
movl -0x10(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10(%rbp)
jmp 0xa961a
jmp 0xa966d
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xb0450
movq -0xa8(%rbp), %rax
cmpl $0x0, 0x60(%rax)
je 0xa96ba
movq -0xa8(%rbp), %rax
cmpl $0x0, 0x690(%rax)
je 0xa96ba
movq -0xa8(%rbp), %rcx
movl 0x690(%rcx), %eax
xorl %edx, %edx
divl 0x60(%rcx)
cmpl $0x0, %edx
jne 0xa96ba
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
movq -0xa8(%rbp), %rdi
movl -0xc(%rbp), %esi
callq 0xaa270
movq -0xa8(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xb0450
cmpl $0x0, -0xc(%rbp)
jne 0xa9a7d
movq -0xa8(%rbp), %rsi
movl 0x28(%rsi), %eax
imull 0x44(%rsi), %eax
movl %eax, %eax
addq 0x698(%rsi), %rax
movq %rax, 0x698(%rsi)
movl 0x28(%rsi), %eax
imull 0x44(%rsi), %eax
addl 0x694(%rsi), %eax
movl %eax, 0x694(%rsi)
addq $0x630, %rsi            # imm = 0x630
leaq -0x28(%rbp), %rdi
callq 0xa4100
movq -0xa8(%rbp), %rax
addq $0x648, %rax            # imm = 0x648
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
callq 0xaa3f0
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rdi
callq 0xaa420
movq %rax, -0x40(%rbp)
leaq -0x38(%rbp), %rdi
leaq -0x40(%rbp), %rsi
callq 0xaa450
testb $0x1, %al
jne 0xa9765
jmp 0xa9978
leaq -0x38(%rbp), %rdi
callq 0xaa490
movq %rax, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq -0xa8(%rbp), %rdi
movq -0x50(%rbp), %rax
movq %rax, -0xb8(%rbp)
addq $0x630, %rdi            # imm = 0x630
callq 0xa41f0
movq %rax, %rcx
movq -0xb8(%rbp), %rax
cmpq %rcx, %rax
jae 0xa9968
movq -0xa8(%rbp), %rdi
addq $0x630, %rdi            # imm = 0x630
movq -0x50(%rbp), %rsi
callq 0xaa4b0
movq %rax, -0xc0(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xaa4f0
movq %rax, %rdi
callq 0xa93e0
movl %eax, -0xc4(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xaa4b0
movq %rax, %rdi
callq 0xa93e0
movl -0xc4(%rbp), %esi
movq -0xc0(%rbp), %rdi
subl %eax, %esi
callq 0xaa4d0
jmp 0xa980f
movq -0xa8(%rbp), %rdi
addq $0x630, %rdi            # imm = 0x630
movq -0x50(%rbp), %rsi
callq 0xaa4b0
movq %rax, -0xd0(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xaa4f0
movq %rax, %rdi
callq 0xa9400
movl %eax, -0xd4(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xaa4b0
movq %rax, %rdi
callq 0xa9400
movl -0xd4(%rbp), %esi
movq -0xd0(%rbp), %rdi
subl %eax, %esi
callq 0xaa510
jmp 0xa9873
movq -0xa8(%rbp), %rdi
addq $0x630, %rdi            # imm = 0x630
movq -0x50(%rbp), %rsi
callq 0xaa4b0
movq %rax, -0xe0(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xaa4f0
movq %rax, %rdi
callq 0xa9440
movl %eax, -0xe4(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xaa4b0
movq %rax, %rdi
callq 0xa9440
movl -0xe4(%rbp), %esi
movq -0xe0(%rbp), %rdi
subl %eax, %esi
callq 0xaa530
jmp 0xa98d7
movq -0xa8(%rbp), %rdi
addq $0x630, %rdi            # imm = 0x630
movq -0x50(%rbp), %rsi
callq 0xaa4b0
movq %rax, -0xf0(%rbp)
movq -0x48(%rbp), %rdi
movq -0x50(%rbp), %rsi
callq 0xaa4f0
movq %rax, %rdi
callq 0xa9420
movl %eax, -0xf4(%rbp)
movq -0x50(%rbp), %rsi
leaq -0x28(%rbp), %rdi
callq 0xaa4b0
movq %rax, %rdi
callq 0xa9420
movl -0xf4(%rbp), %esi
movq -0xf0(%rbp), %rdi
subl %eax, %esi
callq 0xaa550
jmp 0xa993b
jmp 0xa993d
movq -0x50(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x50(%rbp)
jmp 0xa977a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x6e1a0
jmp 0xa9a82
jmp 0xa996a
leaq -0x38(%rbp), %rdi
callq 0xaa570
jmp 0xa974f
movq -0xa8(%rbp), %rax
addq $0x6a0, %rax            # imm = 0x6A0
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0xa8890
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0xa88c0
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0xa88f0
testb $0x1, %al
jne 0xa99b9
jmp 0xa9a66
leaq -0x70(%rbp), %rdi
callq 0xa8930
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x88(%rbp), %rdi
callq 0xaa590
movq %rax, -0x90(%rbp)
movq -0x88(%rbp), %rdi
callq 0xaa5c0
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xaa5f0
testb $0x1, %al
jne 0xa9a10
jmp 0xa9a4f
leaq -0x90(%rbp), %rdi
callq 0xaa630
movq -0xa8(%rbp), %rdi
movq %rax, -0xa0(%rbp)
addq $0x660, %rdi            # imm = 0x660
movq -0xa0(%rbp), %rsi
callq 0xaa650
jmp 0xa9a3f
jmp 0xa9a41
leaq -0x90(%rbp), %rdi
callq 0xaa6c0
jmp 0xa99f7
movq -0x80(%rbp), %rdi
callq 0xaa6e0
leaq -0x70(%rbp), %rdi
callq 0xa8950
jmp 0xa99a3
movq -0xa8(%rbp), %rdi
callq 0xaa700
jmp 0xa9a74
leaq -0x28(%rbp), %rdi
callq 0x6e1a0
jmp 0xa960d
movq -0x58(%rbp), %rdi
callq 0x6830
nopl (%rax,%rax)