// File: Windfisch[P]production-flow/factory.cpp
// Function: Factory::build_flowgraph(item_t, Factory::FactoryConfiguration const&) const
FlowGraph Factory::build_flowgraph(item_t item, const Factory::FactoryConfiguration& conf) const
{
	const auto& toposort = facility_toposort[item];
	const auto& toposort_inv = facility_toposort_inv[item];
	const auto& edge_table = edge_table_per_item[item];

	FlowGraph flowgraph;
	flowgraph.edges.reserve(edge_table.size());
	flowgraph.nodes.reserve(toposort.size());


	// insert all facilities that are relevant for `item` into the flowgraph,
	// topologically sorted from producers to consumers.
	for (size_t facility_index : toposort)
	{
		const auto& facility = facilities[facility_index];
		size_t level = conf.facility_levels[facility_index];
		int production_rate;
		auto iter = facility.upgrade_plan[level].production_or_consumption.find(item);
		if (iter != facility.upgrade_plan[level].production_or_consumption.end())
			production_rate = iter->second;
		else
			production_rate = 0;

		flowgraph.nodes.emplace_back(production_rate);
	}

	// insert all transport lines that are relevant for `item`
	for (size_t edge_index = 0; edge_index < edge_table.size(); edge_index++)
	{
		const auto& edge = transport_lines[edge_table[edge_index]];
		assert(edge.item_type == item);
		size_t level = conf.transport_levels[edge_table[edge_index]];
		int capacity = edge.upgrade_plan[level].capacity;
	
		flowgraph.edges.emplace_back(capacity);
	}

	// fill the nodes' edgetables.
	for (size_t edge_index = 0; edge_index < edge_table.size(); edge_index++)
	{
		const auto& edge = transport_lines[edge_table[edge_index]];
		assert(edge.item_type == item);

		flowgraph.nodes[toposort_inv[edge.from]].outgoing_edges.push_back(
			&flowgraph.edges[edge_index]);
		flowgraph.nodes[toposort_inv[edge.to  ]].incoming_edges.push_back(
			&flowgraph.edges[edge_index]);
	}

	return flowgraph;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x140, %rsp            # imm = 0x140
movq %rdi, -0xe8(%rbp)
movq %rdi, %rax
movq %rax, -0xf8(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
addq $0x30, %rdi
movslq -0x14(%rbp), %rsi
callq 0xa6e0
movq -0xf0(%rbp), %rdi
movq %rax, -0x28(%rbp)
addq $0x48, %rdi
movslq -0x14(%rbp), %rsi
callq 0xa6e0
movq -0xf0(%rbp), %rdi
movq %rax, -0x30(%rbp)
addq $0x60, %rdi
movslq -0x14(%rbp), %rsi
callq 0xa6e0
movq -0xe8(%rbp), %rdi
movq %rax, -0x38(%rbp)
movb $0x0, -0x39(%rbp)
callq 0xa700
movq -0xe8(%rbp), %rdi
addq $0x18, %rdi
movq %rdi, -0xe0(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5870
movq -0xe0(%rbp), %rdi
movq %rax, %rsi
callq 0xa730
jmp 0x8b83
movq -0x28(%rbp), %rdi
callq 0x5870
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0xa830
jmp 0x8b9d
movq -0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x5c50
movq %rax, -0x60(%rbp)
movq -0x58(%rbp), %rdi
callq 0x5c80
movq %rax, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0xa930
testb $0x1, %al
jne 0x8bd5
jmp 0x8cd5
leaq -0x60(%rbp), %rdi
callq 0xa970
movq -0xf0(%rbp), %rdi
movq (%rax), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rsi
callq 0x9bf0
movq %rax, -0x78(%rbp)
movq -0x20(%rbp), %rdi
movq -0x70(%rbp), %rsi
callq 0xa990
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0xa9b0
movq %rax, %rdi
leaq -0x14(%rbp), %rsi
callq 0xa9d0
movq %rax, -0x100(%rbp)
jmp 0x8c2f
movq -0x100(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x78(%rbp), %rdi
movq -0x80(%rbp), %rsi
callq 0xa9b0
movq %rax, %rdi
callq 0xaa30
movq %rax, -0x98(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0xaa00
testb $0x1, %al
jne 0x8c72
jmp 0x8ca6
leaq -0x90(%rbp), %rdi
callq 0xaa60
movl 0x4(%rax), %eax
movl %eax, -0x84(%rbp)
jmp 0x8cb0
movq -0xe8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
callq 0xac50
jmp 0x8f97
movl $0x0, -0x84(%rbp)
movq -0xe8(%rbp), %rdi
leaq -0x84(%rbp), %rsi
callq 0xaa80
jmp 0x8cc5
jmp 0x8cc7
leaq -0x60(%rbp), %rdi
callq 0xab00
jmp 0x8bbf
movq $0x0, -0xa0(%rbp)
movq -0xa0(%rbp), %rax
movq %rax, -0x108(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5870
movq %rax, %rcx
movq -0x108(%rbp), %rax
cmpq %rcx, %rax
jae 0x8dfa
movq -0xf0(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x110(%rbp)
movq -0x38(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0xa990
movq -0x110(%rbp), %rdi
movq (%rax), %rsi
callq 0xab20
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x8d52
jmp 0x8d71
leaq 0x1c603(%rip), %rdi     # 0x2535c
leaq 0x1c57c(%rip), %rsi     # 0x252dc
movl $0xbb, %edx
leaq 0x1c607(%rip), %rcx     # 0x25373
callq 0x31e0
movq -0x20(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x118(%rbp)
movq -0x38(%rbp), %rdi
movq -0xa0(%rbp), %rsi
callq 0xa990
movq -0x118(%rbp), %rdi
movq (%rax), %rsi
callq 0xa990
movq (%rax), %rax
movq %rax, -0xb0(%rbp)
movq -0xa8(%rbp), %rdi
addq $0x18, %rdi
movq -0xb0(%rbp), %rsi
callq 0xab40
movq -0xe8(%rbp), %rdi
movl (%rax), %eax
movl %eax, -0xb4(%rbp)
addq $0x18, %rdi
leaq -0xb4(%rbp), %rsi
callq 0xab60
jmp 0x8de1
jmp 0x8de3
movq -0xa0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xa0(%rbp)
jmp 0x8ce0
movq $0x0, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x120(%rbp)
movq -0x38(%rbp), %rdi
callq 0x5870
movq %rax, %rcx
movq -0x120(%rbp), %rax
cmpq %rcx, %rax
jae 0x8f71
movq -0xf0(%rbp), %rax
addq $0x18, %rax
movq %rax, -0x128(%rbp)
movq -0x38(%rbp), %rdi
movq -0xc0(%rbp), %rsi
callq 0xa990
movq -0x128(%rbp), %rdi
movq (%rax), %rsi
callq 0xab20
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rax
movl (%rax), %eax
cmpl -0x14(%rbp), %eax
jne 0x8e77
jmp 0x8e96
leaq 0x1c4de(%rip), %rdi     # 0x2535c
leaq 0x1c457(%rip), %rsi     # 0x252dc
movl $0xc6, %edx
leaq 0x1c4e2(%rip), %rcx     # 0x25373
callq 0x31e0
movq -0x30(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0xa990
movq -0xe8(%rbp), %rdi
movq (%rax), %rsi
callq 0xabe0
movq -0xe8(%rbp), %rdi
addq $0x28, %rax
movq %rax, -0x130(%rbp)
addq $0x18, %rdi
movq -0xc0(%rbp), %rsi
callq 0xac30
movq -0x130(%rbp), %rdi
movq %rax, -0xd0(%rbp)
leaq -0xd0(%rbp), %rsi
callq 0xac00
jmp 0x8ef7
movq -0x30(%rbp), %rdi
movq -0xc8(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0xa990
movq -0xe8(%rbp), %rdi
movq (%rax), %rsi
callq 0xabe0
movq -0xe8(%rbp), %rdi
addq $0x10, %rax
movq %rax, -0x138(%rbp)
addq $0x18, %rdi
movq -0xc0(%rbp), %rsi
callq 0xac30
movq -0x138(%rbp), %rdi
movq %rax, -0xd8(%rbp)
leaq -0xd8(%rbp), %rsi
callq 0xac00
jmp 0x8f58
jmp 0x8f5a
movq -0xc0(%rbp), %rax
addq $0x1, %rax
movq %rax, -0xc0(%rbp)
jmp 0x8e05
movb $0x1, -0x39(%rbp)
testb $0x1, -0x39(%rbp)
jne 0x8f87
movq -0xe8(%rbp), %rdi
callq 0xac50
movq -0xf8(%rbp), %rax
addq $0x140, %rsp            # imm = 0x140
popq %rbp
retq
movq -0x48(%rbp), %rdi
callq 0x34f0