// 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*, std::vector<XYZ, std::allocator<XYZ>>*)
void cluster_aggregate(const vector< vector<XYZ> >& clusters, const ATOM_NETWORK* atmnet, vector<XYZ>* reduced_clusters)
{
    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;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x90(%rbp), %rdi
callq 0x96bf0
movq -0x8(%rbp), %rdi
callq 0x10ccb0
movq %rax, -0x98(%rbp)
movq -0x8(%rbp), %rdi
callq 0x10cd20
movq %rax, -0xa0(%rbp)
leaq -0x98(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x10cce0
testb $0x1, %al
jne 0x10af03
jmp 0x10b277
leaq -0x98(%rbp), %rdi
callq 0x10cd50
movq %rax, %rdi
callq 0xb0260
movq %rax, -0xa8(%rbp)
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x10cd70
movq -0x1d8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1d0(%rbp)
callq 0x10cd70
movq -0x1d8(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1c8(%rbp)
callq 0x10cd70
movsd -0x1d0(%rbp), %xmm0
movsd -0x1c8(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
leaq -0xc0(%rbp), %rdi
callq 0xea8e0
jmp 0x10af85
movq -0x10(%rbp), %rsi
leaq -0xf0(%rbp), %rdi
leaq -0xc0(%rbp), %rdx
callq 0x976d0
jmp 0x10af9e
movl $0x1, -0xf4(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xb0650
leaq -0x98(%rbp), %rdi
callq 0x10cd50
movq %rax, %rdi
callq 0xb0290
movq %rax, -0x100(%rbp)
leaq -0xa8(%rbp), %rdi
leaq -0x100(%rbp), %rsi
callq 0xb05c0
testb $0x1, %al
jne 0x10afeb
jmp 0x10b177
movq -0x10(%rbp), %rax
movq %rax, -0x1f0(%rbp)
leaq -0xa8(%rbp), %rdi
movq %rdi, -0x1f8(%rbp)
callq 0x10cd70
movq -0x1f8(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x1e8(%rbp)
callq 0x10cd70
movq -0x1f8(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x1e0(%rbp)
callq 0x10cd70
movq -0x1f0(%rbp), %rsi
movsd -0x1e8(%rbp), %xmm0
movsd -0x1e0(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
leaq -0x118(%rbp), %rdi
callq 0x97360
jmp 0x10b064
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 0x10b0b7
movsd -0x120(%rbp), %xmm0
movsd -0x128(%rbp), %xmm1
movsd -0x130(%rbp), %xmm2
leaq -0x160(%rbp), %rdi
callq 0xe9c70
jmp 0x10b0dd
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 0x10b10a
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 -0xa8(%rbp), %rdi
callq 0xb0650
jmp 0x10afb4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xc8(%rbp)
movl %eax, -0xcc(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x122d0
jmp 0x10b28c
movq -0x10(%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 0x10b1bd
movq -0x168(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0x178(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
cvtsi2sdl -0xf4(%rbp), %xmm1
movsd 0xe6e67(%rip), %xmm0    # 0x1f2050
divsd %xmm1, %xmm0
leaq -0x1a8(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
callq 0xea000
jmp 0x10b202
movq -0x198(%rbp), %rax
movq %rax, -0xe0(%rbp)
movups -0x1a8(%rbp), %xmm0
movaps %xmm0, -0xf0(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x200(%rbp)
movsd -0xf0(%rbp), %xmm0
movsd -0xe8(%rbp), %xmm1
movsd -0xe0(%rbp), %xmm2
leaq -0x1c0(%rbp), %rdi
callq 0xea8e0
jmp 0x10b24f
movq -0x200(%rbp), %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x10cbd0
jmp 0x10b264
jmp 0x10b266
leaq -0x98(%rbp), %rdi
callq 0x10cd90
jmp 0x10aed7
leaq -0x90(%rbp), %rdi
callq 0x122d0
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq -0xc8(%rbp), %rdi
callq 0x58f0
nopl (%rax,%rax)