// File: Windfisch[P]production-flow/flowgraph.cpp
// Function: FlowGraph::Node::update_backward()
void FlowGraph::Node::update_backward()
{
	if (excess <= 0)
		return;

	int amount = incoming() - excess;

	multimap<int, Edge*> sorted_edges;
	for (Edge* edge : incoming_edges)
		sorted_edges.insert( std::pair<int, Edge*>(edge->actual_flow, edge) );

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

	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_capacity = fair_share;
		else
		{
			edge->actual_capacity = 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);
	}

	assert(edges_remaining > 0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xe0, %rsp
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xd0(%rbp)
cmpl $0x0, 0x8(%rax)
jg 0x13375
jmp 0x135bc
movq -0xd0(%rbp), %rdi
callq 0x12f70
movq -0xd0(%rbp), %rcx
subl 0x8(%rcx), %eax
movl %eax, -0xc(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x14990
movq -0xd0(%rbp), %rax
addq $0x10, %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 0x133d3
jmp 0x13443
leaq -0x50(%rbp), %rdi
callq 0x12d90
movq (%rax), %rax
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rsi
addq $0x8, %rsi
leaq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x14a20
jmp 0x133fa
leaq -0x40(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0x149f0
movq %rax, -0xd8(%rbp)
jmp 0x13410
movq -0xd8(%rbp), %rax
movq %rax, -0x88(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x14a50
jmp 0x133c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x78(%rbp)
movl %eax, -0x7c(%rbp)
leaq -0x40(%rbp), %rdi
callq 0x14b70
jmp 0x135c5
leaq -0x40(%rbp), %rdi
callq 0x14a70
movq %rax, -0x90(%rbp)
movl -0xc(%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 0x134a9
jmp 0x13588
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 0x13515
movl -0xc8(%rbp), %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x4(%rax)
jmp 0x1354b
movl -0xc4(%rbp), %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x4(%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 0x13556
jmp 0x13575
leaq 0x11fdc(%rip), %rdi     # 0x25539
leaq 0x11feb(%rip), %rsi     # 0x2554f
movl $0x78, %edx
leaq 0x12014(%rip), %rcx     # 0x25584
callq 0x31e0
jmp 0x13577
leaq -0xa8(%rbp), %rdi
callq 0x14b40
jmp 0x1348d
cmpq $0x0, -0x90(%rbp)
jbe 0x13594
jmp 0x135b3
leaq 0x12011(%rip), %rdi     # 0x255ac
leaq 0x11fad(%rip), %rsi     # 0x2554f
movl $0x7b, %edx
leaq 0x11fd6(%rip), %rcx     # 0x25584
callq 0x31e0
leaq -0x40(%rbp), %rdi
callq 0x14b70
addq $0xe0, %rsp
popq %rbp
retq
movq -0x78(%rbp), %rdi
callq 0x34f0
nop