// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movslq %r9d, %r10
movq (%rsi), %rax
movl (%rax,%r10,4), %eax
cmpl 0x8(%rdi), %eax
je 0x4afa
movq %rdi, %rbx
cmpl 0xc(%rdi), %eax
je 0x4afa
movl 0x10(%rbp), %esi
movq %rcx, -0xc0(%rbp)
movq (%rcx), %rcx
pushq $-0x1
popq %r15
movl %r15d, %r14d
testl %r9d, %r9d
jle 0x45da
movl %r9d, %edi
movl -0x4(%rcx,%rdi,4), %r14d
movq (%rdx), %rdx
decl %esi
cmpl %r9d, %esi
jle 0x45e9
movl 0x4(%rcx,%r10,4), %r15d
movslq (%rcx,%r10,4), %rcx
movslq (%rdx,%r10,4), %rdx
movq %rdx, -0x40(%rbp)
movslq %r14d, %rdi
cmpl $-0x1, %r14d
je 0x4610
movq 0x138(%rbx), %rdx
imulq $0x18, %rdi, %rsi
movq (%rdx,%rsi), %rdx
decl (%rdx,%rcx,4)
movq %rdi, -0x38(%rbp)
movslq %r15d, %r13
cmpl $-0x1, %r15d
je 0x4630
movq 0x138(%rbx), %rdx
imulq $0x18, %rcx, %rsi
movq (%rdx,%rsi), %rdx
decl (%rdx,%r13,4)
movq %r10, -0xb8(%rbp)
cltq
testl %ecx, %ecx
jne 0x4663
movq 0x108(%rbx), %rdx
imulq $0x18, %rax, %rsi
movq (%rdx,%rsi), %rdx
movq -0x40(%rbp), %rsi
decl (%rdx,%rsi,4)
movq 0x150(%rbx), %rdx
decl (%rdx,%rsi,4)
movq (%r8), %rdx
decl (%rdx,%rsi,4)
movq %r8, -0xb0(%rbp)
movq 0x120(%rbx), %rdx
imulq $0x18, %rax, %rax
movq %rax, -0x48(%rbp)
movq (%rdx,%rax), %rax
decl (%rax,%rcx,4)
movq 0x168(%rbx), %rax
decl (%rax,%rcx,4)
movslq 0x4(%rbx), %rsi
leaq -0xa8(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x108(%rbp), %r12
leaq -0x90(%rbp), %rcx
movq %r12, %rdi
callq 0x5034
movslq 0x4(%rbx), %rsi
movq (%r12), %rax
cmpl $-0x1, %r14d
je 0x46f6
imulq $0x18, -0x38(%rbp), %rdx
addq 0x138(%rbx), %rdx
xorl %edi, %edi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
cmpq %rdi, %rcx
je 0x46ed
movq (%rdx), %r8
cvtsi2sdl (%r8,%rdi,4), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
jmp 0x46d5
movq -0x108(%rbp), %rax
jmp 0x46fd
xorl %ecx, %ecx
testl %esi, %esi
cmovgl %esi, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x4718
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x46ff
leaq -0x90(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0xa8(%rbp), %rdi
leaq -0x60(%rbp), %rcx
callq 0x5034
movslq 0x4(%rbx), %rsi
movq -0xa8(%rbp), %rax
cmpl $-0x1, %r15d
je 0x477c
movq 0x138(%rbx), %rdx
xorl %edi, %edi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
cmpq %rdi, %rcx
je 0x4773
movq (%rdx), %r8
cvtsi2sdl (%r8,%r13,4), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
addq $0x18, %rdx
jmp 0x4757
movq -0xa8(%rbp), %rax
jmp 0x4783
xorl %ecx, %ecx
testl %esi, %esi
cmovgl %esi, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x479e
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x4785
cmpl $-0x1, %r14d
je 0x47c5
cmpl $-0x1, %r15d
je 0x47c5
cmpl %r15d, %r14d
jne 0x47c5
movq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x2850(%rip), %xmm0     # 0x7010
movsd %xmm0, (%rax,%rcx,8)
leaq -0x60(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0x90(%rbp), %rdi
leaq -0x78(%rbp), %rcx
callq 0x5034
movl 0x4(%rbx), %eax
movq -0x108(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movq -0x90(%rbp), %rsi
xorl %edi, %edi
testl %eax, %eax
cmovlel %edi, %eax
cmpq %rdi, %rax
je 0x4815
movsd (%rcx,%rdi,8), %xmm0
mulsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
jmp 0x47fc
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0x60(%rbp), %rdi
leaq -0x78(%rbp), %rcx
callq 0x5094
cmpl $-0x1, %r14d
movq -0x48(%rbp), %rdi
je 0x4854
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x27c1(%rip), %xmm0     # 0x7010
movsd %xmm0, (%rax,%rcx,8)
movl 0x4(%rbx), %eax
cvtsi2sd %eax, %xmm0
movq -0x60(%rbp), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rdx, %rax
je 0x4883
movsd 0x28(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rcx,%rdx,8), %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
jmp 0x4866
movq 0x120(%rbx), %rax
movq (%rax,%rdi), %rsi
movq 0x8(%rax,%rdi), %rdx
movq %rdi, %r12
leaq -0x78(%rbp), %rdi
leaq -0xf0(%rbp), %rcx
callq 0x5094
movq 0x108(%rbx), %rax
movq (%rax,%r12), %rax
movq -0x40(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0x78(%rbp), %rax
movsd %xmm0, (%rax)
movl 0x4(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rdx, %rcx
je 0x48f5
testq %rdx, %rdx
je 0x48e7
movsd 0x30(%rbx), %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
jmp 0x48f0
addsd 0x20(%rbx), %xmm0
movsd %xmm0, (%rax)
incq %rdx
jmp 0x48cc
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xf0(%rbp), %rdi
leaq -0xd8(%rbp), %rcx
callq 0x5094
movq 0x150(%rbx), %rax
movq -0x40(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0xf0(%rbp), %rax
movsd %xmm0, (%rax)
movslq 0x4(%rbx), %rsi
cvtsi2sdl 0xa0(%rbx), %xmm1
xorl %ecx, %ecx
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
cmpq %rcx, %rdx
je 0x497e
testq %rcx, %rcx
je 0x4968
movsd 0x30(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
jmp 0x4979
movsd 0x20(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%rax)
incq %rcx
jmp 0x4949
leaq -0x128(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0xd8(%rbp), %rdi
leaq -0x29(%rbp), %rcx
callq 0x5034
movl 0x4(%rbx), %eax
movq -0x78(%rbp), %rcx
movq -0xf0(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0xd8(%rbp), %r8
xorl %r9d, %r9d
testl %eax, %eax
cmovlel %r9d, %eax
cmpq %r9, %rax
je 0x49ea
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
jmp 0x49c2
leaq -0x120(%rbp), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x50c6
leaq -0x120(%rbp), %rdi
callq 0x257c
movl %eax, %r12d
leaq -0x120(%rbp), %rdi
callq 0x69c8
movq -0xc0(%rbp), %rax
movq (%rax), %rax
movq -0xb8(%rbp), %rcx
movl %r12d, (%rax,%rcx,4)
movslq %r12d, %rax
cmpl $-0x1, %r14d
je 0x4a49
movq 0x138(%rbx), %rcx
imulq $0x18, -0x38(%rbp), %rdx
movq (%rcx,%rdx), %rcx
incl (%rcx,%rax,4)
cmpl $-0x1, %r15d
movq -0x48(%rbp), %rsi
je 0x4a66
movq 0x138(%rbx), %rcx
imulq $0x18, %rax, %rdx
movq (%rcx,%rdx), %rcx
incl (%rcx,%r13,4)
testl %r12d, %r12d
jne 0x4a94
movq -0xb0(%rbp), %rcx
movq (%rcx), %rcx
movq -0x40(%rbp), %rdx
incl (%rcx,%rdx,4)
movq 0x108(%rbx), %rcx
movq (%rcx,%rsi), %rcx
incl (%rcx,%rdx,4)
movq 0x150(%rbx), %rcx
incl (%rcx,%rdx,4)
movq 0x120(%rbx), %rcx
movq (%rcx,%rsi), %rcx
incl (%rcx,%rax,4)
movq 0x168(%rbx), %rcx
incl (%rcx,%rax,4)
leaq -0xd8(%rbp), %rdi
callq 0x69c8
leaq -0xf0(%rbp), %rdi
callq 0x69c8
leaq -0x78(%rbp), %rdi
callq 0x69c8
leaq -0x60(%rbp), %rdi
callq 0x69c8
leaq -0x90(%rbp), %rdi
callq 0x69c8
leaq -0xa8(%rbp), %rdi
callq 0x69c8
leaq -0x108(%rbp), %rdi
callq 0x69c8
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x120(%rbp), %rdi
callq 0x69c8
jmp 0x4b20
movq %rax, %rbx
leaq -0xd8(%rbp), %rdi
callq 0x69c8
jmp 0x4b31
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x69c8
jmp 0x4b42
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x69c8
jmp 0x4b50
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x69c8
leaq -0x90(%rbp), %rdi
callq 0x69c8
leaq -0xa8(%rbp), %rdi
callq 0x69c8
leaq -0x108(%rbp), %rdi
callq 0x69c8
movq %rbx, %rdi
callq 0x2380
movq %rax, %rbx
jmp 0x4b59
movq %rax, %rbx
jmp 0x4b65
movq %rax, %rbx
jmp 0x4b71