// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movl %edx, %ebp
movq %rdi, 0x30(%rsp)
movl $0xffffffff, 0x64(%rsp) # imm = 0xFFFFFFFF
addq $0x60, %rsi
leaq 0x18(%rsp), %r12
movq %r12, %rdi
movq %rsi, 0xa8(%rsp)
callq 0x38ec6
movq (%r12), %rbx
movq 0x8(%r12), %r15
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x509d8
subq %rbx, %r15
sarq $0x3, %r15
movabsq $-0x71c71c71c71c71c7, %rbx # imm = 0x8E38E38E38E38E39
imulq %rbx, %r15
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %r15, %rsi
callq 0x76d3a
movq 0x20(%rsp), %rsi
subq 0x18(%rsp), %rsi
sarq $0x3, %rsi
imulq %rbx, %rsi
leaq 0x90(%rsp), %rdi
leaq 0x64(%rsp), %rdx
leaq 0x40(%rsp), %rcx
callq 0x38af6
movslq %ebp, %r15
movq 0x18(%rsp), %rax
movq 0x20(%rsp), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rbx, %rdx
cmpq %r15, %rdx
jbe 0x7534b
leaq 0xf0(%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 0xda2aa(%rip), %xmm1    # 0x14f038
movsd %xmm2, 0xb0(%rsp)
mulsd %xmm2, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x74dbb
movq 0x10(%rsi), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
movupd (%rsi), %xmm0
movupd %xmm0, (%rcx)
jmp 0x752c0
movl %ebp, %r15d
cmpq %r15, %rdx
jbe 0x7534b
leaq (%r15,%r15,8), %rbx
movsd 0x38(%rax,%rbx,8), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x90(%rsp), %rax
movl $0x1, (%rax,%r15,4)
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x509d8
movq 0x68(%rsp), %rax
leaq (%r15,%r15,2), %rcx
leaq 0x40(%rsp), %rdi
movl 0x8(%rdi), %edx
movl %edx, 0x8(%rax,%rcx,4)
movq (%rdi), %rdx
movq %rdx, (%rax,%rcx,4)
movq %r12, 0x12294b(%rip)    # 0x197768
leaq 0x5ce(%rip), %rsi       # 0x753f2
callq 0x76dc0
movq 0x18(%rsp), %rcx
movq 0x20(%rcx,%rbx,8), %rdi
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
cmpq %rdi, 0x28(%rcx,%rbx,8)
je 0x74ed0
movl $0x1, %ebp
xorl %esi, %esi
leaq 0x40(%rsp), %r12
leaq (%rsi,%rsi,4), %r8
movslq 0x4(%rdi,%r8,8), %rax
movq 0x20(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %r9 # imm = 0x8E38E38E38E38E39
imulq %r9, %rdx
cmpq %rax, %rdx
jbe 0x75326
leaq (%rdi,%r8,8), %rdx
movsd 0x10(%rdx), %xmm1
leaq (%rax,%rax,8), %rax
movsd 0x38(%rcx,%rax,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x74e9e
movsd %xmm0, 0x10(%rdx)
shlq $0x20, %rsi
orq %r15, %rsi
movq %r12, %rdi
callq 0x76e02
movl %ebp, %esi
movq 0x18(%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 0x74e54
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x58(%rsp), %rax
leaq 0x88(%rsp), %rdx
movq %rax, (%rdx)
callq 0x69775
movb $0x1, %al
movl %eax, 0xc(%rsp)
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd %xmm0, 0x38(%rsp)
movq 0x48(%rsp), %rax
subq 0x40(%rsp), %rax
shrq $0x3, %rax
testl %eax, %eax
je 0x7526d
testb $0x1, 0xc(%rsp)
je 0x7526d
leaq 0x40(%rsp), %rdi
callq 0x76eae
movq %rax, %r13
movl %r13d, %r12d
movslq %r13d, %rax
movq 0x18(%rsp), %rcx
leaq (%rax,%rax,8), %rbx
leaq (%rcx,%rbx,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), %rbp
movq 0x90(%rsp), %rdi
movl (%rdi,%rbp,4), %r9d
cmpl $0x1, %r9d
je 0x75036
cmpl $-0x1, %r9d
jne 0x750ec
movq 0x8(%rdx), %rdx
subq %r8, %rdx
sarq $0x3, %rdx
imulq %r14, %rdx
cmpq %r13, %rdx
jbe 0x7533a
movq 0x20(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %r8 # imm = 0x8E38E38E38E38E39
imulq %r8, %rdx
cmpq %rbp, %rdx
jbe 0x75337
movsd 0x10(%rsi), %xmm2
minsd 0x10(%rsp), %xmm2
leaq (,%rbp,8), %r15
addq %rbp, %r15
movsd 0x38(%rcx,%r15,8), %xmm3
maxsd 0x38(%rsp), %xmm3
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
movsd 0xb0(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x750f4
leaq 0xf0(%rsp), %rdx
movq 0x10(%rdx), %rax
movq 0x30(%rsp), %rcx
movq %rax, 0x10(%rcx)
movupd (%rdx), %xmm0
movupd %xmm0, (%rcx)
movl $0x1, %r12d
movsd %xmm2, 0x10(%rsp)
movsd %xmm3, 0x38(%rsp)
jmp 0x7525e
movq 0x8(%rdx), %rdx
subq %r8, %rdx
sarq $0x3, %rdx
imulq %r14, %rdx
cmpq %r13, %rdx
jbe 0x7533a
movq %r10, %r15
movq 0x68(%rsp), %r14
leaq (%rax,%rax,2), %rax
leaq (%r14,%rax,4), %rdi
addq $0x18, %rsi
callq 0x50a30
xorl %r12d, %r12d
leaq (,%rbp,2), %rcx
addq %rbp, %rcx
leaq (%r14,%rcx,4), %rdi
movq %rax, %rsi
callq 0x509e2
testb %al, %al
jne 0x7524a
movq 0xa8(%rsp), %rax
movq (%rax), %rcx
movq 0x20(%rcx,%rbx,8), %rax
movq 0x28(%rcx,%rbx,8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
imulq %r14, %rdx
cmpq %r13, %rdx
jbe 0x7533a
movsd 0x10(%rax,%r15,8), %xmm0
movl $0x6, %r12d
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x75256
movl $0x0, 0xc(%rsp)
movsd %xmm0, 0x10(%rsp)
jmp 0x7525e
xorl %r12d, %r12d
jmp 0x7525e
movsd %xmm3, 0x38(%rsp)
movsd %xmm2, 0x10(%rsp)
movl $0x1, (%rdi,%rbp,4)
movq 0x68(%rsp), %rcx
leaq (%rax,%rax,2), %rax
movq %r14, %rsi
leaq (%rbp,%rbp,2), %r14
movl 0x8(%rcx,%rax,4), %edx
movl %edx, 0x8(%rcx,%r14,4)
movq (%rcx,%rax,4), %rax
movq %rax, (%rcx,%r14,4)
movq 0x18(%rsp), %rcx
movq 0x20(%rcx,%rbx,8), %rax
movq 0x28(%rcx,%rbx,8), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
imulq %rsi, %rdx
cmpq %r13, %rdx
jbe 0x7535c
leaq (,%r14,4), %rdi
addq 0x68(%rsp), %rdi
leaq (%rax,%r10,8), %rsi
addq $0x18, %rsi
callq 0x50a30
movq 0x68(%rsp), %rcx
movq %rax, (%rcx,%r14,4)
movl %edx, 0x8(%rcx,%r14,4)
movq 0x18(%rsp), %rcx
movq 0x20(%rcx,%r15,8), %rdi
cmpq %rdi, 0x28(%rcx,%r15,8)
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
je 0x75223
movl %ebp, %ebx
movl $0x1, %ebp
xorl %esi, %esi
leaq (%rsi,%rsi,4), %r8
movslq 0x4(%rdi,%r8,8), %rax
movq 0x20(%rsp), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
movabsq $-0x71c71c71c71c71c7, %r9 # imm = 0x8E38E38E38E38E39
imulq %r9, %rdx
cmpq %rax, %rdx
jbe 0x75315
leaq (%rdi,%r8,8), %rdx
movsd 0x10(%rdx), %xmm1
leaq (%rax,%rax,8), %rdi
movsd 0x38(%rcx,%rdi,8), %xmm0
ucomisd %xmm0, %xmm1
jbe 0x751e6
movsd %xmm0, 0x10(%rdx)
cmpl %r12d, %eax
je 0x751fc
shlq $0x20, %rsi
orq %rbx, %rsi
leaq 0x40(%rsp), %rdi
callq 0x76e02
movl %ebp, %esi
movq 0x18(%rsp), %rcx
movq 0x20(%rcx,%r15,8), %rdi
movq 0x28(%rcx,%r15,8), %rax
subq %rdi, %rax
sarq $0x3, %rax
imulq %r14, %rax
incl %ebp
cmpq %rsi, %rax
ja 0x7519c
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
movq 0x58(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x88(%rsp), %rdx
callq 0x69775
xorl %r12d, %r12d
movabsq $-0x3333333333333333, %r14 # imm = 0xCCCCCCCCCCCCCCCD
jmp 0x7525e
movl $0x0, 0xc(%rsp)
testl %r12d, %r12d
je 0x74f0a
cmpl $0x6, %r12d
jne 0x752a9
xorpd %xmm0, %xmm0
movq 0x30(%rsp), %rax
movupd %xmm0, (%rax)
movsd 0x80(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd 0x38(%rsp), %xmm1
addsd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm2
addsd %xmm2, %xmm2
movsd %xmm0, (%rax)
movsd %xmm2, 0x8(%rax)
movsd %xmm1, 0x10(%rax)
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x752c0
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x752dd
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x752f4
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0x18(%rsp), %rdi
callq 0xf07c
movq 0x30(%rsp), %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xda389(%rip), %rdi     # 0x14f6a5
movq %rax, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xda378(%rip), %rdi     # 0x14f6a5
movq %rax, %rsi
xorl %eax, %eax
callq 0x4630
movq %rbp, %r13
leaq 0xda364(%rip), %rdi     # 0x14f6a5
movq %r13, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xda353(%rip), %rdi     # 0x14f6a5
movq %r15, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0xda342(%rip), %rdi     # 0x14f6a5
movq %r13, %rsi
xorl %eax, %eax
callq 0x4630
jmp 0x75392
jmp 0x75381
jmp 0x75392
jmp 0x75392
movq %rax, %rbx
jmp 0x753c9
jmp 0x7537c
movq %rax, %rbx
jmp 0x753e0
movq %rax, %rbx
jmp 0x753ac
jmp 0x75392
jmp 0x75392
jmp 0x75392
jmp 0x75392
jmp 0x75392
jmp 0x75392
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x753ac
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x90(%rsp), %rdi
testq %rdi, %rdi
je 0x753c9
movq 0xa0(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x68(%rsp), %rdi
testq %rdi, %rdi
je 0x753e0
movq 0x78(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
leaq 0x18(%rsp), %rdi
callq 0xf07c
movq %rbx, %rdi
callq 0x4780