// File: /workspace/github_cpp_makefile_star2_O3/abaheti95[P]HMM-LDA/Models/cpp_hmm_lda/model.cpp
// Function: HMMLDA::spl_draw_class(std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, std::vector<int, std::allocator<int>>&, int, int)
void HMMLDA::spl_draw_class(vector<int> &document, vector<int> &topic_assignment, vector<int> &class_assignment, vector<int> &num_words_in_document_assigned_to_topic, int word_idx, int doc_size) {
	int old_class = class_assignment[word_idx];
	int old_topic = topic_assignment[word_idx];
	int word = document[word_idx];
	if(word == start_word_id || word == end_word_id) 
		return;
	// Get neighboring classes
	int previous = -1;
	if(word_idx > 0)
		previous = class_assignment[word_idx - 1];
	int future = -1;
	if(word_idx < doc_size-1)
		future = class_assignment[word_idx + 1];

	// Remove current word from transition counts
	if(previous != -1)
		num_transitions[previous][old_class]--;
	if(future != -1)
		num_transitions[old_class][future]--;
	if(old_class == 0) {
		// Remove word from topic counts
		num_same_words_assigned_to_topic[word][old_topic]--;
		num_words_assigned_to_topic[old_topic]--;
		num_words_in_document_assigned_to_topic[old_topic]--;
	}
	// Remove the word from class counts
	num_same_words_assigned_to_class[word][old_class]--;
	num_words_assigned_to_class[old_class]--;

	// Build first term of numerator
	vector<double> term_1(num_classes, 0.0);
	if(previous != -1) {
		for(int i = 0; i < num_classes; i++)
			term_1[i] = num_transitions[previous][i];
	}
	//Smoothing
	for(int i = 0; i < num_classes; i++)
		term_1[i] += gamma;

	// Build second term of numerator
	vector<double> term_2(num_classes, 0.0);
	if(future != -1) {
		for(int i = 0; i < num_classes; i++)
			term_2[i] = num_transitions[i][future];
	}
	//Smoothing
	for(int i = 0; i < num_classes; i++)
		term_2[i] += gamma;
	// Adjusting for the itentity value in the numerator term 2
	if(previous != -1 && future != -1 && (previous == future))
		term_2[previous] += 1.0;

	// Calculate numerator
	vector<double> numerator(num_classes, 0.0);
	for(int i = 0; i < num_classes; i++)
		numerator[i] = term_1[i] * term_2[i];

	// Build denominator
	vector<double> denominator(num_words_assigned_to_class.begin(), num_words_assigned_to_class.end());
	if(previous != -1)
		denominator[previous] += 1.0;
	for(int i = 0; i < num_classes; i++)
		denominator[i] += num_classes * gamma;
	
	// Calculate multiplier
	
	// Initialize numerator of multiplier with same word class/topic counts
	vector<double> multiplier_numerator(num_same_words_assigned_to_class[word].begin(), 
		num_same_words_assigned_to_class[word].end());
	multiplier_numerator[0] = num_same_words_assigned_to_topic[word][old_topic];
	
	// Smoothing
	for(int i = 0; i < num_classes; i++) {
		if(i == 0)
			multiplier_numerator[0] += beta;
		else
			multiplier_numerator[i] += delta;
	}

	// Initialize denominator of multiplier with global class/topic counts
	vector<double> multiplier_denominator(num_words_assigned_to_class.begin(), num_words_assigned_to_class.end());
	multiplier_denominator[0] = num_words_assigned_to_topic[old_topic];
	
	// Smoothing
	for(int i = 0; i < num_classes; i++) {
		if(i == 0)
			multiplier_denominator[0] += beta * vocab_size;
		else
			multiplier_denominator[i] += delta * vocab_size;
	}

	// Calculate probability proportions
	vector<double> proportions(num_classes, 0.0);
	for(int i = 0; i < num_classes; i++) {
		proportions[i] = (multiplier_numerator[i] / multiplier_denominator[i]) * numerator[i] / denominator[i];
	}
	// Draw class
	int new_class = categorical(proportions);

	class_assignment[word_idx] = new_class;

	// Restore counts counts
	if(previous != -1) {
		num_transitions[previous][new_class] += 1;
	}
	if(future != -1) {
		num_transitions[new_class][future] += 1;
	}
	if(new_class == 0) {
		// Add word to the topic counts
		num_words_in_document_assigned_to_topic[old_topic] += 1;
		num_same_words_assigned_to_topic[word][old_topic] += 1;
		num_words_assigned_to_topic[old_topic] += 1;
	}
	// Add word to the new class counts
	num_same_words_assigned_to_class[word][new_class] += 1;
	num_words_assigned_to_class[new_class] += 1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movslq %r9d, %r13
movq (%rsi), %rax
movl (%rax,%r13,4), %eax
cmpl 0x8(%rdi), %eax
je 0x5724
movq %rdi, %rbx
cmpl 0xc(%rdi), %eax
je 0x5724
movl 0x10(%rbp), %esi
movq (%rcx), %r10
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r9d, %r9d
jle 0x51e3
movl %r9d, %edi
movl -0x4(%r10,%rdi,4), %r15d
movq (%rdx), %rdx
decl %esi
cmpl %r9d, %esi
jle 0x51f5
movl 0x4(%r10,%r13,4), %esi
movl %esi, -0x2c(%rbp)
movq %rcx, -0xb8(%rbp)
movq %r8, -0x40(%rbp)
movslq (%r10,%r13,4), %rcx
movslq (%rdx,%r13,4), %rdx
movq %rdx, -0x38(%rbp)
movslq %r15d, %r12
cmpl $-0x1, %r15d
je 0x5227
movq 0x138(%rbx), %rdx
leaq (%r12,%r12,2), %rsi
movq (%rdx,%rsi,8), %rdx
decl (%rdx,%rcx,4)
cltq
movl -0x2c(%rbp), %edx
movslq %edx, %r14
cmpl $-0x1, %edx
je 0x5247
movq 0x138(%rbx), %rdx
leaq (%rcx,%rcx,2), %rsi
movq (%rdx,%rsi,8), %rdx
decl (%rdx,%r14,4)
leaq (%rax,%rax,2), %rsi
testl %ecx, %ecx
jne 0x5275
movq 0x108(%rbx), %rax
movq (%rax,%rsi,8), %rax
movq -0x38(%rbp), %rdx
decl (%rax,%rdx,4)
movq 0x150(%rbx), %rax
decl (%rax,%rdx,4)
movq -0x40(%rbp), %rax
movq (%rax), %rax
decl (%rax,%rdx,4)
movq 0x120(%rbx), %rax
movq %rsi, -0x48(%rbp)
movq (%rax,%rsi,8), %rax
decl (%rax,%rcx,4)
movq 0x168(%rbx), %rax
decl (%rax,%rcx,4)
movslq 0x4(%rbx), %rsi
leaq -0x78(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0x100(%rbp), %rdi
leaq -0xa8(%rbp), %rcx
callq 0x5d8c
movl 0x4(%rbx), %eax
cmpl $-0x1, %r15d
je 0x52ed
testl %eax, %eax
jle 0x5311
movq 0x138(%rbx), %rcx
leaq (%r12,%r12,2), %rdx
movq (%rcx,%rdx,8), %rcx
movq -0x100(%rbp), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%rsi,4), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x52d8
testl %eax, %eax
jle 0x5311
movq -0x100(%rbp), %rcx
xorl %edx, %edx
movsd 0x28(%rbx), %xmm0
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x52fa
movslq %eax, %rsi
leaq -0xa8(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0x78(%rbp), %rdi
leaq -0x60(%rbp), %rcx
callq 0x5d8c
movl 0x4(%rbx), %eax
cmpl $-0x1, -0x2c(%rbp)
je 0x5366
testl %eax, %eax
jle 0x5387
movq 0x138(%rbx), %rcx
movq -0x78(%rbp), %rdx
xorl %esi, %esi
movq (%rcx), %rdi
xorps %xmm0, %xmm0
cvtsi2sdl (%rdi,%r14,4), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
addq $0x18, %rcx
cmpq %rsi, %rax
jne 0x5349
testl %eax, %eax
jle 0x5387
movq -0x78(%rbp), %rcx
xorl %edx, %edx
movsd 0x28(%rbx), %xmm0
addsd (%rcx,%rdx,8), %xmm0
movsd %xmm0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5370
cmpl $-0x1, %r15d
je 0x53b1
cmpl $-0x1, -0x2c(%rbp)
je 0x53b1
cmpl -0x2c(%rbp), %r15d
jne 0x53b1
movq -0x78(%rbp), %rcx
movsd (%rcx,%r12,8), %xmm0
addsd 0x2c65(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rcx,%r12,8)
movslq %eax, %rsi
leaq -0x60(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xa8(%rbp), %rdi
leaq -0x90(%rbp), %rcx
callq 0x5d8c
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x5406
movq -0x100(%rbp), %rcx
movq -0x78(%rbp), %rdx
movq -0xa8(%rbp), %rsi
xorl %edi, %edi
movsd (%rcx,%rdi,8), %xmm0
mulsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x53ef
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0x60(%rbp), %rdi
leaq -0x90(%rbp), %rcx
callq 0x5e18
cmpl $-0x1, %r15d
movq -0x48(%rbp), %rdi
je 0x5446
movq -0x60(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
addsd 0x2bd0(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r12,8)
movq %r12, -0xb0(%rbp)
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x547e
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq -0x60(%rbp), %rcx
xorl %edx, %edx
movsd 0x28(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rcx,%rdx,8), %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5463
movq 0x120(%rbx), %rax
movq (%rax,%rdi,8), %rsi
movq 0x8(%rax,%rdi,8), %rdx
movq %rdi, %r12
leaq -0x90(%rbp), %rdi
leaq -0xe8(%rbp), %rcx
callq 0x5e18
movq 0x108(%rbx), %rax
movq (%rax,%r12,8), %rax
movq -0x38(%rbp), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x90(%rbp), %rax
movsd %xmm0, (%rax)
movslq 0x4(%rbx), %rcx
testq %rcx, %rcx
jle 0x54f8
xorl %edx, %edx
testq %rdx, %rdx
je 0x54e7
movsd 0x30(%rbx), %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
jmp 0x54f0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, (%rax)
incq %rdx
cmpq %rdx, %rcx
jne 0x54d1
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xe8(%rbp), %rdi
leaq -0xd0(%rbp), %rcx
callq 0x5e18
movq 0x150(%rbx), %rax
movq -0x38(%rbp), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0xe8(%rbp), %rax
movsd %xmm0, (%rax)
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x5580
xorps %xmm1, %xmm1
cvtsi2sdl 0xa0(%rbx), %xmm1
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x5567
movsd 0x30(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
jmp 0x5578
movsd 0x20(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%rax)
incq %rcx
cmpq %rcx, %rsi
jne 0x554d
leaq -0x120(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xd0(%rbp), %rdi
leaq -0x2d(%rbp), %rcx
callq 0x5d8c
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x55f0
movq -0x90(%rbp), %rcx
movq -0xe8(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0xd0(%rbp), %r8
xorl %r9d, %r9d
movsd (%rcx,%r9,8), %xmm0
divsd (%rdx,%r9,8), %xmm0
mulsd (%rsi,%r9,8), %xmm0
divsd (%rdi,%r9,8), %xmm0
movsd %xmm0, (%r8,%r9,8)
incq %r9
cmpq %r9, %rax
jne 0x55ca
leaq -0x118(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x5e52
leaq -0x118(%rbp), %rdi
callq 0x261c
movl %eax, %r12d
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x5623
callq 0x2210
movq -0xb8(%rbp), %rax
movq (%rax), %rax
movl %r12d, (%rax,%r13,4)
movslq %r12d, %rax
cmpl $-0x1, %r15d
je 0x5653
movq 0x138(%rbx), %rcx
movq -0xb0(%rbp), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rcx,%rdx,8), %rcx
incl (%rcx,%rax,4)
cmpl $-0x1, -0x2c(%rbp)
movq -0x48(%rbp), %rsi
je 0x5670
movq 0x138(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %rcx
incl (%rcx,%r14,4)
testl %r12d, %r12d
jne 0x569b
movq -0x40(%rbp), %rcx
movq (%rcx), %rcx
movq -0x38(%rbp), %rdx
incl (%rcx,%rdx,4)
movq 0x108(%rbx), %rcx
movq (%rcx,%rsi,8), %rcx
incl (%rcx,%rdx,4)
movq 0x150(%rbx), %rcx
incl (%rcx,%rdx,4)
movq 0x120(%rbx), %rcx
movq (%rcx,%rsi,8), %rcx
incl (%rcx,%rax,4)
movq 0x168(%rbx), %rcx
incl (%rcx,%rax,4)
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x56c4
callq 0x2210
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x56d5
callq 0x2210
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x56e6
callq 0x2210
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x56f4
callq 0x2210
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x5705
callq 0x2210
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x5713
callq 0x2210
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x5724
callq 0x2210
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x574f
callq 0x2210
jmp 0x574f
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x5765
callq 0x2210
jmp 0x5765
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x577b
callq 0x2210
jmp 0x577b
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x5791
callq 0x2210
jmp 0x5791
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x57a4
callq 0x2210
jmp 0x57a4
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x57ba
callq 0x2210
jmp 0x57ba
movq %rax, %rbx
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x57cd
callq 0x2210
jmp 0x57cd
movq %rax, %rbx
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x57de
callq 0x2210
movq %rbx, %rdi
callq 0x2390