// File: 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:
subq $0x288, %rsp            # imm = 0x288
movq %rdi, 0x280(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2400
movq %rax, %rsi
leaq 0x258(%rsp), %rdi
callq 0x2430
movq 0x280(%rsp), %rdi
callq 0x2470
movl %eax, 0x254(%rsp)
movl $0x1, 0x250(%rsp)
movl 0x250(%rsp), %eax
movq %rax, 0x50(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x50(%rsp), %rax
cmpq %rcx, %rax
jae 0x19ec
movq 0x280(%rsp), %rdi
movl 0x250(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq %rax, %rdx
leaq 0x228(%rsp), %rdi
leaq 0x258(%rsp), %rsi
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x2490
jmp 0x199b
leaq 0x258(%rsp), %rdi
leaq 0x228(%rsp), %rsi
callq 0x2800
leaq 0x228(%rsp), %rdi
callq 0x2840
movl 0x250(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x250(%rsp)
jmp 0x193b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x208a
leaq 0x1f0(%rsp), %rdi
leaq 0x258(%rsp), %rsi
callq 0x2850
jmp 0x1a03
leaq 0x258(%rsp), %rdi
leaq 0x1f0(%rsp), %rsi
callq 0x2800
leaq 0x1f0(%rsp), %rdi
callq 0x2840
leaq 0x1d8(%rsp), %rdi
callq 0x34f0
movl $0x0, 0x1d4(%rsp)
movl 0x1d4(%rsp), %eax
movq %rax, 0x48(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x48(%rsp), %rax
subq $0x1, %rcx
cmpq %rcx, %rax
jae 0x1be9
movl 0x1d4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d0(%rsp)
movl 0x1d0(%rsp), %eax
movq %rax, 0x40(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x40(%rsp), %rax
cmpq %rcx, %rax
jae 0x1bd1
movl $0x0, 0x1cc(%rsp)
movl $0x0, 0x1c8(%rsp)
movq 0x280(%rsp), %rdi
movl 0x1d4(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq %rax, 0x30(%rsp)
movq 0x280(%rsp), %rdi
movl 0x1d0(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq 0x30(%rsp), %rdi
movq %rax, %rsi
leaq 0x1cc(%rsp), %rdx
leaq 0x1c8(%rsp), %rcx
callq 0x3370
movsd %xmm0, 0x38(%rsp)
jmp 0x1b12
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x1c0(%rsp)
movaps 0xb508(%rip), %xmm1     # 0xd030
movsd 0x1c0(%rsp), %xmm0
pxor %xmm1, %xmm0
movlpd %xmm0, 0x1a0(%rsp)
movl 0x1d4(%rsp), %esi
movl 0x1d0(%rsp), %edx
movl 0x1cc(%rsp), %ecx
movl 0x1c8(%rsp), %r8d
leaq 0x190(%rsp), %rdi
callq 0x3560
jmp 0x1b6a
leaq 0x1a8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
leaq 0x190(%rsp), %rdx
callq 0x3530
jmp 0x1b89
leaq 0x1d8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x3500
jmp 0x1ba0
jmp 0x1ba2
movl 0x1d0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d0(%rsp)
jmp 0x1a7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x207d
jmp 0x1bd3
movl 0x1d4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d4(%rsp)
jmp 0x1a3d
leaq 0x1d8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x35e0
movq 0x28(%rsp), %rdi
movq %rax, 0x188(%rsp)
callq 0x3610
movq %rax, 0x180(%rsp)
movq 0x188(%rsp), %rdi
movq 0x180(%rsp), %rsi
callq 0x35a0
jmp 0x1c2c
jmp 0x1c2e
leaq 0x1d8(%rsp), %rdi
callq 0x3640
xorb $-0x1, %al
testb $0x1, %al
jne 0x1c46
jmp 0x1f58
leaq 0x1d8(%rsp), %rdi
callq 0x3690
movq 0x8(%rax), %rcx
movq %rcx, 0x170(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x178(%rsp)
movq 0x280(%rsp), %rdi
movslq 0x170(%rsp), %rsi
callq 0x27e0
movsd 0x18(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c92
jp 0x1c92
jmp 0x1cb7
movq 0x280(%rsp), %rdi
movslq 0x174(%rsp), %rsi
callq 0x27e0
movsd 0x18(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1cc9
jp 0x1cc9
leaq 0x1d8(%rsp), %rdi
callq 0x36d0
jmp 0x1c2e
movq 0x280(%rsp), %rdi
movslq 0x170(%rsp), %rsi
callq 0x27e0
movq %rax, 0x20(%rsp)
movq 0x280(%rsp), %rdi
movslq 0x174(%rsp), %rsi
callq 0x27e0
movq 0x20(%rsp), %rsi
movq %rax, %rdx
movl 0x178(%rsp), %ecx
movl 0x17c(%rsp), %r8d
leaq 0x148(%rsp), %rdi
callq 0x2490
jmp 0x1d1e
leaq 0x120(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x36f0
jmp 0x1d31
movq 0x280(%rsp), %rdi
movslq 0x170(%rsp), %rsi
callq 0x27e0
movq %rax, %rdi
leaq 0x120(%rsp), %rsi
movq %rsi, 0x18(%rsp)
callq 0x2800
movq 0x18(%rsp), %rdi
callq 0x2840
leaq 0xf8(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x36f0
jmp 0x1d78
movq 0x280(%rsp), %rdi
movslq 0x174(%rsp), %rsi
callq 0x27e0
movq %rax, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x2800
leaq 0xf8(%rsp), %rdi
callq 0x2840
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movq %rax, 0x10(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x10(%rsp), %rax
cmpq %rcx, %rax
jae 0x1f1e
movq 0x280(%rsp), %rdi
movl 0xf4(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movsd 0x18(%rax), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1e0a
jp 0x1e0a
jmp 0x1f06
movq 0x280(%rsp), %rdi
movl 0xf4(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
leaq 0xec(%rsp), %rcx
callq 0x3370
movsd %xmm0, 0x8(%rsp)
jmp 0x1e48
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movaps 0xb1d2(%rip), %xmm1     # 0xd030
movsd 0xe0(%rsp), %xmm0
pxor %xmm1, %xmm0
movlpd %xmm0, 0xc0(%rsp)
movq 0x280(%rsp), %rdi
callq 0x2470
movl %eax, %esi
movl 0xf4(%rsp), %edx
movl 0xf0(%rsp), %ecx
movl 0xec(%rsp), %r8d
leaq 0xb0(%rsp), %rdi
callq 0x3560
jmp 0x1ea8
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
leaq 0xb0(%rsp), %rdx
callq 0x3530
jmp 0x1ec7
leaq 0x1d8(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x3500
jmp 0x1ede
jmp 0x1f06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x2840
jmp 0x207d
jmp 0x1f08
movl 0xf4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xf4(%rsp)
jmp 0x1db5
movq 0x280(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x3790
jmp 0x1f35
movl 0x254(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x254(%rsp)
leaq 0x148(%rsp), %rdi
callq 0x2840
jmp 0x1c2e
movl $0x0, 0xac(%rsp)
movl $0x0, 0xa8(%rsp)
movq 0x280(%rsp), %rdi
callq 0x3800
movq %rax, %rsi
leaq 0x258(%rsp), %rdi
leaq 0xac(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
callq 0x3370
jmp 0x1f9d
movl 0x278(%rsp), %esi
movl 0x27c(%rsp), %edx
leaq 0x58(%rsp), %rdi
callq 0x36f0
jmp 0x1fb7
movq 0x280(%rsp), %rdi
callq 0x3800
movq %rax, %rdx
movl 0xac(%rsp), %ecx
movl 0xa8(%rsp), %r8d
leaq 0x80(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x2490
jmp 0x1fea
movq 0xffbf(%rip), %rax      # 0x11fb0
movq (%rax), %rsi
leaq 0x80(%rsp), %rdi
callq 0x2960
jmp 0x2003
leaq 0x80(%rsp), %rdi
callq 0x2840
leaq 0x58(%rsp), %rdi
callq 0x2840
leaq 0x1d8(%rsp), %rdi
callq 0x3840
leaq 0x258(%rsp), %rdi
callq 0x2840
addq $0x288, %rsp            # imm = 0x288
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
jmp 0x2073
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x220(%rsp)
movl %eax, 0x21c(%rsp)
leaq 0x80(%rsp), %rdi
callq 0x2840
leaq 0x58(%rsp), %rdi
callq 0x2840
leaq 0x1d8(%rsp), %rdi
callq 0x3840
leaq 0x258(%rsp), %rdi
callq 0x2840
movq 0x220(%rsp), %rdi
callq 0x11b0
nopw %cs:(%rax,%rax)