// File: abaheti95[P]HMM-LDA/Models/cpp_hmm_lda/model.cpp
// Function: HMMLDA::spl_draw_topic(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_topic(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_topic = topic_assignment[word_idx];
	int old_class = class_assignment[word_idx];
	int word = document[word_idx];

	// Exclude current word from topic counts
	if(old_class == 0) {
		num_words_in_document_assigned_to_topic[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_document_assigned_to_topic.begin(), 
		num_words_in_document_assigned_to_topic.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_assignment[word_idx] = new_topic;

	// Correct counts with new topic
	if(old_class == 0) {
		num_words_in_document_assigned_to_topic[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 $0x170, %rsp            # imm = 0x170
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl %r9d, -0x2c(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0x8f60
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0x8f60
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0x8f60
movl (%rax), %eax
movl %eax, -0x38(%rbp)
cmpl $0x0, -0x34(%rbp)
jne 0x8269
movq -0x28(%rbp), %rdi
movslq -0x30(%rbp), %rsi
callq 0x8f60
movq -0x100(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x108, %rdi            # imm = 0x108
movslq -0x38(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0x30(%rbp), %rsi
callq 0x8f60
movq -0x100(%rbp), %rdi
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
addq $0x150, %rdi            # imm = 0x150
movslq -0x30(%rbp), %rsi
callq 0x8f60
movl (%rax), %ecx
addl $-0x1, %ecx
movl %ecx, (%rax)
movq -0x28(%rbp), %rdi
callq 0x91c0
movq %rax, -0x58(%rbp)
movq -0x28(%rbp), %rdi
callq 0x91f0
movq %rax, -0x60(%rbp)
leaq -0x61(%rbp), %rdi
movq %rdi, -0x108(%rbp)
callq 0x9100
movq -0x108(%rbp), %rcx
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
leaq -0x50(%rbp), %rdi
callq 0x9220
jmp 0x82ad
leaq -0x61(%rbp), %rdi
callq 0x91a0
movl $0x0, -0x78(%rbp)
movq -0x100(%rbp), %rcx
movl -0x78(%rbp), %eax
cmpl (%rcx), %eax
jge 0x8321
movq -0x100(%rbp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, -0x110(%rbp)
movslq -0x78(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x89a0
movsd -0x110(%rbp), %xmm0
addsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl -0x78(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x78(%rbp)
jmp 0x82bd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0x61(%rbp), %rdi
callq 0x91a0
jmp 0x86ea
cmpl $0x0, -0x34(%rbp)
jne 0x85d7
movq -0x100(%rbp), %rdi
addq $0x108, %rdi            # imm = 0x108
movq %rdi, -0x120(%rbp)
movslq -0x38(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91c0
movq -0x120(%rbp), %rdi
movq %rax, -0x98(%rbp)
movslq -0x38(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
callq 0x91f0
movq %rax, -0xa0(%rbp)
leaq -0xa1(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x9100
movq -0x118(%rbp), %rcx
movq -0x98(%rbp), %rsi
movq -0xa0(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x9220
jmp 0x83ad
leaq -0xa1(%rbp), %rdi
callq 0x91a0
movq -0x100(%rbp), %rdi
addq $0x150, %rdi            # imm = 0x150
movq %rdi, -0x130(%rbp)
callq 0x91c0
movq -0x130(%rbp), %rdi
movq %rax, -0xc8(%rbp)
callq 0x91f0
movq %rax, -0xd0(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x128(%rbp)
callq 0x9100
movq -0x128(%rbp), %rcx
movq -0xc8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x9220
jmp 0x8423
leaq -0xd1(%rbp), %rdi
callq 0x91a0
movl $0x0, -0xd8(%rbp)
movq -0x100(%rbp), %rcx
movl -0xd8(%rbp), %eax
cmpl (%rcx), %eax
jge 0x85bf
movq -0x100(%rbp), %rax
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x160(%rbp)
movslq -0xd8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x89a0
movsd -0x160(%rbp), %xmm0
movq %rax, %rcx
movq -0x100(%rbp), %rax
addsd (%rcx), %xmm0
movsd %xmm0, (%rcx)
cvtsi2sdl 0xa0(%rax), %xmm0
movsd %xmm0, -0x158(%rbp)
movsd 0x20(%rax), %xmm0
movsd %xmm0, -0x150(%rbp)
movslq -0xd8(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x89a0
movsd -0x158(%rbp), %xmm0
movsd -0x150(%rbp), %xmm2
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movslq -0xd8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x89a0
movsd (%rax), %xmm0
movsd %xmm0, -0x148(%rbp)
movslq -0xd8(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x89a0
movsd -0x148(%rbp), %xmm0
mulsd (%rax), %xmm0
movsd %xmm0, -0x140(%rbp)
movslq -0xd8(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x89a0
movsd -0x140(%rbp), %xmm0
divsd (%rax), %xmm0
movsd %xmm0, -0x138(%rbp)
movslq -0xd8(%rbp), %rsi
leaq -0x50(%rbp), %rdi
callq 0x89a0
movsd -0x138(%rbp), %xmm0
movsd %xmm0, (%rax)
movl -0xd8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xd8(%rbp)
jmp 0x8439
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xa1(%rbp), %rdi
callq 0x91a0
jmp 0x86e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xd1(%rbp), %rdi
callq 0x91a0
leaq -0x90(%rbp), %rdi
callq 0x9390
jmp 0x86e1
leaq -0xc0(%rbp), %rdi
callq 0x9390
leaq -0x90(%rbp), %rdi
callq 0x9390
leaq -0xf8(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x92a0
jmp 0x85e9
leaq -0xf8(%rbp), %rdi
callq 0x3680
movl %eax, -0x164(%rbp)
jmp 0x85fd
leaq -0xf8(%rbp), %rdi
callq 0x9390
movl -0x164(%rbp), %eax
movl %eax, -0xdc(%rbp)
movl -0xdc(%rbp), %eax
movl %eax, -0x168(%rbp)
movq -0x18(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0x8f60
movl -0x168(%rbp), %ecx
movl %ecx, (%rax)
cmpl $0x0, -0x34(%rbp)
jne 0x86cf
movq -0x28(%rbp), %rdi
movslq -0xdc(%rbp), %rsi
callq 0x8f60
movq -0x100(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x108, %rdi            # imm = 0x108
movslq -0x38(%rbp), %rsi
callq 0x8c10
movq %rax, %rdi
movslq -0xdc(%rbp), %rsi
callq 0x8f60
movq -0x100(%rbp), %rdi
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
addq $0x150, %rdi            # imm = 0x150
movslq -0xdc(%rbp), %rsi
callq 0x8f60
movl (%rax), %ecx
addl $0x1, %ecx
movl %ecx, (%rax)
jmp 0x86cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
jmp 0x86e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x70(%rbp)
movl %eax, -0x74(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x9390
jmp 0x86e1
leaq -0x50(%rbp), %rdi
callq 0x9390
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
leaq -0x50(%rbp), %rdi
callq 0x9390
movq -0x70(%rbp), %rdi
callq 0x3400
nopw %cs:(%rax,%rax)
nopl (%rax)