// File: /workspace/github_cpp_makefile_star2_O1/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 0x451b
cmpl 0xc(%rbx), %eax
je 0x451b
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 0x3fc7
movl %edx, %r9d
movl -0x4(%rdi,%r9,4), %r15d
movq (%r8,%rsi), %rsi
decl %ecx
cmpl %edx, %ecx
jle 0x3fd9
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 0x4000
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 0x4020
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 0x405a
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 0x5d88
cmpl $-0x1, %r15d
je 0x40d4
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x40d4
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 0x40bf
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x40fd
movq -0xf8(%rbp), %rax
xorl %ecx, %ecx
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x40e6
leaq -0xa8(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0x78(%rbp), %rdi
leaq -0x60(%rbp), %rcx
callq 0x5d88
cmpl $-0x1, -0x2c(%rbp)
je 0x4151
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x4151
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 0x4134
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x4177
movq -0x78(%rbp), %rax
xorl %ecx, %ecx
movsd 0x28(%rbx), %xmm0
addsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%rax,%rcx,8)
incq %rcx
cmpq %rcx, %rsi
jne 0x4160
cmpl $-0x1, %r15d
je 0x41a1
cmpl $-0x1, -0x2c(%rbp)
je 0x41a1
cmpl -0x2c(%rbp), %r15d
jne 0x41a1
movq -0x78(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
addsd 0x3e75(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r14,8)
leaq -0x60(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xa8(%rbp), %rdi
leaq -0x90(%rbp), %rcx
callq 0x5d88
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x41f3
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 0x41dc
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0x60(%rbp), %rdi
leaq -0x90(%rbp), %rcx
callq 0x5e14
cmpl $-0x1, %r15d
movq -0x48(%rbp), %rdi
je 0x4233
movq -0x60(%rbp), %rax
movsd (%rax,%r14,8), %xmm0
addsd 0x3de3(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r14,8)
movq %r14, -0xb0(%rbp)
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x426b
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 0x4250
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 0x5e14
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 0x42e2
xorl %edx, %edx
xorl %esi, %esi
testq %rdx, %rdx
setne %sil
shll $0x4, %esi
movsd 0x20(%rbx,%rsi), %xmm0
addsd (%rax,%rdx,8), %xmm0
movsd %xmm0, (%rax,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x42be
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xe0(%rbp), %rdi
leaq -0xc8(%rbp), %rcx
callq 0x5e14
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 0x436a
xorps %xmm0, %xmm0
cvtsi2sdl 0xa0(%rbx), %xmm0
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x4351
movsd 0x30(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
jmp 0x4362
movsd 0x20(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax), %xmm1
movsd %xmm1, (%rax)
incq %rcx
cmpq %rcx, %rsi
jne 0x4337
leaq -0x118(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xc8(%rbp), %rdi
leaq -0x2d(%rbp), %rcx
callq 0x5d88
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x43da
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 0x43b4
leaq -0x110(%rbp), %rdi
leaq -0xc8(%rbp), %rsi
callq 0x5e4e
leaq -0x110(%rbp), %rdi
callq 0x261c
movl %eax, %r14d
movq -0x110(%rbp), %rdi
testq %rdi, %rdi
je 0x440d
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 0x4446
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 0x4463
movq 0x138(%rbx), %rdx
leaq (%rax,%rax,2), %rsi
movq (%rdx,%rsi,8), %rdx
incl (%rdx,%r13,4)
testl %r14d, %r14d
jne 0x4492
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 0x44bb
callq 0x2210
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x44cc
callq 0x2210
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x44dd
callq 0x2210
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x44eb
callq 0x2210
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x44fc
callq 0x2210
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x450a
callq 0x2210
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x451b
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 0x4546
callq 0x2210
jmp 0x4546
movq %rax, %rbx
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0x455c
callq 0x2210
jmp 0x455c
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
testq %rdi, %rdi
je 0x4572
callq 0x2210
jmp 0x4572
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x4588
callq 0x2210
jmp 0x4588
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x459b
callq 0x2210
jmp 0x459b
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x45b1
callq 0x2210
jmp 0x45b1
movq %rax, %rbx
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x45c4
callq 0x2210
jmp 0x45c4
movq %rax, %rbx
movq -0xf8(%rbp), %rdi
testq %rdi, %rdi
je 0x45d5
callq 0x2210
movq %rbx, %rdi
callq 0x2390
nop