// File: /workspace/github_cpp_makefile_star2_O3/CK7[P]abawaca/src/ClusterSeparator.cpp
// Function: ClusterSeparator::separate()
bool ClusterSeparator::separate()
{
	// Start by cleaning everything from previous runs
	if(this->cluster1 != NULL)
		delete(this->cluster1);
	if(this->cluster2 != NULL)
		delete(this->cluster2);
	this->cluster1 = this->cluster2 = NULL;
	this->best_separation->reset();
	this->raw_dps_cluster1.clear();
	this->raw_dps_cluster2.clear();

/*	for(size_t dimension_index=1; dimension_index<=clustering_db.ndimensions(); dimension_index++) {
		this->separate_dimension(dimension_index);
	}*/
	thread*	threads = new thread[clustering_db.ndimensions()];
	for(size_t dimension_index=1; dimension_index<=clustering_db.ndimensions(); dimension_index++) {
		threads[dimension_index-1] = thread(call_separate_dimension, this, dimension_index);
	}
	for(auto i=0; i<clustering_db.ndimensions(); i++)
		threads[i].join();
	delete[] (threads);

	this->reconstruct_best_clusters();

	// If no significant separation was found simply return false
	if(cluster1 == NULL)
		return false;

	// Before switching datapoints between the clusters keep the original sets
	for(auto sit = cluster1->dps_begin(); sit != cluster1->dps_end(); sit++)
		raw_dps_cluster1.insert(*sit);
	for(auto sit = cluster2->dps_begin(); sit != cluster2->dps_end(); sit++)
		raw_dps_cluster2.insert(*sit);

	// Now decide, for each scaffold, to which cluster it belongs. Cluster1 will contain all scaffolds that have at least half
	// of their datapoints in it, cluster2 will contain the rest
	for(auto mit = cluster1->scafs_begin(); mit != cluster1->scafs_end(); mit++)
		if(2*mit->second >= scaf_db.ndps(mit->first)) {
			cluster1->add_assigned_scaf(mit->first);
		}
	for(auto mit = cluster2->scafs_begin(); mit != cluster2->scafs_end(); mit++)
		if(2*mit->second > scaf_db.ndps(mit->first)) {
			cluster2->add_assigned_scaf(mit->first);
		}

	// Now move the datapoints based on their scaffold assignment
	set<size_t> dps;
	for(auto sit = cluster1->dps_begin(); sit != cluster1->dps_end(); sit++)
		if(!cluster1->assigned(scaf_db.dp2scaf(*sit)))
			dps.insert(*sit);

	for(auto sit = dps.begin(); sit != dps.end(); sit++) {
		*cluster1 -= *sit;
		*cluster2 += *sit;
	}

	dps.clear();
	for(auto sit = cluster2->dps_begin(); sit != cluster2->dps_end(); sit++)
		if(!cluster2->assigned(scaf_db.dp2scaf(*sit)))
			dps.insert(*sit);

	for(auto sit = dps.begin(); sit != dps.end(); sit++) {
		*cluster1 += *sit;
		*cluster2 -= *sit;
	}

	// Check again if the resulting clusters are sufficiently large.
	if((cluster1->ndps() < this->cluster_ndps_threshold) || (cluster2->ndps() < this->cluster_ndps_threshold)) {
		delete(cluster1);
		cluster1 = NULL;
		delete(cluster2);
		cluster2 = NULL;
		best_separation->reset();
		return false;
	}

	return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, %rbx
movq 0x28(%rdi), %r14
testq %r14, %r14
je 0x12b36
leaq 0x70(%r14), %rdi
callq 0x9b94
leaq 0x38(%r14), %rdi
callq 0x9bec
leaq 0x8(%r14), %rdi
callq 0x9c62
movq %r14, %rdi
callq 0x5340
leaq 0x28(%rbx), %r15
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0x12b66
leaq 0x70(%r14), %rdi
callq 0x9b94
leaq 0x38(%r14), %rdi
callq 0x9bec
leaq 0x8(%r14), %rdi
callq 0x9c62
movq %r14, %rdi
callq 0x5340
xorps %xmm0, %xmm0
movq %r15, -0x30(%rbp)
movups %xmm0, (%r15)
movq 0xa8(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
leaq 0x38(%rbx), %rdi
movq %rdi, -0x68(%rbp)
callq 0x9c14
leaq 0x70(%rbx), %rdi
movq %rdi, -0x70(%rbp)
callq 0x9c14
movq 0x18(%rbx), %r14
movq 0x8(%r14), %r15
subq (%r14), %r15
movq %r15, %rax
sarq $0x3, %rax
leaq -0x1(%rax), %rdx
movq %rdx, %rcx
movq %rdx, %r13
shrq $0x3d, %rcx
leaq -0x8(,%rax,8), %rax
addq $0x8, %rax
movq $-0x1, %rdx
cmovaeq %rax, %rdx
xorl %edi, %edi
negq %rcx
sbbq %rdi, %rdi
orq %rdx, %rdi
callq 0x5030
movq %rax, %r12
movq %r13, %rax
movq %r13, (%r12)
leaq 0x8(%r12), %r13
testq %rax, %rax
je 0x12c0b
addq $-0x10, %r15
andq $-0x8, %r15
addq $0x8, %r15
movq %r13, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x51f0
movq $0x1, -0x60(%rbp)
movq 0x8(%r14), %rax
movq %rax, %rcx
subq (%r14), %rcx
cmpq $0x8, %rcx
je 0x12c7a
leaq -0x60(%rbp), %r15
movq %rbx, -0x78(%rbp)
leaq -0x80(%rbp), %rdi
leaq -0x31d(%rip), %rsi      # 0x12919
leaq -0x78(%rbp), %rdx
movq %r15, %rcx
callq 0x13046
movq -0x60(%rbp), %rcx
cmpq $0x0, (%r12,%rcx,8)
jne 0x13025
movq -0x80(%rbp), %rax
movq %rax, (%r12,%rcx,8)
incq %rcx
movq %rcx, -0x60(%rbp)
movq 0x18(%rbx), %r14
movq 0x8(%r14), %rax
movq %rax, %rdx
subq (%r14), %rdx
sarq $0x3, %rdx
decq %rdx
cmpq %rdx, %rcx
jbe 0x12c27
subq (%r14), %rax
cmpq $0x8, %rax
je 0x12cac
xorl %r14d, %r14d
movq %r13, %rdi
callq 0x5650
incq %r14
movq 0x18(%rbx), %rax
movq 0x8(%rax), %rcx
subq (%rax), %rcx
sarq $0x3, %rcx
decq %rcx
addq $0x8, %r13
cmpq %r14, %rcx
ja 0x12c86
movq (%r12), %rax
testq %rax, %rax
movq -0x30(%rbp), %r13
je 0x12cce
shlq $0x3, %rax
cmpq $0x0, (%r12,%rax)
jne 0x13025
addq $-0x8, %rax
jne 0x12cbd
movq %r12, %rdi
callq 0x54d0
movq %rbx, %rdi
callq 0x12928
movq 0x28(%rbx), %rax
testq %rax, %rax
je 0x13010
movq 0x48(%rax), %r15
testq %r15, %r15
movq -0x68(%rbp), %r12
je 0x12d17
leaq -0x60(%rbp), %r14
leaq 0x8(%r15), %rsi
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x115d2
movq (%r15), %r15
testq %r15, %r15
jne 0x12cfc
movq 0x30(%rbx), %rax
movq 0x48(%rax), %r15
testq %r15, %r15
movq -0x70(%rbp), %r12
je 0x12d47
leaq -0x60(%rbp), %r14
leaq 0x8(%r15), %rsi
movq %r12, -0x60(%rbp)
movq %r12, %rdi
movq %r14, %rdx
callq 0x115d2
movq (%r15), %r15
testq %r15, %r15
jne 0x12d2c
movq (%r13), %rax
movq 0x20(%rax), %r14
leaq 0x10(%rax), %rcx
cmpq %rcx, %r14
je 0x12da5
leaq -0x60(%rbp), %r15
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r12
addq %r12, %r12
movq 0x8(%rbx), %rdi
callq 0xcce6
cmpq %rax, %r12
jb 0x12d8d
movq (%r13), %rdi
movq 0x20(%r14), %rax
movq %rax, -0x60(%rbp)
addq $0x70, %rdi
movq %r15, %rsi
callq 0xb066
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
movq (%r13), %rax
leaq 0x10(%rax), %rcx
cmpq %rcx, %r14
jne 0x12d5c
movq 0x30(%rbx), %rcx
movq 0x20(%rcx), %r14
addq $0x10, %rcx
cmpq %rcx, %r14
je 0x12e07
leaq -0x60(%rbp), %r15
movq 0x20(%r14), %rsi
movq 0x28(%r14), %r12
addq %r12, %r12
movq 0x8(%rbx), %rdi
callq 0xcce6
cmpq %rax, %r12
jbe 0x12deb
movq 0x30(%rbx), %rdi
movq 0x20(%r14), %rax
movq %rax, -0x60(%rbp)
addq $0x70, %rdi
movq %r15, %rsi
callq 0xb066
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
movq 0x30(%rbx), %rax
addq $0x10, %rax
cmpq %rax, %r14
jne 0x12dba
movq (%r13), %rax
leaq -0x58(%rbp), %r15
movl $0x0, (%r15)
xorl %ecx, %ecx
movq %rcx, 0x8(%r15)
movq %r15, 0x10(%r15)
movq %r15, 0x18(%r15)
movq %rcx, 0x20(%r15)
movq 0x48(%rax), %r12
testq %r12, %r12
je 0x12eca
leaq -0x60(%rbp), %r14
movq 0x8(%rbx), %rdi
movq 0x28(%rbx), %r13
movq 0x8(%r12), %rsi
callq 0xcc30
movq 0x80(%r13), %rcx
testq %rcx, %rcx
je 0x12e7d
addq $0x78, %r13
movq %r13, %rdx
xorl %esi, %esi
cmpq %rax, 0x20(%rcx)
setb %sil
cmovaeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x12e5a
cmpq %r13, %rdx
je 0x12e7d
cmpq %rax, 0x20(%rdx)
jbe 0x12e8a
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb066
movq (%r12), %r12
testq %r12, %r12
jne 0x12e35
movq -0x48(%rbp), %r14
cmpq %r15, %r14
movq -0x30(%rbp), %r12
je 0x12eca
movq 0x20(%r14), %rsi
movq (%r12), %rdi
callq 0x122bc
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0x121cc
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
cmpq %r15, %rax
jne 0x12ea0
leaq -0x60(%rbp), %rdi
callq 0x1311c
movq 0x30(%rbx), %rax
movq 0x48(%rax), %r12
testq %r12, %r12
je 0x12f42
leaq -0x60(%rbp), %r14
movq 0x8(%rbx), %rdi
movq 0x30(%rbx), %r13
movq 0x8(%r12), %rsi
callq 0xcc30
movq 0x80(%r13), %rcx
testq %rcx, %rcx
je 0x12f2c
addq $0x78, %r13
movq %r13, %rdx
xorl %esi, %esi
cmpq %rax, 0x20(%rcx)
setb %sil
cmovaeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x12f09
cmpq %r13, %rdx
je 0x12f2c
cmpq %rax, 0x20(%rdx)
jbe 0x12f39
leaq 0x8(%r12), %rsi
movq %r14, %rdi
callq 0xb066
movq (%r12), %r12
testq %r12, %r12
jne 0x12ee4
movq -0x48(%rbp), %r14
cmpq %r15, %r14
movq -0x30(%rbp), %r12
je 0x12f79
movq 0x20(%r14), %rsi
movq (%r12), %rdi
callq 0x121cc
movq 0x20(%r14), %rsi
movq 0x30(%rbx), %rdi
callq 0x122bc
movq %r14, %rdi
callq 0x5430
movq %rax, %r14
cmpq %r15, %rax
jne 0x12f4f
movq 0x28(%rbx), %r14
movq 0xb0(%rbx), %rax
cmpq %rax, 0x50(%r14)
jb 0x12f97
movq 0x30(%rbx), %rcx
movb $0x1, %r15b
cmpq %rax, 0x50(%rcx)
jae 0x13005
leaq 0x70(%r14), %rdi
callq 0x9b94
leaq 0x38(%r14), %rdi
callq 0x9bec
leaq 0x8(%r14), %rdi
callq 0x9c62
movq %r14, %rdi
callq 0x5340
movq $0x0, 0x28(%rbx)
movq 0x30(%rbx), %r14
testq %r14, %r14
je 0x12fe6
leaq 0x70(%r14), %rdi
callq 0x9b94
leaq 0x38(%r14), %rdi
callq 0x9bec
leaq 0x8(%r14), %rdi
callq 0x9c62
movq %r14, %rdi
callq 0x5340
movq $0x0, 0x30(%rbx)
movq 0xa8(%rbx), %rdi
movq (%rdi), %rax
callq *(%rax)
xorl %r15d, %r15d
leaq -0x60(%rbp), %rdi
callq 0x9b94
jmp 0x13013
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x50e0
jmp 0x13032
jmp 0x13032
jmp 0x13032
jmp 0x13032
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x9b94
movq %rbx, %rdi
callq 0x56a0