// File: /workspace/github_cpp_makefile_star2_O1/Windfisch[P]production-flow/actiongraph.cpp
// Function: ActionGraph::dijkstra(Factory::FactoryConfiguration)
pair<Factory::FactoryConfiguration, double> ActionGraph::dijkstra(Factory::FactoryConfiguration initial_config)
{
	auto start_node = make_unique<ActionGraph::Node>();
	start_node->conf = initial_config;
	start_node->current_item_type = item_t(MAX_ITEM-1);
	start_node->total_cost = 0.;

	vector< unique_ptr< ActionGraph::Node> > openlist;
	vector< unique_ptr< ActionGraph::Node> > closedlist;
	openlist.push_back(move(start_node));

	while (!openlist.empty())
	{
		cout << "openlist has size " << openlist.size() << ", total expanded = " << openlist.size() + closedlist.size() << endl;

		// find and remove smallest element
		auto smallest = openlist.begin();
		for (auto it = openlist.begin(); it != openlist.end(); it++)
			if ((*it)->total_cost < (*smallest)->total_cost)
				smallest=it;
		
		closedlist.push_back(move(*smallest));
		auto& nodeptr = closedlist.back();
		*smallest = move(openlist.back());
		openlist.pop_back();

		cout << "inspecting item: " << nodeptr->current_item_type << ", ";
		cout << "nodes:";
		for (auto lvl : nodeptr->conf.facility_levels)
			cout << " " << lvl;
		cout << ", edges:";
		for (auto lvl : nodeptr->conf.transport_levels)
			cout << " " << lvl;
		cout << endl;


		// expand node
		auto successor_nodes = nodeptr->successors(factory);
		for (auto& successor : successor_nodes)
		{
			cout << "  -> successor item: " << successor->current_item_type << ", ";
			cout << "nodes:";
			for (auto lvl : successor->conf.facility_levels)
				cout << " " << lvl;
			cout << ", edges:";
			for (auto lvl : successor->conf.transport_levels)
				cout << " " << lvl;
			if (successor->current_item_type == DONE)
			{
				// we've found a goal state! :)
				cout << endl << "success, cost = " << successor->total_cost << ", expanded " << closedlist.size() + openlist.size() << " nodes" << endl;
				return pair<Factory::FactoryConfiguration, double>(successor->conf, successor->total_cost);
			}

			bool found = false;
			for (auto& openlist_node : openlist)
				if (openlist_node->equals(*successor, factory))
				{
					openlist_node = move(successor);
					cout << "; already in openlist" << endl;
					found = true;
					break;
				}
			if (!found)
				for (auto& closedlist_node : closedlist)
					if (closedlist_node->equals(*successor, factory))
					{
						found = true;
						cout << "; already in closedlist" << endl;
						break;
					}

			if (!found)
			{
				openlist.push_back(move(successor));
				cout << "; not seen yet, adding to openlist" << endl;
			}
		}
	}

	cout << "could not find a solution :(" << endl;
	return pair<Factory::FactoryConfiguration, double>(initial_config, -1.);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r15
movq %rsi, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
movl $0x40, %edi
callq 0x3240
movq %rax, %rbx
xorpd %xmm0, %xmm0
movupd %xmm0, (%rax)
movupd %xmm0, 0x10(%rax)
movupd %xmm0, 0x20(%rax)
movupd %xmm0, 0x30(%rax)
movq %rax, 0x18(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0xadbe
addq $0x18, %rbx
leaq 0x18(%r15), %rsi
movq %rbx, %rdi
callq 0xadbe
leaq 0x18(%rsp), %rsi
movq (%rsi), %rax
movl $0xa, 0x30(%rax)
xorl %ecx, %ecx
movq %rcx, 0x38(%rax)
movq %rsp, %rdi
movq %rcx, 0x10(%rdi)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rdi)
movq %rcx, 0x30(%rsp)
movapd %xmm0, 0x20(%rsp)
callq 0xac92
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0xab8a
movq 0xaa05(%rip), %r12      # 0x14fd0
leaq 0x5008(%rip), %rbp      # 0xf5da
movq %r15, 0x68(%rsp)
movl $0x12, %edx
movq %r12, %rdi
leaq 0x521a(%rip), %rsi      # 0xf800
callq 0x3290
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq %r12, %rdi
callq 0x3190
movq %rax, %rbx
movl $0x13, %edx
movq %rax, %rdi
leaq 0x5201(%rip), %rsi      # 0xf813
callq 0x3290
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x3, %rax
movq 0x28(%rsp), %rsi
subq 0x20(%rsp), %rsi
sarq $0x3, %rsi
addq %rax, %rsi
movq %rbx, %rdi
callq 0x3190
movq %rax, %rbx
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq (%rsp), %rbx
movq 0x8(%rsp), %rax
movq %rbx, %rcx
cmpq %rax, %rbx
je 0xa69c
movq (%rcx), %rdx
movq (%rbx), %rsi
movsd 0x38(%rsi), %xmm0
ucomisd 0x38(%rdx), %xmm0
movq %rcx, %rdx
ja 0xa690
movq %rbx, %rdx
addq $0x8, %rcx
movq %rdx, %rbx
cmpq %rax, %rcx
jne 0xa678
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0xac92
movq 0x28(%rsp), %r14
movq 0x8(%rsp), %rax
movq -0x8(%rax), %rcx
movq $0x0, -0x8(%rax)
movq (%rbx), %rsi
movq %rcx, (%rbx)
testq %rsi, %rsi
je 0xa6d2
movq %rbx, %rdi
callq 0xae94
movq 0x8(%rsp), %rax
leaq -0x8(%rax), %rbx
movq %rbx, 0x8(%rsp)
movq -0x8(%rax), %rsi
testq %rsi, %rsi
je 0xa6f1
movq %rbx, %rdi
callq 0xae94
movq $0x0, (%rbx)
movl $0x11, %edx
movq %r12, %rdi
leaq 0x5120(%rip), %rsi      # 0xf827
callq 0x3290
movq -0x8(%r14), %rax
movl 0x30(%rax), %esi
movq %r12, %rdi
callq 0x33f0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4eaf(%rip), %rsi      # 0xf5d9
callq 0x3290
movl $0x6, %edx
movq %r12, %rdi
leaq 0x50fb(%rip), %rsi      # 0xf839
callq 0x3290
movq -0x8(%r14), %rax
movq (%rax), %r15
movq 0x8(%rax), %r13
cmpq %r13, %r15
je 0xa777
movq (%r15), %rbx
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa74e
movl $0x8, %edx
movq %r12, %rdi
leaq 0x50ba(%rip), %rsi      # 0xf840
callq 0x3290
movq -0x8(%r14), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %r13
cmpq %r13, %r15
je 0xa7c0
movq (%r15), %rbx
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa797
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r12, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq -0x8(%r14), %rsi
movq 0x40(%rsp), %rax
movq (%rax), %rdx
leaq 0x50(%rsp), %rdi
callq 0x9e40
movq 0x50(%rsp), %r14
movq 0x58(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, %r14
je 0xab6c
movl $0x15, %edx
movq %r12, %rdi
leaq 0x5024(%rip), %rsi      # 0xf849
callq 0x3290
movq (%r14), %rax
movl 0x30(%rax), %esi
movq %r12, %rdi
callq 0x33f0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4d92(%rip), %rsi      # 0xf5d9
callq 0x3290
movl $0x6, %edx
movq %r12, %rdi
leaq 0x4fde(%rip), %rsi      # 0xf839
callq 0x3290
movq (%r14), %rax
movq (%rax), %r15
movq 0x8(%rax), %r13
cmpq %r13, %r15
je 0xa893
movq (%r15), %rbx
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa86a
movl $0x8, %edx
movq %r12, %rdi
leaq 0x4f9e(%rip), %rsi      # 0xf840
callq 0x3290
movq (%r14), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %r13
cmpq %r13, %r15
je 0xa8db
movq (%r15), %rbx
movl $0x1, %edx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbx, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa8b2
movq (%r14), %rax
movl 0x30(%rax), %r15d
cmpl $-0x1, %r15d
je 0xaa78
movl %r15d, 0x4c(%rsp)
movq (%rsp), %rbx
movq 0x8(%rsp), %rbp
xorl %r15d, %r15d
cmpq %rbp, %rbx
je 0xa993
movq (%rbx), %rdi
movq (%r14), %rsi
movq 0x40(%rsp), %rax
movq (%rax), %rdx
callq 0x9c74
movl %eax, %r13d
testb %al, %al
je 0xa97c
movq (%r14), %rax
movq $0x0, (%r14)
movq (%rbx), %rsi
movq %rax, (%rbx)
testq %rsi, %rsi
je 0xa93d
movq %rbx, %rdi
callq 0xae94
movl $0x15, %edx
movq %r12, %rdi
leaq 0x4f37(%rip), %rsi      # 0xf883
callq 0x3290
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r12, %rdi
callq 0x3030
movb $0x1, %r15b
movq %rax, %rdi
callq 0x31a0
addq $0x8, %rbx
cmpq %rbp, %rbx
sete %al
orb %al, %r13b
cmpb $0x1, %r13b
jne 0xa906
testb $0x1, %r15b
jne 0xaa16
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rbp
cmpq %rbp, %r13
je 0xaa16
addq $0x8, %r13
movq -0x8(%r13), %rdi
movq (%r14), %rsi
movq 0x40(%rsp), %rax
movq (%rax), %rdx
callq 0x9c74
movl %eax, %ebx
testb %al, %al
je 0xaa05
movl $0x17, %edx
movq %r12, %rdi
leaq 0x4ec4(%rip), %rsi      # 0xf899
callq 0x3290
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r12, %rdi
callq 0x3030
movb $0x1, %r15b
movq %rax, %rdi
callq 0x31a0
cmpq %rbp, %r13
sete %al
orb %al, %bl
addq $0x8, %r13
cmpb $0x1, %bl
jne 0xa9ac
testb $0x1, %r15b
leaq 0x4bb9(%rip), %rbp      # 0xf5da
movl 0x4c(%rsp), %r15d
jne 0xab53
movq %rsp, %rdi
movq %r14, %rsi
callq 0xac92
movl $0x22, %edx
movq %r12, %rdi
leaq 0x4e6b(%rip), %rsi      # 0xf8b1
callq 0x3290
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r12, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
jmp 0xab53
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r12, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq %rax, %rbx
movl $0x10, %edx
movq %rax, %rdi
leaq 0x4dad(%rip), %rsi      # 0xf85f
callq 0x3290
movq (%r14), %rax
movsd 0x38(%rax), %xmm0
movq %rbx, %rdi
callq 0x3380
movq %rax, %rbx
movl $0xb, %edx
movq %rax, %rdi
leaq 0x4d97(%rip), %rsi      # 0xf870
callq 0x3290
movq 0x28(%rsp), %rax
subq 0x20(%rsp), %rax
sarq $0x3, %rax
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
addq %rax, %rsi
movq %rbx, %rdi
callq 0x3190
movq %rax, %rbx
movl $0x6, %edx
movq %rax, %rdi
leaq 0x4d66(%rip), %rsi      # 0xf87c
callq 0x3290
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %rbx, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq (%r14), %rsi
leaq 0x38(%rsi), %rdx
movq 0x38(%rsp), %rdi
callq 0xad1a
cmpl $-0x1, %r15d
je 0xabf1
addq $0x8, %r14
cmpq 0x70(%rsp), %r14
jne 0xa816
leaq 0x50(%rsp), %rdi
callq 0xacca
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
movq 0x68(%rsp), %r15
jne 0xa5d7
movq 0xa43f(%rip), %rbx      # 0x14fd0
leaq 0x4d3c(%rip), %rsi      # 0xf8d4
movl $0x1c, %edx
movq %rbx, %rdi
callq 0x3290
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0xa40d(%rip), %rdi      # 0x14fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0x50(%rsp), %rdx
movq %rax, (%rdx)
movq 0x38(%rsp), %rdi
movq %r15, %rsi
callq 0xad6c
jmp 0xabfb
leaq 0x50(%rsp), %rdi
callq 0xacca
leaq 0x20(%rsp), %rdi
callq 0xacca
movq %rsp, %rdi
callq 0xacca
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0xac21
leaq 0x18(%rsp), %rdi
callq 0xae94
movq 0x38(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xac4a
jmp 0xac4a
jmp 0xac4a
movq %rax, %rbx
jmp 0xac76
jmp 0xac4a
jmp 0xac4a
jmp 0xac4a
jmp 0xac57
jmp 0xac4a
movq %rax, %rbx
jmp 0xac64
jmp 0xac57
jmp 0xac57
jmp 0xac57
jmp 0xac57
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xacca
leaq 0x20(%rsp), %rdi
callq 0xacca
movq %rsp, %rdi
callq 0xacca
movq 0x18(%rsp), %rsi
testq %rsi, %rsi
je 0xac8a
leaq 0x18(%rsp), %rdi
callq 0xae94
movq %rbx, %rdi
callq 0x3400