// File: /workspace/github_cpp_makefile_star2_O2/ADAPTLab[P]MuDBSCAN/MuC_RTree.cpp
// Function: populateMuCRTree(dataHdr*, int, int)
GHdrNd populateMuCRTree(DataHdr dataHdrLst, int gMinEntries, int gMaxEntries)
{
	GHdrNd hdrNdTree = GinitHdrNd();
	hdrNdTree->ptrFirstNd = GinitLstNd(GinitIntNd(NULL, NULL));
	hdrNdTree->uiCnt++;
	hdrNdTree->ptrFirstNd->ptrChildLst = GinitHdrNd();

	int cnt = 0, i;
	vectorc* unassigned = (vectorc*)malloc(sizeof(vectorc));
	VECTOR_INIT(unassigned, INTEGER);
	// int alloc = 0;

	Data currDataPoint;
	Group newGroup;
	for(i=0;i<dataHdrLst->uiCnt;i++)
	{	
		currDataPoint = dataHdrLst->dataClstElem + i; 
		Region currRegion = createCellRegOfPoint(currDataPoint, EPS);
		Region x2currRegion = createCellRegOfPoint(currDataPoint, 2*EPS);

		

		struct assign* assignStruct = GcanAssign2(currDataPoint, hdrNdTree, x2currRegion, EPS);

		if(assignStruct->canBeAssigned == 1) //Implies the current point is in the eps neighborhood of an existing group.
		{
			Group currGroup = groupList->groupItems[assignStruct->groupid];

			insertPointintoGroup(currGroup, currDataPoint);
			currDataPoint->group_id = currGroup->id;

			// if(currGroup != NULL)
			// {
			// 	insertPointintoGroup(currGroup, currDataPoint);
			// 	currDataPoint->group_id = currGroup->id;
			// 	// if(currDataPoint->debugPoint == TRUE)
			// 	// {
			// 	// 	printf("%lf\t%lf\t%lf\t populateMuCRTree Method Group ID %d\n",currDataPoint->iData[0],currDataPoint->iData[1],currDataPoint->iData[2],currDataPoint->group_id);
			// 	// }
			// }
			// else
			// {
			// 	printf("\n\n\nGcanAssign is giving WRONG OUTPUT!!!\n\n\n");
			// 	exit(0);
			// }
		}
		else if(assignStruct->canBeAssigned==0) //Implies the current point is not in the eps nbhd as well as 2*eps nbhd of existing groups.
		{

			newGroup = initGroup(currDataPoint);
			newGroup->bcell = initBCell(currRegion);
			BCell newBCell = newGroup->bcell;
			insertPointintoGroup(newGroup, currDataPoint);
			
			currDataPoint->group_id = newGroup->id;
			insertGroupIntoGroupList(newGroup);
			hdrNdTree = insertGroupIntoRTree(hdrNdTree, newGroup, gMinEntries, gMaxEntries);
			// if(currDataPoint->debugPoint == TRUE)
			// {
			// 	printf("%lf\t%lf\t%lf\t populateMuCRTree Method Can Not Be assigned To any existing group%d\n",currDataPoint->iData[0],currDataPoint->iData[1],currDataPoint->iData[2],currDataPoint->group_id);
			// }

		}			
		else
		{
			addHelper[0] = i;
			VECTOR_ADD(unassigned, addHelper);
		}

		free(currRegion->iBottomLeft);
   		free(currRegion->iTopRight);
    	free(currRegion);
    	free(x2currRegion->iBottomLeft);
   		free(x2currRegion->iTopRight);
    	free(x2currRegion);
	}
	
	// FOR UNASSIGNED POINTS, DON'T CONSTRUCT AUXILLARY R TREE....
	// CONSTRUCT AUX R TREES ALONG WITH INSERTION
	Region currRegion = NULL;
	Region x2currRegion = NULL;
	//Data currDataPoint;
	//Group newGroup;
	for(i=0; i<VECTOR_TOTAL(unassigned); i++)
	{
		
			currDataPoint = dataHdrLst->dataClstElem + unassigned->intItems[i];

			currRegion = createCellRegOfPoint(currDataPoint, EPS);
			x2currRegion = createCellRegOfPoint(currDataPoint, 2*EPS);

			struct assign* assignStruct = GcanAssign2(currDataPoint, hdrNdTree, x2currRegion, EPS);
			
			if(assignStruct->canBeAssigned==1)
			{
				Group currGroup = groupList->groupItems[assignStruct->groupid];

				insertPointintoGroup(currGroup, currDataPoint);
				currDataPoint->group_id=currGroup->id;

				//Modify the implementation of GfindCell to return BCell only when distance between data point and center is less than or equal to Epsilon
				// if(currGroup != NULL){
				// 	insertPointintoGroup(currGroup, currDataPoint);
				// 	currDataPoint->group_id=currGroup->id;			
				// }
				// else{
				// 	printf("\n\n\n GcanAssign is giving WRONG OUTPUT\n\n\n");
				// }
			}
			else
			{
				// createa a new Group and insert it into the R Tree
				// create a BCell and insert it into the R Tree
				// printf("Creating a new BCell and inserting the new point into it\n");
					
				newGroup = initGroup(currDataPoint);//(Group)malloc(sizeof(struct group));
				newGroup->bcell = initBCell(currRegion);
				BCell newBCell = newGroup->bcell;

				insertPointintoGroup(newGroup, currDataPoint);
					
				currDataPoint->group_id = newGroup->id;
				insertGroupIntoGroupList(newGroup);

				hdrNdTree = insertGroupIntoRTree(hdrNdTree, newGroup, gMinEntries, gMaxEntries);
			}			
		
		if(currRegion != NULL)
		{
			free(currRegion->iBottomLeft);
	   		free(currRegion->iTopRight);
	    	free(currRegion);
	    }

	    if(x2currRegion != NULL)
	    {
	    	free(x2currRegion->iBottomLeft);
	   		free(x2currRegion->iTopRight);
	    	free(x2currRegion);	
    	}
	}
	VECTOR_FREE(unassigned);
	free(unassigned);
	return hdrNdTree;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %edx, -0x38(%rbp)
movl %esi, -0x34(%rbp)
movq %rdi, %r15
callq 0x2f2c
movq %rax, %r12
xorl %r13d, %r13d
xorl %edi, %edi
xorl %esi, %esi
callq 0x520c
movq %rax, %rdi
callq 0x2f4a
movq %rax, 0x8(%r12)
incl (%r12)
callq 0x2f2c
movq 0x8(%r12), %rcx
movq %rax, 0x8(%rcx)
pushq $0x30
popq %rdi
callq 0x2320
movq %rax, -0x50(%rbp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x27ac
xorl %ecx, %ecx
movq %r15, -0x40(%rbp)
movq %r12, -0x30(%rbp)
movslq (%r15), %rax
cmpq %rax, %rcx
jge 0x7115
movq %rcx, -0x48(%rbp)
movq 0x8(%r15), %r12
movq %r13, -0x58(%rbp)
addq %r12, %r13
leaq 0xd348(%rip), %r15      # 0x14328
movsd (%r15), %xmm0
movq %r13, %rdi
callq 0x6b91
movq %rax, %r14
movsd (%r15), %xmm0
addsd %xmm0, %xmm0
movq %r13, %rdi
callq 0x6b91
movq %rax, %rbx
movsd (%r15), %xmm0
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x6308
movl (%rax), %ecx
testl %ecx, %ecx
je 0x705d
cmpl $0x1, %ecx
jne 0x70ae
leaq 0xd342(%rip), %rcx      # 0x14370
movq (%rcx), %rcx
movq (%rcx), %rcx
movslq 0x4(%rax), %rax
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x6e2d
movl (%r15), %eax
movq -0x40(%rbp), %r15
movq -0x58(%rbp), %r13
movl %eax, 0x20(%r12,%r13)
movq -0x30(%rbp), %r12
jmp 0x70d3
movq %r13, %rdi
callq 0x3940
movq %rax, %r15
movq %r14, %rdi
callq 0x39e3
movq %rax, 0x38(%r15)
movq %r15, %rdi
movq %r13, %rsi
callq 0x6e2d
movl (%r15), %eax
movq -0x58(%rbp), %r13
movl %eax, 0x20(%r12,%r13)
movq %r15, %rdi
callq 0x726c
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq -0x40(%rbp), %r15
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
callq 0x7283
movq %rax, %r12
jmp 0x70d3
leaq 0xd2ab(%rip), %rax      # 0x14360
movq (%rax), %rsi
movq -0x48(%rbp), %rax
movl %eax, (%rsi)
movq -0x50(%rbp), %rdi
callq 0x29f0
movq -0x30(%rbp), %r12
movq -0x40(%rbp), %r15
movq -0x58(%rbp), %r13
movq (%r14), %rdi
callq 0x2280
movq 0x8(%r14), %rdi
callq 0x2280
movq %r14, %rdi
callq 0x2280
movq (%rbx), %rdi
callq 0x2280
movq 0x8(%rbx), %rdi
callq 0x2280
movq %rbx, %rdi
callq 0x2280
movq -0x48(%rbp), %rcx
incq %rcx
addq $0x40, %r13
jmp 0x6fba
xorl %ebx, %ebx
movq -0x50(%rbp), %r12
movq %r12, %rdi
callq 0x2831
cltq
cmpq %rax, %rbx
jge 0x7249
movq 0x8(%r12), %rax
movq %rbx, -0x48(%rbp)
movslq (%rax,%rbx,4), %r13
shlq $0x6, %r13
addq 0x8(%r15), %r13
leaq 0xd1de(%rip), %r15      # 0x14328
movsd (%r15), %xmm0
movq %r13, %rdi
callq 0x6b91
movq %rax, %r14
movsd (%r15), %xmm0
addsd %xmm0, %xmm0
movq %r13, %rdi
callq 0x6b91
movq %rax, %rbx
movsd (%r15), %xmm0
movq %r13, %rdi
movq -0x30(%rbp), %r12
movq %r12, %rsi
movq %rax, %rdx
callq 0x6308
cmpl $0x1, (%rax)
jne 0x71b3
leaq 0xd1df(%rip), %rcx      # 0x14370
movq (%rcx), %rcx
movq (%rcx), %rcx
movslq 0x4(%rax), %rax
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x6e2d
movl (%r15), %eax
movl %eax, 0x20(%r13)
jmp 0x71f9
movq %r13, %rdi
callq 0x3940
movq %rax, %r15
movq %r14, %rdi
callq 0x39e3
movq %rax, 0x38(%r15)
movq %r15, %rdi
movq %r13, %rsi
callq 0x6e2d
movl (%r15), %eax
movl %eax, 0x20(%r13)
movq %r15, %rdi
callq 0x726c
movq %r12, %rdi
movq %r15, %rsi
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %ecx
callq 0x7283
movq %rax, -0x30(%rbp)
testq %r14, %r14
movq -0x50(%rbp), %r12
je 0x721b
movq (%r14), %rdi
callq 0x2280
movq 0x8(%r14), %rdi
callq 0x2280
movq %r14, %rdi
callq 0x2280
testq %rbx, %rbx
movq -0x40(%rbp), %r15
je 0x723d
movq (%rbx), %rdi
callq 0x2280
movq 0x8(%rbx), %rdi
callq 0x2280
movq %rbx, %rdi
callq 0x2280
movq -0x48(%rbp), %rbx
incq %rbx
jmp 0x711b
movq %r12, %rdi
callq 0x2a78
movq %r12, %rdi
callq 0x2280
movq -0x30(%rbp), %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq