// File: /workspace/github_cpp_makefile_star2_O3/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 0x70(%rsp), %rdi
movq %rax, 0x10(%rdi)
movapd %xmm0, (%rdi)
movq %rax, 0x60(%rsp)
movapd %xmm0, 0x50(%rsp)
movq %rax, 0x40(%rsp)
movapd %xmm0, 0x30(%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 0x48a3e
movq 0x68(%rbx), %rsi
subq 0x60(%rbx), %rsi
sarq $0x3, %rsi
imulq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
callq 0x48a3e
movslq 0x138(%rbx), %rsi
leaq 0xc(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x30(%rsp), %rdi
callq 0x48a3e
movl 0x138(%rbx), %eax
testl %eax, %eax
jle 0x7af51
leaq 0x10(%rsp), %rax
movq (%rax), %rdi
movq 0x8(%rax), %r8
xorpd %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %r13d, %r13d
xorl %r14d, %r14d
xorl %r12d, %r12d
cmpq %rdi, %r8
je 0x7ad90
movq %rdi, 0x18(%rsp)
movl $0x0, 0x8(%rsp)
movq 0x60(%rbx), %rcx
movq 0x68(%rbx), %rsi
movq %rsi, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
testl %edx, %edx
jle 0x7ae21
xorl %edi, %edi
movslq %edi, %rax
movq 0x140(%rbx), %rdx
movl (%rdx,%rax,4), %eax
cmpq %rax, %r13
jne 0x7adf9
movq 0x18(%rsp), %rax
cmpq 0x20(%rsp), %rax
je 0x7addf
movl %edi, (%rax)
addq $0x4, %rax
movq %rax, 0x18(%rsp)
jmp 0x7adf9
leaq 0x10(%rsp), %rdi
movq %rax, %rsi
leaq 0x8(%rsp), %rdx
callq 0xf864
movq 0x60(%rbx), %rcx
movq 0x68(%rbx), %rsi
movl 0x8(%rsp), %edi
incl %edi
movl %edi, 0x8(%rsp)
movq %rsi, %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
imulq %rbp, %rdx
cmpl %edx, %edi
jl 0x7adb4
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %r8
jmp 0x7ae24
movq %rdi, %r8
movq %r8, %rax
subq %rdi, %rax
cmpq $0x4, %rax
jne 0x7af3c
movslq (%rdi), %rax
movq 0x50(%rsp), %r9
cmpl $0x0, (%r9,%rax,4)
jne 0x7af3c
movl %r14d, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movl $0x1, (%rax,%r13,4)
movslq (%rdi), %rax
cmpq %rax, %rdx
jbe 0x7b1b5
movq %r9, 0x88(%rsp)
movq %rax, 0x90(%rsp)
leaq (%rax,%rax,8), %rax
movq 0x20(%rcx,%rax,8), %r9
movq 0x28(%rcx,%rax,8), %r11
movq %r11, %rdx
subq %r9, %rdx
sarq $0x3, %rdx
movabsq $-0x3333333333333333, %rax # imm = 0xCCCCCCCCCCCCCCCD
imulq %rax, %rdx
movl $0x0, %r14d
testl %edx, %edx
cmovgl %edx, %r14d
jle 0x7aefb
movq %r9, %r10
xorl %eax, %eax
testq %rax, %rax
je 0x7aed3
cmpq %rax, %rdx
jbe 0x7b1b5
movsd 0x8(%r10), %xmm0
movsd 0x48(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %r10, %r15
ja 0x7aee5
jmp 0x7aeef
cmpq %r9, %r11
je 0x7b1b3
movsd 0x8(%r9), %xmm0
movq %r9, %r15
movl 0x4(%r15), %r12d
movsd %xmm0, 0x48(%rsp)
incq %rax
addq $0x28, %r10
cmpq %rax, %r14
jne 0x7aeae
movslq %r12d, %r12
movq 0x70(%rsp), %rax
movl (%rax,%r12,4), %edx
testl %edx, %edx
cmovnsl %edx, %r12d
movq 0x90(%rsp), %rdx
movl %r12d, (%rax,%rdx,4)
movslq %r12d, %rax
movl $0x1, %edx
movq 0x88(%rsp), %r9
movl %edx, (%r9,%rax,4)
movslq (%rdi), %rax
movl %edx, (%r9,%rax,4)
movl 0x2c(%rsp), %r14d
incl %r14d
incq %r13
movslq 0x138(%rbx), %rax
cmpq %rax, %r13
jl 0x7ad86
jmp 0x7af5c
movq 0x60(%rbx), %rcx
movq 0x68(%rbx), %rsi
xorl %r14d, %r14d
subq %rcx, %rsi
subl %r14d, %eax
movl %eax, 0x1b8(%rbx)
leaq 0x1d8(%rbx), %r14
sarq $0x3, %rsi
imulq %rbp, %rsi
leaq 0xc(%rsp), %rdx
movl $0xffffffff, (%rdx)     # imm = 0xFFFFFFFF
movq %r14, %rdi
callq 0x48a3e
movl $0x0, 0x4(%rsp)
movl 0x138(%rbx), %ecx
testl %ecx, %ecx
jle 0x7b099
leaq 0x1c0(%rbx), %r15
leaq 0x250(%rbx), %r12
xorl %eax, %eax
xorl %r13d, %r13d
movq 0x30(%rsp), %rdx
cmpl $0x0, (%rdx,%r13,4)
jne 0x7b08a
movq 0x1c8(%rbx), %rsi
cmpq 0x1d0(%rbx), %rsi
je 0x7afe2
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x1c8(%rbx)
jmp 0x7afef
movq %r15, %rdi
leaq 0x4(%rsp), %rdx
callq 0xf864
leaq (,%r13,8), %rdx
addq 0x238(%rbx), %rdx
movq 0x258(%rbx), %rsi
cmpq 0x260(%rbx), %rsi
je 0x7b023
movsd (%rdx), %xmm0
movsd %xmm0, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x258(%rbx)
jmp 0x7b02b
movq %r12, %rdi
callq 0x1481e
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 0x7b07a
movq 0x140(%rbx), %rcx
movq 0x1d8(%rbx), %rdx
xorl %esi, %esi
movslq %esi, %rdi
movl (%rcx,%rdi,4), %r8d
cmpq %r8, %r13
jne 0x7b070
movl 0x4(%rsp), %esi
movl %esi, (%rdx,%rdi,4)
movl 0x8(%rsp), %esi
incl %esi
movl %esi, 0x8(%rsp)
cmpl %eax, %esi
jl 0x7b059
movl 0x4(%rsp), %eax
incl %eax
movl %eax, 0x4(%rsp)
movl 0x138(%rbx), %ecx
incq %r13
movslq %ecx, %rdx
cmpq %rdx, %r13
jl 0x7afb3
movl $0x0, 0x4(%rsp)
movq 0x68(%rbx), %rax
subq 0x60(%rbx), %rax
sarq $0x3, %rax
imulq %rbp, %rax
testl %eax, %eax
jle 0x7b104
movq 0x70(%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 0x7b0dc
movl (%rdx,%r8,4), %r8d
movl %r8d, (%rdx,%rdi,4)
incl 0x4(%rsp)
incq %rdi
cmpq %rdi, %rsi
jne 0x7b0c7
testl %eax, %eax
jle 0x7b104
movq (%r14), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
xorl %edx, %edx
cmpl $-0x1, (%rcx,%rdx,4)
je 0x7b1c6
incq %rdx
cmpq %rdx, %rax
jne 0x7b0f2
movq 0x119eb5(%rip), %rdi    # 0x194fc0
leaq 0xd9d8e(%rip), %rsi     # 0x154ea0
movl $0x2b, %edx
callq 0x44e0
movl 0x1b8(%rbx), %esi
movq 0x119e97(%rip), %rdi    # 0x194fc0
callq 0x4750
leaq 0xd84e9(%rip), %rsi     # 0x15361e
movl $0x1, %edx
movq %rax, %rdi
callq 0x44e0
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7b159
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x7b170
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x7b18a
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7b1a1
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
xorl %eax, %eax
leaq 0xd2539(%rip), %rdi     # 0x14d6f5
movq %rax, %rsi
xorl %eax, %eax
callq 0x4630
movq 0x119df3(%rip), %rdi    # 0x194fc0
leaq 0xd9ca8(%rip), %rsi     # 0x154e7c
movl $0x23, %edx
callq 0x44e0
movq 0x119ddb(%rip), %rdi    # 0x194fc0
leaq 0xd8432(%rip), %rsi     # 0x15361e
callq 0x4440
callq 0x4260
jmp 0x7b202
jmp 0x7b202
jmp 0x7b202
jmp 0x7b202
jmp 0x7b202
jmp 0x7b202
movq %rax, %rbx
movq 0x30(%rsp), %rdi
testq %rdi, %rdi
je 0x7b21c
movq 0x40(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x7b233
movq 0x60(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x70(%rsp), %rdi
testq %rdi, %rdi
je 0x7b24d
movq 0x80(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x7b264
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780