// File: /workspace/github_cpp_makefile_star2_O1/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, 0x50(%rsp)
callq 0x61aa
leaq 0x48(%rbx), %rdi
movl $0xb, %esi
movq %rdi, 0x48(%rsp)
callq 0x61aa
xorl %r15d, %r15d
movabsq $-0x3333333333333333, %rdi # imm = 0xCCCCCCCCCCCCCCCD
leaq 0x80(%rsp), %r13
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
movq 0x50(%rsp), %rax
movq (%rax), %rcx
leaq (,%r15,8), %rax
leaq (%rax,%rax,2), %rdx
leaq (%rcx,%rdx), %r12
movq 0x48(%rsp), %rax
movq (%rax), %rax
addq %rdx, %rax
movq %rax, 0x10(%rsp)
movq (%rcx,%rdx), %rax
cmpq %rax, 0x8(%rcx,%rdx)
je 0x53ab
movq %rax, 0x8(%r12)
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %rdi, %rsi
movq %rdi, %r14
movq 0x10(%rsp), %rdi
callq 0x61e6
leaq 0x30(%rsp), %rdi
movq %rbx, %rsi
movl %r15d, %edx
callq 0x5128
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %r14, %rsi
leaq 0x18(%rsp), %rdi
movq %r13, %rdx
callq 0x6218
movq 0x8(%rbx), %rsi
subq (%rbx), %rsi
sarq $0x4, %rsi
imulq %r14, %rsi
leaq 0xa0(%rsp), %rdi
movq %r13, %rdx
callq 0x6218
movq $0x0, 0x60(%rsp)
movq 0x18(%rbx), %r14
cmpq %r14, 0x20(%rbx)
je 0x5555
xorl %eax, %eax
movq %r12, 0x58(%rsp)
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
movl (%r14,%rax), %ecx
cmpq %rcx, %r15
jne 0x5497
movq %rbp, %r12
movq %r15, %rbp
addq %rax, %r14
imulq $0x38, 0x10(%r14), %rdi
addq 0x18(%rsp), %rdi
movq %rdi, 0x80(%rsp)
leaq 0x60(%rsp), %r15
movq %r15, %rsi
movq %r13, %rdx
callq 0x703e
imulq $0x38, 0x8(%r14), %rdi
addq 0xa0(%rsp), %rdi
movq %rdi, 0x80(%rsp)
movq %r15, %rsi
movq %r13, %rdx
callq 0x703e
movq %rbp, %r15
movq %r12, %rbp
movq 0x58(%rsp), %r12
movq 0x60(%rsp), %rax
incq %rax
movq %rax, 0x60(%rsp)
movq 0x18(%rbx), %r14
movq 0x20(%rbx), %rcx
subq %r14, %rcx
sarq $0x4, %rcx
imulq %rbp, %rcx
cmpq %rcx, %rax
jb 0x5430
jmp 0x5555
movq 0x8(%r12), %rsi
cmpq 0x10(%r12), %rsi
je 0x54df
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%r12)
jmp 0x54ec
movq %r12, %rdi
leaq 0x8(%rsp), %rdx
callq 0x6bc4
movq 0x8(%r12), %rax
subq (%r12), %rax
sarq $0x3, %rax
decq %rax
movq 0x8(%rsp), %rcx
movq 0x10(%rsp), %rdx
movq (%rdx), %rdx
movq %rax, (%rdx,%rcx,8)
movq 0xa0(%rsp), %rax
imulq $0x38, 0x8(%rsp), %rcx
movq 0x10(%rax,%rcx), %r14
testq %r14, %r14
je 0x5555
movq 0x8(%r14), %rax
movq %rax, 0x80(%rsp)
movq 0x18(%rbx), %rcx
leaq (%rax,%rax,2), %rax
shlq $0x4, %rax
imulq $0x38, 0x10(%rcx,%rax), %rdi
addq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x7320
movq (%r14), %r14
jmp 0x5520
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rdx
movq $-0x1, %rax
cmpq %rdx, %rcx
je 0x55aa
movq 0x18(%rsp), %rsi
movq %rdx, %rdi
movq (%rcx), %r10
imulq $0x38, %r10, %r9
movq 0x18(%rsi,%r9), %r9
testq %r9, %r9
jne 0x5597
movq -0x8(%rdi), %r8
addq $-0x8, %rdi
movq %r8, (%rcx)
movq %rdi, 0x38(%rsp)
movq %r10, %r8
testq %r9, %r9
je 0x55a7
addq $0x8, %rcx
cmpq %rdx, %rcx
jne 0x5573
jmp 0x55aa
movq %r8, %rax
movq %rax, 0x8(%rsp)
cmpq $-0x1, %rax
jne 0x54c5
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rcx
cmpq %rcx, %rax
je 0x55d5
cmpq $0x0, 0x18(%rax)
jne 0x562c
addq $0x38, %rax
jmp 0x55c3
leaq 0xa0(%rsp), %rdi
callq 0x63d4
leaq 0x18(%rsp), %rdi
callq 0x63d4
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x5603
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
incq %r15
cmpq $0xb, %r15
movabsq $-0x3333333333333333, %rdi # imm = 0xCCCCCCCCCCCCCCCD
jne 0x5373
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 0x60(%rsp), %rdi
movl %r15d, %esi
callq 0x6331
leaq 0x9cd1(%rip), %rsi      # 0xf31e
leaq 0x80(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x62c1
movb $0x1, %bpl
leaq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x33a0
xorl %ebp, %ebp
movq 0xf975(%rip), %rsi      # 0x14ff0
movq 0xf93e(%rip), %rdx      # 0x14fc0
movq %r14, %rdi
callq 0x33e0
movq %rax, %rbx
jmp 0x5713
movq %rax, %rbx
jmp 0x5709
movq %rax, %rbx
leaq 0x90(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56c3
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0x56c3
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x70(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56e6
movq 0x70(%rsp), %rsi
incq %rsi
callq 0x3250
jmp 0x56e6
movq %rax, %rbx
movb $0x1, %bpl
testb %bpl, %bpl
je 0x56fc
movq %r14, %rdi
callq 0x31d0
jmp 0x56fc
jmp 0x56f9
jmp 0x56f9
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x63d4
leaq 0x18(%rsp), %rdi
callq 0x63d4
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x572a
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x3250
movq %rbx, %rdi
callq 0x3400