// File: /workspace/github_cpp_makefile_star2_O3/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, 0x50(%rsp)
movq %rdi, 0x48(%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, 0x20(%rsp)
movq %rax, %rdi
movq %r15, %rsi
callq 0xac92
addq $0x18, %rbx
leaq 0x18(%r15), %rsi
movq %rbx, %rdi
callq 0xac92
leaq 0x20(%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, 0x40(%rsp)
movapd %xmm0, 0x30(%rsp)
callq 0xab5a
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
je 0xa96c
movq 0xab1b(%rip), %r12      # 0x14fd0
leaq 0x511e(%rip), %r14      # 0xf5da
movq %r15, 0x28(%rsp)
movl $0x12, %edx
movq %r12, %rdi
leaq 0x5330(%rip), %rsi      # 0xf800
callq 0x3290
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq %r12, %rdi
callq 0x3190
movq %rax, %r13
movl $0x13, %edx
movq %rax, %rdi
leaq 0x5317(%rip), %rsi      # 0xf813
callq 0x3290
movq 0x8(%rsp), %rax
subq (%rsp), %rax
sarq $0x3, %rax
movq 0x38(%rsp), %rsi
subq 0x30(%rsp), %rsi
sarq $0x3, %rsi
addq %rax, %rsi
movq %r13, %rdi
callq 0x3190
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq %r13, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq (%rsp), %rcx
movq 0x8(%rsp), %rax
cmpq %rax, %rcx
je 0xa58e
movq (%rcx), %rsi
movq %rcx, %rdx
movq (%rdx), %rdi
movsd 0x38(%rsi), %xmm0
ucomisd 0x38(%rdi), %xmm0
movq %rdx, %r13
ja 0xa57d
movq %rsi, %rdi
movq %rcx, %r13
addq $0x8, %rdx
movq %rdi, %rsi
movq %r13, %rcx
cmpq %rax, %rdx
jne 0xa565
jmp 0xa591
movq %rcx, %r13
leaq 0x30(%rsp), %rdi
movq %r13, %rsi
callq 0xab5a
movq 0x38(%rsp), %rbx
movq 0x8(%rsp), %rax
movq -0x8(%rax), %rcx
movq $0x0, -0x8(%rax)
movq (%r13), %rsi
movq %rcx, (%r13)
testq %rsi, %rsi
je 0xa5c9
movq %r13, %rdi
callq 0xad76
movq 0x8(%rsp), %rax
leaq -0x8(%rax), %r13
movq %r13, 0x8(%rsp)
movq -0x8(%rax), %rsi
testq %rsi, %rsi
je 0xa5e8
movq %r13, %rdi
callq 0xad76
movq $0x0, (%r13)
movl $0x11, %edx
movq %r12, %rdi
leaq 0x5228(%rip), %rsi      # 0xf827
callq 0x3290
movq -0x8(%rbx), %rax
movl 0x30(%rax), %esi
movq %r12, %rdi
callq 0x33f0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4fb7(%rip), %rsi      # 0xf5d9
callq 0x3290
movl $0x6, %edx
movq %r12, %rdi
leaq 0x5203(%rip), %rsi      # 0xf839
callq 0x3290
movq -0x8(%rbx), %rax
movq (%rax), %r15
movq 0x8(%rax), %rbp
cmpq %rbp, %r15
je 0xa66f
movq (%r15), %r13
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3290
movq %r12, %rdi
movq %r13, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa646
movl $0x8, %edx
movq %r12, %rdi
leaq 0x51c2(%rip), %rsi      # 0xf840
callq 0x3290
movq -0x8(%rbx), %rax
movq 0x18(%rax), %r15
movq 0x20(%rax), %rbp
cmpq %rbp, %r15
je 0xa6b8
movq (%r15), %r13
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3290
movq %r12, %rdi
movq %r13, %rsi
callq 0x3190
addq $0x8, %r15
jmp 0xa68f
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(%rbx), %rsi
movq 0x50(%rsp), %rax
movq (%rax), %rdx
leaq 0x58(%rsp), %rdi
callq 0x9d62
movq 0x58(%rsp), %r13
movq 0x60(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq %rax, %r13
movq 0x28(%rsp), %r15
je 0xa953
movl $0x15, %edx
movq %r12, %rdi
leaq 0x5127(%rip), %rsi      # 0xf849
callq 0x3290
movq (%r13), %rax
movl 0x30(%rax), %esi
movq %r12, %rdi
callq 0x33f0
movl $0x2, %edx
movq %rax, %rdi
leaq 0x4e94(%rip), %rsi      # 0xf5d9
callq 0x3290
movl $0x6, %edx
movq %r12, %rdi
leaq 0x50e0(%rip), %rsi      # 0xf839
callq 0x3290
movq (%r13), %rax
movq (%rax), %rbx
movq 0x8(%rax), %r15
cmpq %r15, %rbx
je 0xa792
movq (%rbx), %rbp
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3190
addq $0x8, %rbx
jmp 0xa769
movl $0x8, %edx
movq %r12, %rdi
leaq 0x509f(%rip), %rsi      # 0xf840
callq 0x3290
movq (%r13), %rax
movq 0x18(%rax), %rbx
movq 0x20(%rax), %r15
cmpq %r15, %rbx
je 0xa7e2
movq (%rbx), %rbp
movl $0x1, %edx
movq %r12, %rdi
movq %r14, %rsi
callq 0x3290
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3190
addq $0x8, %rbx
cmpq %r15, %rbx
jne 0xa7b7
movq (%r13), %rax
cmpl $-0x1, 0x30(%rax)
je 0xa9d6
movq (%rsp), %rbp
movq 0x8(%rsp), %rbx
cmpq %rbx, %rbp
je 0xa81d
movq (%rbp), %rdi
movq (%r13), %rsi
movq 0x50(%rsp), %rax
movq (%rax), %rdx
callq 0x9bc4
testb %al, %al
jne 0xa89f
addq $0x8, %rbp
jmp 0xa7f5
movq 0x30(%rsp), %rbx
movq 0x38(%rsp), %r15
cmpq %r15, %rbx
je 0xa84e
movq (%rbx), %rdi
movq (%r13), %rsi
movq 0x50(%rsp), %rax
movq (%rax), %rdx
callq 0x9bc4
testb %al, %al
jne 0xa903
addq $0x8, %rbx
jmp 0xa827
movq %rsp, %rdi
movq %r13, %rsi
callq 0xab5a
movq 0x28(%rsp), %r15
movl $0x22, %edx
movq %r12, %rdi
leaq 0x5044(%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 0xa944
movq (%r13), %rax
movq $0x0, (%r13)
movq (%rbp), %rsi
movq %rax, (%rbp)
testq %rsi, %rsi
je 0xa8c0
movq %rbp, %rdi
callq 0xad76
movl $0x15, %edx
movq %r12, %rdi
leaq 0x4fb4(%rip), %rsi      # 0xf883
callq 0x3290
movq 0x28(%rsp), %r15
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 0xa944
movl $0x17, %edx
movq %r12, %rdi
leaq 0x4f87(%rip), %rsi      # 0xf899
callq 0x3290
movq 0x28(%rsp), %r15
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
addq $0x8, %r13
cmpq 0x70(%rsp), %r13
jne 0xa713
leaq 0x58(%rsp), %rdi
callq 0xab98
movq (%rsp), %rax
cmpq 0x8(%rsp), %rax
jne 0xa4c1
movq 0xa65d(%rip), %rbx      # 0x14fd0
leaq 0x4f5a(%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 0xa62b(%rip), %rdi      # 0x14fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movabsq $-0x4010000000000000, %rax # imm = 0xBFF0000000000000
leaq 0x58(%rsp), %rdx
movq %rax, (%rdx)
movq 0x48(%rsp), %rdi
movq %r15, %rsi
callq 0xac40
jmp 0xaac1
movq (%r12), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
movl $0xa, %esi
callq 0x3220
movsbl %al, %esi
movq 0xa5db(%rip), %rdi      # 0x14fd0
callq 0x3030
movq %rax, %rdi
callq 0x31a0
movq %rax, %rbx
leaq 0x4e53(%rip), %rsi      # 0xf85f
movl $0x10, %edx
movq %rax, %rdi
callq 0x3290
movq (%r13), %rax
movsd 0x38(%rax), %xmm0
movq %rbx, %rdi
callq 0x3380
movq %rax, %rbx
leaq 0x4e3c(%rip), %rsi      # 0xf870
movl $0xb, %edx
movq %rax, %rdi
callq 0x3290
movq 0x38(%rsp), %rax
subq 0x30(%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
leaq 0x4e0b(%rip), %rsi      # 0xf87c
movl $0x6, %edx
movq %rax, %rdi
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 (%r13), %rsi
leaq 0x38(%rsi), %rdx
movq 0x48(%rsp), %rdi
callq 0xabee
leaq 0x58(%rsp), %rdi
callq 0xab98
leaq 0x30(%rsp), %rdi
callq 0xab98
movq %rsp, %rdi
callq 0xab98
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0xaae7
leaq 0x20(%rsp), %rdi
callq 0xad76
movq 0x48(%rsp), %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xab16
jmp 0xab16
jmp 0xab16
movq %rax, %rbx
jmp 0xab3e
jmp 0xab16
jmp 0xab1f
jmp 0xab16
jmp 0xab16
jmp 0xab1f
jmp 0xab1f
jmp 0xab1f
jmp 0xab16
movq %rax, %rbx
jmp 0xab2c
jmp 0xab1f
jmp 0xab1f
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xab98
leaq 0x30(%rsp), %rdi
callq 0xab98
movq %rsp, %rdi
callq 0xab98
movq 0x20(%rsp), %rsi
testq %rsi, %rsi
je 0xab52
leaq 0x20(%rsp), %rdi
callq 0xad76
movq %rbx, %rdi
callq 0x3400