// File: /workspace/github_cpp_makefile_star2_O1/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 0x572f
movq %rdi, %rbx
cmpl 0xc(%rdi), %eax
je 0x572f
movl 0x10(%rbp), %esi
movq (%rcx), %r10
movl $0xffffffff, -0x2c(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
testl %r9d, %r9d
jle 0x51e9
movl %r9d, %edi
movl -0x4(%r10,%rdi,4), %r15d
movq (%rdx), %rdx
decl %esi
cmpl %r9d, %esi
jle 0x51fb
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 0x522d
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 0x524d
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 0x527b
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 0x5d88
cmpl $-0x1, %r15d
je 0x52f5
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x52f5
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 0x52e0
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x531e
movq -0x100(%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 0x5307
leaq -0xa8(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0x78(%rbp), %rdi
leaq -0x60(%rbp), %rcx
callq 0x5d88
cmpl $-0x1, -0x2c(%rbp)
je 0x5372
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x5372
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 0x5355
movslq 0x4(%rbx), %rsi
testq %rsi, %rsi
jle 0x5398
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 0x5381
cmpl $-0x1, %r15d
je 0x53c2
cmpl $-0x1, -0x2c(%rbp)
je 0x53c2
cmpl -0x2c(%rbp), %r15d
jne 0x53c2
movq -0x78(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
addsd 0x2c54(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r12,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 0x5414
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 0x53fd
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 0x5454
movq -0x60(%rbp), %rax
movsd (%rax,%r12,8), %xmm0
addsd 0x2bc2(%rip), %xmm0     # 0x8010
movsd %xmm0, (%rax,%r12,8)
movq %r12, -0xb0(%rbp)
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x548c
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 0x5471
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 0x5e14
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 0x5503
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 0x54df
movq 0x168(%rbx), %rsi
movq 0x170(%rbx), %rdx
leaq -0xe8(%rbp), %rdi
leaq -0xd0(%rbp), %rcx
callq 0x5e14
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 0x558b
xorps %xmm0, %xmm0
cvtsi2sdl 0xa0(%rbx), %xmm0
xorl %ecx, %ecx
testq %rcx, %rcx
je 0x5572
movsd 0x30(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax,%rcx,8), %xmm1
movsd %xmm1, (%rax,%rcx,8)
jmp 0x5583
movsd 0x20(%rbx), %xmm1
mulsd %xmm0, %xmm1
addsd (%rax), %xmm1
movsd %xmm1, (%rax)
incq %rcx
cmpq %rcx, %rsi
jne 0x5558
leaq -0x120(%rbp), %rdx
movq $0x0, (%rdx)
leaq -0xd0(%rbp), %rdi
leaq -0x2d(%rbp), %rcx
callq 0x5d88
movslq 0x4(%rbx), %rax
testq %rax, %rax
jle 0x55fb
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 0x55d5
leaq -0x118(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x5e4e
leaq -0x118(%rbp), %rdi
callq 0x261c
movl %eax, %r12d
movq -0x118(%rbp), %rdi
testq %rdi, %rdi
je 0x562e
callq 0x2210
movq -0xb8(%rbp), %rax
movq (%rax), %rax
movl %r12d, (%rax,%r13,4)
movslq %r12d, %rax
cmpl $-0x1, %r15d
je 0x565e
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 0x567b
movq 0x138(%rbx), %rcx
leaq (%rax,%rax,2), %rdx
movq (%rcx,%rdx,8), %rcx
incl (%rcx,%r14,4)
testl %r12d, %r12d
jne 0x56a6
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 0x56cf
callq 0x2210
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x56e0
callq 0x2210
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x56f1
callq 0x2210
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x56ff
callq 0x2210
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x5710
callq 0x2210
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x571e
callq 0x2210
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x572f
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 0x575a
callq 0x2210
jmp 0x575a
movq %rax, %rbx
movq -0xd0(%rbp), %rdi
testq %rdi, %rdi
je 0x5770
callq 0x2210
jmp 0x5770
movq %rax, %rbx
movq -0xe8(%rbp), %rdi
testq %rdi, %rdi
je 0x5786
callq 0x2210
jmp 0x5786
movq %rax, %rbx
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0x579c
callq 0x2210
jmp 0x579c
movq %rax, %rbx
movq -0x60(%rbp), %rdi
testq %rdi, %rdi
je 0x57af
callq 0x2210
jmp 0x57af
movq %rax, %rbx
movq -0xa8(%rbp), %rdi
testq %rdi, %rdi
je 0x57c5
callq 0x2210
jmp 0x57c5
movq %rax, %rbx
movq -0x78(%rbp), %rdi
testq %rdi, %rdi
je 0x57d8
callq 0x2210
jmp 0x57d8
movq %rax, %rbx
movq -0x100(%rbp), %rdi
testq %rdi, %rdi
je 0x57e9
callq 0x2210
movq %rbx, %rdi
callq 0x2390
nop