// File: /workspace/github_cpp_makefile_star2_O1/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), %r12
movq %r12, -0x10(%r12)
movq $0x0, -0x8(%r12)
movb $0x0, (%r12)
movslq %esi, %rax
imulq $0x38, %rax, %r14
movq (%rdi), %rdi
addq %r14, %rdi
leaq 0x88(%rbx), %rsi
callq 0x2040
testl %eax, %eax
je 0x2ae5
movq 0x28(%rbx), %rcx
xorl %r15d, %r15d
cmpq %rcx, 0x48(%rbx)
je 0x2be6
leaq 0x18(%rbx), %r13
movq (%rbx), %rax
movq (%rax,%r14), %rdx
addq %r14, %rax
movzbl (%rdx), %edx
orl $0x20, %edx
cmpl $0x72, %edx
jne 0x2af0
movb (%rcx), %cl
addq $0x20, %rax
movq %rax, %r14
movq (%r14), %r14
cmpq %rax, %r14
je 0x2be6
cmpb %cl, 0x30(%r14)
jne 0x2a57
leaq 0x10(%r14), %rsi
leaq -0x58(%rbp), %rdi
callq 0x20e0
movb 0x31(%r14), %al
movb %al, -0x29(%rbp)
movq %r13, %rdi
callq 0x38d8
movq (%rbx), %rsi
cmpq %rsi, 0x8(%rbx)
setne %r14b
je 0x2b64
movq %r13, -0x38(%rbp)
xorl %r13d, %r13d
xorl %r12d, %r12d
addq %r13, %rsi
leaq -0x58(%rbp), %rdi
callq 0x2040
testl %eax, %eax
je 0x2b85
incq %r12
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x3, %rax
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rax
addq $0x38, %r13
cmpq %r12, %rax
seta %r14b
ja 0x2aa0
jmp 0x2bd9
movl $0x1, %r15d
jmp 0x2be6
movq 0x20(%rax), %rsi
addq $0x10, %rsi
leaq -0x58(%rbp), %rdi
callq 0x20e0
movq (%rbx), %rax
movq 0x20(%rax,%r14), %rax
movb 0x31(%rax), %al
movb %al, -0x29(%rbp)
movq (%rbx), %rsi
cmpq %rsi, 0x8(%rbx)
setne %r14b
je 0x2b64
movq %r13, -0x38(%rbp)
xorl %r13d, %r13d
xorl %r12d, %r12d
addq %r13, %rsi
leaq -0x58(%rbp), %rdi
callq 0x2040
testl %eax, %eax
je 0x2b66
incq %r12
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
sarq $0x3, %rax
movabsq $0x6db6db6db6db6db7, %rcx # imm = 0x6DB6DB6DB6DB6DB7
imulq %rcx, %rax
addq $0x38, %r13
cmpq %r12, %rax
seta %r14b
ja 0x2b26
jmp 0x2bd9
jmp 0x2bd9
movb -0x29(%rbp), %al
cmpb $0x24, %al
je 0x2bb1
movq 0x48(%rbx), %rcx
movq 0x58(%rbx), %rdx
decq %rdx
cmpq %rdx, %rcx
je 0x2ba4
movb %al, (%rcx)
incq 0x48(%rbx)
jmp 0x2bb1
movb -0x29(%rbp), %al
cmpb $0x24, %al
je 0x2bcb
movq 0x48(%rbx), %rcx
movq 0x58(%rbx), %rdx
decq %rdx
cmpq %rdx, %rcx
je 0x2bbe
movb %al, (%rcx)
incq 0x48(%rbx)
jmp 0x2bcb
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x392e
movq %rbx, %rdi
movl %r12d, %esi
callq 0x29d0
jmp 0x2bd6
leaq -0x29(%rbp), %rsi
movq -0x38(%rbp), %rdi
callq 0x392e
movq %rbx, %rdi
movl %r12d, %esi
callq 0x29d0
movl %eax, %r15d
testb $0x1, %r14b
leaq -0x48(%rbp), %r12
jne 0x2be6
xorl %r15d, %r15d
movq -0x58(%rbp), %rdi
cmpq %r12, %rdi
je 0x2bfb
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
jmp 0x2c15
jmp 0x2c15
jmp 0x2c15
jmp 0x2c15
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0x2c31
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x2180
movq %rbx, %rdi
callq 0x2250
nop