// File: Windfisch[P]production-flow/flowgraph.cpp
// Function: FlowGraph::Node::update_forward()
void FlowGraph::Node::update_forward()
{
	if (max_production > 0)
		actual_production = max_production;
	else
		actual_production = -min(incoming(), -max_production); // never consume more than incoming
	
	multimap<int, Edge*> sorted_edges;
	for (Edge* edge : outgoing_edges)
		sorted_edges.insert( std::pair<int, Edge*>(edge->actual_capacity, edge) );

	size_t edges_remaining = sorted_edges.size();
	int amount_remaining = available();

	for (auto& it : sorted_edges)
	{
		Edge* edge = it.second;
		auto capacity = it.first;

		int fair_share = amount_remaining / edges_remaining; // beware: integer division!
		if (fair_share < capacity)
			edge->actual_flow = fair_share;
		else
		{
			edge->actual_flow = capacity; // set edge flow to maximum possible value
			amount_remaining -= capacity; // the remaining amount must now be shared over even less edges
			edges_remaining--;
		}

		assert(amount_remaining >= 0);
	}

	if (edges_remaining == 0)
	{
		excess = amount_remaining;
		if (actual_production > 0)
		{
			int reduction = min(excess, actual_production);
			actual_production -= reduction;
			excess -= reduction;
		}
	}
	else
		excess = 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpl $0x0, (%rax)
jle 0x1305d
movq -0xd8(%rbp), %rax
movl (%rax), %ecx
movl %ecx, 0x4(%rax)
jmp 0x13098
movq -0xd8(%rbp), %rdi
callq 0x12f70
movq -0xd8(%rbp), %rcx
movl %eax, -0xc(%rbp)
xorl %eax, %eax
subl (%rcx), %eax
movl %eax, -0x10(%rbp)
leaq -0xc(%rbp), %rdi
leaq -0x10(%rbp), %rsi
callq 0x14950
movq %rax, %rdx
movq -0xd8(%rbp), %rax
xorl %ecx, %ecx
subl (%rdx), %ecx
movl %ecx, 0x4(%rax)
leaq -0x40(%rbp), %rdi
callq 0x14990
movq -0xd8(%rbp), %rax
addq $0x28, %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x129c0
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
callq 0x12810
movq %rax, -0x58(%rbp)
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x149b0
testb $0x1, %al
jne 0x130dd
jmp 0x1314d
leaq -0x50(%rbp), %rdi
callq 0x12d90
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
addq $0x4, %rsi
leaq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x14a20
jmp 0x13104
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x149f0
movq %rax, -0xe0(%rbp)
jmp 0x1311a
movq -0xe0(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x14a50
jmp 0x130ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x14b70
jmp 0x1333d
leaq -0x40(%rbp), %rdi
callq 0x14a70
movq -0xd8(%rbp), %rdi
movq %rax, -0x90(%rbp)
callq 0x12ff0
movl %eax, -0xe4(%rbp)
jmp 0x13171
movl -0xe4(%rbp), %eax
movl %eax, -0x94(%rbp)
leaq -0x40(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x14a90
movq %rax, -0xa8(%rbp)
movq -0xa0(%rbp), %rdi
callq 0x14ac0
movq %rax, -0xb0(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x14af0
testb $0x1, %al
jne 0x131ca
jmp 0x132a9
leaq -0xa8(%rbp), %rdi
callq 0x14b20
movq %rax, -0xb8(%rbp)
movq -0xb8(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0xc0(%rbp)
movq -0xb8(%rbp), %rax
movl (%rax), %eax
movl %eax, -0xc4(%rbp)
movslq -0x94(%rbp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq -0x90(%rbp)
movl %eax, -0xc8(%rbp)
movl -0xc8(%rbp), %eax
cmpl -0xc4(%rbp), %eax
jge 0x13236
movl -0xc8(%rbp), %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x8(%rax)
jmp 0x1326c
movl -0xc4(%rbp), %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x8(%rax)
movl -0xc4(%rbp), %ecx
movl -0x94(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x94(%rbp)
movq -0x90(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x90(%rbp)
cmpl $0x0, -0x94(%rbp)
jl 0x13277
jmp 0x13296
leaq 0x122bb(%rip), %rdi     # 0x25539
leaq 0x122ca(%rip), %rsi     # 0x2554f
movl $0x46, %edx
leaq 0x122cc(%rip), %rcx     # 0x2555d
callq 0x31e0
jmp 0x13298
leaq -0xa8(%rbp), %rdi
callq 0x14b40
jmp 0x131ae
cmpq $0x0, -0x90(%rbp)
jne 0x1331d
movq -0xd8(%rbp), %rax
movl -0x94(%rbp), %ecx
movl %ecx, 0x8(%rax)
cmpl $0x0, 0x4(%rax)
jle 0x1331b
movq -0xd8(%rbp), %rsi
movq %rsi, %rdi
addq $0x8, %rdi
addq $0x4, %rsi
callq 0x14950
movq %rax, -0xf0(%rbp)
jmp 0x132e9
movq -0xd8(%rbp), %rax
movq -0xf0(%rbp), %rcx
movl (%rcx), %ecx
movl %ecx, -0xcc(%rbp)
movl -0xcc(%rbp), %edx
movl 0x4(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x4(%rax)
movl -0xcc(%rbp), %edx
movl 0x8(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x8(%rax)
jmp 0x1332b
movq -0xd8(%rbp), %rax
movl $0x0, 0x8(%rax)
leaq -0x40(%rbp), %rdi
callq 0x14b70
addq $0xf0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x34f0
nopw %cs:(%rax,%rax)