// File: /workspace/github_cpp_makefile_star2_O2/ADAPTLab[P]MuDBSCAN/clustering.cpp
// Function: get_clusters_distributed(dataHdr*)
struct result* get_clusters_distributed(DataHdr dataList)
{
	int rank, nproc, i;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &nproc);

	vector < vector <int > > merge_received;
	vector < vector <int > > merge_send1;
	vector < vector <int > > merge_send2;
	vector <int> init;

	merge_received.resize(nproc, init);
	merge_send1.resize(nproc, init);
	merge_send2.resize(nproc, init);

	int pid;

    for(pid = 0; pid < nproc; pid++)
	{
    	merge_received[pid].reserve(dataList->localCnt);
    	merge_send1[pid].reserve(dataList->localCnt);
		merge_send2[pid].reserve(dataList->localCnt);
	}

	vector < vector <int > >* pswap;
	vector < vector <int > >* p_cur_send;
	vector < vector <int > >* p_cur_insert;

	p_cur_send = &merge_send1;
	p_cur_insert = &merge_send2;


	int root, local_continue_to_run = 0, global_continue_to_run;
	for(i = 0; i < dataList->localCnt; i++)
	{
		// find the point containing i
		root = i;
		Data dataroot = dataList->dataClstElem + root;
		// Find highest ancestor in this node
		while(dataroot->parentProcessId == rank)
		{
			if(dataroot->parentId == root)
				break;
			root = dataroot->parentId;
			dataroot = dataList->dataClstElem + root;
		}
		
		// if the highest ancestor from the previous loop is local point
		if(dataroot->parentId == root && dataroot->parentProcessId == rank) // root is a local root
		{
			Data datai = dataList->dataClstElem + i;
			datai->parentId = root;
			dataroot->childCount = dataroot->childCount + 1;
		}
		else
		{
			(*p_cur_insert)[dataroot->parentProcessId].push_back(0); // flag: 0 means query and 1 means a reply
			(*p_cur_insert)[dataroot->parentProcessId].push_back(dataroot->parentId);
   			(*p_cur_insert)[dataroot->parentProcessId].push_back(i);
			(*p_cur_insert)[dataroot->parentProcessId].push_back(rank);
			local_continue_to_run++;
		}			
	}

	int pos, round = 0, quadraples, scount, tid, tag = 0, rtag, rsource, rcount, isend[nproc], irecv[nproc], flag;

	MPI_Request s_req_recv[nproc], s_req_send[nproc], d_req_send[nproc], d_req_recv[nproc]; // better to malloc the memory
	MPI_Status  s_stat, d_stat_send[nproc], d_stat;
	int target_point, source_point, source_pr;

	while(1)
	{
		global_continue_to_run = 0;

		MPI_Allreduce(&local_continue_to_run, &global_continue_to_run, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
		
		if(global_continue_to_run == 0)
    		break;

		pswap = p_cur_insert;
   		p_cur_insert = p_cur_send;
   		p_cur_send = pswap;

		for(tid = 0; tid < nproc; tid++)
   			(*p_cur_insert)[tid].clear();

		scount = 0;
		for(tid = 0; tid < nproc; tid++)
		{
			if(tid != rank)
			{
				isend[tid] = (*p_cur_send)[tid].size();
				if(isend[tid] > 0)
	    		{
	    			MPI_Isend(&(*p_cur_send)[tid][0], isend[tid], MPI_INT, tid, tag + 1, MPI_COMM_WORLD, &d_req_send[scount]);
	        		scount++;
	    		}	
			}
		}

		MPI_Alltoall(&isend[0], 1, MPI_INT, &irecv[0], 1, MPI_INT, MPI_COMM_WORLD);

		rcount = 0;
		for(tid = 0; tid < nproc; tid++)
		{
			if(tid != rank)
			{
				if(irecv[tid] > 0)
				{
					merge_received[tid].clear();
					merge_received[tid].assign(irecv[tid], -1);
					MPI_Irecv(&merge_received[tid][0], irecv[tid], MPI_INT, tid, tag + 1, MPI_COMM_WORLD, &d_req_recv[rcount]);
					rcount++;
				}
			}
		}

		local_continue_to_run = 0;
		Data dataroot;
		Data datatarget;
		for(tid = 0; tid < rcount; tid++)
		{
			MPI_Waitany(rcount, &d_req_recv[0], &pos, &d_stat);
		
			rtag = d_stat.MPI_TAG;
			rsource = d_stat.MPI_SOURCE;

			if(rtag == tag + 1)
			{
				quadraples = merge_received[rsource].size()/4;

				for(pid = 0; pid < quadraples; pid++)
				{
					source_pr = merge_received[rsource].back();
					merge_received[rsource].pop_back();

        			source_point = merge_received[rsource].back();
        			merge_received[rsource].pop_back();

        			target_point = merge_received[rsource].back();
        			merge_received[rsource].pop_back();
		
					flag = merge_received[rsource].back();
        			merge_received[rsource].pop_back();

					if(flag == 0)
					{					
						root = target_point;
						dataroot = dataList->dataClstElem + root;
						while(dataroot->parentProcessId == rank)
						{
							if(dataroot->parentId == root)
								break;
							root = dataroot->parentId;
							dataroot = dataList->dataClstElem + root;
						}
						if(dataroot->parentId == root && dataroot->parentProcessId == rank) // root is a local root
						{
							dataroot->childCount = dataroot->childCount + 1; // increase the child count by one
							// have to return the child about root
							
            				(*p_cur_insert)[source_pr].push_back(1);
            				(*p_cur_insert)[source_pr].push_back(source_point);
            				(*p_cur_insert)[source_pr].push_back(dataroot->parentId);
            				(*p_cur_insert)[source_pr].push_back(dataroot->parentProcessId);
							local_continue_to_run++;
						}
						else
						{
							(*p_cur_insert)[dataroot->parentProcessId].push_back(0);
							(*p_cur_insert)[dataroot->parentProcessId].push_back(dataroot->parentId);
		         			(*p_cur_insert)[dataroot->parentProcessId].push_back(source_point);
        	    			(*p_cur_insert)[dataroot->parentProcessId].push_back(source_pr);
							local_continue_to_run++;
						}
					}
					else
					{
						datatarget = dataList->dataClstElem + target_point;
						datatarget->parentId = source_point;
						datatarget->parentProcessId = source_pr;
					}
				}
			}
		}
		tag++;
		round++;

		if(scount > 0)
			MPI_Waitall(scount, &d_req_send[0], &d_stat_send[0]); // wait for all the sending operation
	}

	int final_cluster_root = 0, total_final_cluster_root = 0;
	int points_in_cluster_final = 0, total_points_in_cluster_final = 0;
	int noise = 0;

	/*FILE* debug;
	string currFile("");
	currFile = neighbourFileName + patch::to_string(rank);
	debug = fopen(currFile.c_str(), "w");
*/
	Data datai;
	for(i = 0; i < dataList->localCnt; i++)
	{

		datai = dataList->dataClstElem + i;

		if(datai->ClusterID == 0 && datai->haloPoint == FALSE) noise++;

		//fprintf(debug, "%d_%d\t%d_%d\t%d\t%d\n", datai->id, datai->actualProcessId, datai->parentId, datai->parentProcessId, datai->ClusterID, datai->core_tag);
		
		if(datai->parentId == i && datai->parentProcessId == rank && datai->childCount >= 1)
		{
			points_in_cluster_final += datai->childCount;
			final_cluster_root++;
		}

	}

	MPI_Allreduce(&final_cluster_root, &total_final_cluster_root, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);

	struct result* result = (struct result*)calloc(1, sizeof(struct result));

	result->noise = noise;
	result->clusters = total_final_cluster_root;
	result->corepoints = -1;

	merge_received.clear();
	merge_send1.clear();
	merge_send2.clear();
	init.clear();

	return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdi, -0x48(%rbp)
movq 0x7b17(%rip), %rbx      # 0x13fb0
leaq -0x34(%rbp), %rsi
movq %rbx, %rdi
callq 0x2370
leaq -0x30(%rbp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2250
leaq -0xc0(%rbp), %rdi
andq $0x0, 0x10(%rdi)
xorps %xmm0, %xmm0
andq $0x0, -0x100(%rbp)
movaps %xmm0, (%rdi)
andq $0x0, -0x90(%rbp)
movaps %xmm0, -0x110(%rbp)
movaps %xmm0, -0xa0(%rbp)
leaq -0xf0(%rbp), %rdx
movaps %xmm0, (%rdx)
andq $0x0, 0x10(%rdx)
movslq (%r14), %rsi
callq 0x95a2
movslq -0x30(%rbp), %rsi
leaq -0x110(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
movq %rdi, -0x68(%rbp)
callq 0x95a2
movslq -0x30(%rbp), %rsi
leaq -0xa0(%rbp), %r15
leaq -0xf0(%rbp), %rdx
movq %r15, %rdi
callq 0x95a2
xorl %ebx, %ebx
xorl %r14d, %r14d
movq -0x48(%rbp), %r12
movslq -0x30(%rbp), %rax
cmpq %rax, %r14
jge 0xc587
movq -0xc0(%rbp), %rdi
addq %rbx, %rdi
movslq 0x4(%r12), %rsi
callq 0xd602
movq -0x110(%rbp), %rdi
addq %rbx, %rdi
movslq 0x4(%r12), %rsi
callq 0xd602
movq -0xa0(%rbp), %rdi
addq %rbx, %rdi
movslq 0x4(%r12), %rsi
callq 0xd602
incq %r14
addq $0x18, %rbx
jmp 0xc539
andl $0x0, -0x3c(%rbp)
xorl %eax, %eax
leaq -0x140(%rbp), %r14
leaq -0x40(%rbp), %r12
leaq -0x34(%rbp), %r13
movl %eax, -0x40(%rbp)
movq -0x48(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jge 0xc650
movq 0x8(%rcx), %rcx
movslq %eax, %rdx
movl -0x34(%rbp), %esi
movq %rdx, %r8
movl %eax, %edi
shlq $0x6, %r8
leaq (%rcx,%r8), %rbx
movslq 0x18(%rcx,%r8), %rax
cmpl %esi, %eax
jne 0xc5e3
movl 0x14(%rbx), %eax
movslq %eax, %r8
cmpl %edi, %eax
jne 0xc5b9
shlq $0x6, %rdx
movl %edi, 0x14(%rcx,%rdx)
incl 0x24(%rbx)
jmp 0xc646
imulq $0x18, %rax, %rdi
addq -0xa0(%rbp), %rdi
andl $0x0, -0x140(%rbp)
movq %r14, %rsi
callq 0xd80e
leaq 0x14(%rbx), %rsi
movslq 0x18(%rbx), %rax
imulq $0x18, %rax, %rdi
addq -0xa0(%rbp), %rdi
callq 0x3b0a
movslq 0x18(%rbx), %rax
imulq $0x18, %rax, %rdi
addq -0xa0(%rbp), %rdi
movq %r12, %rsi
callq 0x3b0a
movslq 0x18(%rbx), %rax
imulq $0x18, %rax, %rdi
addq -0xa0(%rbp), %rdi
movq %r13, %rsi
callq 0x3b0a
incl -0x3c(%rbp)
movl -0x40(%rbp), %eax
incl %eax
jmp 0xc59c
movq %rsp, -0x118(%rbp)
movl -0x30(%rbp), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0xd0(%rbp)
movq %rcx, %rsp
movl -0x30(%rbp), %eax
movq %rsp, %rdx
leaq 0xf(,%rax,4), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x70(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
leaq 0xf(,%rax,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0xc8(%rbp)
movq %rdx, %rsp
movq %rsp, %rdx
subq %rcx, %rdx
movq %rdx, -0xd8(%rbp)
movq %rdx, %rsp
movq %rsp, %rcx
imulq $0x18, %rax, %rax
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x120(%rbp)
movq %rcx, %rsp
movq $0x0, -0x50(%rbp)
movq -0x68(%rbp), %r14
movq %r15, -0x68(%rbp)
andl $0x0, -0x5c(%rbp)
leaq -0x3c(%rbp), %rdi
leaq -0x5c(%rbp), %rsi
pushq $0x1
popq %rdx
movq 0x78d0(%rip), %rcx      # 0x13fd0
movq 0x7891(%rip), %r8       # 0x13f98
movq 0x78a2(%rip), %r9       # 0x13fb0
callq 0x22c0
cmpl $0x0, -0x5c(%rbp)
movq -0x48(%rbp), %r10
je 0xca8f
movl -0x30(%rbp), %eax
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
imulq $0x18, %rcx, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0xc752
movq (%r14), %rsi
movq (%rsi,%rdx), %rdi
cmpq %rdi, 0x8(%rsi,%rdx)
je 0xc74c
movq %rdi, 0x8(%rsi,%rdx)
addq $0x18, %rdx
jmp 0xc734
movq -0x50(%rbp), %rcx
incl %ecx
movq %rcx, -0x50(%rbp)
xorl %r15d, %r15d
pushq $0x2
popq %rbx
xorl %r12d, %r12d
movslq %eax, %rcx
cmpq %rcx, %r15
jge 0xc7d9
movl -0x34(%rbp), %ecx
cmpq %rcx, %r15
je 0xc7d0
movq -0x68(%rbp), %rcx
movq (%rcx), %rcx
movq -0x8(%rcx,%rbx,4), %rdi
movq (%rcx,%rbx,4), %rsi
subq %rdi, %rsi
shrq $0x2, %rsi
movq -0xd0(%rbp), %rcx
movl %esi, (%rcx,%r15,4)
testl %esi, %esi
jle 0xc7d0
movslq %r12d, %rax
movq -0xc8(%rbp), %rcx
leaq (%rcx,%rax,8), %rax
subq $0x8, %rsp
movq 0x781c(%rip), %rdx      # 0x13fd0
movl %r15d, %ecx
movq -0x50(%rbp), %r8
movq 0x77ee(%rip), %r9       # 0x13fb0
pushq %rax
callq 0x2120
popq %rcx
popq %rdx
incl %r12d
movl -0x30(%rbp), %eax
incq %r15
addq $0x6, %rbx
jmp 0xc765
subq $0x8, %rsp
movq -0xd0(%rbp), %rdi
pushq $0x1
popq %rsi
movq 0x77e2(%rip), %rdx      # 0x13fd0
movq -0x70(%rbp), %rcx
movl %esi, %r8d
movq %rdx, %r9
pushq 0x77b2(%rip)            # 0x13fb0
callq 0x2150
popq %rcx
popq %rdx
xorl %ebx, %ebx
xorl %r15d, %r15d
xorl %r13d, %r13d
movslq -0x30(%rbp), %rax
cmpq %rax, %r15
jge 0xc8a7
movl -0x34(%rbp), %eax
cmpq %rax, %r15
je 0xc89b
movq -0x70(%rbp), %rax
movslq (%rax,%r15,4), %rsi
testq %rsi, %rsi
jle 0xc89b
movq -0xc0(%rbp), %rcx
leaq (%rcx,%rbx), %rdi
movq (%rcx,%rbx), %rax
cmpq %rax, 0x8(%rcx,%rbx)
je 0xc849
movq %rax, 0x8(%rdi)
orl $-0x1, -0x2c(%rbp)
leaq -0x2c(%rbp), %rdx
callq 0xd6ca
movq -0xc0(%rbp), %rax
movq (%rax,%rbx), %rdi
movq -0x70(%rbp), %rax
movl (%rax,%r15,4), %esi
movslq %r13d, %rax
movq -0xd8(%rbp), %rcx
leaq (%rcx,%rax,8), %rax
subq $0x8, %rsp
movq 0x774e(%rip), %rdx      # 0x13fd0
movl %r15d, %ecx
movq -0x50(%rbp), %r8
movq 0x7720(%rip), %r9       # 0x13fb0
pushq %rax
callq 0x2340
popq %rcx
popq %rdx
incl %r13d
incq %r15
addq $0x18, %rbx
jmp 0xc80d
andl $0x0, -0x3c(%rbp)
xorl %ebx, %ebx
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
movl %eax, -0x7c(%rbp)
movq -0x48(%rbp), %r15
movl %r12d, -0x78(%rbp)
movl %r13d, -0x74(%rbp)
cmpl -0x7c(%rbp), %ebx
je 0xca65
movl %r13d, %edi
movq -0xd8(%rbp), %rsi
leaq -0x124(%rbp), %rdx
leaq -0x140(%rbp), %rcx
callq 0x2310
movl %ebx, -0x80(%rbp)
movq -0x50(%rbp), %rax
cmpl %eax, -0x13c(%rbp)
jne 0xca53
movslq -0x140(%rbp), %rax
movq -0xc0(%rbp), %rcx
imulq $0x18, %rax, %r13
movq 0x8(%rcx,%r13), %r12
subq (%rcx,%r13), %r12
shrq $0x4, %r12
xorl %ebx, %ebx
testl %r12d, %r12d
cmovlel %ebx, %r12d
cmpl %r12d, %ebx
je 0xca53
movq -0xc0(%rbp), %rcx
movq 0x8(%rcx,%r13), %rdi
movslq -0x4(%rdi), %rax
movl %eax, -0x38(%rbp)
movl -0x8(%rdi), %esi
movl %esi, -0x58(%rbp)
movl -0xc(%rdi), %edx
leaq -0x10(%rdi), %r8
cmpl $0x0, -0x10(%rdi)
movq %r8, 0x8(%rcx,%r13)
movq 0x8(%r15), %rcx
je 0xc978
movslq %edx, %rax
shlq $0x6, %rax
movl %esi, 0x14(%rcx,%rax)
movl -0x38(%rbp), %edx
movl %edx, 0x18(%rcx,%rax)
jmp 0xca4c
movl -0x34(%rbp), %esi
movslq %edx, %rdi
shlq $0x6, %rdi
leaq (%rcx,%rdi), %r15
movslq 0x18(%rcx,%rdi), %rdi
cmpl %esi, %edi
jne 0xc9f5
movl 0x14(%r15), %edi
cmpl %edx, %edi
movl %edi, %edx
jne 0xc97b
incl 0x24(%r15)
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
movl $0x1, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0xd80e
movslq -0x38(%rbp), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3b0a
leaq 0x14(%r15), %rsi
movslq -0x38(%rbp), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
callq 0x3b0a
addq $0x18, %r15
movslq -0x38(%rbp), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
movq %r15, %rsi
callq 0x3b0a
jmp 0xca45
imulq $0x18, %rdi, %rdi
addq (%r14), %rdi
andl $0x0, -0x2c(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0xd80e
leaq 0x14(%r15), %rsi
movslq 0x18(%r15), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
callq 0x3b0a
movslq 0x18(%r15), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
leaq -0x58(%rbp), %rsi
callq 0x3b0a
movslq 0x18(%r15), %rax
imulq $0x18, %rax, %rdi
addq (%r14), %rdi
leaq -0x38(%rbp), %rsi
callq 0x3b0a
incl -0x3c(%rbp)
movq -0x48(%rbp), %r15
incl %ebx
jmp 0xc929
movl -0x80(%rbp), %ebx
incl %ebx
movl -0x78(%rbp), %r12d
movl -0x74(%rbp), %r13d
jmp 0xc8c8
movq %r14, %r15
testl %r12d, %r12d
jle 0xc6e2
movl %r12d, %edi
movq -0xc8(%rbp), %rsi
movq -0x120(%rbp), %rdx
callq 0x2290
movq %r14, %r15
jmp 0xc6e2
andl $0x0, -0x2c(%rbp)
andl $0x0, -0x54(%rbp)
movl 0x4(%r10), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl -0x34(%rbp), %edx
pushq $0x30
popq %rsi
xorl %edi, %edi
xorl %r14d, %r14d
movl %ecx, -0x40(%rbp)
cmpq %rcx, %rax
je 0xcaf1
movq 0x8(%r10), %r8
cmpl $0x0, (%r8,%rsi)
jne 0xcaca
cmpl $0x1, -0x28(%r8,%rsi)
adcl $0x0, %r14d
movl -0x1c(%r8,%rsi), %r9d
cmpq %r9, %rcx
jne 0xcae8
cmpl %edx, -0x18(%r8,%rsi)
jne 0xcae8
cmpl $0x0, -0xc(%r8,%rsi)
jle 0xcae8
incl %edi
movl %edi, -0x2c(%rbp)
incq %rcx
addq $0x40, %rsi
jmp 0xcaad
movq 0x74d8(%rip), %rcx      # 0x13fd0
movq 0x7499(%rip), %r8       # 0x13f98
movq 0x74aa(%rip), %r9       # 0x13fb0
leaq -0x2c(%rbp), %rdi
leaq -0x54(%rbp), %rsi
pushq $0x1
popq %rdx
callq 0x22c0
pushq $0x1
popq %rdi
pushq $0xc
popq %rsi
callq 0x2180
movq %rax, %rbx
movl %r14d, (%rax)
movl -0x54(%rbp), %eax
movl %eax, 0x4(%rbx)
orl $-0x1, 0x8(%rbx)
leaq -0xc0(%rbp), %rdi
callq 0x95f8
leaq -0x110(%rbp), %rdi
callq 0x95f8
leaq -0xa0(%rbp), %rdi
callq 0x95f8
movq -0xf0(%rbp), %rax
cmpq %rax, -0xe8(%rbp)
je 0xcb6c
movq %rax, -0xe8(%rbp)
movq -0x118(%rbp), %rsp
leaq -0xf0(%rbp), %rdi
callq 0x9d22
leaq -0xa0(%rbp), %rdi
callq 0x9606
leaq -0x110(%rbp), %rdi
callq 0x9606
leaq -0xc0(%rbp), %rdi
callq 0x9606
movq %rbx, %rax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
jmp 0xcbcf
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x9d22
leaq -0xa0(%rbp), %rdi
callq 0x9606
leaq -0x110(%rbp), %rdi
callq 0x9606
leaq -0xc0(%rbp), %rdi
callq 0x9606
movq %rbx, %rdi
callq 0x2460