// File: /workspace/github_cpp_makefile_star2_O2/abaheti95[P]HMM-LDA/Models/cpp_hmm_lda/model.cpp
// Function: HMMLDA::draw_class(int, int, int)
void HMMLDA::draw_class(int document_idx, int word_idx, int doc_size) {
	int old_class = class_assignments[document_idx][word_idx];
	int old_topic = topic_assignments[document_idx][word_idx];
	int word = documents[document_idx][word_idx];
	if(word == start_word_id || word == end_word_id) 
		return;
	// Get neighboring classes
	int previous = -1;
	if(word_idx > 0)
		previous = class_assignments[document_idx][word_idx - 1];
	int future = -1;
	if(word_idx < doc_size-1)
		future = class_assignments[document_idx][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_doc_assigned_to_topic[document_idx][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
	// logging.info('proportions = %s', proportions)
	int new_class = categorical(proportions);
	// if(document_idx == 0) {
	// 	if(new_class == 0) {
	// 		cout << "Printing proportions for class sampling" << document_idx << " " << word_idx << endl;
	// 		print_vector<double>(proportions);
	// 		print_vector<double>(numerator);
	// 		print_vector<double>(multiplier_numerator);
	// 		cout << old_class << " New class chosen " << new_class << endl;
	// 		cout << alpha << " " << beta << " " << gamma << " " << delta << endl; 
	// 	}
	// }
	// logging.info('drew class %d', new_class)
	class_assignments[document_idx][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_doc_assigned_to_topic[document_idx][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 %esi, %rax
imulq $0x18, %rax, %r9
movslq %edx, %r10
movq 0xa8(%rdi), %rax
movq (%rax,%r9), %rax
movl (%rax,%r10,4), %eax
cmpl 0x8(%rdi), %eax
je 0x3d35
movq %rdi, %rbx
cmpl 0xc(%rdi), %eax
je 0x3d35
movq 0xd8(%rbx), %rsi
movq (%rsi,%r9), %rsi
movq 0xc0(%rbx), %rdi
pushq $-0x1
popq %r15
movl %r15d, %r13d
testl %edx, %edx
jle 0x3805
movl %edx, %r8d
movl -0x4(%rsi,%r8,4), %r13d
movq (%rdi,%r9), %rdi
decl %ecx
cmpl %edx, %ecx
jle 0x3814
movl 0x4(%rsi,%r10,4), %r15d
movslq (%rsi,%r10,4), %rcx
movslq (%rdi,%r10,4), %rdx
movq %rdx, -0x40(%rbp)
movslq %r13d, %rdi
cmpl $-0x1, %r13d
je 0x383b
movq 0x138(%rbx), %rdx
imulq $0x18, %rdi, %rsi
movq (%rdx,%rsi), %rdx
decl (%rdx,%rcx,4)
movq %rdi, -0x38(%rbp)
movslq %r15d, %r12
cmpl $-0x1, %r15d
je 0x385b
movq 0x138(%rbx), %rdx
imulq $0x18, %rcx, %rsi
movq (%rdx,%rsi), %rdx
decl (%rdx,%r12,4)
movq %r10, -0xb0(%rbp)
cltq
testl %ecx, %ecx
jne 0x3896
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 0xf0(%rbx), %rdx
movq (%rdx,%r9), %rdx
decl (%rdx,%rsi,4)
movq %r9, -0xb8(%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 -0x100(%rbp), %r14
leaq -0x90(%rbp), %rcx
movq %r14, %rdi
callq 0x5034
movslq 0x4(%rbx), %rsi
movq (%r14), %rax
cmpl $-0x1, %r13d
je 0x3928
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 0x391f
movq (%rdx), %r8
cvtsi2sdl (%r8,%rdi,4), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
jmp 0x3907
movq -0x100(%rbp), %rax
jmp 0x392f
xorl %ecx, %ecx
testl %esi, %esi
cmovgl %esi, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x394a
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x3931
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 0x39ae
movq 0x138(%rbx), %rdx
xorl %edi, %edi
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
cmpq %rdi, %rcx
je 0x39a5
movq (%rdx), %r8
cvtsi2sdl (%r8,%r12,4), %xmm0
movsd %xmm0, (%rax,%rdi,8)
incq %rdi
addq $0x18, %rdx
jmp 0x3989
movq -0xa8(%rbp), %rax
jmp 0x39b5
xorl %ecx, %ecx
testl %esi, %esi
cmovgl %esi, %ecx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x39d0
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
jmp 0x39b7
cmpl $-0x1, %r13d
je 0x39f7
cmpl $-0x1, %r15d
je 0x39f7
cmpl %r15d, %r13d
jne 0x39f7
movq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x361e(%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 -0x100(%rbp), %rcx
movq -0xa8(%rbp), %rdx
movq -0x90(%rbp), %rsi
xorl %edi, %edi
testl %eax, %eax
cmovlel %edi, %eax
cmpq %rdi, %rax
je 0x3a47
movsd (%rcx,%rdi,8), %xmm0
mulsd (%rdx,%rdi,8), %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
jmp 0x3a2e
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0x60(%rbp), %rdi
leaq -0x78(%rbp), %rcx
callq 0x5094
cmpl $-0x1, %r13d
movq -0x48(%rbp), %rdi
je 0x3a86
movq -0x60(%rbp), %rax
movq -0x38(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x358f(%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 0x3ab5
movsd 0x28(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rcx,%rdx,8), %xmm1
movsd %xmm1, (%rcx,%rdx,8)
incq %rdx
jmp 0x3a98
movq 0x120(%rbx), %rax
movq (%rax,%rdi), %rsi
movq 0x8(%rax,%rdi), %rdx
movq %rdi, %r14
leaq -0x78(%rbp), %rdi
leaq -0xe8(%rbp), %rcx
callq 0x5094
movq 0x108(%rbx), %rax
movq (%rax,%r14), %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 0x3b27
testq %rdx, %rdx
je 0x3b19
movsd 0x30(%rbx), %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
jmp 0x3b22
addsd 0x20(%rbx), %xmm0
movsd %xmm0, (%rax)
incq %rdx
jmp 0x3afe
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xe8(%rbp), %rdi
leaq -0xd0(%rbp), %rcx
callq 0x5094
movq 0x150(%rbx), %rax
movq -0x40(%rbp), %rcx
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0xe8(%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 0x3bb0
testq %rcx, %rcx
je 0x3b9a
movsd 0x30(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
jmp 0x3bab
movsd 0x20(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%rax)
incq %rcx
jmp 0x3b7b
leaq -0x120(%rbp), %rdx
andq $0x0, (%rdx)
leaq -0xd0(%rbp), %rdi
leaq -0x29(%rbp), %rcx
callq 0x5034
movl 0x4(%rbx), %eax
movq -0x78(%rbp), %rcx
movq -0xe8(%rbp), %rdx
movq -0x90(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0xd0(%rbp), %r8
xorl %r9d, %r9d
testl %eax, %eax
cmovlel %r9d, %eax
cmpq %r9, %rax
je 0x3c1c
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 0x3bf4
leaq -0x118(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x50c6
leaq -0x118(%rbp), %rdi
callq 0x257c
movl %eax, %r14d
leaq -0x118(%rbp), %rdi
callq 0x69c8
movq 0xd8(%rbx), %rax
movq -0xb8(%rbp), %rsi
movq (%rax,%rsi), %rax
movq -0xb0(%rbp), %rcx
movl %r14d, (%rax,%rcx,4)
movslq %r14d, %rax
cmpl $-0x1, %r13d
je 0x3c83
movq 0x138(%rbx), %rcx
imulq $0x18, -0x38(%rbp), %rdx
movq (%rcx,%rdx), %rcx
incl (%rcx,%rax,4)
cmpl $-0x1, %r15d
movq -0x48(%rbp), %rdi
je 0x3ca0
movq 0x138(%rbx), %rcx
imulq $0x18, %rax, %rdx
movq (%rcx,%rdx), %rcx
incl (%rcx,%r12,4)
testl %r14d, %r14d
jne 0x3ccf
movq 0xf0(%rbx), %rcx
movq (%rcx,%rsi), %rcx
movq -0x40(%rbp), %rdx
incl (%rcx,%rdx,4)
movq 0x108(%rbx), %rcx
movq (%rcx,%rdi), %rcx
incl (%rcx,%rdx,4)
movq 0x150(%rbx), %rcx
incl (%rcx,%rdx,4)
movq 0x120(%rbx), %rcx
movq (%rcx,%rdi), %rcx
incl (%rcx,%rax,4)
movq 0x168(%rbx), %rcx
incl (%rcx,%rax,4)
leaq -0xd0(%rbp), %rdi
callq 0x69c8
leaq -0xe8(%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 -0x100(%rbp), %rdi
callq 0x69c8
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq -0x118(%rbp), %rdi
callq 0x69c8
jmp 0x3d5b
movq %rax, %rbx
leaq -0xd0(%rbp), %rdi
callq 0x69c8
jmp 0x3d6c
movq %rax, %rbx
leaq -0xe8(%rbp), %rdi
callq 0x69c8
jmp 0x3d7d
movq %rax, %rbx
leaq -0x78(%rbp), %rdi
callq 0x69c8
jmp 0x3d8b
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x69c8
leaq -0x90(%rbp), %rdi
callq 0x69c8
leaq -0xa8(%rbp), %rdi
callq 0x69c8
leaq -0x100(%rbp), %rdi
callq 0x69c8
movq %rbx, %rdi
callq 0x2380
movq %rax, %rbx
jmp 0x3d94
movq %rax, %rbx
jmp 0x3da0
movq %rax, %rbx
jmp 0x3dac
nop