// File: Luthaf[P]zeopp/cluster.cc
// Function: cluster_aggregate(std::vector<std::vector<XYZ, std::allocator<XYZ>>, std::allocator<std::vector<XYZ, std::allocator<XYZ>>>> const&, ATOM_NETWORK const*)
vector<XYZ> cluster_aggregate(const vector< vector<XYZ> >& clusters, const ATOM_NETWORK* atmnet)
{
    MIN_PER_DISTANCE distcalc = atmnet->getDistCalc();
    vector<XYZ> reduced_clusters;
    vector <vector<XYZ> >::const_iterator clust_it = clusters.begin();


    for (; clust_it != clusters.end(); ++clust_it) {
        vector<XYZ>::const_iterator it = clust_it->begin();
        XYZ centroid(it->x, it->y, it->z);  // Assign the centroid as first point
        Point c_pt = atmnet->xyz_to_abc(centroid); // Fractional coords
        int count = 1;  //measures how many points are added in c_pt
        ++it;
        for (; it != clust_it->end(); ++it) {
            // Find the closest periodic of *it to centroid and add it to it.
            Point curr_pt = atmnet->xyz_to_abc(it->x, it->y, it->z);
            double tmp_x, tmp_y, tmp_z;    // C.P.I. of curr_pt w.r.t. centroid
            distcalc.closest_periodic_image(c_pt.vals[0], c_pt.vals[1], c_pt.vals[2], 
                                 curr_pt.vals[0], curr_pt.vals[1], curr_pt.vals[2], 
                                 tmp_x, tmp_y, tmp_z);
            c_pt = c_pt + Point(tmp_x, tmp_y, tmp_z);
            ++count;
        }
        c_pt = atmnet->abc_to_xyz(c_pt);     // Convert to absolute coordinates
        c_pt = c_pt.scale(1.0/double(count));
        reduced_clusters.push_back(XYZ(c_pt.vals[0], c_pt.vals[1], c_pt.vals[2]));
    }
    return reduced_clusters;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x1c8(%rbp)
movq %rdi, %rax
movq %rax, -0x1d0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x96bf0
movq -0x1c8(%rbp), %rdi
movb $0x0, -0x91(%rbp)
callq 0xada60
movq -0x10(%rbp), %rdi
callq 0x10ccb0
movq %rax, -0xa0(%rbp)
movq -0x10(%rbp), %rdi
callq 0x10cd20
movq %rax, -0xa8(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x10cce0
testb $0x1, %al
jne 0x10aae7
jmp 0x10ae5c
leaq -0xa0(%rbp), %rdi
callq 0x10cd50
movq %rax, %rdi
callq 0xb0260
movq %rax, -0xb0(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x10cd70
movq -0x1e8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1e0(%rbp)
callq 0x10cd70
movq -0x1e8(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1d8(%rbp)
callq 0x10cd70
movsd -0x1e0(%rbp), %xmm0
movsd -0x1d8(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
leaq -0xc8(%rbp), %rdi
callq 0xea8e0
jmp 0x10ab69
movq -0x18(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
leaq -0xc8(%rbp), %rdx
callq 0x976d0
jmp 0x10ab82
movl $0x1, -0xf4(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xb0650
leaq -0xa0(%rbp), %rdi
callq 0x10cd50
movq %rax, %rdi
callq 0xb0290
movq %rax, -0x100(%rbp)
leaq -0xb0(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xb05c0
testb $0x1, %al
jne 0x10abcf
jmp 0x10ad67
movq -0x18(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0xb0(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x10cd70
movq -0x208(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1f8(%rbp)
callq 0x10cd70
movq -0x208(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1f0(%rbp)
callq 0x10cd70
movq -0x200(%rbp), %rsi
movsd -0x1f8(%rbp), %xmm0
movsd -0x1f0(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
leaq -0x118(%rbp), %rdi
callq 0x97360
jmp 0x10ac48
movsd -0xf0(%rbp), %xmm0
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
movsd -0x118(%rbp), %xmm3
movsd -0x110(%rbp), %xmm4
movsd -0x108(%rbp), %xmm5
leaq -0x90(%rbp), %rdi
leaq -0x120(%rbp), %rsi
leaq -0x128(%rbp), %rdx
leaq -0x130(%rbp), %rcx
callq 0xd7df0
jmp 0x10ac9b
movsd -0x120(%rbp), %xmm0
movsd -0x128(%rbp), %xmm1
movsd -0x130(%rbp), %xmm2
leaq -0x160(%rbp), %rdi
callq 0xe9c70
jmp 0x10acc1
movq -0x150(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movups -0x160(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x148(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xea420
jmp 0x10acee
movq -0x148(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0x140(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x138(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl -0xf4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xf4(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0xb0650
jmp 0x10ab98
movq -0x1c8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xd0(%rbp)
movl %eax, -0xd4(%rbp)
callq 0x12210
leaq -0x90(%rbp), %rdi
callq 0x122d0
jmp 0x10ae94
movq -0x18(%rbp), %rsi
movq -0xe0(%rbp), %rax
movq %rax, -0x180(%rbp)
movups -0xf0(%rbp), %xmm0
movaps %xmm0, -0x190(%rbp)
movq -0x180(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0x190(%rbp), %xmm0
movups %xmm0, (%rax)
leaq -0x178(%rbp), %rdi
callq 0x961d0
jmp 0x10adad
movq -0x168(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0x178(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
cvtsi2sdl -0xf4(%rbp), %xmm1
movsd 0xe7277(%rip), %xmm0    # 0x1f2050
divsd %xmm1, %xmm0
leaq -0x1a8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xea000
jmp 0x10adf2
movq -0x198(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0x1a8(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
movsd -0xf0(%rbp), %xmm0
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
leaq -0x1c0(%rbp), %rdi
callq 0xea8e0
jmp 0x10ae34
movq -0x1c8(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x10cbd0
jmp 0x10ae49
jmp 0x10ae4b
leaq -0xa0(%rbp), %rdi
callq 0x10cd90
jmp 0x10aabb
movb $0x1, -0x91(%rbp)
testb $0x1, -0x91(%rbp)
jne 0x10ae78
movq -0x1c8(%rbp), %rdi
callq 0x12210
leaq -0x90(%rbp), %rdi
callq 0x122d0
movq -0x1d0(%rbp), %rax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0xd0(%rbp), %rdi
callq 0x58f0