// File: /workspace/github_cpp_makefile_star2_O3/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 0x7931
movq 0x10(%rdi), %rdx
movq 0x18(%rdi), %rsi
xorl %ecx, %ecx
cmpq %rsi, %rdx
je 0x7928
movq (%rdx), %r8
addl 0x8(%r8), %ecx
addq $0x8, %rdx
jmp 0x7916
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
movl $0x0, %r14d
cmpq %rbx, %rbp
je 0x799e
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 0x9aec
addq $0x8, %rbp
cmpq %rbx, %rbp
jne 0x7975
movq 0x40(%rsp), %r14
movq (%rsp), %rbx
movq 0x10(%rbx), %rax
movq 0x18(%rbx), %rcx
cmpq %rcx, %rax
je 0x79c2
xorl %r12d, %r12d
movq (%rax), %rdx
addl 0x8(%rdx), %r12d
addq $0x8, %rax
cmpq %rcx, %rax
jne 0x79b2
movl 0x4(%rbx), %ebp
addl %ebp, %r12d
xorl %r15d, %r15d
testl %r12d, %r12d
cmovlel %r15d, %r12d
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x7a0c
movq 0x28(%rdi), %rcx
movl 0x20(%rdi), %esi
movl %r12d, %eax
xorl %edx, %edx
divq %r14
cmpl %eax, %esi
jle 0x79f4
movl %eax, 0x8(%rcx)
jmp 0x79ff
movl %esi, 0x8(%rcx)
subl %esi, %r12d
js 0x7a4d
decq %r14
callq 0x31b0
movq %rax, %rdi
cmpq %r13, %rax
jne 0x79dc
testq %r14, %r14
jne 0x7a30
movl %r12d, 0x8(%rbx)
testl %ebp, %ebp
jle 0x7a34
movl 0x4(%rbx), %eax
cmpl %r12d, %eax
movl %r12d, %ecx
cmovll %eax, %ecx
subl %ecx, %eax
movl %eax, 0x4(%rbx)
subl %ecx, %r12d
movl %r12d, %r15d
movl %r15d, 0x8(%rbx)
leaq 0x18(%rsp), %rdi
callq 0x9a9c
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x7ad5(%rip), %rdi      # 0xf529
leaq 0x7ae4(%rip), %rsi      # 0xf53f
leaq 0x7aeb(%rip), %rcx      # 0xf54d
movl $0x46, %edx
callq 0x3180
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x9a9c
movq %rbx, %rdi
callq 0x3400
nop