// File: leobru[P]ACPUfont/averager.cc
// Function: averageToMean(std::vector<Picture, std::allocator<Picture>>&, bool)
void averageToMean(vector<Picture> & pics, bool contrastMean) {
    Picture mean = pics.front();
    for (uint i = 1; i < pics.size(); ++i) {
        mean = mean.add(pics[i], 0, 0);
    }
    mean = mean.divide();
    mean.dump(fopen("mean.pgm", "w"));
    if (contrastMean) {
        int pivot = (mean.find_knee(false) + mean.find_knee(true))/2;
        mean = mean.contrast(pivot, 2);
        mean.dump(fopen("contrasted.pgm", "w"));
    }
    Picture & res = mean;
    // Sort by closeness to the mean
    vector<std::pair<double, int> > order(pics.size());
    for (uint i = 0; i < pics.size(); ++i) {
        order[i] = std::make_pair(mean.mse(pics[i], 0, 0), i);
    }
    std::sort(order.begin(), order.end());
    for (uint i = 0; i < pics.size(); ++i) { 
        int voff = 0, hoff = 0;
        res.best_offset(pics[order[i].second], voff, hoff);
        res = res.add(pics[order[i].second], voff, hoff);
    }
    res.dump(stdout);
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movb %sil, %al
movq %rdi, 0x1f0(%rsp)
andb $0x1, %al
movb %al, 0x1ef(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x2400
movq %rax, %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x2430
movl $0x1, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movq %rax, 0x88(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x88(%rsp), %rax
cmpq %rcx, %rax
jae 0x141a
movq 0x1f0(%rsp), %rdi
movl 0x1bc(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq %rax, %rdx
leaq 0x190(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
xorl %r8d, %r8d
movl %r8d, %ecx
callq 0x2490
jmp 0x13c9
leaq 0x1c0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
callq 0x2800
leaq 0x190(%rsp), %rdi
callq 0x2840
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x1363
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
jmp 0x18ca
leaq 0x158(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x2850
jmp 0x1431
leaq 0x1c0(%rsp), %rdi
leaq 0x158(%rsp), %rsi
movq %rsi, 0x78(%rsp)
callq 0x2800
movq 0x78(%rsp), %rdi
callq 0x2840
leaq 0xbd73(%rip), %rdi      # 0xd1cf
leaq 0xbd75(%rip), %rsi      # 0xd1d8
callq 0x1100
movq %rax, 0x80(%rsp)
jmp 0x1472
movq 0x80(%rsp), %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x2960
jmp 0x1489
testb $0x1, 0x1ef(%rsp)
je 0x1555
leaq 0x1c0(%rsp), %rdi
xorl %esi, %esi
callq 0x2a40
movl %eax, 0x74(%rsp)
jmp 0x14ac
leaq 0x1c0(%rsp), %rdi
movl $0x1, %esi
callq 0x2a40
movl %eax, 0x70(%rsp)
jmp 0x14c4
movl 0x70(%rsp), %ecx
movl 0x74(%rsp), %eax
addl %ecx, %eax
movl %eax, %ecx
shrl $0x1f, %ecx
addl %ecx, %eax
sarl %eax
movl %eax, 0x154(%rsp)
movl 0x154(%rsp), %edx
leaq 0x128(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
movl $0x2, %ecx
callq 0x2da0
jmp 0x1501
leaq 0x1c0(%rsp), %rdi
leaq 0x128(%rsp), %rsi
movq %rsi, 0x60(%rsp)
callq 0x2800
movq 0x60(%rsp), %rdi
callq 0x2840
leaq 0xbcae(%rip), %rdi      # 0xd1da
leaq 0xbca5(%rip), %rsi      # 0xd1d8
callq 0x1100
movq %rax, 0x68(%rsp)
jmp 0x153f
movq 0x68(%rsp), %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x2960
jmp 0x1553
jmp 0x1555
leaq 0x1c0(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x2470
movq %rax, 0x50(%rsp)
leaq 0x107(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x2fe0
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x108(%rsp), %rdi
callq 0x2ff0
jmp 0x15a2
leaq 0x107(%rsp), %rdi
callq 0x3070
movl $0x0, 0x100(%rsp)
movl 0x100(%rsp), %eax
movq %rax, 0x48(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x48(%rsp), %rax
cmpq %rcx, %rax
jae 0x170a
movq 0x1f0(%rsp), %rdi
movl 0x100(%rsp), %eax
movl %eax, %esi
callq 0x27e0
movq %rax, %rsi
leaq 0x1c0(%rsp), %rdi
xorl %ecx, %ecx
movl %ecx, %edx
callq 0x30c0
movsd %xmm0, 0x40(%rsp)
jmp 0x1616
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x3080
movl %eax, 0x34(%rsp)
movsd %xmm0, 0x38(%rsp)
jmp 0x1646
movl 0x34(%rsp), %eax
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
movl %eax, 0xe0(%rsp)
movl 0xe0(%rsp), %eax
movl %eax, 0xf8(%rsp)
movq 0xd8(%rsp), %rax
movq %rax, 0xf0(%rsp)
movl 0x100(%rsp), %eax
movl %eax, %esi
leaq 0x108(%rsp), %rdi
callq 0x3280
movq %rax, %rdi
leaq 0xf0(%rsp), %rsi
callq 0x32a0
jmp 0x16a6
jmp 0x16a8
movl 0x100(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x100(%rsp)
jmp 0x15ba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
leaq 0x107(%rsp), %rdi
callq 0x3070
jmp 0x18ca
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x188(%rsp)
movl %eax, 0x184(%rsp)
leaq 0x108(%rsp), %rdi
callq 0x3490
jmp 0x18ca
leaq 0x108(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x3310
movq 0x28(%rsp), %rdi
movq %rax, 0xd0(%rsp)
callq 0x3340
movq %rax, 0xc8(%rsp)
movq 0xd0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x32d0
jmp 0x174d
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movq %rax, 0x20(%rsp)
movq 0x1f0(%rsp), %rdi
callq 0x2470
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x188f
movl $0x0, 0xc0(%rsp)
movl $0x0, 0xbc(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, 0x10(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, %esi
leaq 0x108(%rsp), %rdi
callq 0x3280
movq 0x10(%rsp), %rdi
movslq 0x8(%rax), %rsi
callq 0x27e0
movq 0x18(%rsp), %rdi
movq %rax, %rsi
leaq 0xc0(%rsp), %rdx
leaq 0xbc(%rsp), %rcx
callq 0x3370
jmp 0x17f5
movq 0x120(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x1f0(%rsp), %rax
movq %rax, (%rsp)
movl 0xc4(%rsp), %eax
movl %eax, %esi
leaq 0x108(%rsp), %rdi
callq 0x3280
movq (%rsp), %rdi
movslq 0x8(%rax), %rsi
callq 0x27e0
movq 0x8(%rsp), %rsi
movq %rax, %rdx
movl 0xc0(%rsp), %ecx
movl 0xbc(%rsp), %r8d
leaq 0x90(%rsp), %rdi
callq 0x2490
jmp 0x1857
movq 0x120(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x2800
leaq 0x90(%rsp), %rdi
callq 0x2840
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x1758
movq 0x120(%rsp), %rdi
movq 0x10712(%rip), %rax     # 0x11fb0
movq (%rax), %rsi
callq 0x2960
jmp 0x18a8
leaq 0x108(%rsp), %rdi
callq 0x3490
leaq 0x1c0(%rsp), %rdi
callq 0x2840
addq $0x1f8, %rsp            # imm = 0x1F8
retq
leaq 0x1c0(%rsp), %rdi
callq 0x2840
movq 0x188(%rsp), %rdi
callq 0x11b0
nopw %cs:(%rax,%rax)