// File: /workspace/github_cpp_makefile_star2_O3/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
movq %rdi, %rbx
movb $0x24, -0x29(%rbp)
leaq -0x48(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movslq %esi, %rax
imulq $0x38, %rax, %r12
movq (%rdi), %rdi
addq %r12, %rdi
leaq 0x88(%rbx), %rsi
callq 0x2040
testl %eax, %eax
je 0x2adb
movq 0x28(%rbx), %rcx
xorl %r15d, %r15d
cmpq %rcx, 0x48(%rbx)
je 0x2ae1
leaq 0x18(%rbx), %rsi
movq (%rbx), %rax
movq (%rax,%r12), %rdx
addq %r12, %rax
movzbl (%rdx), %edx
orl $0x20, %edx
cmpl $0x72, %edx
jne 0x2b08
movb (%rcx), %cl
addq $0x20, %rax
movq %rax, %r14
movq (%r14), %r14
cmpq %rax, %r14
je 0x2ae1
cmpb %cl, 0x30(%r14)
jne 0x2a55
movq %rsi, -0x38(%rbp)
leaq 0x10(%r14), %rsi
leaq -0x58(%rbp), %rdi
callq 0x20e0
movb 0x31(%r14), %al
movb %al, -0x29(%rbp)
movq -0x38(%rbp), %rdi
callq 0x3836
movq (%rbx), %rsi
cmpq %rsi, 0x8(%rbx)
je 0x2ae1
xorl %r14d, %r14d
leaq -0x58(%rbp), %r12
movabsq $0x6db6db6db6db6db7, %r13 # imm = 0x6DB6DB6DB6DB6DB7
xorl %r15d, %r15d
addq %r14, %rsi
movq %r12, %rdi
callq 0x2040
testl %eax, %eax
je 0x2ba1
incq %r15
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x3, %rax
imulq %r13, %rax
addq $0x38, %r14
cmpq %r15, %rax
ja 0x2aa5
jmp 0x2b72
movl $0x1, %r15d
movq -0x58(%rbp), %rdi
cmpq %r13, %rdi
je 0x2af6
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x2180
movl %r15d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, -0x38(%rbp)
movq 0x20(%rax), %rsi
addq $0x10, %rsi
leaq -0x58(%rbp), %rdi
callq 0x20e0
movq (%rbx), %rsi
movq 0x20(%rsi,%r12), %rax
movb 0x31(%rax), %al
movb %al, -0x29(%rbp)
cmpq %rsi, 0x8(%rbx)
je 0x2ae1
xorl %r14d, %r14d
leaq -0x58(%rbp), %r12
movabsq $0x6db6db6db6db6db7, %r13 # imm = 0x6DB6DB6DB6DB6DB7
xorl %r15d, %r15d
addq %r14, %rsi
movq %r12, %rdi
callq 0x2040
testl %eax, %eax
je 0x2b7e
incq %r15
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x3, %rax
imulq %r13, %rax
addq $0x38, %r14
cmpq %r15, %rax
ja 0x2b45
xorl %r15d, %r15d
leaq -0x48(%rbp), %r13
jmp 0x2ae1
movb -0x29(%rbp), %al
cmpb $0x24, %al
leaq -0x48(%rbp), %r13
je 0x2bd1
movq 0x48(%rbx), %rcx
movq 0x58(%rbx), %rdx
decq %rdx
cmpq %rdx, %rcx
je 0x2bc4
movb %al, (%rcx)
incq 0x48(%rbx)
jmp 0x2bd1
movb -0x29(%rbp), %al
cmpb $0x24, %al
leaq -0x48(%rbp), %r13
je 0x2beb
movq 0x48(%rbx), %rcx
movq 0x58(%rbx), %rdx
decq %rdx
cmpq %rdx, %rcx
je 0x2bde
movb %al, (%rcx)
incq 0x48(%rbx)
jmp 0x2beb
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x388c
movq %rbx, %rdi
movl %r15d, %esi
callq 0x29d0
jmp 0x2bf6
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x388c
movq %rbx, %rdi
movl %r15d, %esi
callq 0x29d0
movl %eax, %r15d
jmp 0x2ae1
jmp 0x2c06
jmp 0x2c06
jmp 0x2c06
jmp 0x2c06
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x2c22
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2250