// File: /workspace/github_cpp_makefile_star2_O1/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, -0x40(%rbp)
movl %esi, -0x3c(%rbp)
movq %rdi, %r15
callq 0x423c
movq %rax, %rbx
xorl %edi, %edi
xorl %esi, %esi
callq 0x696e
movq %rax, %rdi
callq 0x425e
movq %rax, 0x8(%rbx)
incl (%rbx)
callq 0x423c
movq %rbx, -0x30(%rbp)
movq 0x8(%rbx), %rcx
movq %rax, 0x8(%rcx)
movl $0x30, %edi
callq 0x33a0
movq %rax, -0x50(%rbp)
movq %rax, %rdi
xorl %esi, %esi
callq 0x39bc
cmpl $0x0, (%r15)
movq %r15, -0x48(%rbp)
jle 0x8ccd
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x8(%r15), %rax
movq %rax, -0x58(%rbp)
leaq (%rax,%r12), %r13
leaq 0xe7c6(%rip), %r15      # 0x17358
movsd (%r15), %xmm0
movq %r13, %rdi
callq 0x86c1
movq %rax, -0x38(%rbp)
movsd (%r15), %xmm0
addsd %xmm0, %xmm0
movq %r13, %rdi
callq 0x86c1
movq %rax, %rbx
movsd (%r15), %xmm0
movq %r13, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
callq 0x7ceb
movl (%rax), %ecx
testl %ecx, %ecx
je 0x8c0c
cmpl $0x1, %ecx
jne 0x8c69
leaq 0xe7bf(%rip), %rcx      # 0x173a0
movq (%rcx), %rcx
movq (%rcx), %rcx
movslq 0x4(%rax), %rax
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x899a
movl (%r15), %eax
movq -0x48(%rbp), %r15
movq -0x58(%rbp), %rcx
movl %eax, 0x20(%rcx,%r12)
jmp 0x8c83
movq %r13, %rdi
callq 0x4eb8
movq %rax, %r15
movq -0x38(%rbp), %rdi
callq 0x4f7d
movq %rax, 0x38(%r15)
movq %r15, %rdi
movq %r13, %rsi
callq 0x899a
movl (%r15), %eax
movq -0x58(%rbp), %rcx
movl %eax, 0x20(%rcx,%r12)
leaq 0xe75e(%rip), %rax      # 0x173a0
movq (%rax), %rdi
movq %r15, %rsi
callq 0x3c19
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movq -0x48(%rbp), %r15
movl -0x3c(%rbp), %edx
movl -0x40(%rbp), %ecx
callq 0x8e61
movq %rax, -0x30(%rbp)
jmp 0x8c83
leaq 0xe720(%rip), %rax      # 0x17390
movq (%rax), %rsi
movl %r14d, (%rsi)
movq -0x50(%rbp), %rdi
callq 0x3c19
movq -0x48(%rbp), %r15
movq -0x38(%rbp), %r13
movq (%r13), %rdi
callq 0x3310
movq 0x8(%r13), %rdi
callq 0x3310
movq %r13, %rdi
callq 0x3310
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
incq %r14
movslq (%r15), %rax
addq $0x40, %r12
cmpq %rax, %r14
jl 0x8b7f
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x3a4d
testl %eax, %eax
jle 0x8e24
xorl %r12d, %r12d
movq -0x30(%rbp), %r13
movq 0x8(%rbx), %rax
movq %r13, %r14
movslq (%rax,%r12,4), %r13
shlq $0x6, %r13
addq 0x8(%r15), %r13
leaq 0xe656(%rip), %r15      # 0x17358
movsd (%r15), %xmm0
movq %r13, %rdi
callq 0x86c1
movq %rax, -0x38(%rbp)
movsd (%r15), %xmm0
addsd %xmm0, %xmm0
movq %r13, %rdi
callq 0x86c1
movq %rax, %rbx
movsd (%r15), %xmm0
movq %r13, %rdi
movq %r14, -0x30(%rbp)
movq %r14, %rsi
movq %rax, %rdx
callq 0x7ceb
cmpl $0x1, (%rax)
jne 0x8d74
leaq 0xe656(%rip), %rcx      # 0x173a0
movq (%rcx), %rcx
movq (%rcx), %rcx
movslq 0x4(%rax), %rax
movq (%rcx,%rax,8), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x899a
movl (%r15), %eax
movl %eax, 0x20(%r13)
movq -0x30(%rbp), %r13
movq -0x38(%rbp), %r14
jmp 0x8dc8
movq %r13, %rdi
callq 0x4eb8
movq %rax, %r15
movq -0x38(%rbp), %r14
movq %r14, %rdi
callq 0x4f7d
movq %rax, 0x38(%r15)
movq %r15, %rdi
movq %r13, %rsi
callq 0x899a
movl (%r15), %eax
movl %eax, 0x20(%r13)
leaq 0xe5f8(%rip), %rax      # 0x173a0
movq (%rax), %rdi
movq %r15, %rsi
callq 0x3c19
movq -0x30(%rbp), %rdi
movq %r15, %rsi
movl -0x3c(%rbp), %edx
movl -0x40(%rbp), %ecx
callq 0x8e61
movq %rax, %r13
testq %r14, %r14
je 0x8de6
movq (%r14), %rdi
callq 0x3310
movq 0x8(%r14), %rdi
callq 0x3310
movq %r14, %rdi
callq 0x3310
testq %rbx, %rbx
movq -0x48(%rbp), %r15
je 0x8e08
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
incq %r12
movq -0x50(%rbp), %rbx
movq %rbx, %rdi
callq 0x3a4d
cltq
cmpq %rax, %r12
jl 0x8ce8
jmp 0x8e28
movq -0x30(%rbp), %r13
movq %rbx, %rdi
callq 0x3ca0
movq %rbx, %rdi
callq 0x3310
movq %r13, %rax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq