// 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*, 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 0x527f2
movq (%r15), %r12
movq %r15, 0x60(%rsp)
cmpq 0x8(%r15), %r12
je 0x9b44d
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 0x84364
movq %r13, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x53338
addq $0x18, %r15
movsd 0xb1dd1(%rip), %xmm0    # 0x14d058
movsd %xmm0, 0x58(%rsp)
cmpq 0x8(%r12), %r15
je 0x9b383
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 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 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 0x9b29d
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 0x51d04
movq 0x50(%rsp), %rax
movq %rax, 0x30(%rsp)
movups 0x40(%rsp), %xmm0
movaps %xmm0, 0x20(%rsp)
movsd 0xb1c99(%rip), %xmm0    # 0x14d058
divsd 0x58(%rsp), %xmm0
movq %rbp, %rdi
movq %r13, %rsi
callq 0x84096
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 0x84364
movq 0x8(%rbx), %rsi
cmpq 0x10(%rbx), %rsi
je 0x9b427
movq 0x50(%rsp), %rax
movq %rax, 0x10(%rsi)
movupd 0x40(%rsp), %xmm0
movupd %xmm0, (%rsi)
addq $0x18, 0x8(%rbx)
jmp 0x9b432
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x9d006
addq $0x18, %r12
movq 0x60(%rsp), %rax
cmpq 0x8(%rax), %r12
leaq 0xb8(%rsp), %rdi
jne 0x9b250
movq 0x100(%rsp), %rdi
testq %rdi, %rdi
je 0x9b46a
movq 0x110(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xe8(%rsp), %rdi
testq %rdi, %rdi
je 0x9b487
movq 0xf8(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0xd0(%rsp), %rdi
testq %rdi, %rdi
je 0x9b4a4
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 0x9b4c4
jmp 0x9b4c4
jmp 0x9b4c4
jmp 0x9b4c4
jmp 0x9b4c4
jmp 0x9b4c4
jmp 0x9b4c4
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x385cc
movq %rbx, %rdi
callq 0x4780