// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movq %rdx, 0x68(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0xd0(%rsp), %rdi
callq 0x52aea
movq (%r15), %r12
movq %r15, 0x60(%rsp)
cmpq 0x8(%r15), %r12
je 0x9d003
leaq 0xb8(%rsp), %rdi
leaq 0x20(%rsp), %r13
leaq 0x40(%rsp), %rbp
movq (%r12), %r15
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
movq %rdi, %rbx
callq 0x85982
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x53636
addq $0x18, %r15
movsd 0xb221b(%rip), %xmm0    # 0x14f058
movsd %xmm0, 0x58(%rsp)
cmpq 0x8(%r12), %r15
je 0x9cf39
movl $0x1, %ebx
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
movq %rbp, %rdi
movq %r14, %rsi
callq 0x531ea
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 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
leaq 0x78(%rsp), %rdx
leaq 0x70(%rsp), %rcx
callq 0x78f58
movsd 0x80(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x70(%rsp), %xmm2
leaq 0x88(%rsp), %rdi
callq 0x85436
movq 0x98(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x88(%rsp), %xmm0
movupd %xmm0, (%rsp)
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x857a0
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 0x9ce53
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm0, 0x58(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x10(%rsp)
movapd 0x20(%rsp), %xmm0
movupd %xmm0, (%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x52144
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0xb20e3(%rip), %xmm0    # 0x14f058
divsd 0x58(%rsp), %xmm0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x85590
movq 0x68(%rsp), %rbx
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 %rbp, %rdi
callq 0x85982
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x9cfdd
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd 0x40(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x8(%rbx)
jmp 0x9cfe8
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x9eb8a
addq $0x18, %r12
movq 0x60(%rsp), %rax
cmpq 0x8(%rax), %r12
leaq 0xb8(%rsp), %rdi
jne 0x9ce06
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x9d020
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x9d03d
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x9d05a
movq 0xe0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x9d07a
jmp 0x9d07a
jmp 0x9d07a
jmp 0x9d07a
jmp 0x9d07a
jmp 0x9d07a
jmp 0x9d07a
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x38a78
movq %rbx, %rdi
callq 0x4780