// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/feature.cc
// Function: FEATURE::merge_singlenode_segments()
void FEATURE::merge_singlenode_segments()
{
	int i,j,k;
	int mink=0;
	int count;

	int nsingle=0;
	double min=0;
	vector <int> nodelist;  // list of nodes in current segment
	vector <int> tobemerged; // flag for nodes to be merged with other segments
	vector <int> tobeextended; // flag for nodes that will be extended by toher nodes
	vector <int> listofsinglenodesegments;
	vector <int> tobemerged_segments;

	tobemerged.resize(nodes.size(),-1);
	tobeextended.resize(nodes.size(),0);
	tobemerged_segments.resize(nsegments,0);

	for(i=0;i<nsegments;i++)
	{
		nodelist.clear();
		for(j=0;j<(int)nodes.size();j++)
		{
			if(segments[j]==i) nodelist.push_back(j);
		};

		if(nodelist.size()==1)
		{


			if(tobeextended[nodelist[0]]==0)  // if the current node is not planned to be extended with other node, merge with others
			{
				tobemerged_segments[i]=1; // mark current segment as one to e merged

				for(k=0;k<(int)nodes.at(nodelist[0]).connections.size();k++) // iterate over all connections to find closes node
				{
					if(k==0) {min=nodes.at(nodelist[0]).connections.at(k).length;mink=nodes.at(nodelist[0]).connections.at(k).to;};
					if(k>0&&nodes.at(nodelist[0]).connections.at(k).length<min)
					{
						mink=nodes.at(nodelist[0]).connections.at(k).to;
						min=nodes.at(nodelist[0]).connections.at(k).length;
					};
				};
	// shortest connection leads to mink

				if(tobemerged[mink]>-1) mink=tobemerged[mink]; // if node to which we are going to merge the current node
													// also will be merged, merge into the other node

				tobemerged[nodelist[0]]=mink; // curr node to be connected to mink
				tobeextended[mink]=1;
				tobeextended[nodelist[0]]=1; // "write protect" current node

				nsingle++;
			};
		};

	};

/* now merging */

	nfeatures=nsegments-nsingle;

	feature_node_mapping.resize(nodes.size(),-1);

	count=0;
	for(i=0;i<nsegments;i++)
	{
		if(tobemerged_segments[i]==0)
		{
			features.push_back(count);
			feature_max_r.push_back(segment_max_r[i]);

			for(j=0;j<(int)nodes.size();j++)
			{
				if(segments[j]==i) feature_node_mapping[j]=count;
			};

			count++;
		};
	};
	count=0;
	for(i=0;i<(int)nodes.size();i++)
	{
		if(tobemerged[i]>-1)
		{
			feature_node_mapping[i]=feature_node_mapping[tobemerged[i]];
			count++;
		};
	};


	for(i=0;i<(int)nodes.size();i++)
	{
		if(feature_node_mapping[i]==-1) 
		{
			cout << "Feature construction failed. Abort."<< "\n";abort();
		}
	}

	cout << "After merging single segments, nfeatures = " << nfeatures << "\n";


}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rdi, %rbx
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
leaq 0x80(%rsp), %rdi
movapd %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movapd %xmm0, 0x60(%rsp)
andq $0x0, 0x70(%rsp)
movapd %xmm0, 0xa0(%rsp)
andq $0x0, 0xb0(%rsp)
movapd %xmm0, 0x40(%rsp)
andq $0x0, 0x50(%rsp)
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
pushq $0x48
popq %rbp
cqto
idivq %rbp
leaq 0xc(%rsp), %rdx
orl $-0x1, (%rdx)
movq %rax, %rsi
callq 0x36f18
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
idivq %rbp
leaq 0xc(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x36f18
movslq 0x138(%rbx), %rsi
leaq 0xc(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x40(%rsp), %rdi
callq 0x36f18
leaq 0x60(%rbx), %r14
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorl %r15d, %r15d
leaq 0x20(%rsp), %r13
xorl %r12d, %r12d
movl $0x0, 0x1c(%rsp)
movslq 0x138(%rbx), %rax
cmpq %rax, %r15
jge 0x5c2f0
movq 0x20(%rsp), %rax
cmpq %rax, 0x28(%rsp)
je 0x5c136
movq %rax, 0x28(%rsp)
xorl %ecx, %ecx
movl %ecx, 0x8(%rsp)
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
idivq %rbp
cmpl %eax, %ecx
jge 0x5c174
movslq %ecx, %rax
movq 0x140(%rbx), %rdx
movl (%rdx,%rax,4), %eax
cmpq %rax, %r15
jne 0x5c170
movq %r13, %rdi
leaq 0x8(%rsp), %rsi
callq 0x9434
movl 0x8(%rsp), %ecx
incl %ecx
jmp 0x5c138
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
cmpq $0x4, %rcx
jne 0x5c2e8
movslq (%rax), %rcx
movq 0x60(%rsp), %rdx
cmpl $0x0, (%rdx,%rcx,4)
jne 0x5c2e8
movq 0x40(%rsp), %rcx
movl $0x1, (%rcx,%r15,4)
xorl %r13d, %r13d
movslq (%rax), %rsi
movq %r14, %rdi
callq 0xabe2
movq 0x28(%rax), %rcx
subq 0x20(%rax), %rcx
movq %rcx, %rax
cqto
pushq $0x28
popq %rcx
idivq %rcx
cltq
cmpq %rax, %r13
jge 0x5c2aa
movq 0x20(%rsp), %rax
movslq (%rax), %rsi
testq %r13, %r13
je 0x5c25a
movq %r14, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movsd 0x10(%rsp), %xmm0
ucomisd 0x8(%rax), %xmm0
jbe 0x5c29d
movq 0x20(%rsp), %rax
movslq (%rax), %rsi
movq %r14, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movl 0x4(%rax), %r12d
movq 0x20(%rsp), %rax
movslq (%rax), %rsi
movq %r14, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
movq %r13, %rsi
callq 0x29efa
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x5c29d
movq %r14, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
xorl %esi, %esi
callq 0x29efa
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x20(%rsp), %rax
movslq (%rax), %rsi
movq %r14, %rdi
callq 0xabe2
addq $0x20, %rax
movq %rax, %rdi
xorl %esi, %esi
callq 0x29efa
movl 0x4(%rax), %r12d
incq %r13
movq 0x20(%rsp), %rax
jmp 0x5c1ad
movslq %r12d, %r12
movq 0x80(%rsp), %rax
movl (%rax,%r12,4), %ecx
testl %ecx, %ecx
cmovnsl %ecx, %r12d
movq 0x20(%rsp), %rcx
movslq (%rcx), %rdx
movl %r12d, (%rax,%rdx,4)
movslq %r12d, %rax
movq 0x60(%rsp), %rdx
pushq $0x1
popq %rsi
movl %esi, (%rdx,%rax,4)
movslq (%rcx), %rax
movl %esi, (%rdx,%rax,4)
incl 0x1c(%rsp)
leaq 0x20(%rsp), %r13
incq %r15
jmp 0x5c115
subl 0x1c(%rsp), %eax
movl %eax, 0x1b8(%rbx)
leaq 0x1d8(%rbx), %rdi
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
cqto
idivq %rbp
leaq 0xc(%rsp), %rdx
orl $-0x1, (%rdx)
movq %rdi, 0x10(%rsp)
movq %rax, %rsi
callq 0x36f18
leaq 0x4(%rsp), %r15
andl $0x0, (%r15)
leaq 0x1c0(%rbx), %r12
leaq 0x250(%rbx), %r13
xorl %r14d, %r14d
movslq 0x138(%rbx), %rax
cmpq %rax, %r14
jge 0x5c3c3
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax,%r14,4)
jne 0x5c3bb
movq %r12, %rdi
movq %r15, %rsi
callq 0x9434
leaq (,%r14,8), %rsi
addq 0x238(%rbx), %rsi
movq %r13, %rdi
callq 0xac88
movq 0x68(%rbx), %rax
movq 0x140(%rbx), %rcx
subq 0x60(%rbx), %rax
cqto
idivq %rbp
movq 0x1d8(%rbx), %rdx
xorl %esi, %esi
movl %esi, 0x8(%rsp)
cmpl %eax, %esi
jge 0x5c3b7
movslq %esi, %rdi
movl (%rcx,%rdi,4), %r8d
cmpq %r8, %r14
jne 0x5c3b3
movl 0x4(%rsp), %esi
movl %esi, (%rdx,%rdi,4)
movl 0x8(%rsp), %esi
incl %esi
jmp 0x5c394
incl 0x4(%rsp)
incq %r14
jmp 0x5c33d
andl $0x0, 0x4(%rsp)
movq 0x68(%rbx), %rax
movq 0x1d8(%rbx), %rcx
subq 0x60(%rbx), %rax
cqto
idivq %rbp
movq 0x80(%rsp), %rdx
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
cmpq %rsi, %rax
je 0x5c408
movslq (%rdx,%rsi,4), %rdi
testq %rdi, %rdi
js 0x5c403
movl (%rcx,%rdi,4), %edi
movl %edi, (%rcx,%rsi,4)
incl 0x4(%rsp)
incq %rsi
jmp 0x5c3eb
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
xorl %edx, %edx
cmpq %rdx, %rax
je 0x5c448
cmpl $-0x1, (%rcx,%rdx,4)
leaq 0x1(%rdx), %rdx
jne 0x5c412
movq 0xf3b98(%rip), %rdi     # 0x14ffc0
leaq 0xa79a5(%rip), %rsi     # 0x103dd4
callq 0x4480
leaq 0xa6143(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
callq 0x4290
movq 0xf3b71(%rip), %rdi     # 0x14ffc0
leaq 0xa79a2(%rip), %rsi     # 0x103df8
callq 0x4480
movl 0x1b8(%rbx), %esi
movq %rax, %rdi
callq 0x4760
leaq 0xa610e(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
leaq 0x40(%rsp), %rdi
callq 0xb290
leaq 0xa0(%rsp), %rdi
callq 0xb290
leaq 0x60(%rsp), %rdi
callq 0xb290
leaq 0x80(%rsp), %rdi
callq 0xb290
leaq 0x20(%rsp), %rdi
callq 0xb290
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5c4d0
jmp 0x5c4d0
jmp 0x5c4d0
jmp 0x5c4d0
jmp 0x5c4d0
jmp 0x5c4d0
jmp 0x5c4d0
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xb290
leaq 0xa0(%rsp), %rdi
callq 0xb290
leaq 0x60(%rsp), %rdi
callq 0xb290
leaq 0x80(%rsp), %rdi
callq 0xb290
leaq 0x20(%rsp), %rdi
callq 0xb290
movq %rbx, %rdi
callq 0x4780
nop