// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/feature.cc
// Function: FEATURE::segment_identify_connections()
void FEATURE::segment_identify_connections(){
int i,j,k;
int nfaces;
vector <int> segfaces;
vector <double> segfaces_max_r;

vector <int> nodes_in_segment; // list IDs of nodes assigned to segment
vector <int> face_nodes_in_segment; // list IDs of nodes assigned to segment and located on segment's "face", that is
                                    // nodes with connections to other segments
vector <int>  connected_segment_for_face_nodes_in_segment; // for each node, if it is a face node, store segment id to which it is connected
vector <SEGCONN> edges_from_segment; // list of edges that connect the current segment to other segments
SEGCONN segc; // inter-segment connection
vector <int> edges_in_face; // for each edge connecting to other semgent assign to a face, this svector stores face id for each edge

/* loop over all segments and identify all connections to other segments */

for(i=0;i<nsegments;i++)
   {
   nodes_in_segment.clear();
   edges_from_segment.clear();
   for(j=0;j<(int)nodes.size();j++)
      {
      if(segments[j]==i) nodes_in_segment.push_back(j);
      };
   face_nodes_in_segment.clear();
   connected_segment_for_face_nodes_in_segment.clear();
   face_nodes_in_segment.resize(nodes_in_segment.size(),0);
   connected_segment_for_face_nodes_in_segment.resize(nodes_in_segment.size(),-1);
   for(j=0;j<(int)nodes_in_segment.size();j++)
      {
      for(k=0;k<(int)nodes.at(nodes_in_segment[j]).connections.size();k++)
         {
         if(segments[nodes.at(nodes_in_segment[j]).connections.at(k).to]!=i) 
             {
             face_nodes_in_segment[j]++; 
             connected_segment_for_face_nodes_in_segment[j]=segments[nodes.at(nodes_in_segment[j]).connections.at(k).to];
        
             segc.from=nodes_in_segment[j]; // this is ID id in channel)
             segc.to=nodes.at(nodes_in_segment[j]).connections.at(k).to; // node id in channel
             segc.from_seg=i; 
             segc.to_seg=segments[nodes.at(nodes_in_segment[j]).connections.at(k).to];
             segc.max_radius=nodes.at(nodes_in_segment[j]).connections.at(k).max_radius;
             segc.deltaPos=nodes.at(nodes_in_segment[j]).connections.at(k).deltaPos;
             edges_from_segment.push_back(segc);
             };
         };
      };

// now connected_segment_for_face_nodes_in_segment has a list of segments each node in the current segment is conneed to (-1 if not) 
// we need now to find "clusters" of these nodes that will correspond to different connections;
// edges_from_segment stores all edges going out from a segment to other segments

   int nedges_tobe_assigned=edges_from_segment.size();
   edges_in_face.clear();
   edges_in_face.resize(nedges_tobe_assigned,-1);

   nfaces=0;
   int neigh_seg; // id of neighboring segment
//   while(nedges_tobe_assigned>-1)
     {
     for(k=0;k<(int)edges_from_segment.size();k++)
        {
        if(edges_in_face[k]==-1) // find first edge not assigned to face
          {
          neigh_seg=edges_from_segment[k].to_seg;
          edges_in_face[k]=nfaces;
          nedges_tobe_assigned--;
          segment_identify_connections_recurr(&nodes_in_segment,&edges_in_face,&nedges_tobe_assigned,nfaces,neigh_seg,&edges_from_segment,k);
          nfaces++;
          };
//        nfaces++;
        };
     };

/*
   cout << "Segment " << i <<": " << nodes_in_segment.size() << " nodes, " << edges_in_face.size() << " edges, " << nfaces << " faces, nodes-to-go " << nedges_tobe_assigned << "\n";
   for(k=0;k<(int)edges_from_segment.size();k++)
      {
      cout << "Edge " << edges_from_segment[k].from << "  " << edges_from_segment[k].to << "  Segments:" << edges_from_segment[k].from_seg << "  " << edges_from_segment[k].to_seg << "    "; edges_from_segment[k].deltaPos.print();
      };
*/


   }; // end of loop over all segments

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
xorps %xmm0, %xmm0
movaps %xmm0, 0x110(%rsp)
andq $0x0, 0x120(%rsp)
movaps %xmm0, 0xf0(%rsp)
andq $0x0, 0x100(%rsp)
movaps %xmm0, 0x10(%rsp)
andq $0x0, 0x20(%rsp)
movaps %xmm0, 0x90(%rsp)
andq $0x0, 0xa0(%rsp)
movaps %xmm0, 0x70(%rsp)
andq $0x0, 0x80(%rsp)
movaps %xmm0, 0x30(%rsp)
andq $0x0, 0x40(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x3d98a
leaq 0x50(%rsp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
leaq 0x60(%rbx), %r15
xorl %ebp, %ebp
leaq 0xe4(%rsp), %r14
pushq $0x28
popq %r12
cmpl 0x138(%rbx), %ebp
jge 0x5cf85
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x5cc06
movq %rax, 0x18(%rsp)
movq 0x30(%rsp), %rax
cmpq %rax, 0x38(%rsp)
je 0x5cc17
movq %rax, 0x38(%rsp)
xorl %ecx, %ecx
movl %ecx, 0x8(%rsp)
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
pushq $0x48
popq %rsi
idivq %rsi
cmpl %eax, %ecx
jge 0x5cc57
movslq %ecx, %rax
movq 0x140(%rbx), %rdx
cmpl %ebp, (%rdx,%rax,4)
jne 0x5cc53
leaq 0x10(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x9434
movl 0x8(%rsp), %ecx
incl %ecx
jmp 0x5cc19
movq 0x90(%rsp), %rax
cmpq %rax, 0x98(%rsp)
je 0x5cc71
movq %rax, 0x98(%rsp)
movq 0x70(%rsp), %rax
cmpq %rax, 0x78(%rsp)
leaq 0xc(%rsp), %rdx
je 0x5cc87
movq %rax, 0x78(%rsp)
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x2, %rsi
andl $0x0, 0xc(%rsp)
leaq 0x90(%rsp), %rdi
movq %rdx, %r13
callq 0x36f18
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x2, %rsi
orl $-0x1, 0xc(%rsp)
leaq 0x70(%rsp), %rdi
movq %r13, %rdx
callq 0x36f18
xorl %eax, %eax
movl %eax, 0x8(%rsp)
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
subq %rcx, %rdx
shrq $0x2, %rdx
cmpl %edx, %eax
jge 0x5cea6
xorl %r13d, %r13d
cltq
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
movq 0x28(%rax), %rcx
subq 0x20(%rax), %rcx
movq %rcx, %rax
cqto
idivq %r12
movslq %eax, %rcx
movslq 0x8(%rsp), %rax
cmpq %rcx, %r13
jge 0x5ce9f
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movslq 0x4(%rax), %rax
movq 0x140(%rbx), %rcx
cmpl %ebp, (%rcx,%rax,4)
je 0x5ce8e
movslq 0x8(%rsp), %rax
movq 0x90(%rsp), %rcx
incl (%rcx,%rax,4)
movslq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movslq 0x4(%rax), %rax
movq 0x140(%rbx), %rcx
movl (%rcx,%rax,4), %eax
movslq 0x8(%rsp), %rcx
movq 0x70(%rsp), %rdx
movl %eax, (%rdx,%rcx,4)
movslq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movl %esi, 0xc0(%rsp)
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movl 0x4(%rax), %eax
movl %eax, 0xc4(%rsp)
movl %ebp, 0xc8(%rsp)
movslq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movslq 0x4(%rax), %rax
movq 0x140(%rbx), %rcx
movl (%rcx,%rax,4), %eax
movl %eax, 0xcc(%rsp)
movslq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movsd 0x10(%rax), %xmm0
movsd %xmm0, 0xd0(%rsp)
movslq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movslq (%rcx,%rax,4), %rsi
movq %r15, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movl 0x20(%rax), %ecx
movl %ecx, 0x8(%r14)
movq 0x18(%rax), %rax
movq %rax, (%r14)
leaq 0x30(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x5f28a
incq %r13
movl 0x8(%rsp), %eax
movq 0x10(%rsp), %rcx
jmp 0x5ccec
incl %eax
jmp 0x5cccc
movq 0x38(%rsp), %rax
subq 0x30(%rsp), %rax
cqto
pushq $0x30
popq %rcx
idivq %rcx
movq 0x50(%rsp), %rcx
cmpq %rcx, 0x58(%rsp)
je 0x5cec9
movq %rcx, 0x58(%rsp)
movq %rax, 0xb0(%rsp)
movslq %eax, %rsi
orl $-0x1, 0x4c(%rsp)
leaq 0x50(%rsp), %rdi
leaq 0x4c(%rsp), %rdx
callq 0x36f18
xorl %r13d, %r13d
pushq $0x3
popq %rdi
xorl %r14d, %r14d
movq 0x30(%rsp), %rcx
movq 0x38(%rsp), %rax
subq %rcx, %rax
cqto
pushq $0x30
popq %rsi
idivq %rsi
cltq
cmpq %rax, %r13
jge 0x5cf76
movq 0x50(%rsp), %rax
cmpl $-0x1, (%rax,%r13,4)
jne 0x5cf6a
movq %rdi, 0xb8(%rsp)
movl (%rcx,%rdi,4), %r9d
movl %r14d, (%rax,%r13,4)
movq %rbx, %rdi
leaq 0x10(%rsp), %rsi
leaq 0x50(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movl %r14d, %r8d
pushq %r13
leaq 0x38(%rsp), %rax
pushq %rax
callq 0x5d050
popq %rax
popq %rcx
movq 0xb0(%rsp), %rax
decl %eax
movq %rax, 0xb0(%rsp)
incl %r14d
movq 0xb8(%rsp), %rdi
incq %r13
addq $0xc, %rdi
jmp 0x5cef1
incl %ebp
leaq 0xe4(%rsp), %r14
jmp 0x5cbe9
leaq 0x50(%rsp), %rdi
callq 0xb290
leaq 0x30(%rsp), %rdi
callq 0x5f324
leaq 0x70(%rsp), %rdi
callq 0xb290
leaq 0x90(%rsp), %rdi
callq 0xb290
leaq 0x10(%rsp), %rdi
callq 0xb290
leaq 0xf0(%rsp), %rdi
callq 0xb25c
leaq 0x110(%rsp), %rdi
callq 0xb290
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5d002
jmp 0x5cff5
jmp 0x5cff5
jmp 0x5cff5
jmp 0x5cff5
jmp 0x5cff5
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xb290
leaq 0x30(%rsp), %rdi
callq 0x5f324
leaq 0x70(%rsp), %rdi
callq 0xb290
leaq 0x90(%rsp), %rdi
callq 0xb290
leaq 0x10(%rsp), %rdi
callq 0xb290
leaq 0xf0(%rsp), %rdi
callq 0xb25c
leaq 0x110(%rsp), %rdi
callq 0xb290
movq %rbx, %rdi
callq 0x4780
nop