// File: /workspace/github_cpp_makefile_star2_O2/leobru[P]ACPUfont/averager.cc
// Function: averageSpanningTree(std::vector<Picture, std::allocator<Picture>>&)
void averageSpanningTree(vector<Picture> & pics) {
    Picture mean = pics.front();
    int remaining = pics.size();
    for (uint i = 1; i < pics.size(); ++i) {
        mean = mean.add(pics[i], 0, 0);
    }
    mean = mean.divide();
    vector<std::pair<double, offset> > offsets;
    // Try merging following the min spanning tree of pictures.
    for (uint i = 0; i < pics.size()-1; ++i) {
        for (uint j = i+1; j < pics.size(); ++j) {
            int voff = 0, hoff = 0;
            double best = pics[i].best_offset(pics[j], voff, hoff);
            offsets.push_back(std::make_pair(-best, offset(i, j, voff, hoff)));
        }
    }
    // Sort by negated best; closest pictures are at the end of the array.
    std::sort(offsets.begin(), offsets.end());
    while (!offsets.empty()) {
        offset best = offsets.back().second;
        if (pics[best.what].weight == 0 || pics[best.with].weight == 0) {
            offsets.pop_back();
            continue;
        }
        Picture next = pics[best.what].add(pics[best.with], best.voff, best.hoff);
        pics[best.what] = Picture();
        pics[best.with] = Picture();
        for (uint i = 0; i < pics.size(); ++i) {
            int voff, hoff;
            if (pics[i].weight) {
                double best = next.best_offset(pics[i], voff, hoff);
                offsets.push_back(std::make_pair(-best, offset(pics.size(), i, voff, hoff)));
            }
        }
        pics.push_back(next);
        --remaining;
    }
    // Re-center the result by correlating to the mean
    int voff = 0, hoff = 0;
    mean.best_offset(pics.back(), voff, hoff);
    Picture(mean.width, mean.height).add(pics.back(), voff, hoff).dump(stdout);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %rdi, %rbx
movq (%rdi), %rsi
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0x1b9a
pushq $0x1
popq %r12
leaq 0x30(%rsp), %r15
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
movl %r12d, %r12d
subq %rcx, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %r12, %rax
jbe 0x1652
imulq $0x28, %r12, %rax
addq %rax, %rcx
movq %r15, %rdi
movq %r14, %rsi
movq %rcx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x1bba
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d3c
movq %r15, %rdi
callq 0x256e
incl %r12d
jmp 0x1606
leaq 0x30(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x1d60
leaq 0x80(%rsp), %rdi
leaq 0x30(%rsp), %r14
movq %r14, %rsi
callq 0x1d3c
movq %r14, %rdi
callq 0x256e
leaq 0x10(%rsp), %rax
andq $0x0, 0x10(%rax)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rax)
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
xorl %ebp, %ebp
leaq 0x58(%rsp), %r12
leaq 0x4(%rsp), %r13
movl %ebp, %edi
movq %rsi, %rax
subq %rcx, %rax
cqto
pushq $0x28
popq %r8
idivq %r8
decq %rax
cmpq %rdi, %rax
jbe 0x1753
leal 0x1(%rbp), %r15d
imulq $0x28, %rdi, %r14
movl %r15d, 0xc(%rsp)
movl %r15d, %r8d
movq %rsi, %rax
subq %rcx, %rax
cqto
pushq $0x28
popq %rdi
idivq %rdi
cmpq %r8, %rax
jbe 0x1748
andl $0x0, 0x58(%rsp)
andl $0x0, 0x4(%rsp)
leaq (%rcx,%r14), %rdi
imulq $0x28, %r8, %rsi
addq %rcx, %rsi
movq %r12, %rdx
movq %r13, %rcx
callq 0x2288
xorpd 0x3922(%rip), %xmm0     # 0x5030
movl 0x58(%rsp), %eax
movl 0x4(%rsp), %ecx
movlpd %xmm0, 0x30(%rsp)
movl %ebp, 0x38(%rsp)
movl %r15d, 0x3c(%rsp)
movl %eax, 0x40(%rsp)
movl %ecx, 0x44(%rsp)
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x38ae
incl %r15d
movq (%rbx), %rcx
movq 0x8(%rbx), %rsi
jmp 0x16d0
movl 0xc(%rsp), %eax
movl %eax, %ebp
jmp 0x16a6
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x3a5d
leaq 0x30(%rsp), %r14
leaq 0x58(%rsp), %r15
leaq 0x4(%rsp), %r12
movq 0x18(%rsp), %rax
cmpq %rax, 0x10(%rsp)
je 0x18c6
movslq -0x10(%rax), %rcx
movq (%rbx), %rdx
imulq $0x28, %rcx, %r13
movsd 0x18(%rdx,%r13), %xmm0
ucomisd 0x388d(%rip), %xmm0     # 0x5028
jne 0x179f
jnp 0x17b9
movslq -0xc(%rax), %rcx
imulq $0x28, %rcx, %rbp
movsd 0x18(%rdx,%rbp), %xmm0
ucomisd 0x3873(%rip), %xmm0     # 0x5028
jne 0x17c4
jp 0x17c4
addq $-0x18, %rax
movq %rax, 0x18(%rsp)
jmp 0x1771
movl -0x8(%rax), %ecx
movl -0x4(%rax), %r8d
leaq (%rdx,%r13), %rsi
addq %rbp, %rdx
movq %r14, %rdi
callq 0x1bba
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x233a
addq (%rbx), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x1d3c
movq %r15, %rdi
callq 0x256e
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x233a
addq (%rbx), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x1d3c
movq %r15, %rdi
callq 0x256e
xorl %ebp, %ebp
leaq 0x8(%rsp), %r13
movl %ebp, %ecx
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rdi
idivq %rdi
cmpq %rcx, %rax
jbe 0x18ae
imulq $0x28, %rcx, %rax
movsd 0x18(%rsi,%rax), %xmm0
ucomisd 0x37d8(%rip), %xmm0     # 0x5028
jne 0x1854
jnp 0x18a7
addq %rax, %rsi
movq %r14, %rdi
movq %r12, %rdx
movq %r13, %rcx
callq 0x2288
movq 0x8(%rbx), %rax
subq (%rbx), %rax
xorpd 0x37bc(%rip), %xmm0     # 0x5030
cqto
pushq $0x28
popq %rcx
idivq %rcx
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %edx
movlpd %xmm0, 0x58(%rsp)
movl %eax, 0x60(%rsp)
movl %ebp, 0x64(%rsp)
movl %ecx, 0x68(%rsp)
movl %edx, 0x6c(%rsp)
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x38ae
incl %ebp
jmp 0x1825
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2372
movq %r14, %rdi
callq 0x256e
jmp 0x1771
leaq 0x4(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x8(%rsp), %rcx
andl $0x0, (%rcx)
movq 0x8(%rbx), %rsi
addq $-0x28, %rsi
leaq 0x80(%rsp), %r14
movq %r14, %rdi
callq 0x2288
movl 0x20(%r14), %esi
movl 0x24(%r14), %edx
leaq 0x58(%rsp), %rdi
callq 0x233a
movq 0x8(%rbx), %rdx
addq $-0x28, %rdx
movl 0x4(%rsp), %ecx
movl 0x8(%rsp), %r8d
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x1bba
movq 0x7689(%rip), %rax      # 0x8fb0
movq (%rax), %rsi
leaq 0x30(%rsp), %rdi
callq 0x1dea
leaq 0x30(%rsp), %rdi
callq 0x256e
leaq 0x58(%rsp), %rdi
callq 0x256e
leaq 0x10(%rsp), %rdi
callq 0x3872
leaq 0x80(%rsp), %rdi
callq 0x256e
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x256e
jmp 0x1983
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
jmp 0x19a5
jmp 0x19ac
jmp 0x19ac
jmp 0x1998
jmp 0x199d
jmp 0x199d
jmp 0x199d
jmp 0x19ac
movq %rax, %rbx
jmp 0x19b9
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x256e
jmp 0x19af
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3872
leaq 0x80(%rsp), %rdi
callq 0x256e
movq %rbx, %rdi
callq 0x11b0