// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/channel.cc
// Function: CHANNEL::findFreeIncludedSphereDiameterforNode(int, std::pair<double, std::pair<double, double>>)
pair <double, pair<double,double> > CHANNEL::findFreeIncludedSphereDiameterforNode(int nodeid, pair<double, pair<double,double> > maxdidfdif){
    //Define three access types
    const int ACCESSED   =  1;
    const int UNKNOWN      = -1;
    
    // Make a local copy of nodes vector as it will be modified
    vector<DIJKSTRA_NODE> lnodes = nodes;
    
    // Initialize the unit cell position of each node
    vector<DELTA_POS> directions = vector<DELTA_POS> (lnodes.size(), DELTA_POS(0,0,0));
    
    // Initialize the status of each node as UNKNOWN
    vector<int> accessStatuses = vector<int> (lnodes.size(), UNKNOWN);
    unsigned int nodeIndex = 0;
    
    double maxdi,di,df;
    
    
    // In case of early termination, be ready to return the current node Di
    maxdidfdif.first = 2.0 * lnodes.at(nodeid).max_radius; // .first is use to store the current Di for the current (nodeid) node
    
    // Test if it is worth to run the funtion (e.g. starting node's radius is smaller than the current Df)
    if(lnodes.at(nodeid).max_radius < maxdidfdif.second.first/2.0)
    {
        return maxdidfdif; // if not, just return the current best numbers (maxdidfdif)
    };
    
    // Start analysis for node of id = nodeid
    
    nodeIndex = nodeid;
    
    // accept the initial node
    maxdi = lnodes.at(nodeIndex).max_radius; // (it is not really the max but will be analyzed to get max)
    di=lnodes.at(nodeIndex).max_radius;
    df=lnodes.at(nodeIndex).max_radius;
    
    accessStatuses[nodeIndex] = ACCESSED;
    
    directions[nodeIndex] = DELTA_POS(0,0,0);
    
    compareConnections_ptr = &lnodes;
    HEAP< pair<int,int> > stack(compareConnections);
    
    // loop over all connections and add them to stack
    for(unsigned int i=0; i<lnodes[nodeIndex].connections.size(); i++)
    {
        if(lnodes[nodeIndex].connections.at(i).max_radius > lnodes.at(lnodes[nodeIndex].connections.at(i).to).max_radius)
            lnodes[nodeIndex].connections.at(i).max_radius = lnodes.at(lnodes[nodeIndex].connections.at(i).to).max_radius;
        
        stack.insert(pair<int,int>(nodeIndex,i));
    };
    
    stack.reHeapify();
    
    bool flag=true;
    
    while(stack.size() != 0&&flag){
        //loop with Dijkstra-like propagation
        
        // select the path with largest opening
        pair<int,int> best = stack.pop();
        
        int best_node = lnodes[best.first].connections[best.second].to;
        
        if(accessStatuses[best_node]==UNKNOWN)
        {
            // going to accept the best_node and update di and df
            // update df and di
            if(lnodes[best.first].connections.at(best.second).max_radius<df) df=lnodes[best.first].connections.at(best.second).max_radius;
            
            if(lnodes.at(best_node).max_radius > di) di = lnodes.at(best_node).max_radius;
            
            // termine early if the current Df is smaller than the current record
            
            if(df * 2.0 < maxdidfdif.second.first)
            {
                return maxdidfdif; // if not, just return the current best numbers (maxdidfdif) 
            };
            
            
            // check new connections, add to heap or terminane
            
            accessStatuses[best_node]=ACCESSED; 
            directions[best_node]=directions[best.first];
            directions[best_node]=directions[best_node] + (lnodes[best.first].connections.at(best.second).deltaPos);
            
            // going to add new connections to the heap
            for(unsigned int i=0; i<lnodes[best_node].connections.size(); i++)
            {
                if(lnodes[best_node].connections.at(i).max_radius > lnodes.at(lnodes[best_node].connections.at(i).to).max_radius)
                    lnodes[best_node].connections.at(i).max_radius = lnodes.at(lnodes[best_node].connections.at(i).to).max_radius;
                
                if(lnodes[best_node].connections[i].to!=best.first)
                    stack.insert(pair<int,int>(best_node,i));
            };	
            stack.reHeapify(); 
        }
        
        else if(accessStatuses[best_node]==ACCESSED){
            //selected connection leads to a node that is on stact
            //need to check in which unit cell it is
            
            
            if(!(directions[best_node]).equals(directions[best.first] + lnodes[best.first].connections.at(best.second).deltaPos))
            {
                // if nodes are in different unit cells, a loop is found, the resulting df can be calculated
                if(df>nodes[best.first].connections.at(best.second).max_radius) df=nodes[best.first].connections.at(best.second).max_radius;
                flag=false;
                break;
            };
            // otherwise do nothing
            // need to make sure heap is correct
        }
        
        else{
            //
            // need this section ?
            //
        };
        
    };
    
    // change radii to diameter
    df=df*2.0;
    di=di*2.0;
    maxdi=maxdi*2.0;
    
    pair <double, pair<double,double> > resultdidfdif;
    resultdidfdif.first=maxdi;
    resultdidfdif.second.first=df;
    resultdidfdif.second.second=di;
    
    return resultdidfdif;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %edx, %ebx
movq %rdi, %r12
orl $-0x1, 0x3c(%rsp)
addq $0x60, %rsi
leaq 0x8(%rsp), %r14
movq %r14, %rdi
movq %rsi, 0x50(%rsp)
callq 0x2a78c
movq (%r14), %r15
movq 0x8(%r14), %r14
leaq 0x58(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d328
subq %r15, %r14
pushq $0x48
popq %r15
movq %r14, %rax
cqto
idivq %r15
leaq 0x78(%rsp), %rdi
leaq 0x58(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rax, %rsi
callq 0x579e8
movq %r12, 0x30(%rsp)
movq 0x10(%rsp), %rax
subq 0x8(%rsp), %rax
cqto
idivq %r15
leaq 0x90(%rsp), %rdi
leaq 0x3c(%rsp), %rdx
leaq 0x58(%rsp), %rcx
movq %rax, %rsi
callq 0x2a44a
movslq %ebx, %r15
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xabe2
leaq 0x100(%rsp), %r14
movsd 0x38(%rax), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, (%r14)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xabe2
movsd 0x8(%r14), %xmm1
movsd 0xa60c6(%rip), %xmm0    # 0xfc038
movsd %xmm1, 0xa8(%rsp)
mulsd %xmm1, %xmm0
ucomisd 0x38(%rax), %xmm0
jbe 0x55fa1
movq 0x10(%r14), %rax
movq 0x30(%rsp), %rbx
movq %rax, 0x10(%rbx)
movupd (%r14), %xmm0
movupd %xmm0, (%rbx)
jmp 0x563f1
movl %ebx, %r15d
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movapd %xmm0, 0xb0(%rsp)
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movapd %xmm0, 0x40(%rsp)
movq 0x90(%rsp), %rax
movl $0x1, (%rax,%r15,4)
leaq 0x58(%rsp), %rdi
xorl %ebx, %ebx
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x3d328
movq 0x78(%rsp), %rax
imulq $0xc, %r15, %rcx
leaq 0x58(%rsp), %r13
movl 0x8(%r13), %edx
movl %edx, 0x8(%rax,%rcx)
movq (%r13), %rdx
movq %rdx, (%rax,%rcx)
movq %r12, 0xfa737(%rip)     # 0x150770
leaq 0x4ab(%rip), %rsi       # 0x564eb
movq %r13, %rdi
callq 0x57a3e
imulq $0x48, %r15, %r14
movl %ebx, %ebx
movq 0x8(%rsp), %rcx
movq 0x28(%rcx,%r14), %rax
subq 0x20(%rcx,%r14), %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %rbx, %rax
jbe 0x5611c
leaq (%rcx,%r14), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
callq 0x29efa
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
callq 0x29efa
movslq 0x4(%rax), %rsi
movq %r12, %rdi
callq 0xabe2
movsd 0x20(%rsp), %xmm0
ucomisd 0x38(%rax), %xmm0
jbe 0x56103
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
callq 0x29efa
movslq 0x4(%rax), %rsi
movq %r12, %rdi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq (%rax,%r14), %rdi
addq $0x20, %rdi
movq %rbx, %rsi
callq 0x29efa
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movq %rbx, %rsi
shlq $0x20, %rsi
orq %r15, %rsi
movq %r13, %rdi
callq 0x57a74
incl %ebx
jmp 0x5604c
leaq 0x58(%rsp), %rdi
callq 0x57aa2
leaq 0x58(%rsp), %r15
movq 0x60(%rsp), %rax
subq 0x58(%rsp), %rax
shrq $0x3, %rax
testl %eax, %eax
je 0x563b8
movq %r15, %rdi
callq 0x57ab6
movq %rax, %r13
movslq %r13d, %r14
movq 0x8(%rsp), %rax
imulq $0x48, %r14, %r12
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %r13, %rbp
sarq $0x20, %rbp
movq (%rdi), %rax
imulq $0x28, %rbp, %rcx
movslq 0x4(%rax,%rcx), %rbx
movq 0x90(%rsp), %rax
movl (%rax,%rbx,4), %eax
cmpl $0x1, %eax
je 0x5636f
cmpl $-0x1, %eax
jne 0x5612b
movq %rbp, %rsi
callq 0x29efa
movapd 0x40(%rsp), %xmm0
ucomisd 0x10(%rax), %xmm0
jbe 0x561c1
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movsd 0x10(%rax), %xmm0
movapd %xmm0, 0x40(%rsp)
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xabe2
movsd 0x38(%rax), %xmm0
ucomisd 0x28(%rsp), %xmm0
jbe 0x561f3
leaq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movapd 0x40(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd 0xa8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x56472
movq 0x90(%rsp), %rax
movl $0x1, (%rax,%rbx,4)
movq 0x78(%rsp), %rax
imulq $0xc, %r14, %rcx
imulq $0xc, %rbx, %r15
movl 0x8(%rax,%rcx), %edx
movl %edx, 0x8(%rax,%r15)
movq (%rax,%rcx), %rcx
movq %rcx, (%rax,%r15)
movq 0x78(%rsp), %r14
movq 0x8(%rsp), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
addq %r15, %r14
addq $0x18, %rax
movq %r14, %rdi
movq %rax, %rsi
callq 0x3d380
movq 0x78(%rsp), %rcx
movq %rax, (%rcx,%r15)
movl %edx, 0x8(%rcx,%r15)
movl %ebx, %r14d
xorl %ebp, %ebp
imulq $0x48, %rbx, %rbx
leaq 0x58(%rsp), %r15
leaq 0x8(%rsp), %r12
movl %ebp, %ebp
movq 0x8(%rsp), %rcx
movq 0x28(%rcx,%rbx), %rax
subq 0x20(%rcx,%rbx), %rax
cqto
pushq $0x28
popq %rsi
idivq %rsi
cmpq %rbp, %rax
jbe 0x563ab
leaq (%rcx,%rbx), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movslq 0x4(%rax), %rsi
movq %r12, %rdi
callq 0xabe2
movsd 0x20(%rsp), %xmm0
ucomisd 0x38(%rax), %xmm0
jbe 0x56341
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movslq 0x4(%rax), %rsi
movq %r12, %rdi
callq 0xabe2
movsd 0x38(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x8(%rsp), %rax
leaq (%rax,%rbx), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
movq 0x8(%rsp), %rax
movq 0x20(%rax,%rbx), %rax
imulq $0x28, %rbp, %rcx
cmpl %r13d, 0x4(%rax,%rcx)
je 0x56368
movq %rbp, %rsi
shlq $0x20, %rsi
orq %r14, %rsi
movq %r15, %rdi
callq 0x57a74
incl %ebp
jmp 0x5628a
movq 0x78(%rsp), %r13
movq %rbp, %rsi
callq 0x29efa
imulq $0xc, %r14, %rdi
addq %r13, %rdi
addq $0x18, %rax
movq %rax, %rsi
callq 0x3d380
imulq $0xc, %rbx, %rcx
addq %rcx, %r13
movq %r13, %rdi
movq %rax, %rsi
callq 0x3d332
testb %al, %al
jne 0x5612b
jmp 0x56427
movq %r15, %rdi
callq 0x57aa2
jmp 0x5612b
movapd 0x40(%rsp), %xmm1
movapd 0xb0(%rsp), %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
addpd %xmm0, %xmm0
movsd 0x28(%rsp), %xmm1
addsd %xmm1, %xmm1
movq 0x30(%rsp), %rbx
movupd %xmm0, (%rbx)
movsd %xmm1, 0x10(%rbx)
leaq 0x58(%rsp), %rdi
callq 0x10942
leaq 0x90(%rsp), %rdi
callq 0xb290
leaq 0x78(%rsp), %rdi
callq 0xb758
leaq 0x8(%rsp), %rdi
callq 0xae22
movq %rbx, %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x50(%rsp), %rax
movq (%rax), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movapd 0x40(%rsp), %xmm1
ucomisd 0x10(%rax), %xmm1
jbe 0x563be
movq 0x50(%rsp), %rax
movq (%rax), %rax
leaq (%rax,%r12), %rdi
addq $0x20, %rdi
movq %rbp, %rsi
callq 0x29efa
movsd 0x10(%rax), %xmm1
jmp 0x563be
leaq 0x100(%rsp), %rcx
movq 0x10(%rcx), %rax
movq 0x30(%rsp), %rbx
movq %rax, 0x10(%rbx)
movupd (%rcx), %xmm0
movupd %xmm0, (%rbx)
jmp 0x563e7
jmp 0x564b5
jmp 0x564b5
jmp 0x564b5
jmp 0x564aa
jmp 0x564b5
movq %rax, %rbx
jmp 0x564cf
jmp 0x564a5
movq %rax, %rbx
jmp 0x564d9
movq %rax, %rbx
jmp 0x564c2
jmp 0x564b5
jmp 0x564b5
jmp 0x564b5
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x10942
leaq 0x90(%rsp), %rdi
callq 0xb290
leaq 0x78(%rsp), %rdi
callq 0xb758
leaq 0x8(%rsp), %rdi
callq 0xae22
movq %rbx, %rdi
callq 0x4780