// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl (%rdi), %eax
testl %eax, %eax
jg 0x79b5
movq 0x10(%rdi), %rdx
movq 0x18(%rdi), %rsi
xorl %ecx, %ecx
cmpq %rsi, %rdx
je 0x79ac
movq (%rdx), %r8
addl 0x8(%r8), %ecx
addq $0x8, %rdx
jmp 0x799a
negl %eax
cmpl %eax, %ecx
cmovll %ecx, %eax
negl %eax
movl %eax, 0x4(%rdi)
leaq 0x20(%rsp), %r13
movl $0x0, (%r13)
xorl %r12d, %r12d
movq %r12, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %r12, 0x20(%r13)
movq 0x28(%rdi), %rbp
movq %rdi, (%rsp)
movq 0x30(%rdi), %rbx
cmpq %rbx, %rbp
je 0x7a17
leaq 0x18(%rsp), %r14
leaq 0x8(%rsp), %r15
movq (%rbp), %rax
movl 0x4(%rax), %ecx
movl %ecx, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x9b9e
addq $0x8, %rbp
cmpq %rbx, %rbp
jne 0x79f3
movq (%rsp), %rbx
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
cmpq %rcx, %rax
je 0x7a3b
xorl %r12d, %r12d
movq (%rax), %rdx
addl 0x8(%rdx), %r12d
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x7a2b
movq 0x40(%rsp), %r15
movl 0x4(%rbx), %ebp
addl %ebp, %r12d
xorl %r14d, %r14d
testl %r12d, %r12d
cmovlel %r14d, %r12d
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x7a8d
movq 0x28(%rdi), %rcx
movl 0x20(%rdi), %esi
movslq %r12d, %rax
xorl %edx, %edx
divq %r15
cmpl %eax, %esi
jle 0x7a72
movl %eax, 0x8(%rcx)
jmp 0x7a7b
movl %esi, 0x8(%rcx)
subl %esi, %r12d
decq %r15
testl %r12d, %r12d
js 0x7acf
callq 0x31b0
movq %rax, %rdi
cmpq %r13, %rax
jne 0x7a5a
testq %r15, %r15
jne 0x7ab2
movl %r12d, 0x8(%rbx)
testl %ebp, %ebp
jle 0x7ab6
movl 0x4(%rbx), %eax
movl %eax, %ecx
cmpl %r12d, %eax
jl 0x7aa7
movl 0x8(%rbx), %ecx
subl %ecx, %eax
movl %eax, 0x4(%rbx)
subl %ecx, %r12d
movl %r12d, %r14d
movl %r14d, 0x8(%rbx)
leaq 0x18(%rsp), %rdi
callq 0x9b4e
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7a53(%rip), %rdi      # 0xf529
leaq 0x7a62(%rip), %rsi      # 0xf53f
leaq 0x7a69(%rip), %rcx      # 0xf54d
movl $0x46, %edx
callq 0x3180
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x9b4e
movq %rbx, %rdi
callq 0x3400
nop