// File: Windfisch[P]production-flow/main.cpp
// Function: main
int main(int argc, const char** argv)
{
	if (argc != 2)
	{
		cout << "Usage: " << argv[0] << " factory.tgf" << endl;
		exit(1);
	}
	
	Factory factory = read_factory(argv[1]);
	factory.initialize();

	Factory::FactoryConfiguration conf;
	for (size_t i=0; i<factory.facilities.size(); i++)
		conf.facility_levels.push_back(0);
	for (size_t i=0; i<factory.transport_lines.size(); i++)
		conf.transport_levels.push_back(0);

	ActionGraph actiongraph(&factory);
	auto result = actiongraph.dijkstra(conf);

	cout << endl << endl << endl << endl;
	
	cout << "it follows the node/edge map. for item i, a->b means that flowgraph-node/edge a\n"
	        "corresponds to factory-facility/transportline b. Note that b is given as zero\n"
		"indexed number, but the .tgf file is one-based-indexed. Add 1 before looking it up" << endl;
	for (int i=0; i<MAX_ITEM; i++)
	{
		cout << "item " << i << ": facilities ";
		for (int j=0; j<factory.facility_toposort[i].size(); j++)
			cout << j<<"->"<<factory.facility_toposort[i][j] << ", ";
		cout << "transport lines ";
		for (int j=0; j<factory.edge_table_per_item[i].size(); j++)
			cout << j<<"->"<<factory.edge_table_per_item[i][j] << ", ";
		cout << endl;
	}

	cout << delimiter;
	cout << "now simulating the entire factory in its before-state" << endl;
	factory.simulate_debug(conf);
	
	cout << delimiter;
	cout << "now simulating the entire factory in its after-state" << endl;
	factory.simulate_debug(result.first);
	cout << delimiter;
	cout << "bye." << endl;

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x250, %rsp            # imm = 0x250
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
je 0x4f69
movq 0x340a2(%rip), %rdi     # 0x38fc8
leaq 0x20122(%rip), %rsi     # 0x2504f
callq 0x3290
movq %rax, %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rsi
callq 0x3290
movq %rax, %rdi
leaq 0x2010c(%rip), %rsi     # 0x25057
callq 0x3290
movq %rax, %rdi
movq 0x34046(%rip), %rsi     # 0x38fa0
callq 0x32f0
movl $0x1, %edi
callq 0x3370
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x1b8(%rbp)
leaq -0xc1(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x3500
movq -0x1b8(%rbp), %rsi
movq -0x1b0(%rbp), %rdx
leaq -0xc0(%rbp), %rdi
callq 0x5630
jmp 0x4fa7
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
callq 0x190b0
jmp 0x4fbc
leaq -0xc0(%rbp), %rdi
callq 0x3588
leaq -0xc1(%rbp), %rdi
callq 0x3310
leaq -0xa0(%rbp), %rdi
callq 0x8190
jmp 0x4fe2
leaq -0x108(%rbp), %rdi
callq 0x56e0
movq $0x0, -0x110(%rbp)
movq -0x110(%rbp), %rax
movq %rax, -0x1c0(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5710
movq %rax, %rcx
movq -0x1c0(%rbp), %rax
cmpq %rcx, %rax
jae 0x50cd
movq $0x0, -0x118(%rbp)
leaq -0x108(%rbp), %rdi
leaq -0x118(%rbp), %rsi
callq 0x5740
jmp 0x5046
jmp 0x5048
movq -0x110(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x110(%rbp)
jmp 0x4ff9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x508e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0xc0(%rbp), %rdi
callq 0x3588
leaq -0xc1(%rbp), %rdi
callq 0x3310
jmp 0x5621
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x5615
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
jmp 0x5609
movq $0x0, -0x120(%rbp)
movq -0x120(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq -0xa0(%rbp), %rdi
addq $0x18, %rdi
callq 0x5770
movq %rax, %rcx
movq -0x1c8(%rbp), %rax
cmpq %rcx, %rax
jae 0x513b
leaq -0xf0(%rbp), %rdi
movq $0x0, -0x128(%rbp)
leaq -0x128(%rbp), %rsi
callq 0x5740
jmp 0x5125
jmp 0x5127
movq -0x120(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x120(%rbp)
jmp 0x50d8
leaq -0x130(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x57a0
jmp 0x5150
leaq -0x198(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x57c0
jmp 0x5165
leaq -0x168(%rbp), %rdi
leaq -0x130(%rbp), %rsi
leaq -0x198(%rbp), %rdx
callq 0x16640
jmp 0x5181
leaq -0x198(%rbp), %rdi
callq 0x5820
movq 0x33e34(%rip), %rdi     # 0x38fc8
movq 0x33e05(%rip), %rsi     # 0x38fa0
callq 0x32f0
movq %rax, -0x1d0(%rbp)
jmp 0x51a9
movq -0x1d0(%rbp), %rdi
movq 0x33de9(%rip), %rsi     # 0x38fa0
callq 0x32f0
movq %rax, -0x1d8(%rbp)
jmp 0x51c5
movq -0x1d8(%rbp), %rdi
movq 0x33dcd(%rip), %rsi     # 0x38fa0
callq 0x32f0
movq %rax, -0x1e0(%rbp)
jmp 0x51e1
movq -0x1e0(%rbp), %rdi
movq 0x33db1(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x51f6
movq 0x33dcb(%rip), %rdi     # 0x38fc8
leaq 0x1fe60(%rip), %rsi     # 0x25064
callq 0x3290
movq %rax, -0x1e8(%rbp)
jmp 0x5212
movq -0x1e8(%rbp), %rdi
movq 0x33d80(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x5227
movl $0x0, -0x19c(%rbp)
cmpl $0xb, -0x19c(%rbp)
jge 0x54d6
movq 0x33d83(%rip), %rdi     # 0x38fc8
leaq 0x2010a(%rip), %rsi     # 0x25356
callq 0x3290
movq %rax, -0x1f0(%rbp)
jmp 0x525a
movq -0x1f0(%rbp), %rdi
movl -0x19c(%rbp), %esi
callq 0x34e0
movq %rax, -0x1f8(%rbp)
jmp 0x5275
movq -0x1f8(%rbp), %rdi
leaq 0x1fed2(%rip), %rsi     # 0x25155
callq 0x3290
jmp 0x528a
movl $0x0, -0x1a0(%rbp)
movslq -0x1a0(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0xa0(%rbp), %rdi
addq $0x30, %rdi
movslq -0x19c(%rbp), %rsi
callq 0x5850
movq %rax, %rdi
callq 0x5870
movq %rax, %rcx
movq -0x200(%rbp), %rax
cmpq %rcx, %rax
jae 0x53b3
movl -0x1a0(%rbp), %esi
movq 0x33ce7(%rip), %rdi     # 0x38fc8
callq 0x34e0
movq %rax, -0x208(%rbp)
jmp 0x52ef
movq -0x208(%rbp), %rdi
leaq 0x1fe66(%rip), %rsi     # 0x25163
callq 0x3290
movq %rax, -0x210(%rbp)
jmp 0x530b
leaq -0x70(%rbp), %rdi
movslq -0x19c(%rbp), %rsi
callq 0x5850
movq %rax, %rdi
movslq -0x1a0(%rbp), %rsi
callq 0x5890
movq -0x210(%rbp), %rdi
movq (%rax), %rsi
callq 0x30a0
movq %rax, -0x218(%rbp)
jmp 0x5342
movq -0x218(%rbp), %rdi
leaq 0x202a1(%rip), %rsi     # 0x255f1
callq 0x3290
jmp 0x5357
jmp 0x5359
movl -0x1a0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1a0(%rbp)
jmp 0x5294
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x198(%rbp), %rdi
callq 0x5820
jmp 0x5609
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x58b0
jmp 0x5609
movq 0x33c0e(%rip), %rdi     # 0x38fc8
leaq 0x1fda5(%rip), %rsi     # 0x25166
callq 0x3290
jmp 0x53c8
movl $0x0, -0x1a4(%rbp)
movslq -0x1a4(%rbp), %rax
movq %rax, -0x220(%rbp)
leaq -0xa0(%rbp), %rdi
addq $0x60, %rdi
movslq -0x19c(%rbp), %rsi
callq 0x5850
movq %rax, %rdi
callq 0x5870
movq %rax, %rcx
movq -0x220(%rbp), %rax
cmpq %rcx, %rax
jae 0x54ab
movl -0x1a4(%rbp), %esi
movq 0x33ba9(%rip), %rdi     # 0x38fc8
callq 0x34e0
movq %rax, -0x228(%rbp)
jmp 0x542d
movq -0x228(%rbp), %rdi
leaq 0x1fd28(%rip), %rsi     # 0x25163
callq 0x3290
movq %rax, -0x230(%rbp)
jmp 0x5449
leaq -0x40(%rbp), %rdi
movslq -0x19c(%rbp), %rsi
callq 0x5850
movq %rax, %rdi
movslq -0x1a4(%rbp), %rsi
callq 0x5890
movq -0x230(%rbp), %rdi
movq (%rax), %rsi
callq 0x30a0
movq %rax, -0x238(%rbp)
jmp 0x5480
movq -0x238(%rbp), %rdi
leaq 0x20163(%rip), %rsi     # 0x255f1
callq 0x3290
jmp 0x5495
jmp 0x5497
movl -0x1a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1a4(%rbp)
jmp 0x53d2
movq 0x33b16(%rip), %rdi     # 0x38fc8
movq 0x33ae7(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x54c0
jmp 0x54c2
movl -0x19c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x19c(%rbp)
jmp 0x5231
movq 0x33aeb(%rip), %rdi     # 0x38fc8
leaq 0x33e04(%rip), %rsi     # 0x392e8
callq 0x3280
jmp 0x54eb
movq 0x33ad6(%rip), %rdi     # 0x38fc8
leaq 0x1fc7e(%rip), %rsi     # 0x25177
callq 0x3290
movq %rax, -0x240(%rbp)
jmp 0x5507
movq -0x240(%rbp), %rdi
movq 0x33a8b(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x551c
leaq -0xa0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x8fa0
jmp 0x5531
movq 0x33a90(%rip), %rdi     # 0x38fc8
leaq 0x33da9(%rip), %rsi     # 0x392e8
callq 0x3280
jmp 0x5546
movq 0x33a7b(%rip), %rdi     # 0x38fc8
leaq 0x1fc59(%rip), %rsi     # 0x251ad
callq 0x3290
movq %rax, -0x248(%rbp)
jmp 0x5562
movq -0x248(%rbp), %rdi
movq 0x33a30(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x5577
leaq -0xa0(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x8fa0
jmp 0x558c
movq 0x33a35(%rip), %rdi     # 0x38fc8
leaq 0x33d4e(%rip), %rsi     # 0x392e8
callq 0x3280
jmp 0x55a1
movq 0x33a20(%rip), %rdi     # 0x38fc8
leaq 0x1fc33(%rip), %rsi     # 0x251e2
callq 0x3290
movq %rax, -0x250(%rbp)
jmp 0x55bd
movq -0x250(%rbp), %rdi
movq 0x339d5(%rip), %rsi     # 0x38fa0
callq 0x32f0
jmp 0x55d2
movl $0x0, -0x4(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x58b0
leaq -0x108(%rbp), %rdi
callq 0x5820
leaq -0xa0(%rbp), %rdi
callq 0x58d0
movl -0x4(%rbp), %eax
addq $0x250, %rsp            # imm = 0x250
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x5820
leaq -0xa0(%rbp), %rdi
callq 0x58d0
movq -0xd0(%rbp), %rdi
callq 0x34f0
nopl (%rax)