// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movl %esi, %r12d
movq %rdi, %rbx
movq (%rdi), %rsi
leaq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x1b9a
pushq $0x1
popq %r13
leaq 0x50(%rsp), %r15
movl %r13d, %r13d
movq (%rbx), %rcx
movq 0x8(%rbx), %rax
subq %rcx, %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %r13, %rax
jbe 0x1395
imulq $0x28, %r13, %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 %r13d
jmp 0x1349
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x1d60
leaq 0x28(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d3c
movq %r15, %rdi
callq 0x256e
leaq 0x3c78(%rip), %rdi      # 0x5040
leaq 0x3c7a(%rip), %rsi      # 0x5049
callq 0x1110
movq %r14, %rdi
movq %rax, %rsi
callq 0x1dea
testb %r12b, %r12b
je 0x1455
leaq 0x28(%rsp), %rdi
xorl %esi, %esi
callq 0x1e80
movl %eax, %r14d
leaq 0x28(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x1e80
addl %r14d, %eax
pushq $0x2
popq %rcx
cltd
idivl %ecx
leaq 0x50(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movl %eax, %edx
callq 0x1fa4
leaq 0x28(%rsp), %r14
leaq 0x50(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1d3c
movq %r15, %rdi
callq 0x256e
leaq 0x3c0d(%rip), %rdi      # 0x504b
leaq 0x3c04(%rip), %rsi      # 0x5049
callq 0x1110
movq %r14, %rdi
movq %rax, %rsi
callq 0x1dea
movq 0x8(%rbx), %rax
subq (%rbx), %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %rax, %rsi
callq 0x212c
xorl %ebp, %ebp
leaq 0x28(%rsp), %r14
movl %ebp, %r15d
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %r15, %rax
jbe 0x14c2
imulq $0x28, %r15, %rax
addq %rax, %rsi
movq %r14, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x217e
movq 0x10(%rsp), %rax
shlq $0x4, %r15
movsd %xmm0, (%rax,%r15)
movl %ebp, 0x8(%rax,%r15)
incl %ebp
jmp 0x147d
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x321b
xorl %r15d, %r15d
leaq 0x28(%rsp), %r14
leaq 0x8(%rsp), %r12
leaq 0x50(%rsp), %r13
movl %r15d, %r15d
movq (%rbx), %rsi
movq 0x8(%rbx), %rax
subq %rsi, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cmpq %r15, %rax
jbe 0x156f
andl $0x0, 0xc(%rsp)
andl $0x0, 0x8(%rsp)
movq 0x10(%rsp), %rax
movq %r15, %rbp
shlq $0x4, %rbp
movslq 0x8(%rax,%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movq %r14, %rdi
leaq 0xc(%rsp), %rdx
movq %r12, %rcx
callq 0x2288
movq 0x10(%rsp), %rax
movslq 0x8(%rax,%rbp), %rax
imulq $0x28, %rax, %rdx
addq (%rbx), %rdx
movl 0xc(%rsp), %ecx
movl 0x8(%rsp), %r8d
movq %r13, %rdi
movq %r14, %rsi
callq 0x1bba
movq %r14, %rdi
movq %r13, %rsi
callq 0x1d3c
movq %r13, %rdi
callq 0x256e
incl %r15d
jmp 0x14e3
movq 0x7a3a(%rip), %rax      # 0x8fb0
movq (%rax), %rsi
leaq 0x28(%rsp), %rdi
callq 0x1dea
leaq 0x10(%rsp), %rdi
callq 0x316a
leaq 0x28(%rsp), %rdi
callq 0x256e
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15c1
jmp 0x15c1
jmp 0x15c1
jmp 0x15c1
jmp 0x15c1
jmp 0x15b2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x316a
jmp 0x15c4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x256e
movq %rbx, %rdi
callq 0x11b0