// File: abaheti95[P]HMM-LDA/Models/cpp_hmm_lda/model.cpp
// Function: HMMLDA::draw_topic(int, int, int)
void HMMLDA::draw_topic(int document_idx, int word_idx, int doc_size) {
	int old_topic = topic_assignments[document_idx][word_idx];
	int old_class = class_assignments[document_idx][word_idx];
	int word = documents[document_idx][word_idx];

	// Exclude current word from topic counts
	if(old_class == 0) {
		num_words_in_doc_assigned_to_topic[document_idx][old_topic]--;
		num_same_words_assigned_to_topic[word][old_topic]--;
		num_words_assigned_to_topic[old_topic]--;
	}


	// Initialize probability proportions with document topic counts
	vector<double> proportions(num_words_in_doc_assigned_to_topic[document_idx].begin(), 
		num_words_in_doc_assigned_to_topic[document_idx].end());

	// Smoothing
	for(int i = 0; i < num_topics; i++)
		proportions[i] += alpha;

	// If the current word is assigned to the semantic class
	// TODO: I have commented next line. Remove it later
	if(old_class == 0) {
		// Initialize numerator with same word topic counts
		vector<double> numerator(num_same_words_assigned_to_topic[word].begin(),
			num_same_words_assigned_to_topic[word].end());
		
		// Initialize denominator with global topic counts
		vector<double> denominator(num_words_assigned_to_topic.begin(), num_words_assigned_to_topic.end());

		for(int i = 0; i < num_topics; i++) {
			// Smoothing
			numerator[i] += beta;
			denominator[i] += vocab_size * beta;
			// Apply multiplier
			proportions[i] = proportions[i] * numerator[i] / denominator[i];
		}
	// TODO: I have commented next line. Remove it later
	}
	
	// Draw topic
	// logging.info('proportions = %s', proportions)
	int new_topic = categorical(proportions);
	// logging.info('drew topic %d', new_topic)
	topic_assignments[document_idx][word_idx] = new_topic;

	// Correct counts with new topic
	if(old_class == 0) {
		num_words_in_doc_assigned_to_topic[document_idx][new_topic] += 1;
		num_same_words_assigned_to_topic[word][new_topic] += 1;
		num_words_assigned_to_topic[new_topic] += 1;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x160, %rsp            # imm = 0x160
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xe8(%rbp)
addq $0xc0, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x10(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x18(%rbp)
addq $0xd8, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x10(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0x1c(%rbp)
addq $0xa8, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x10(%rbp), %rsi
callq 0x8f60
movl (%rax), %eax
movl %eax, -0x20(%rbp)
cmpl $0x0, -0x1c(%rbp)
jne 0x64ec
movq -0xe8(%rbp), %rdi
addq $0xf0, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x18(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x108, %rdi            # imm = 0x108
movslq -0x20(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x18(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x150, %rdi            # imm = 0x150
movslq -0x18(%rbp), %rsi
callq 0x8f60
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq -0xe8(%rbp), %rdi
addq $0xf0, %rdi
movq %rdi, -0xf8(%rbp)
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91c0
movq -0xf8(%rbp), %rdi
movq %rax, -0x40(%rbp)
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91f0
movq %rax, -0x48(%rbp)
leaq -0x49(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x9100
movq -0xf0(%rbp), %rcx
movq -0x40(%rbp), %rsi
movq -0x48(%rbp), %rdx
leaq -0x38(%rbp), %rdi
callq 0x9220
jmp 0x655c
leaq -0x49(%rbp), %rdi
callq 0x91a0
movl $0x0, -0x60(%rbp)
movq -0xe8(%rbp), %rcx
movl -0x60(%rbp), %eax
cmpl (%rcx), %eax
jge 0x65d0
movq -0xe8(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0x100(%rbp)
movslq -0x60(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x89a0
movsd -0x100(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl -0x60(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x60(%rbp)
jmp 0x656c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x49(%rbp), %rdi
callq 0x91a0
jmp 0x69b0
cmpl $0x0, -0x1c(%rbp)
jne 0x6871
movq -0xe8(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
movq %rdi, -0x110(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91c0
movq -0x110(%rbp), %rdi
movq %rax, -0x80(%rbp)
movslq -0x20(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91f0
movq %rax, -0x88(%rbp)
leaq -0x89(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x9100
movq -0x108(%rbp), %rcx
movq -0x80(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x78(%rbp), %rdi
callq 0x9220
jmp 0x6653
leaq -0x89(%rbp), %rdi
callq 0x91a0
movq -0xe8(%rbp), %rdi
addq $0x150, %rdi            # imm = 0x150
movq %rdi, -0x120(%rbp)
callq 0x91c0
movq -0x120(%rbp), %rdi
movq %rax, -0xb0(%rbp)
callq 0x91f0
movq %rax, -0xb8(%rbp)
leaq -0xb9(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x9100
movq -0x118(%rbp), %rcx
movq -0xb0(%rbp), %rsi
movq -0xb8(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
callq 0x9220
jmp 0x66c9
leaq -0xb9(%rbp), %rdi
callq 0x91a0
movl $0x0, -0xc0(%rbp)
movq -0xe8(%rbp), %rcx
movl -0xc0(%rbp), %eax
cmpl (%rcx), %eax
jge 0x685c
movq -0xe8(%rbp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x89a0
movsd -0x150(%rbp), %xmm0
movq %rax, %rcx
movq -0xe8(%rbp), %rax
addsd (%rcx), %xmm0
movsd %xmm0, (%rcx)
cvtsi2sdl 0xa0(%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x140(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x89a0
movsd -0x148(%rbp), %xmm0
movsd -0x140(%rbp), %xmm2
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movslq -0xc0(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x89a0
movsd (%rax), %xmm0
movsd %xmm0, -0x138(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x89a0
movsd -0x138(%rbp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, -0x130(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0xa8(%rbp), %rdi
callq 0x89a0
movsd -0x130(%rbp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, -0x128(%rbp)
movslq -0xc0(%rbp), %rsi
leaq -0x38(%rbp), %rdi
callq 0x89a0
movsd -0x128(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0xc0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xc0(%rbp)
jmp 0x66df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x91a0
jmp 0x69a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xb9(%rbp), %rdi
callq 0x91a0
leaq -0x78(%rbp), %rdi
callq 0x9390
jmp 0x69a7
leaq -0xa8(%rbp), %rdi
callq 0x9390
leaq -0x78(%rbp), %rdi
callq 0x9390
leaq -0xe0(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x92a0
jmp 0x6883
leaq -0xe0(%rbp), %rdi
callq 0x3680
movl %eax, -0x154(%rbp)
jmp 0x6897
leaq -0xe0(%rbp), %rdi
callq 0x9390
movl -0x154(%rbp), %eax
movq -0xe8(%rbp), %rdi
movl %eax, -0xc4(%rbp)
movl -0xc4(%rbp), %eax
movl %eax, -0x158(%rbp)
addq $0xc0, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x10(%rbp), %rsi
callq 0x8f60
movl -0x158(%rbp), %ecx
movl %ecx, (%rax)
cmpl $0x0, -0x1c(%rbp)
jne 0x6995
movq -0xe8(%rbp), %rdi
addq $0xf0, %rdi
movslq -0xc(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0xc4(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x108, %rdi            # imm = 0x108
movslq -0x20(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0xc4(%rbp), %rsi
callq 0x8f60
movq -0xe8(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x150, %rdi            # imm = 0x150
movslq -0xc4(%rbp), %rsi
callq 0x8f60
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x6995
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x69a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x9390
jmp 0x69a7
leaq -0x38(%rbp), %rdi
callq 0x9390
addq $0x160, %rsp            # imm = 0x160
popq %rbp
retq
leaq -0x38(%rbp), %rdi
callq 0x9390
movq -0x58(%rbp), %rdi
callq 0x3400
nopl (%rax)