// File: Luthaf[P]zeopp/cluster.cc
// Function: cluster_partition(ATOM_NETWORK*, float)
vector< vector<XYZ> > cluster_partition(ATOM_NETWORK* atmnet, const float cutoff_dist)
{
    //Algorithm:
    
    // Partition a high accuracy voronoi net into clusters.
    ATOM_NETWORK ha_atmnet = *atmnet;
    string acc_setting = "S30";
    setupHighAccuracyAtomNetwork(&ha_atmnet, acc_setting);

    vector<VOR_CELL> vcell;
    vector<BASIC_VCELL> bvcell;
    VORONOI_NETWORK vornet, ha_vornet;
    performVoronoiDecomp(true, atmnet, &vornet, &vcell, false, &bvcell);
    vcell.clear();
    bvcell.clear();
    performVoronoiDecomp(true, &ha_atmnet, &ha_vornet, &vcell, false, &bvcell);
    DIJKSTRA_NETWORK dnet;
    DIJKSTRA_NETWORK::buildDijkstraNetwork(&ha_vornet, &dnet);

    for (DnIt it = dnet.nodes.begin(); it != dnet.nodes.end(); ++it)
        it->active = false;
    
    for (VnIt it = vornet.nodes.begin(); it != vornet.nodes.end(); ++it)
        cout << it->x << " " << it->y << " " << it->z << endl;

    vector <vector<XYZ> > clusters;
    
    for (VnIt nodeit = vornet.nodes.begin(); nodeit != vornet.nodes.end(); ++nodeit) {
        // Get the first non assigned high accuracy voronoi node within the 
        // cutoff distance to current low accuracy voronoi node
        XYZ low_acc_node_loc(nodeit->x, nodeit->y, nodeit->z);
        pair<DnIt, DnIt> p = get_first_closer_nonassigned_node(   // Highaccuracy dijkstra
                low_acc_node_loc, *atmnet, dnet.nodes, cutoff_dist);

        // Initialize cluster and tovisit_nodes
        vector<XYZ> cluster;
        vector<int> tovisit_ids;
        vector<int> visited_ids;

        // Add the current high accuracy node to tovisit ids
        tovisit_ids.push_back(p.first->id);

        // Populate the cluster
        while (!tovisit_ids.empty()){
            //Work on the current node
            int curr_id = tovisit_ids.back();
            DIJKSTRA_NODE curr_nd = dnet.nodes.at(curr_id);
            cluster.push_back(XYZ(curr_nd.x, curr_nd.y, curr_nd.z));
            visited_ids.push_back(curr_nd.id);
            tovisit_ids.pop_back();

            //If any connected nodes are unvisited and are within cutoff distance,
            //push them to tovisit_ids
            vector<CONN>::iterator connit = curr_nd.connections.begin();
            for (; connit != curr_nd.connections.end(); ++connit){
                int to_nodeid = connit->to;
                DIJKSTRA_NODE to_node = dnet.nodes.at(to_nodeid);
                double dist_to_low_acc_node = atmnet->calcDistanceXYZ(
                        nodeit->x, nodeit->y, nodeit->z,
                        to_node.x, to_node.y, to_node.z);
                if (find(visited_ids.begin(), visited_ids.end(), to_nodeid) == visited_ids.end() 
                        && dist_to_low_acc_node < cutoff_dist)
                    tovisit_ids.push_back(to_nodeid); 
            }
        }
        
        clusters.push_back(cluster);
        tovisit_ids.clear();
        visited_ids.clear();
        cluster.clear();
        
    }
    // assert (clusters.size() == vornet.nodes.size());

    return clusters;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x910, %rsp            # imm = 0x910
movq %rdi, -0x838(%rbp)
movq %rdi, %rax
movq %rax, -0x830(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movss %xmm0, -0x14(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x478(%rbp), %rdi
callq 0x10c910
leaq -0x499(%rbp), %rdi
movq %rdi, -0x828(%rbp)
callq 0x5900
movq -0x828(%rbp), %rdx
leaq 0xf2925(%rip), %rsi     # 0x1fc96f
leaq -0x498(%rbp), %rdi
callq 0x47530
jmp 0x10a058
leaq -0x499(%rbp), %rdi
callq 0x55d0
leaq -0x4d0(%rbp), %rdi
leaq -0x498(%rbp), %rsi
callq 0x5220
jmp 0x10a079
leaq -0x478(%rbp), %rdi
leaq -0x4d0(%rbp), %rsi
callq 0xfc1c0
jmp 0x10a08e
leaq -0x4d0(%rbp), %rdi
callq 0x5300
leaq -0x4e8(%rbp), %rdi
callq 0xfd00
leaq -0x500(%rbp), %rdi
callq 0xfce0
leaq -0x578(%rbp), %rdi
callq 0x99ba0
jmp 0x10a0c0
leaq -0x5f0(%rbp), %rdi
callq 0x99ba0
jmp 0x10a0ce
movq -0x10(%rbp), %rsi
movl $0x1, %edi
leaq -0x578(%rbp), %rdx
leaq -0x4e8(%rbp), %rcx
xorl %r8d, %r8d
leaq -0x500(%rbp), %r9
callq 0x72d0
jmp 0x10a0f6
leaq -0x4e8(%rbp), %rdi
movq %rdi, -0x848(%rbp)
callq 0x15be0
leaq -0x500(%rbp), %rdi
movq %rdi, -0x840(%rbp)
callq 0x15bc0
movq -0x848(%rbp), %rcx
movq -0x840(%rbp), %r9
movl $0x1, %edi
leaq -0x478(%rbp), %rsi
leaq -0x5f0(%rbp), %rdx
xorl %r8d, %r8d
callq 0x72d0
jmp 0x10a147
leaq -0x650(%rbp), %rdi
callq 0x94790
jmp 0x10a155
leaq -0x5f0(%rbp), %rdi
leaq -0x650(%rbp), %rsi
callq 0x948c0
jmp 0x10a16a
leaq -0x650(%rbp), %rdi
callq 0x2a000
movq %rax, -0x658(%rbp)
leaq -0x650(%rbp), %rdi
callq 0x29c80
movq %rax, -0x660(%rbp)
leaq -0x658(%rbp), %rdi
leaq -0x660(%rbp), %rsi
callq 0x67c30
testb $0x1, %al
jne 0x10a1ac
jmp 0x10a283
leaq -0x658(%rbp), %rdi
callq 0x10c8c0
movb $0x0, 0x40(%rax)
leaq -0x658(%rbp), %rdi
callq 0x67c70
jmp 0x10a17d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
leaq -0x499(%rbp), %rdi
callq 0x55d0
jmp 0x10aa42
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10aa36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
leaq -0x4d0(%rbp), %rdi
callq 0x5300
jmp 0x10aa36
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10aa1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10aa12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10aa06
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10a9fa
leaq -0x578(%rbp), %rdi
addq $0x48, %rdi
callq 0x25230
movq %rax, -0x668(%rbp)
leaq -0x578(%rbp), %rdi
addq $0x48, %rdi
callq 0x250b0
movq %rax, -0x670(%rbp)
leaq -0x668(%rbp), %rdi
leaq -0x670(%rbp), %rsi
callq 0x599b0
testb $0x1, %al
jne 0x10a2cd
jmp 0x10a39e
leaq -0x668(%rbp), %rdi
callq 0x599f0
movsd (%rax), %xmm0
movq 0x168cdc(%rip), %rdi    # 0x272fc0
callq 0x5980
movq %rax, -0x850(%rbp)
jmp 0x10a2f2
movq -0x850(%rbp), %rdi
leaq 0xf1fae(%rip), %rsi     # 0x1fc2ae
callq 0x5540
movq %rax, -0x858(%rbp)
jmp 0x10a30e
leaq -0x668(%rbp), %rdi
callq 0x599f0
movq -0x858(%rbp), %rdi
movsd 0x8(%rax), %xmm0
callq 0x5980
movq %rax, -0x860(%rbp)
jmp 0x10a334
movq -0x860(%rbp), %rdi
leaq 0xf1f6c(%rip), %rsi     # 0x1fc2ae
callq 0x5540
movq %rax, -0x868(%rbp)
jmp 0x10a350
leaq -0x668(%rbp), %rdi
callq 0x599f0
movq -0x868(%rbp), %rdi
movsd 0x10(%rax), %xmm0
callq 0x5980
movq %rax, -0x870(%rbp)
jmp 0x10a376
movq -0x870(%rbp), %rdi
movq 0x168c14(%rip), %rsi    # 0x272f98
callq 0x55b0
jmp 0x10a38b
jmp 0x10a38d
leaq -0x668(%rbp), %rdi
callq 0xb4bd0
jmp 0x10a29a
movq -0x838(%rbp), %rdi
movb $0x0, -0x671(%rbp)
callq 0xafd80
leaq -0x578(%rbp), %rdi
addq $0x48, %rdi
callq 0x25230
movq %rax, -0x680(%rbp)
leaq -0x578(%rbp), %rdi
addq $0x48, %rdi
callq 0x250b0
movq %rax, -0x688(%rbp)
leaq -0x680(%rbp), %rdi
leaq -0x688(%rbp), %rsi
callq 0x599b0
testb $0x1, %al
jne 0x10a3fb
jmp 0x10a96e
leaq -0x680(%rbp), %rdi
movq %rdi, -0x888(%rbp)
callq 0x599f0
movq -0x888(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x880(%rbp)
callq 0x599f0
movq -0x888(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x878(%rbp)
callq 0x599f0
movsd -0x880(%rbp), %xmm0
movsd -0x878(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
leaq -0x6a0(%rbp), %rdi
callq 0xea8e0
jmp 0x10a462
movq -0x690(%rbp), %rax
movq %rax, -0x6c0(%rbp)
movups -0x6a0(%rbp), %xmm0
movaps %xmm0, -0x6d0(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x890(%rbp)
leaq -0x6e8(%rbp), %rdi
leaq -0x650(%rbp), %rsi
callq 0x67230
jmp 0x10a49e
movq -0x890(%rbp), %rdi
movss -0x14(%rbp), %xmm0
movq -0x6c0(%rbp), %rcx
movq %rsp, %rax
movq %rcx, 0x10(%rax)
movaps -0x6d0(%rbp), %xmm1
movups %xmm1, (%rax)
leaq -0x6e8(%rbp), %rsi
callq 0x109ec0
movq %rdx, -0x8a0(%rbp)
movq %rax, -0x898(%rbp)
jmp 0x10a4de
movq -0x8a0(%rbp), %rax
movq -0x898(%rbp), %rcx
movq %rcx, -0x6b0(%rbp)
movq %rax, -0x6a8(%rbp)
leaq -0x6e8(%rbp), %rdi
callq 0x10e40
leaq -0x700(%rbp), %rdi
callq 0xada60
leaq -0x718(%rbp), %rdi
movq %rdi, -0x8a8(%rbp)
callq 0xd0d0
leaq -0x730(%rbp), %rdi
callq 0xd0d0
leaq -0x6b0(%rbp), %rdi
callq 0x10c8c0
movq -0x8a8(%rbp), %rdi
movq %rax, %rsi
callq 0xd1b0
jmp 0x10a54e
jmp 0x10a550
leaq -0x718(%rbp), %rdi
callq 0x10cb90
xorb $-0x1, %al
testb $0x1, %al
jne 0x10a567
jmp 0x10a8d7
leaq -0x718(%rbp), %rdi
callq 0x29870
movl (%rax), %eax
movl %eax, -0x734(%rbp)
movslq -0x734(%rbp), %rsi
leaq -0x650(%rbp), %rdi
callq 0x10130
movq %rax, -0x8b0(%rbp)
jmp 0x10a597
movq -0x8b0(%rbp), %rsi
leaq -0x780(%rbp), %rdi
callq 0x64400
jmp 0x10a5ac
movsd -0x778(%rbp), %xmm0
movsd -0x770(%rbp), %xmm1
movsd -0x768(%rbp), %xmm2
leaq -0x798(%rbp), %rdi
callq 0xea8e0
jmp 0x10a5d2
leaq -0x700(%rbp), %rdi
leaq -0x798(%rbp), %rsi
callq 0x10cbd0
jmp 0x10a5e7
leaq -0x730(%rbp), %rdi
leaq -0x780(%rbp), %rsi
callq 0xd1b0
jmp 0x10a5fc
leaq -0x718(%rbp), %rdi
callq 0xaefa0
leaq -0x780(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a8e0
movq %rax, -0x7a0(%rbp)
leaq -0x780(%rbp), %rdi
addq $0x20, %rdi
callq 0x2a710
movq %rax, -0x7a8(%rbp)
leaq -0x7a0(%rbp), %rdi
leaq -0x7a8(%rbp), %rsi
callq 0x65370
testb $0x1, %al
jne 0x10a652
jmp 0x10a8b8
leaq -0x7a0(%rbp), %rdi
callq 0x653b0
movl 0x4(%rax), %eax
movl %eax, -0x7ac(%rbp)
movslq -0x7ac(%rbp), %rsi
leaq -0x650(%rbp), %rdi
callq 0x10130
movq %rax, -0x8b8(%rbp)
jmp 0x10a683
movq -0x8b8(%rbp), %rsi
leaq -0x7f8(%rbp), %rdi
callq 0x64400
jmp 0x10a698
movq -0x10(%rbp), %rax
movq %rax, -0x8d8(%rbp)
leaq -0x680(%rbp), %rdi
movq %rdi, -0x8e0(%rbp)
callq 0x599f0
movq -0x8e0(%rbp), %rdi
movsd (%rax), %xmm0
movsd %xmm0, -0x8d0(%rbp)
callq 0x599f0
movq -0x8e0(%rbp), %rdi
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0x8c8(%rbp)
callq 0x599f0
movq -0x8d8(%rbp), %rdi
movsd -0x8d0(%rbp), %xmm0
movsd -0x8c8(%rbp), %xmm1
movsd 0x10(%rax), %xmm2
movsd -0x7f0(%rbp), %xmm3
movsd -0x7e8(%rbp), %xmm4
movsd -0x7e0(%rbp), %xmm5
callq 0x97a10
movsd %xmm0, -0x8c0(%rbp)
jmp 0x10a72a
movsd -0x8c0(%rbp), %xmm0
movsd %xmm0, -0x800(%rbp)
leaq -0x730(%rbp), %rdi
movq %rdi, -0x8f0(%rbp)
callq 0x15890
movq -0x8f0(%rbp), %rdi
movq %rax, -0x810(%rbp)
callq 0x15720
movq %rax, -0x818(%rbp)
movq -0x810(%rbp), %rdi
movq -0x818(%rbp), %rsi
leaq -0x7ac(%rbp), %rdx
callq 0x10cc40
movq %rax, -0x8e8(%rbp)
jmp 0x10a78a
movq -0x8e8(%rbp), %rax
movq %rax, -0x808(%rbp)
leaq -0x730(%rbp), %rdi
callq 0x15720
movq %rax, -0x820(%rbp)
leaq -0x808(%rbp), %rdi
leaq -0x820(%rbp), %rsi
callq 0x10cc00
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x8f1(%rbp)
jne 0x10a7cf
jmp 0x10a7ed
movsd -0x800(%rbp), %xmm1
movss -0x14(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
seta %al
movb %al, -0x8f1(%rbp)
movb -0x8f1(%rbp), %al
testb $0x1, %al
jne 0x10a7fc
jmp 0x10a89b
leaq -0x718(%rbp), %rdi
leaq -0x7ac(%rbp), %rsi
callq 0xd1b0
jmp 0x10a811
jmp 0x10a89b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10a9ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
leaq -0x6e8(%rbp), %rdi
callq 0x10e40
jmp 0x10a9ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10a945
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
jmp 0x10a8c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x4a8(%rbp)
movl %eax, -0x4ac(%rbp)
leaq -0x7f8(%rbp), %rdi
callq 0x100e0
jmp 0x10a8c9
leaq -0x7f8(%rbp), %rdi
callq 0x100e0
leaq -0x7a0(%rbp), %rdi
callq 0x10cc90
jmp 0x10a61f
leaq -0x780(%rbp), %rdi
callq 0x100e0
jmp 0x10a550
leaq -0x780(%rbp), %rdi
callq 0x100e0
jmp 0x10a945
movq -0x838(%rbp), %rdi
leaq -0x700(%rbp), %rsi
callq 0xafda0
jmp 0x10a8ec
leaq -0x718(%rbp), %rdi
callq 0x163b0
leaq -0x730(%rbp), %rdi
callq 0x163b0
leaq -0x700(%rbp), %rdi
callq 0xadfc0
leaq -0x730(%rbp), %rdi
callq 0xec80
leaq -0x718(%rbp), %rdi
callq 0xec80
leaq -0x700(%rbp), %rdi
callq 0x12210
leaq -0x680(%rbp), %rdi
callq 0xb4bd0
jmp 0x10a3c8
leaq -0x730(%rbp), %rdi
callq 0xec80
leaq -0x718(%rbp), %rdi
callq 0xec80
leaq -0x700(%rbp), %rdi
callq 0x12210
jmp 0x10a9ee
movb $0x1, -0x671(%rbp)
testb $0x1, -0x671(%rbp)
jne 0x10a98a
movq -0x838(%rbp), %rdi
callq 0xafe50
leaq -0x650(%rbp), %rdi
callq 0xf840
leaq -0x5f0(%rbp), %rdi
callq 0xf8a0
leaq -0x578(%rbp), %rdi
callq 0xf8a0
leaq -0x500(%rbp), %rdi
callq 0xfd80
leaq -0x4e8(%rbp), %rdi
callq 0xfd20
leaq -0x498(%rbp), %rdi
callq 0x5300
leaq -0x478(%rbp), %rdi
callq 0xfde0
movq -0x830(%rbp), %rax
addq $0x910, %rsp            # imm = 0x910
popq %rbp
retq
movq -0x838(%rbp), %rdi
callq 0xafe50
leaq -0x650(%rbp), %rdi
callq 0xf840
leaq -0x5f0(%rbp), %rdi
callq 0xf8a0
leaq -0x578(%rbp), %rdi
callq 0xf8a0
leaq -0x500(%rbp), %rdi
callq 0xfd80
leaq -0x4e8(%rbp), %rdi
callq 0xfd20
leaq -0x498(%rbp), %rdi
callq 0x5300
leaq -0x478(%rbp), %rdi
callq 0xfde0
movq -0x4a8(%rbp), %rdi
callq 0x58f0
nopw (%rax,%rax)