// File: giovifazi[P]knossos-bloodbath/Node.cpp
// Function: Node::search_node(int, int, Node&, Map&)
Node* Node::search_node(int x, int y, Node& current, Map& search_map)
{
 
   //std::cout << "cerco il nodo " << x << " " << y << "\ne sono nel " << current.x << " " << current.y << std::endl;

   // inizializzo il valore di ritorno a null ad ogni chiamata ricorsiva
	Node* ret = NULL;

	// Parte di debugging, cambia i valori nella mappa di ricerca per mostrare in
	// quale nodo sta avvenendo la ricerca e poi stampa la mappa
	search_map.changeValue(current.get_x(), current.get_y(), 2, 0);
	//search_map.draw_search();
	search_map.changeValue(current.get_x(), current.get_y(), 1, 0);
	//std::cout << "\n";

   // Caso base ricorsione (se il nodo e` stato trovato)
	if (current.get_x() == x && current.get_y() == y)
	{
			ret = &current;
			return ret;
	}
	else
	{
      // In base al nodo su cui mi trovo, se il puntatore a nord/est/sud/ovest
      // non e` nullo e se secondo la mappa non ho gia`  visitato il nodo a
      // nord/sud/est/ovest, aggiorno la mappa di ricerca e chiamo la funzione
      // sul nodo nel quale ho itenzione di spostarmi.
      // Se il nodo viene trovato, si assegna a ret l' indirizzo del nodo
      // e tramite if (ret != NULL) viene ritornato fino alla prima chiamata di
      // ricerca

		if (current.get_cardinal('n') != NULL && search_map.getValueAt(current.get_x(), current.get_y()+1) != 1)
		{
			if (search_map.getValueAt(current.get_x(), current.get_y()+1) == -99999)
				search_map.addLine(true);

			ret = search_node(x,y, (*current.nord), search_map);
		}

		if (ret != NULL)
			return ret;
		else
		{
			if (current.get_cardinal('e') != NULL && search_map.getValueAt(current.get_x()+1, current.get_y()) != 1)
			{
				if (search_map.getValueAt(current.get_x()+1, current.get_y()) == -99999)
					search_map.addColumn(false);

				ret = search_node(x,y, (*current.est), search_map);
			}
		}

		if (ret != NULL)
			return ret;
		else
		{
			if (current.get_cardinal('s') != NULL && search_map.getValueAt(current.get_x(), current.get_y()-1) != 1)
			{
				if (search_map.getValueAt(current.get_x(), current.get_y()-1) == -99999)
					search_map.addLine(false);

				ret = search_node(x,y, (*current.sud), search_map);
			}
		}

		if (ret != NULL)
			return ret;
		else
		{
			if (current.get_cardinal('o') != NULL && search_map.getValueAt(current.get_x()-1, current.get_y()) != 1)
			{
				if (search_map.getValueAt(current.get_x()-1, current.get_y()) == -99999)
					search_map.addColumn(true);

				ret = search_node(x,y, (*current.ovest), search_map);
			}
		}

		return ret;
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0x44(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x50(%rbp), %rdi
movl -0x44(%rbp), %esi
movl %eax, %edx
movl $0x2, %ecx
xorl %r8d, %r8d
callq 0x4f50
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x40(%rbp), %rdi
movl -0x34(%rbp), %esi
movl %eax, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x4f50
movq -0x20(%rbp), %rdi
callq 0x3510
cmpl -0x14(%rbp), %eax
jne 0x3754
movq -0x20(%rbp), %rdi
callq 0x3530
cmpl -0x18(%rbp), %eax
jne 0x3754
movq -0x20(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3a8d
movq -0x20(%rbp), %rdi
movl $0x6e, %esi
callq 0x3590
cmpq $0x0, %rax
je 0x3800
movq -0x28(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0x5c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x68(%rbp), %rdi
movl -0x5c(%rbp), %esi
movl %eax, %edx
addl $0x1, %edx
callq 0x4e60
cmpl $0x1, %eax
je 0x3800
movq -0x28(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0x6c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x78(%rbp), %rdi
movl -0x6c(%rbp), %esi
movl %eax, %edx
addl $0x1, %edx
callq 0x4e60
cmpl $0xfffe7961, %eax       # imm = 0xFFFE7961
jne 0x37e2
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x4ca0
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x20(%rbp), %rax
movq (%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x3690
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
je 0x3814
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3a8d
movq -0x20(%rbp), %rdi
movl $0x65, %esi
callq 0x3590
cmpq $0x0, %rax
je 0x38d0
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x88(%rbp), %rdi
movl -0x7c(%rbp), %esi
movl %eax, %edx
callq 0x4e60
cmpl $0x1, %eax
je 0x38d0
movq -0x28(%rbp), %rax
movq %rax, -0x98(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0x98(%rbp), %rdi
movl -0x8c(%rbp), %esi
movl %eax, %edx
callq 0x4e60
cmpl $0xfffe7961, %eax       # imm = 0xFFFE7961
jne 0x38b1
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x4af0
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x10(%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x3690
movq %rax, -0x30(%rbp)
jmp 0x38d2
cmpq $0x0, -0x30(%rbp)
je 0x38e6
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3a8d
movq -0x20(%rbp), %rdi
movl $0x73, %esi
callq 0x3590
cmpq $0x0, %rax
je 0x39a8
movq -0x28(%rbp), %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0x9c(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0xa8(%rbp), %rdi
movl -0x9c(%rbp), %esi
movl %eax, %edx
subl $0x1, %edx
callq 0x4e60
cmpl $0x1, %eax
je 0x39a8
movq -0x28(%rbp), %rax
movq %rax, -0xb8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
movl %eax, -0xac(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0xb8(%rbp), %rdi
movl -0xac(%rbp), %esi
movl %eax, %edx
subl $0x1, %edx
callq 0x4e60
cmpl $0xfffe7961, %eax       # imm = 0xFFFE7961
jne 0x3989
movq -0x28(%rbp), %rdi
xorl %esi, %esi
callq 0x4ca0
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x3690
movq %rax, -0x30(%rbp)
jmp 0x39aa
cmpq $0x0, -0x30(%rbp)
je 0x39be
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
jmp 0x3a8d
movq -0x20(%rbp), %rdi
movl $0x6f, %esi
callq 0x3590
cmpq $0x0, %rax
je 0x3a83
movq -0x28(%rbp), %rax
movq %rax, -0xc8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
subl $0x1, %eax
movl %eax, -0xbc(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0xc8(%rbp), %rdi
movl -0xbc(%rbp), %esi
movl %eax, %edx
callq 0x4e60
cmpl $0x1, %eax
je 0x3a83
movq -0x28(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3510
subl $0x1, %eax
movl %eax, -0xcc(%rbp)
movq -0x20(%rbp), %rdi
callq 0x3530
movq -0xd8(%rbp), %rdi
movl -0xcc(%rbp), %esi
movl %eax, %edx
callq 0x4e60
cmpl $0xfffe7961, %eax       # imm = 0xFFFE7961
jne 0x3a64
movq -0x28(%rbp), %rdi
movl $0x1, %esi
callq 0x4af0
movq -0x58(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
movq -0x28(%rbp), %r8
callq 0x3690
movq %rax, -0x30(%rbp)
jmp 0x3a85
movq -0x30(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0xe0, %rsp
popq %rbp
retq
nopw (%rax,%rax)