// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xc0(%rsp), %rdi
movq %rdx, %rsi
callq 0x3ec9e
xorpd %xmm0, %xmm0
andq $0x0, 0x10(%rbx)
movq %rbx, 0x38(%rsp)
movupd %xmm0, (%rbx)
movq %r15, 0x58(%rsp)
movq (%r15), %r12
leaq 0xa8(%rsp), %r15
leaq 0x20(%rsp), %rbp
leaq 0x40(%rsp), %rbx
movq 0x58(%rsp), %rax
cmpq 0x8(%rax), %r12
je 0x73d77
movq (%r12), %r13
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movq %r15, %rdi
callq 0x6402e
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3f424
addq $0x18, %r13
pushq $0x1
popq %r15
cmpq 0x8(%r12), %r13
je 0x73cdb
movsd (%r13), %xmm0
movsd 0x8(%r13), %xmm1
movsd 0x10(%r13), %xmm2
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3f1ce
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x40(%rsp), %xmm3
movsd 0x48(%rsp), %xmm4
movsd 0x50(%rsp), %xmm5
leaq 0xc0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0x68(%rsp), %rdx
leaq 0x60(%rsp), %rcx
callq 0x59d06
movsd 0x70(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0x63c46
movq 0x88(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x78(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0x90(%rsp), %rdi
movq %rbp, %rsi
callq 0x63e70
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%rsp)
movupd 0x90(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
incl %r15d
addq $0x18, %r13
jmp 0x73c07
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3e536
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
cvtsi2sd %r15d, %xmm1
movsd 0x8833c(%rip), %xmm0    # 0xfc058
divsd %xmm1, %xmm0
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x63d70
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq %rbx, %rdi
callq 0x6402e
leaq 0xa8(%rsp), %r15
movq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x75542
addq $0x18, %r12
jmp 0x73bc4
leaq 0xc0(%rsp), %rdi
callq 0xb0f0
movq 0x38(%rsp), %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x73da9
jmp 0x73da9
jmp 0x73da9
jmp 0x73da9
jmp 0x73da9
jmp 0x73da9
jmp 0x73da9
movq %rax, %rbx
movq 0x38(%rsp), %rdi
callq 0xb132
leaq 0xc0(%rsp), %rdi
callq 0xb0f0
movq %rbx, %rdi
callq 0x4780