// File: /workspace/github_cpp_makefile_star2_O3/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 $0xb8, %rsp
movq %rdi, %rbx
addq $0x30, %rdi
movl $0xb, %esi
movq %rdi, 0x68(%rsp)
callq 0x61a6
leaq 0x48(%rbx), %rdi
movl $0xb, %esi
movq %rdi, 0x60(%rsp)
callq 0x61a6
xorl %r15d, %r15d
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x80(%rsp), %r13
leaq 0x40(%rsp), %rbp
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
movq 0x68(%rsp), %rax
movq (%rax), %rcx
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rdx
leaq (%rcx,%rdx), %rsi
movq 0x60(%rsp), %rax
movq (%rax), %rdi
addq %rdx, %rdi
movq (%rcx,%rdx), %rax
cmpq %rax, 0x8(%rcx,%rdx)
je 0x53c8
movq %rax, 0x8(%rsi)
movq %rsi, 0x78(%rsp)
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %r12, %rsi
movq %rdi, 0x70(%rsp)
callq 0x61e2
leaq 0x10(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x514c
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %r12, %rsi
leaq 0x28(%rsp), %rdi
movq %r13, %rdx
callq 0x6214
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %r12, %rsi
leaq 0xa0(%rsp), %rdi
movq %r13, %rdx
callq 0x6214
movq $0x0, 0x40(%rsp)
movq 0x18(%rbx), %r12
movq 0x20(%rbx), %rax
cmpq %r12, %rax
je 0x555e
xorl %ecx, %ecx
leaq (%rcx,%rcx,2), %rdx
shlq $0x4, %rdx
movl (%r12,%rdx), %esi
cmpq %rsi, %r15
jne 0x54ad
addq %rdx, %r12
imulq $0x38, 0x10(%r12), %rdi
addq 0x28(%rsp), %rdi
movq %rdi, 0x80(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
callq 0x6ffc
imulq $0x38, 0x8(%r12), %rdi
addq 0xa0(%rsp), %rdi
movq %rdi, 0x80(%rsp)
movq %rbp, %rsi
movq %r13, %rdx
callq 0x6ffc
movq 0x40(%rsp), %rcx
movq 0x18(%rbx), %r12
movq 0x20(%rbx), %rax
incq %rcx
movq %rcx, 0x40(%rsp)
movq %rax, %rdx
subq %r12, %rdx
sarq $0x4, %rdx
imulq %r14, %rdx
cmpq %rdx, %rcx
jb 0x544d
jmp 0x555e
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x54e4
movq %r8, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rdi)
jmp 0x54f9
leaq 0x8(%rsp), %rdx
movq %rdi, %r12
callq 0x6b84
movq 0x8(%r12), %rsi
movq %r12, %rdi
subq (%rdi), %rsi
sarq $0x3, %rsi
decq %rsi
movq 0x8(%rsp), %rax
movq 0x70(%rsp), %rcx
movq (%rcx), %rcx
movq %rsi, (%rcx,%rax,8)
movq 0xa0(%rsp), %rax
imulq $0x38, 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx), %r12
testq %r12, %r12
je 0x555e
movq 0x8(%r12), %rax
movq %rax, 0x80(%rsp)
movq 0x18(%rbx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
imulq $0x38, 0x10(%rcx,%rax), %rdi
addq 0x28(%rsp), %rdi
movq %r13, %rsi
callq 0x72a6
movq (%r12), %r12
jmp 0x5527
movq 0x28(%rsp), %rax
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
cmpq %rdx, %rcx
movabsq $-0x3333333333333333, %r12 # imm = 0xCCCCCCCCCCCCCCCD
je 0x55bc
movq 0x78(%rsp), %rdi
movq (%rcx), %r8
imulq $0x38, %r8, %rsi
cmpq $0x0, 0x18(%rax,%rsi)
je 0x559b
addq $0x8, %rcx
cmpq %rdx, %rcx
jne 0x5581
jmp 0x55bc
movq -0x8(%rdx), %rsi
addq $-0x8, %rdx
movq %rsi, (%rcx)
movq %rdx, 0x18(%rsp)
movq %r8, 0x8(%rsp)
cmpq $-0x1, %r8
jne 0x54cd
jmp 0x55c5
movq $-0x1, 0x8(%rsp)
movq 0x30(%rsp), %rcx
cmpq %rcx, %rax
je 0x55dc
cmpq $0x0, 0x18(%rax)
jne 0x5629
addq $0x38, %rax
jmp 0x55ca
leaq 0xa0(%rsp), %rdi
callq 0x63ca
leaq 0x28(%rsp), %rdi
callq 0x63ca
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x560a
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
incq %r15
cmpq $0xb, %r15
jne 0x5396
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x3120
movq %rax, %r14
leaq 0x40(%rsp), %rdi
movl %r15d, %esi
callq 0x6328
leaq 0x9cd4(%rip), %rsi      # 0xf31e
leaq 0x80(%rsp), %rdi
leaq 0x40(%rsp), %rdx
callq 0x62b8
movb $0x1, %bpl
leaq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x33a0
xorl %ebp, %ebp
movq 0xf978(%rip), %rsi      # 0x14ff0
movq 0xf941(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33e0
jmp 0x5711
movq %rax, %rbx
jmp 0x572b
movq %rax, %rbx
jmp 0x5721
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56c0
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56db
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3250
testb %bpl, %bpl
jne 0x5705
jmp 0x5714
movq %rax, %rbx
leaq 0x50(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5705
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0x5705
movq %rax, %rbx
movq %r14, %rdi
callq 0x31d0
jmp 0x5714
jmp 0x5711
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x63ca
leaq 0x28(%rsp), %rdi
callq 0x63ca
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x5742
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x3400