// File: /workspace/github_cpp_makefile_star2_O2/luizaes[P]post-machine-simulator/post_machine_sim.cpp
// Function: PostMachineSimulator::TraverseDiagram(int)
int PostMachineSimulator::TraverseDiagram(int current_node) {

	char symbol, current_queue_symbol = NONE;
	bool transition_found = false;
	string name;
	list<state>::iterator it;

	/*
		If the work queue is empty and we haven't reached the accepting state, reject entry
		If we reached the accepting state, accept entry
	*/ 
	if(flow_diagram[current_node].name.compare(accept_state) == 0) {
		return ACCEPT;
	} else if(work_queue.empty()) {
		return REJECT;
	}

	/* If current state is READ */
	if(flow_diagram[current_node].name.front() == 'R' || flow_diagram[current_node].name.front() == 'r') {
		
		/* Check first element of the queue */
		symbol = work_queue.front();
		for(it = flow_diagram[current_node].flows.begin(); it != flow_diagram[current_node].flows.end(); it++) {
			if((*it).read_symbol == symbol) {
				transition_found = true;
				name = (*it).name;
				current_queue_symbol = (*it).add_symbol;
				break;
			}
		}

		/* If the transition was not found, the machine halts by indefinition */
		if(!transition_found) {
			return REJECT;
		} else {
			/* Remove element from the queue, since it was already read */
			work_queue.pop();
			for(int i = 0; i < flow_diagram.size(); i++) {
				if(name.compare(flow_diagram[i].name) == 0) {
					if(current_queue_symbol != NONE) {
						work_queue.push(current_queue_symbol);
					}
					return TraverseDiagram(i);
				}
			}
		}

	/* If current state is ADD or START */
	} else {
		name = flow_diagram[current_node].flows.front().name;
		current_queue_symbol = flow_diagram[current_node].flows.front().add_symbol;

		for(int i = 0; i < flow_diagram.size(); i++) {
			if(name.compare(flow_diagram[i].name) == 0) {
				if(current_queue_symbol != NONE) {
					work_queue.push(current_queue_symbol);
				}
				return TraverseDiagram(i);
			}
		}
	}
	return REJECT;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movq %rdi, %rbx
movb $0x24, -0x29(%rbp)
movb $0x0, (%rax)
movslq %esi, %rax
imulq $0x38, %rax, %r12
movq (%rdi), %rdi
addq %r12, %rdi
leaq 0x88(%rbx), %rsi
callq 0x2030
testl %eax, %eax
je 0x2a72
movq 0x28(%rbx), %rcx
xorl %r13d, %r13d
cmpq %rcx, 0x48(%rbx)
je 0x2b1e
leaq 0x18(%rbx), %r15
movq (%rbx), %rax
movq (%rax,%r12), %rdx
addq %r12, %rax
movzbl (%rdx), %edx
orl $0x20, %edx
cmpl $0x72, %edx
jne 0x2a7b
movb (%rcx), %cl
addq $0x20, %rax
movq %rax, %r14
movq (%r14), %r14
cmpq %rax, %r14
je 0x2b1e
cmpb %cl, 0x30(%r14)
jne 0x29fe
leaq 0x10(%r14), %rsi
leaq -0x58(%rbp), %rdi
callq 0x20f0
movb 0x31(%r14), %al
movb %al, -0x29(%rbp)
movq %r15, -0x38(%rbp)
movq %r15, %rdi
callq 0x33e2
pushq $0x38
popq %r14
leaq -0x58(%rbp), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
idivq %r14
cmpq %r12, %rax
jbe 0x2ad8
addq %r15, %rsi
movq %r13, %rdi
callq 0x2030
testl %eax, %eax
je 0x2afd
incq %r12
addq $0x38, %r15
jmp 0x2a3e
pushq $0x1
popq %r13
jmp 0x2b1e
movq 0x20(%rax), %rsi
addq $0x10, %rsi
leaq -0x58(%rbp), %rdi
callq 0x20f0
movq %r15, -0x38(%rbp)
movq (%rbx), %rsi
movq 0x20(%rsi,%r12), %rax
movb 0x31(%rax), %al
movb %al, -0x29(%rbp)
pushq $0x38
popq %r14
leaq -0x58(%rbp), %r13
xorl %r15d, %r15d
xorl %r12d, %r12d
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
idivq %r14
cmpq %r12, %rax
jbe 0x2ad8
addq %r15, %rsi
movq %r13, %rdi
callq 0x2030
testl %eax, %eax
je 0x2add
incq %r12
movq (%rbx), %rsi
addq $0x38, %r15
jmp 0x2aac
xorl %r13d, %r13d
jmp 0x2b1e
cmpb $0x24, -0x29(%rbp)
je 0x2af0
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x3448
movq %rbx, %rdi
movl %r12d, %esi
callq 0x297e
jmp 0x2b1b
cmpb $0x24, -0x29(%rbp)
je 0x2b10
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x3448
movq %rbx, %rdi
movl %r12d, %esi
callq 0x297e
movl %eax, %r13d
leaq -0x58(%rbp), %rdi
callq 0x20c0
movl %r13d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2b41
jmp 0x2b41
jmp 0x2b41
jmp 0x2b41
movq %rax, %rbx
leaq -0x58(%rbp), %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2250
nop