// File: /workspace/github_cpp_makefile_star2_O3/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, %ebp
movq %rdi, 0x40(%rsp)
movl $0xffffffff, 0x64(%rsp) # imm = 0xFFFFFFFF
addq $0x60, %rsi
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %rsi, 0xb0(%rsp)
callq 0x38b26
movq (%r12), %rbx
movq 0x8(%r12), %r15
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x506b0
subq %rbx, %r15
sarq $0x3, %r15
movabsq $-0x71c71c71c71c71c7, %rbx # imm = 0x8E38E38E38E38E39
imulq %rbx, %r15
leaq 0x68(%rsp), %rdi
movq %rsp, %rdx
leaq 0x88(%rsp), %rcx
movq %r15, %rsi
callq 0x7608a
movq 0x30(%rsp), %rsi
subq 0x28(%rsp), %rsi
sarq $0x3, %rsi
imulq %rbx, %rsi
leaq 0x88(%rsp), %rdi
leaq 0x64(%rsp), %rdx
movq %rsp, %rcx
callq 0x3864a
movslq %ebp, %r15
movq 0x28(%rsp), %rax
movq 0x30(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbx, %rdx
cmpq %r15, %rdx
jbe 0x7462c
leaq 0x100(%rsp), %rsi
leaq (%r15,%r15,8), %rcx
movsd 0x38(%rax,%rcx,8), %xmm0
movapd %xmm0, %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsi)
movsd 0x8(%rsi), %xmm2
movsd 0xd8fce(%rip), %xmm1    # 0x14d038
movsd %xmm2, 0xb8(%rsp)
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x74097
movq 0x10(%rsi), %rax
movq 0x40(%rsp), %rcx
movq %rax, 0x10(%rcx)
movupd (%rsi), %xmm0
movupd %xmm0, (%rcx)
jmp 0x74521
movl %ebp, %r15d
cmpq %r15, %rdx
jbe 0x7462c
leaq (%r15,%r15,8), %rbx
movsd 0x38(%rax,%rbx,8), %xmm0
movapd %xmm0, 0xa0(%rsp)
movq 0x88(%rsp), %rax
movl $0x1, (%rax,%r15,4)
movq %rsp, %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x506b0
movq 0x68(%rsp), %rax
leaq (%r15,%r15,2), %rcx
movl 0x8(%rsp), %edx
movl %edx, 0x8(%rax,%rcx,4)
movq (%rsp), %rdx
movq %rdx, (%rax,%rcx,4)
movq %r12, 0x121674(%rip)    # 0x195768
leaq 0x5c6(%rip), %rax       # 0x746c1
movq %rax, 0x18(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, (%rsp)
movq $0x0, 0x10(%rsp)
movq 0x28(%rsp), %rcx
movq 0x20(%rcx,%rbx,8), %rdi
cmpq %rdi, 0x28(%rcx,%rbx,8)
je 0x741c7
movl $0x1, %ebp
xorl %esi, %esi
movq %rsp, %r12
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
leaq (%rsi,%rsi,4), %r8
movslq 0x4(%rdi,%r8,8), %rax
movq 0x30(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %r9 # imm = 0x8E38E38E38E38E39
imulq %r9, %rdx
cmpq %rax, %rdx
jbe 0x745f6
leaq (%rdi,%r8,8), %rdx
movsd 0x10(%rdx), %xmm1
leaq (%rax,%rax,8), %rax
movsd 0x38(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x74185
movsd %xmm0, 0x10(%rdx)
shlq $0x20, %rsi
orq %r15, %rsi
movq %r12, %rdi
callq 0x7611c
movl %ebp, %esi
movq 0x28(%rsp), %rcx
movq 0x20(%rcx,%rbx,8), %rdi
movq 0x28(%rcx,%rbx,8), %rax
subq %rdi, %rax
sarq $0x3, %rax
imulq %r14, %rax
incl %ebp
cmpq %rsi, %rax
ja 0x7413b
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rax
jmp 0x741cb
xorl %esi, %esi
xorl %edi, %edi
leaq 0x80(%rsp), %rdx
movq %rax, (%rdx)
callq 0x68ddd
movabsq $-0x71c71c71c71c71c7, %r15 # imm = 0x8E38E38E38E38E39
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movabsq $0x7fffffff8, %rcx      # imm = 0x7FFFFFFF8
movapd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movapd %xmm0, %xmm1
testq %rcx, %rax
je 0x744e2
movapd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rsp, %rdi
callq 0x761b8
movq %rax, %r13
movl %r13d, %r14d
movslq %r13d, %rax
movq 0x28(%rsp), %rcx
leaq (%rax,%rax,8), %rbp
leaq (%rcx,%rbp,8), %rdx
addq $0x20, %rdx
sarq $0x20, %r13
movq (%rdx), %r8
leaq (,%r13,4), %r10
addq %r13, %r10
leaq (%r8,%r10,8), %rsi
movslq 0x4(%rsi), %rbx
movq 0x88(%rsp), %rdi
movl (%rdi,%rbx,4), %r9d
cmpl $0x1, %r9d
je 0x74467
cmpl $-0x1, %r9d
jne 0x744ba
movq 0x8(%rdx), %rdx
subq %r8, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %r8 # imm = 0xCCCCCCCCCCCCCCCD
imulq %r8, %rdx
cmpq %r13, %rdx
jbe 0x7460a
movq 0x30(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
cmpq %rbx, %rdx
jbe 0x74607
movsd 0x10(%rsi), %xmm2
minsd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
movsd 0xb8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x74576
movapd %xmm2, 0x50(%rsp)
leaq (%rbx,%rbx,8), %r12
movsd 0x38(%rcx,%r12,8), %xmm0
movsd %xmm0, 0xc0(%rsp)
movl $0x1, (%rdi,%rbx,4)
movq 0x68(%rsp), %rcx
leaq (%rax,%rax,2), %rax
leaq (%rbx,%rbx,2), %r15
movl 0x8(%rcx,%rax,4), %edx
movl %edx, 0x8(%rcx,%r15,4)
movq (%rcx,%rax,4), %rax
movq %rax, (%rcx,%r15,4)
movq 0x28(%rsp), %rcx
movq 0x20(%rcx,%rbp,8), %rax
movq 0x28(%rcx,%rbp,8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %r13, %rdx
jbe 0x7461b
leaq (,%r15,4), %rdi
addq 0x68(%rsp), %rdi
leaq (%rax,%r10,8), %rsi
addq $0x18, %rsi
callq 0x50708
movq 0x68(%rsp), %rcx
movq %rax, (%rcx,%r15,4)
movl %edx, 0x8(%rcx,%r15,4)
movq 0x28(%rsp), %rcx
movq 0x20(%rcx,%r12,8), %rdi
cmpq %rdi, 0x28(%rcx,%r12,8)
movabsq $-0x71c71c71c71c71c7, %r15 # imm = 0x8E38E38E38E38E39
je 0x74427
movl %ebx, %ebx
movl $0x1, %ebp
xorl %esi, %esi
leaq (%rsi,%rsi,4), %r8
movslq 0x4(%rdi,%r8,8), %rax
movq 0x30(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %r15, %rdx
cmpq %rax, %rdx
jbe 0x745e5
leaq (%rdi,%r8,8), %rdx
movsd 0x10(%rdx), %xmm1
leaq (%rax,%rax,8), %rdi
movsd 0x38(%rcx,%rdi,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x743e2
movsd %xmm0, 0x10(%rdx)
cmpl %r14d, %eax
je 0x743fb
shlq $0x20, %rsi
orq %rbx, %rsi
movq %rsp, %rdi
callq 0x7611c
movq 0x28(%rsp), %rcx
movl %ebp, %esi
movq 0x20(%rcx,%r12,8), %rdi
movq 0x28(%rcx,%r12,8), %rax
subq %rdi, %rax
sarq $0x3, %rax
movabsq $-0x3333333333333333, %rdx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rdx, %rax
incl %ebp
cmpq %rsi, %rax
ja 0x743a2
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq 0x18(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x80(%rsp), %rdx
callq 0x68ddd
movsd 0xc0(%rsp), %xmm0
maxsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movapd 0x50(%rsp), %xmm1
jmp 0x744c0
movq 0x8(%rdx), %rdx
subq %r8, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %r13, %rdx
jbe 0x7460a
movq %r10, %r12
movq 0x68(%rsp), %r14
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,4), %rdi
addq $0x18, %rsi
callq 0x50708
leaq (%rbx,%rbx,2), %rcx
leaq (%r14,%rcx,4), %rdi
movq %rax, %rsi
callq 0x506ba
testb %al, %al
je 0x74596
movsd 0x50(%rsp), %xmm1
movq 0x8(%rsp), %rax
subq (%rsp), %rax
movsd %xmm1, 0x50(%rsp)
movabsq $0x7fffffff8, %rcx      # imm = 0x7FFFFFFF8
testq %rcx, %rax
jne 0x74229
movapd 0xa0(%rsp), %xmm0
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
addpd %xmm0, %xmm0
movsd 0x48(%rsp), %xmm1
addsd %xmm1, %xmm1
movq 0x40(%rsp), %rax
movupd %xmm0, (%rax)
movsd %xmm1, 0x10(%rax)
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x74521
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x7453e
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x74555
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0x28(%rsp), %rdi
callq 0xf10e
movq 0x40(%rsp), %rax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x100(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x40(%rsp), %rcx
movq %rax, 0x10(%rcx)
movups (%rdx), %xmm0
movups %xmm0, (%rcx)
jmp 0x7450b
movq 0xb0(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rcx,%rbp,8), %rax
movq 0x28(%rcx,%rbp,8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
imulq %rcx, %rdx
cmpq %r13, %rdx
jbe 0x7460a
movsd 0x10(%rax,%r12,8), %xmm1
movsd 0x50(%rsp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x744e2
movapd %xmm0, %xmm1
jmp 0x744e2
leaq 0xd9109(%rip), %rdi     # 0x14d6f5
movq %rax, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xd90f8(%rip), %rdi     # 0x14d6f5
movq %rax, %rsi
xorl %eax, %eax
callq 0x4630
movq %rbx, %r13
leaq 0xd90e4(%rip), %rdi     # 0x14d6f5
movq %r13, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xd90d3(%rip), %rdi     # 0x14d6f5
movq %r13, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xd90c2(%rip), %rdi     # 0x14d6f5
movq %r15, %rsi
xorl %eax, %eax
callq 0x4630
jmp 0x74662
jmp 0x7464d
movq %rax, %rbx
jmp 0x74698
jmp 0x74648
movq %rax, %rbx
jmp 0x746af
movq %rax, %rbx
jmp 0x7467b
jmp 0x74662
jmp 0x74662
jmp 0x74662
jmp 0x74662
jmp 0x74662
jmp 0x74662
jmp 0x74662
jmp 0x74662
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x7467b
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x88(%rsp), %rdi
testq %rdi, %rdi
je 0x74698
movq 0x98(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x746af
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0x28(%rsp), %rdi
callq 0xf10e
movq %rbx, %rdi
callq 0x4780