// File: /workspace/github_cpp_makefile_star2_O1/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 $0x98, %rsp
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, 0x20(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x10(%rsp)
leaq 0x80(%rsp), %rdi
movq %rax, 0x10(%rdi)
movapd %xmm0, (%rdi)
movq %rax, 0x70(%rsp)
movapd %xmm0, 0x60(%rsp)
movq %rax, 0x50(%rsp)
movapd %xmm0, 0x40(%rsp)
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
movabsq $-0x71c71c71c71c71c7, %rbp # imm = 0x8E38E38E38E38E39
imulq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
callq 0x49326
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
imulq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x60(%rsp), %rdi
callq 0x49326
movslq 0x138(%rbx), %rsi
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x40(%rsp), %rdi
callq 0x49326
movl 0x138(%rbx), %eax
testl %eax, %eax
jle 0x7c046
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0x8(%rsp), %r15
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r12d, %r12d
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
je 0x7bea9
movq %rax, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x60(%rbx), %rdx
movq 0x68(%rbx), %rax
subq %rdx, %rax
sarq $0x3, %rax
imulq %rbp, %rax
testl %eax, %eax
jle 0x7bf23
xorl %ecx, %ecx
movslq %ecx, %rax
movq 0x140(%rbx), %rdx
movl (%rdx,%rax,4), %eax
cmpq %rax, %r13
jne 0x7bf02
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x7bef5
movl %ecx, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x7bf02
leaq 0x10(%rsp), %rdi
movq %r15, %rdx
callq 0xf7b4
movl 0x8(%rsp), %ecx
incl %ecx
movl %ecx, 0x8(%rsp)
movq 0x60(%rbx), %rdx
movq 0x68(%rbx), %rax
subq %rdx, %rax
sarq $0x3, %rax
imulq %rbp, %rax
cmpl %eax, %ecx
jl 0x7beca
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rsi
subq %rcx, %rsi
cmpq $0x4, %rsi
jne 0x7c031
movslq (%rcx), %rsi
movq 0x60(%rsp), %rdi
cmpl $0x0, (%rdi,%rsi,4)
jne 0x7c031
movl %r14d, 0x3c(%rsp)
movq 0x40(%rsp), %rsi
movl $0x1, (%rsi,%r13,4)
movslq (%rcx), %r14
cmpq %r14, %rax
jbe 0x7c2a3
leaq (%r14,%r14,8), %rsi
movq 0x20(%rdx,%rsi,8), %r8
movq 0x28(%rdx,%rsi,8), %r9
movq %r9, %rdx
subq %r8, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rsi # imm = 0xCCCCCCCCCCCCCCCD
imulq %rsi, %rdx
leaq 0x8(%r8), %r10
testl %edx, %edx
movl $0x0, %r11d
cmovgl %edx, %r11d
xorl %esi, %esi
cmpq %rsi, %r11
je 0x7bfff
testq %rsi, %rsi
jne 0x7bfc5
cmpq %r8, %r9
je 0x7c2ad
movsd 0x8(%r8), %xmm0
movsd %xmm0, 0x30(%rsp)
movl 0x4(%r8), %r12d
testq %rsi, %rsi
je 0x7bfee
cmpq %rsi, %rdx
jbe 0x7c2a8
movsd (%r10), %xmm0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x7bfee
movl -0x4(%r10), %r12d
movsd %xmm0, 0x30(%rsp)
incq %rsi
addq $0x28, %r10
cmpq %r14, %rax
ja 0x7bfa2
jmp 0x7c2a3
movslq %r12d, %r12
movq 0x80(%rsp), %rax
movl (%rax,%r12,4), %edx
testl %edx, %edx
cmovnsl %edx, %r12d
movl %r12d, (%rax,%r14,4)
movslq %r12d, %rax
movl $0x1, %edx
movl %edx, (%rdi,%rax,4)
movslq (%rcx), %rax
movl %edx, (%rdi,%rax,4)
movl 0x3c(%rsp), %r14d
incl %r14d
incq %r13
movslq 0x138(%rbx), %rax
cmpq %rax, %r13
jl 0x7be98
jmp 0x7c049
xorl %r14d, %r14d
subl %r14d, %eax
movl %eax, 0x1b8(%rbx)
leaq 0x1d8(%rbx), %r14
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
imulq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x49326
movl $0x0, 0x4(%rsp)
cmpl $0x0, 0x138(%rbx)
jle 0x7c183
leaq 0x1c0(%rbx), %r15
leaq 0x250(%rbx), %r12
xorl %r13d, %r13d
movq 0x40(%rsp), %rax
cmpl $0x0, (%rax,%r13,4)
jne 0x7c170
movq 0x1c8(%rbx), %rsi
cmpq 0x1d0(%rbx), %rsi
je 0x7c0d5
movl 0x4(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x1c8(%rbx)
jmp 0x7c0e2
movq %r15, %rdi
leaq 0x4(%rsp), %rdx
callq 0xf7b4
leaq (,%r13,8), %rdx
addq 0x238(%rbx), %rdx
movq 0x258(%rbx), %rsi
cmpq 0x260(%rbx), %rsi
je 0x7c116
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x258(%rbx)
jmp 0x7c11e
movq %r12, %rdi
callq 0x148c4
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
shrq $0x3, %rax
imull $0x38e38e39, %eax, %eax # imm = 0x38E38E39
movl $0x0, 0x8(%rsp)
testl %eax, %eax
jle 0x7c16c
movq 0x140(%rbx), %rcx
movq 0x1d8(%rbx), %rdx
xorl %esi, %esi
movslq %esi, %rsi
movl (%rcx,%rsi,4), %edi
cmpq %rdi, %r13
jne 0x7c15e
movl 0x4(%rsp), %edi
movl %edi, (%rdx,%rsi,4)
movl 0x8(%rsp), %esi
incl %esi
movl %esi, 0x8(%rsp)
cmpl %eax, %esi
jl 0x7c14c
incl 0x4(%rsp)
incq %r13
movslq 0x138(%rbx), %rax
cmpq %rax, %r13
jl 0x7c0a2
movl $0x0, 0x4(%rsp)
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
sarq $0x3, %rax
imulq %rbp, %rax
testl %eax, %eax
jle 0x7c1d1
movq 0x80(%rsp), %rcx
movq (%r14), %rdx
movl %eax, %esi
andl $0x7fffffff, %esi       # imm = 0x7FFFFFFF
xorl %edi, %edi
movslq (%rcx,%rdi,4), %r8
testq %r8, %r8
js 0x7c1c9
movl (%rdx,%r8,4), %r8d
movl %r8d, (%rdx,%rdi,4)
incl 0x4(%rsp)
incq %rdi
cmpq %rdi, %rsi
jne 0x7c1b4
testl %eax, %eax
jle 0x7c1f1
movq (%r14), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpl $-0x1, (%rcx,%rdx,4)
je 0x7c2c1
incq %rdx
cmpq %rdx, %rax
jne 0x7c1df
movq 0x11adc8(%rip), %rdi    # 0x196fc0
leaq 0xdab89(%rip), %rsi     # 0x156d88
movl $0x2b, %edx
callq 0x44e0
movl 0x1b8(%rbx), %esi
movq 0x11adaa(%rip), %rdi    # 0x196fc0
callq 0x4750
leaq 0xd930c(%rip), %rsi     # 0x15552e
movl $0x1, %edx
movq %rax, %rdi
callq 0x44e0
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7c246
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7c25d
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7c27a
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7c291
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rdx
jmp 0x7c2b0
movq %rsi, %r14
jmp 0x7c2b0
xorl %r14d, %r14d
leaq 0xd33ee(%rip), %rdi     # 0x14f6a5
movq %r14, %rsi
xorl %eax, %eax
callq 0x4630
movq 0x11acf8(%rip), %rdi    # 0x196fc0
leaq 0xdaa95(%rip), %rsi     # 0x156d64
movl $0x23, %edx
callq 0x44e0
movq 0x11ace0(%rip), %rdi    # 0x196fc0
leaq 0xd9247(%rip), %rsi     # 0x15552e
callq 0x4440
callq 0x4260
jmp 0x7c2fd
jmp 0x7c2fd
jmp 0x7c2fd
jmp 0x7c2fd
jmp 0x7c2fd
jmp 0x7c2fd
movq %rax, %rbx
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x7c317
movq 0x50(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x60(%rsp), %rdi
testq %rdi, %rdi
je 0x7c32e
movq 0x70(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x7c34b
movq 0x90(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7c362
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780