// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/channel.cc
// Function: PORE::reconstruct()
void PORE::reconstruct(){
    vector<bool> haveVisited = vector<bool>(nodes.size(),false);
    vector<DELTA_POS> displacements = vector<DELTA_POS>(nodes.size());
    unsigned int visitCount = 0;
    
    //Search through the PORE, emphasizing nodes located in the current unit
    //cell or in previously visited unit cells. Record the unit cell within which
    //each node is encountered
    comparer = ReconstructorComparator();
    HEAP< pair<int,DELTA_POS> > stack (compareNodes);
    stack.insert(pair<int,DELTA_POS> (0, DELTA_POS(0,0,0))); // Pick the first node as the starting point
    DELTA_POS curPos = DELTA_POS(0,0,0);
    
    // Continue reconstruction until all nodes have been visited
    while(visitCount < nodes.size()){
        if(stack.size() == 0){
            cerr << "Error: Stack empties prior to pore reconstruction completion." << "\n"
            << "Please contact the source code provided with this message." << "\n"
            << "Exiting..." << "\n"
            << "Nnodes = " << nodes.size() << "  visitCount= " << visitCount << "\n";
            
            exit(1);
        }
        
        pair<int,DELTA_POS> best = stack.pop();
        
        if(!haveVisited.at(best.first)){
            visitCount++;
            haveVisited.at(best.first) = true;
            displacements.at(best.first) = best.second;
            
            // If changed unit cell, need to restructure heap
            if(!best.second.equals(curPos)){
                comparer.setPosition(best.second);
                stack.reHeapify();
                curPos = best.second;
            }
            
            // Add all the nodes from the current node to the stack that
            // have not yet been visited
            DIJKSTRA_NODE curNode = nodes.at(best.first);
            for(unsigned int j = 0; j < curNode.connections.size(); j++){
                CONN curConn = curNode.connections.at(j);
                if(!haveVisited.at(curConn.to)){
                    DELTA_POS newPos = curPos + curConn.deltaPos;
                    stack.insert(pair<int,DELTA_POS>(curConn.to,newPos));
                }
            }
        }
    }
    
    map<DELTA_POS, vector<int> ,  bool(*)(DELTA_POS,DELTA_POS)> results (fn_pt);
    
    //Store the list of nodes and unit cells in a usable format
    for(unsigned int i = 0; i < nodes.size(); i++){
        map<DELTA_POS, vector<int> >::iterator iter = results.find(displacements.at(i));
        if(iter == results.end()){
            vector<int> newList; newList.push_back(i);
            results.insert(pair<DELTA_POS, vector<int> > (displacements.at(i),newList));
        }
        else
            iter->second.push_back(i);
    }
    
    // Copy the unit cells visited and their corresponding node ids to intrinsic
    // data structures
    map<DELTA_POS, vector<int> >::iterator rIter = results.begin();
    while(rIter != results.end()){
        unitCells.push_back(rIter->first);
        ucNodes.push_back(rIter->second);
        rIter++;
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
movq 0x68(%rdi), %rax
subq 0x60(%rdi), %rax
pushq $0x48
popq %r14
cqto
idivq %r14
leaq 0x98(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x100(%rsp), %rdi
leaq 0x40(%rsp), %rcx
movq %rax, %rsi
callq 0x2a0d6
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
idivq %r14
leaq 0x80(%rsp), %rdi
leaq 0x98(%rsp), %rdx
movq %rax, %rsi
callq 0x576de
leaq 0x98(%rsp), %rdi
callq 0x54a78
leaq 0xfdfcc(%rip), %rdi     # 0x150730
leaq 0x98(%rsp), %rsi
callq 0x57730
leaq 0xa8(%rsp), %rdi
callq 0x2abfe
leaq 0x246a(%rip), %rsi      # 0x54bef
leaq 0xe0(%rsp), %rdi
callq 0x57752
leaq 0x98(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d328
movq 0x9c(%rsp), %rdx
movl 0x98(%rsp), %esi
shlq $0x20, %rsi
leaq 0xe0(%rsp), %rdi
callq 0x57788
leaq 0x30(%rsp), %rdi
movl $0x0, 0xc(%rsp)
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d328
leaq 0x60(%rbx), %rax
movq %rax, 0x68(%rsp)
leaq 0xb8(%rsp), %r14
leaq 0xe0(%rsp), %rbp
leaq 0x100(%rsp), %r15
pushq $0x28
popq %r12
movl 0xc(%rsp), %r13d
movq 0x60(%rbx), %rsi
movq 0x68(%rbx), %rcx
movq %rcx, %rax
subq %rsi, %rax
cqto
pushq $0x48
popq %rdi
idivq %rdi
cmpq %r13, %rax
jbe 0x5299c
movq 0xe8(%rsp), %rax
subq 0xe0(%rsp), %rax
shrq $0x4, %rax
testl %eax, %eax
je 0x52b29
movq %rbp, %rdi
callq 0x577b8
movq %rax, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movslq %eax, %rsi
movq %r15, %rdi
callq 0xaba4
testq %rdx, (%rax)
jne 0x52802
movslq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0xaba4
orq %rdx, (%rax)
movslq 0x10(%rsp), %rsi
leaq 0x80(%rsp), %rdi
callq 0x576b6
leaq 0x14(%rsp), %rdi
movl 0x8(%rdi), %ecx
movl %ecx, 0x8(%rax)
movq (%rdi), %rcx
movq %rcx, (%rax)
movq 0x30(%rsp), %rsi
movl 0x38(%rsp), %edx
callq 0x3d332
testb %al, %al
jne 0x528da
movq 0x14(%rsp), %rsi
movl 0x1c(%rsp), %edx
leaq 0xfde77(%rip), %rdi     # 0x150730
callq 0x54b26
movq %rbp, %rdi
callq 0x577e2
leaq 0x14(%rsp), %rcx
movl 0x8(%rcx), %eax
movl %eax, 0x38(%rsp)
movq (%rcx), %rax
movq %rax, 0x30(%rsp)
movslq 0x10(%rsp), %rsi
movq 0x68(%rsp), %rdi
callq 0xabe2
leaq 0x98(%rsp), %rdi
movq %rax, %rsi
callq 0x29ebe
incl 0xc(%rsp)
xorl %r13d, %r13d
movl %r13d, %r13d
movq 0xc0(%rsp), %rax
subq 0xb8(%rsp), %rax
cqto
idivq %r12
cmpq %r13, %rax
jbe 0x5298f
movq %r14, %rdi
movq %r13, %rsi
callq 0x29efa
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movaps %xmm0, 0x40(%rsp)
movq 0x20(%rax), %rax
movq %rax, 0x60(%rsp)
movaps %xmm1, 0x50(%rsp)
movslq 0x44(%rsp), %rsi
movq %r15, %rdi
callq 0xaba4
testq %rdx, (%rax)
jne 0x52987
leaq 0x30(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x3d380
movq %rax, 0x70(%rsp)
movl %edx, 0x78(%rsp)
movl 0x44(%rsp), %ecx
movq 0x74(%rsp), %rdx
shlq $0x20, %rax
orq %rcx, %rax
movq %rbp, %rdi
movq %rax, %rsi
callq 0x57788
incl %r13d
jmp 0x52900
movq %r14, %rdi
callq 0xaf14
jmp 0x52802
movq 0xfdabd(%rip), %rax     # 0x150460
leaq 0xa0(%rsp), %r13
movq %rax, -0x8(%r13)
andl $0x0, (%r13)
andq $0x0, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
andq $0x0, 0x20(%r13)
xorl %r15d, %r15d
leaq 0x98(%rsp), %r12
leaq 0x40(%rsp), %rbp
subq %rsi, %rcx
movl %r15d, %r14d
movq %rcx, %rax
cqto
pushq $0x48
popq %rcx
idivq %rcx
cmpq %r14, %rax
jbe 0x52aa3
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x576b6
movq %r12, %rdi
movq %rax, %rsi
callq 0x58cf8
cmpq %r13, %rax
je 0x52a26
addq $0x30, %rax
movl %r15d, 0x40(%rsp)
movq %rax, %rdi
movq %rbp, %rsi
callq 0x10324
jmp 0x52a93
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movl %r15d, 0x40(%rsp)
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x10324
leaq 0x80(%rsp), %rdi
movq %r14, %rsi
callq 0x576b6
movl 0x8(%rax), %ecx
movl %ecx, 0x48(%rsp)
movq (%rax), %rax
movq %rax, 0x40(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
callq 0xa6ce
movq %r12, %rdi
movq %rbp, %rsi
callq 0x58d96
leaq 0x50(%rsp), %rdi
callq 0xb290
leaq 0x10(%rsp), %rdi
callq 0xb290
incl %r15d
movq 0x60(%rbx), %rsi
movq 0x68(%rbx), %rcx
jmp 0x529d6
movq 0xb0(%rsp), %r15
leaq 0x90(%rbx), %r14
addq $0xa8, %rbx
cmpq %r13, %r15
je 0x52ae3
leaq 0x20(%r15), %rsi
movq %r14, %rdi
callq 0x577f6
leaq 0x30(%r15), %rsi
movq %rbx, %rdi
callq 0xad52
movq %r15, %rdi
callq 0x4300
movq %rax, %r15
jmp 0x52ab9
leaq 0x98(%rsp), %rdi
callq 0x57c14
leaq 0xe0(%rsp), %rdi
callq 0x57d1c
leaq 0x80(%rsp), %rdi
callq 0xb758
leaq 0x100(%rsp), %rdi
callq 0xa976
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xfd4c0(%rip), %rdi     # 0x14fff0
leaq 0xb0e20(%rip), %rsi     # 0x103957
callq 0x4480
leaq 0xafa3b(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
leaq 0xb0e43(%rip), %rsi     # 0x103995
movq %rax, %rdi
callq 0x4480
leaq 0xafa1d(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
leaq 0xb0ab2(%rip), %rsi     # 0x103622
movq %rax, %rdi
callq 0x4480
leaq 0xaf9ff(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
leaq 0xb0e42(%rip), %rsi     # 0x1039d0
movq %rax, %rdi
callq 0x4480
movq %rax, %rdi
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
pushq $0x48
popq %rcx
idivq %rcx
movq %rax, %rsi
callq 0x42e0
leaq 0xb0e22(%rip), %rsi     # 0x1039da
movq %rax, %rdi
callq 0x4480
movq %rax, %rdi
movq %r13, %rsi
callq 0x42e0
leaq 0xaf9ac(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
pushq $0x1
popq %rdi
callq 0x524c
movq %rax, %rbx
jmp 0x52c4d
movq %rax, %rbx
jmp 0x52c5a
jmp 0x52c2c
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xb290
jmp 0x52c06
jmp 0x52c03
jmp 0x52c2c
jmp 0x52c1a
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xb290
jmp 0x52c1d
jmp 0x52c2c
jmp 0x52c2c
jmp 0x52c2c
jmp 0x52c1a
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x57c14
jmp 0x52c40
movq %rax, %rbx
jmp 0x52c40
jmp 0x52c35
jmp 0x52c35
movq %rax, %rbx
movq %r14, %rdi
callq 0xaf14
leaq 0xe0(%rsp), %rdi
callq 0x57d1c
leaq 0x80(%rsp), %rdi
callq 0xb758
leaq 0x100(%rsp), %rdi
callq 0xa976
movq %rbx, %rdi
callq 0x4780
nop