// File: /workspace/github_cpp_makefile_star2_O3/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 $0x148, %rsp            # imm = 0x148
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
leaq 0xd0(%rsp), %rdi
movq %rdx, %rsi
callq 0x527f2
xorpd %xmm0, %xmm0
movupd %xmm0, (%rbx)
movq %rbx, 0x40(%rsp)
movq $0x0, 0x10(%rbx)
movq (%r15), %r12
movq %r15, 0x68(%rsp)
cmpq 0x8(%r15), %r12
je 0x9b156
leaq 0xb8(%rsp), %rbx
leaq 0x20(%rsp), %r13
leaq 0x48(%rsp), %rbp
movq (%r12), %r15
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
movq %rbx, %rdi
callq 0x84364
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x53338
addq $0x18, %r15
movsd 0xb20c5(%rip), %xmm0    # 0x14d058
movsd %xmm0, 0x60(%rsp)
cmpq 0x8(%r12), %r15
je 0x9b097
movl $0x1, %ebx
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
movq %rbp, %rdi
movq %r14, %rsi
callq 0x52eec
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x48(%rsp), %xmm3
movsd 0x50(%rsp), %xmm4
movsd 0x58(%rsp), %xmm5
leaq 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq 0x77fcc
movsd 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
leaq 0x88(%rsp), %rdi
callq 0x83f68
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x88(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x84196
movq 0xb0(%rsp), %rax
movq %rax, 0x30(%rsp)
movupd 0xa0(%rsp), %xmm0
movapd %xmm0, 0x20(%rsp)
incl %ebx
addq $0x18, %r15
cmpq 0x8(%r12), %r15
jne 0x9afa9
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0xb8(%rsp), %rbx
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x51d04
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x48(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0xb1f85(%rip), %xmm0    # 0x14d058
divsd 0x60(%rsp), %xmm0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x84096
movq 0x58(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x48(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movq %rbp, %rdi
callq 0x84364
movq 0x40(%rsp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
je 0x9b13b
movq 0x58(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd 0x48(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x9b143
movq %rbp, %rdx
callq 0x9d006
addq $0x18, %r12
movq 0x68(%rsp), %rax
cmpq 0x8(%rax), %r12
jne 0x9af5c
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x9b173
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x9b190
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x9b1ad
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x40(%rsp), %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9b1d2
jmp 0x9b1d2
jmp 0x9b1d2
jmp 0x9b1d2
jmp 0x9b1d2
jmp 0x9b1d2
jmp 0x9b1d2
movq %rax, %rbx
movq 0x40(%rsp), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x9b1ee
movq 0x10(%rax), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0xd0(%rsp), %rdi
callq 0x385cc
movq %rbx, %rdi
callq 0x4780