// File: kelvinabrokwa[P]bottleneck/./CBottleneckDistance.cpp
// Function: CBottleneckDistance::Distance(double)
double CBottleneckDistance::Distance(double maxLevel)
{
    // If both diagrams are empty the distance is 0
    if (Generators1.size() == 0 &&  Generators2.size() == 0)
        return 0;

    PrepareEdges();

    // Clear the pairing
    Pair.clear();
    Pair.assign(2 * Max_Size, -1);

    // Clearing Layers
    Layers.clear();
    Layers.resize(Max_Size + 1);

    // No vertices are matched
    unsigned int matching = 0;

    // Clear the connection matrix and set it to the right size
    Connections.clear();
    Connections.resize(Max_Size);

    // The maximal weight of the edges which are used for the matching
    double current_weight = 0;

    // First non added edge is an iterator pointing to the first edge
    // in Edges which was added to the Connections */
    unsigned int first_not_added_edge = 0;

    // Repeat till all the vertices are matched
    while(matching < Max_Size) {

        // Add the edges with the current weight (distance) to the connection matrix
        while (Edges[first_not_added_edge].weight == current_weight && first_not_added_edge < Edges.size()) {
            // Add the edge to Connections
            Connections[Edges[first_not_added_edge].vertex_1].push_back(Edges[first_not_added_edge].vertex_2);
            ++first_not_added_edge;
        }

        // Do matching
        Hopcroft_Karp(matching);
        if (matching == Max_Size)
            return current_weight;

        // Check if we did not run out of edges. This should never happen.
        if (first_not_added_edge == Edges.size()) {
            std::cout << "Serious problem - Not enough eddges to find the matching! \n";
            return -1;
        }

        // Increase the value of the current weight
        current_weight = Edges[ first_not_added_edge ].weight;
    }

    return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x38(%rbp)
addq $0x20, %rdi
callq 0x2b10
cmpq $0x0, %rax
jne 0x2328
movq -0x38(%rbp), %rdi
addq $0x38, %rdi
callq 0x2b10
cmpq $0x0, %rax
jne 0x2328
xorps %xmm0, %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x2525
movq -0x38(%rbp), %rdi
callq 0x1810
movq -0x38(%rbp), %rdi
addq $0x70, %rdi
callq 0x3110
movq -0x38(%rbp), %rax
movq %rax, %rdi
addq $0x70, %rdi
movl 0x50(%rax), %eax
shll %eax
movl %eax, %eax
movl %eax, %esi
movl $0xffffffff, -0x1c(%rbp) # imm = 0xFFFFFFFF
leaq -0x1c(%rbp), %rdx
callq 0x3130
movq -0x38(%rbp), %rdi
addq $0xa0, %rdi
callq 0x3110
movq -0x38(%rbp), %rax
movq %rax, %rdi
addq $0xa0, %rdi
movl 0x50(%rax), %eax
addl $0x1, %eax
movl %eax, %eax
movl %eax, %esi
callq 0x3160
movq -0x38(%rbp), %rdi
movl $0x0, -0x20(%rbp)
addq $0x88, %rdi
callq 0x31f0
movq -0x38(%rbp), %rax
movq %rax, %rdi
addq $0x88, %rdi
movl 0x50(%rax), %eax
movl %eax, %esi
callq 0x3210
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x50(%rcx), %eax
jae 0x2518
jmp 0x23df
movq -0x38(%rbp), %rdi
addq $0x58, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x32a0
movsd 0x8(%rax), %xmm0
xorl %eax, %eax
ucomisd -0x28(%rbp), %xmm0
movb %al, -0x39(%rbp)
jne 0x2428
jp 0x2428
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movq %rax, -0x48(%rbp)
addq $0x58, %rdi
callq 0x32c0
movq %rax, %rcx
movq -0x48(%rbp), %rax
cmpq %rcx, %rax
setb %al
movb %al, -0x39(%rbp)
movb -0x39(%rbp), %al
testb $0x1, %al
jne 0x2431
jmp 0x2490
movq -0x38(%rbp), %rdi
movq %rdi, %rax
addq $0x88, %rax
movq %rax, -0x58(%rbp)
addq $0x58, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x32a0
movq -0x58(%rbp), %rdi
movslq (%rax), %rsi
callq 0x2d50
movq -0x38(%rbp), %rdi
movq %rax, -0x50(%rbp)
addq $0x58, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x32a0
movq -0x50(%rbp), %rdi
movq %rax, %rsi
addq $0x4, %rsi
callq 0x32e0
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x23df
movq -0x38(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x21f0
movq -0x38(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl 0x50(%rcx), %eax
jne 0x24b5
movsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0x2525
movq -0x38(%rbp), %rdi
movl -0x2c(%rbp), %eax
movq %rax, -0x60(%rbp)
addq $0x58, %rdi
callq 0x32c0
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jne 0x24f7
movq 0xdafc(%rip), %rdi      # 0xffd8
leaq 0x7b9c(%rip), %rsi      # 0xa07f
callq 0x10e0
movsd 0x7b18(%rip), %xmm0     # 0xa008
movsd %xmm0, -0x8(%rbp)
jmp 0x2525
movq -0x38(%rbp), %rdi
addq $0x58, %rdi
movl -0x2c(%rbp), %eax
movl %eax, %esi
callq 0x32a0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x28(%rbp)
jmp 0x23cd
movsd 0x7ae8(%rip), %xmm0     # 0xa008
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
addq $0x60, %rsp
popq %rbp
retq