// File: /workspace/github_cpp_makefile_star2_O2/Windfisch[P]production-flow/factory.cpp
// Function: Factory::build_topological_sort()
void Factory::build_topological_sort()
{
	facility_toposort.resize(MAX_ITEM);
	facility_toposort_inv.resize(MAX_ITEM);

	for (int item = 0; item < MAX_ITEM; item++)
	{
		vector<size_t>& toposort = facility_toposort[item];
		vector<size_t>& toposort_inv = facility_toposort_inv[item];
		toposort.clear();
		toposort_inv.resize(facilities.size());


		vector<size_t> relevant_facilities = collect_relevant_facilities(item_t(item));


		// calculate incident / outgoing edges
		vector< unordered_set<size_t> > incident_edges(facilities.size());
		vector< unordered_set<size_t> > outgoing_edges(facilities.size());
		for (size_t edge_id = 0; edge_id < transport_lines.size(); edge_id++)
		{
			const auto& edge = transport_lines[edge_id];

			if (edge.item_type == item)
			{
				incident_edges[edge.to].insert(edge_id);
				outgoing_edges[edge.from].insert(edge_id);
			}
		}


		// do the actual topological sorting
		size_t root;
		while ((root = pop_root_node(relevant_facilities, incident_edges)) != INVALID_INDEX)
		{
			toposort.push_back(root);
			toposort_inv[root] = toposort.size()-1;

			for (size_t edge_id : outgoing_edges[root])
				incident_edges[transport_lines[edge_id].to].erase(edge_id);
		}

		for (const auto& edges : incident_edges)
			if (!edges.empty())
				throw runtime_error("Factory is not a directed acyclic graph for item " + to_string(item));
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
addq $0x30, %rdi
pushq $0xb
popq %r14
movq %rdi, 0x28(%rsp)
movq %r14, %rsi
callq 0x59c2
leaq 0x48(%rbx), %rdi
movq %rdi, 0x20(%rsp)
movq %r14, %rsi
callq 0x59c2
xorl %r15d, %r15d
pushq $0x50
popq %rbp
leaq 0x50(%rsp), %r14
pushq $0x30
popq %r12
cmpq $0xb, %r15
je 0x507f
movq 0x28(%rsp), %rax
movq (%rax), %rcx
imulq $0x18, %r15, %rdx
leaq (%rcx,%rdx), %rsi
movq 0x20(%rsp), %rax
movq (%rax), %r13
addq %rdx, %r13
movq (%rcx,%rdx), %rax
cmpq %rax, 0x8(%rcx,%rdx)
je 0x4ec3
movq %rax, 0x8(%rsi)
movq %rsi, 0x30(%rsp)
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %rbp
movq %r13, %rdi
movq %rax, %rsi
callq 0x59f8
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x4cc8
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %rbp
leaq 0x8(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x5a2a
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
idivq %rbp
leaq 0x70(%rsp), %rdi
movq %rax, %rsi
movq %r14, %rdx
callq 0x5a2a
xorl %ecx, %ecx
movq %rcx, 0x50(%rsp)
movq 0x18(%rbx), %rbp
movq 0x20(%rbx), %rax
subq %rbp, %rax
cqto
idivq %r12
cmpq %rax, %rcx
jae 0x4ff1
imulq $0x30, %rcx, %rax
movl (%rbp,%rax), %edx
cmpq %rdx, %r15
jne 0x4f80
addq %rax, %rbp
imulq $0x38, 0x10(%rbp), %rdi
addq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6a9e
imulq $0x38, 0x8(%rbp), %rdi
addq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x6a9e
movq 0x50(%rsp), %rcx
incq %rcx
jmp 0x4f29
movq 0x30(%rsp), %rbp
movq %rbp, %rdi
movq %rsp, %rsi
callq 0x59a2
movq 0x8(%rbp), %rax
subq (%rbp), %rax
sarq $0x3, %rax
decq %rax
movq (%rsp), %rcx
movq (%r13), %rdx
movq %rax, (%rdx,%rcx,8)
movq 0x70(%rsp), %rax
imulq $0x38, (%rsp), %rcx
leaq (%rax,%rcx), %rbp
addq $0x10, %rbp
movq (%rbp), %rbp
testq %rbp, %rbp
je 0x4ff1
movq 0x8(%rbp), %rax
movq %rax, 0x50(%rsp)
movq 0x18(%rbx), %rcx
imulq $0x30, %rax, %rax
imulq $0x38, 0x10(%rcx,%rax), %rdi
addq 0x8(%rsp), %rdi
movq %r14, %rsi
callq 0x6e14
jmp 0x4fc2
movq 0x38(%rsp), %rcx
movq 0x40(%rsp), %rdx
movq 0x8(%rsp), %rax
pushq $0x50
popq %rbp
cmpq %rdx, %rcx
je 0x503d
movq (%rcx), %rsi
imulq $0x38, %rsi, %rdi
cmpq $0x0, 0x18(%rax,%rdi)
je 0x501d
addq $0x8, %rcx
jmp 0x5003
movq -0x8(%rdx), %rdi
addq $-0x8, %rdx
movq %rdi, (%rcx)
movq %rdx, 0x40(%rsp)
movq %rsi, (%rsp)
cmpq $-0x1, %rsi
jne 0x4f85
jmp 0x5042
orq $-0x1, (%rsp)
movq 0x10(%rsp), %rcx
cmpq %rcx, %rax
je 0x5059
cmpq $0x0, 0x18(%rax)
leaq 0x38(%rax), %rax
je 0x5047
jmp 0x5091
leaq 0x70(%rsp), %rdi
callq 0x5af8
leaq 0x8(%rsp), %rdi
callq 0x5af8
leaq 0x38(%rsp), %rdi
callq 0x474a
incq %r15
jmp 0x4e8f
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x3120
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
movl %r15d, %esi
callq 0x5a9f
leaq 0x8272(%rip), %rsi      # 0xd325
leaq 0x50(%rsp), %rdi
leaq 0x88(%rsp), %rdx
callq 0x5a7b
movb $0x1, %bpl
leaq 0x50(%rsp), %rsi
movq %rbx, %rdi
callq 0x3360
xorl %ebp, %ebp
movq 0xff12(%rip), %rsi      # 0x14ff0
movq 0xfedb(%rip), %rdx      # 0x14fc0
movq %rbx, %rdi
callq 0x33a0
jmp 0x5131
movq %rax, %r14
jmp 0x5148
movq %rax, %r14
jmp 0x513e
movq %rax, %r14
leaq 0x50(%rsp), %rdi
callq 0x3418
jmp 0x510e
movq %rax, %r14
movb $0x1, %bpl
leaq 0x88(%rsp), %rdi
callq 0x3418
testb %bpl, %bpl
jne 0x5125
jmp 0x5134
movq %rax, %r14
movq %rbx, %rdi
callq 0x31b0
jmp 0x5134
jmp 0x5131
movq %rax, %r14
leaq 0x70(%rsp), %rdi
callq 0x5af8
leaq 0x8(%rsp), %rdi
callq 0x5af8
leaq 0x38(%rsp), %rdi
callq 0x474a
movq %r14, %rdi
callq 0x33c0