// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %rbx
movslq %esi, %rdi
leaq (,%rdi,8), %rax
leaq (%rax,%rax,2), %rsi
movslq %edx, %r12
movq 0xa8(%rbx), %rax
movq (%rax,%rsi), %rax
movl (%rax,%r12,4), %eax
cmpl 0x8(%rbx), %eax
je 0x451a
cmpl 0xc(%rbx), %eax
je 0x451a
movq %rdi, -0x40(%rbp)
movq 0xd8(%rbx), %rdi
movq (%rdi,%rsi), %rdi
movq 0xc0(%rbx), %r8
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %edx, %edx
jle 0x3fcb
movl %edx, %r9d
movl -0x4(%rdi,%r9,4), %r15d
movq (%r8,%rsi), %rsi
decl %ecx
cmpl %edx, %ecx
jle 0x3fdd
movl 0x4(%rdi,%r12,4), %ecx
movl %ecx, -0x2c(%rbp)
movslq (%rdi,%r12,4), %rcx
movslq (%rsi,%r12,4), %rdx
movq %rdx, -0x38(%rbp)
movslq %r15d, %r14
cmpl $-0x1, %r15d
je 0x4004
movq 0x138(%rbx), %rdx
leaq (%r14,%r14,2), %rsi
movq (%rdx,%rsi,8), %rdx
decl (%rdx,%rcx,4)
cltq
movl -0x2c(%rbp), %edx
movslq %edx, %r13
cmpl $-0x1, %edx
je 0x4024
movq 0x138(%rbx), %rdx
leaq (%rcx,%rcx,2), %rsi
movq (%rdx,%rsi,8), %rdx
decl (%rdx,%r13,4)
leaq (%rax,%rax,2), %rdi
testl %ecx, %ecx
jne 0x405e
movq 0x108(%rbx), %rax
movq (%rax,%rdi,8), %rax
movq -0x38(%rbp), %rsi
decl (%rax,%rsi,4)
movq 0x150(%rbx), %rax
decl (%rax,%rsi,4)
movq 0xf0(%rbx), %rax
movq -0x40(%rbp), %rdx
leaq (%rdx,%rdx,2), %rdx
movq (%rax,%rdx,8), %rax
decl (%rax,%rsi,4)
movq 0x120(%rbx), %rax
movq %rdi, -0x48(%rbp)
movq (%rax,%rdi,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 -0xf8(%rbp), %rdi
leaq -0xa8(%rbp), %rcx
callq 0x5d8c
movl 0x4(%rbx), %eax
cmpl $-0x1, %r15d
je 0x40d6
testl %eax, %eax
jle 0x40fa
movq 0x138(%rbx), %rcx
leaq (%r14,%r14,2), %rdx
movq (%rcx,%rdx,8), %rcx
movq -0xf8(%rbp), %rdx
xorl %esi, %esi
xorps %xmm0, %xmm0
cvtsi2sdl (%rcx,%rsi,4), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rax
jne 0x40c1
testl %eax, %eax
jle 0x40fa
movq -0xf8(%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 0x40e3
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 0x414f
testl %eax, %eax
jle 0x4170
movq 0x138(%rbx), %rcx
movq -0x78(%rbp), %rdx
xorl %esi, %esi
movq (%rcx), %rdi
xorps %xmm0, %xmm0
cvtsi2sdl (%rdi,%r13,4), %xmm0
movsd %xmm0, (%rdx,%rsi,8)
incq %rsi
addq $0x18, %rcx
cmpq %rsi, %rax
jne 0x4132
testl %eax, %eax
jle 0x4170
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 0x4159
cmpl $-0x1, %r15d
je 0x419a
cmpl $-0x1, -0x2c(%rbp)
je 0x419a
cmpl -0x2c(%rbp), %r15d
jne 0x419a
movq -0x78(%rbp), %rcx
movsd (%rcx,%r14,8), %xmm0
addsd 0x3e7c(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rcx,%r14,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 0x41ef
movq -0xf8(%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 0x41d8
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 0x422f
movq -0x60(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
addsd 0x3de7(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r14,8)
movq %r14, -0xb0(%rbp)
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x4267
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 0x424c
movq 0x120(%rbx), %rax
movq (%rax,%rdi,8), %rsi
movq 0x8(%rax,%rdi,8), %rdx
movq %rdi, %r14
leaq -0x90(%rbp), %rdi
leaq -0xe0(%rbp), %rcx
callq 0x5e18
movq 0x108(%rbx), %rax
movq (%rax,%r14,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 0x42e1
xorl %edx, %edx
testq %rdx, %rdx
je 0x42d0
movsd 0x30(%rbx), %xmm1
addsd (%rax,%rdx,8), %xmm1
movsd %xmm1, (%rax,%rdx,8)
jmp 0x42d9
addsd 0x20(%rbx), %xmm0
movsd %xmm0, (%rax)
incq %rdx
cmpq %rdx, %rcx
jne 0x42ba
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xe0(%rbp), %rdi
leaq -0xc8(%rbp), %rcx
callq 0x5e18
movq 0x150(%rbx), %rax
movq -0x38(%rbp), %rcx
xorps %xmm0, %xmm0
cvtsi2sdl (%rax,%rcx,4), %xmm0
movq -0xe0(%rbp), %rax
movsd %xmm0, (%rax)
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x4369
xorps %xmm1, %xmm1
cvtsi2sdl 0xa0(%rbx), %xmm1
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x4350
movsd 0x30(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd (%rax,%rcx,8), %xmm2
movsd %xmm2, (%rax,%rcx,8)
jmp 0x4361
movsd 0x20(%rbx), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
movsd %xmm0, (%rax)
incq %rcx
cmpq %rcx, %rsi
jne 0x4336
leaq -0x118(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xc8(%rbp), %rdi
leaq -0x2d(%rbp), %rcx
callq 0x5d8c
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x43d9
movq -0x90(%rbp), %rcx
movq -0xe0(%rbp), %rdx
movq -0xa8(%rbp), %rsi
movq -0x60(%rbp), %rdi
movq -0xc8(%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 0x43b3
leaq -0x110(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5e52
leaq -0x110(%rbp), %rdi
callq 0x261c
movl %eax, %r14d
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x440c
callq 0x2210
movq 0xd8(%rbx), %rax
movq -0x40(%rbp), %rcx
leaq (%rcx,%rcx,2), %rcx
movq (%rax,%rcx,8), %rax
movl %r14d, (%rax,%r12,4)
movslq %r14d, %rax
cmpl $-0x1, %r15d
je 0x4445
movq 0x138(%rbx), %rdx
movq -0xb0(%rbp), %rsi
leaq (%rsi,%rsi,2), %rsi
movq (%rdx,%rsi,8), %rdx
incl (%rdx,%rax,4)
cmpl $-0x1, -0x2c(%rbp)
movq -0x48(%rbp), %rdi
je 0x4462
movq 0x138(%rbx), %rdx
leaq (%rax,%rax,2), %rsi
movq (%rdx,%rsi,8), %rdx
incl (%rdx,%r13,4)
testl %r14d, %r14d
jne 0x4491
movq 0xf0(%rbx), %rdx
movq (%rdx,%rcx,8), %rcx
movq -0x38(%rbp), %rdx
incl (%rcx,%rdx,4)
movq 0x108(%rbx), %rcx
movq (%rcx,%rdi,8), %rcx
incl (%rcx,%rdx,4)
movq 0x150(%rbx), %rcx
incl (%rcx,%rdx,4)
movq 0x120(%rbx), %rcx
movq (%rcx,%rdi,8), %rcx
incl (%rcx,%rax,4)
movq 0x168(%rbx), %rcx
incl (%rcx,%rax,4)
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x44ba
callq 0x2210
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x44cb
callq 0x2210
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x44dc
callq 0x2210
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x44ea
callq 0x2210
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x44fb
callq 0x2210
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x4509
callq 0x2210
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x451a
callq 0x2210
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x4545
callq 0x2210
jmp 0x4545
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x455b
callq 0x2210
jmp 0x455b
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x4571
callq 0x2210
jmp 0x4571
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x4587
callq 0x2210
jmp 0x4587
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x459a
callq 0x2210
jmp 0x459a
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x45b0
callq 0x2210
jmp 0x45b0
movq %rax, %rbx
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x45c3
callq 0x2210
jmp 0x45c3
movq %rax, %rbx
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x45d4
callq 0x2210
movq %rbx, %rdi
callq 0x2390