// File: /workspace/github_cpp_makefile_star2_O3/ADAPTLab[P]MuDBSCAN/RTree.cpp
// Function: RpickSeeds(RhdrNd*, RlstNd**, RlstNd**)
void RpickSeeds(RHdrNd ptrChildLst, RLstNd *lstNdChildOne, RLstNd *lstNdChildTwo)
{
	if(ptrChildLst == NULL)
		return;
	RTreeNode* tnInfoMin = (RTreeNode *)calloc(DIMENSION, sizeof(struct RtreeNode));
	// assert(tnInfoMin != NULL);

	RTreeNode* tnInfoMax = (RTreeNode *)calloc(DIMENSION, sizeof(struct RtreeNode));
	// assert(tnInfoMax != NULL);
	RTreeNode temp;
	RLstNd lstNdTemp = NULL;
    int iCnt = 0;
	Boolean bIsFirst = TRUE;
	double dNormSep = 0;
	double dMaxNormSep = 0;
	dimension dimMaxNormSep = 0;
	Region rgnRectTemp = RinitRgnRect(NULL, NULL);
	// assert(rgnRectTemp != NULL);

	Region rgnRectOut = RinitRgnRect(NULL, NULL);
	// assert(rgnRectOut != NULL);

	switch(ptrChildLst->ptrFirstNd->tnInfo->ndType)
    {	
    	case INTNODE:   
    		lstNdTemp = ptrChildLst->ptrFirstNd;

            while(lstNdTemp != NULL)
            {
                for(iCnt = 0; iCnt < DIMENSION; iCnt++)
                {     
                  	if(bIsFirst)
                    {     
			            rgnRectTemp->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            rgnRectTemp->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            rgnRectOut->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            rgnRectOut->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            tnInfoMin[iCnt] = lstNdTemp->tnInfo;
			            tnInfoMax[iCnt] = lstNdTemp->tnInfo;
			            continue;
                    }
			 
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt] > rgnRectTemp->iBottomLeft[iCnt])
                    {     
			            rgnRectTemp->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];
			            tnInfoMin[iCnt] = lstNdTemp->tnInfo;
                    }
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt] < rgnRectTemp->iTopRight[iCnt])
                    {     
			            rgnRectTemp->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
			            tnInfoMax[iCnt] = lstNdTemp->tnInfo;
                    }
			                        
			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt] < rgnRectOut->iBottomLeft[iCnt])
                        rgnRectOut->iBottomLeft[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iBottomLeft[iCnt];

			        if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt] > rgnRectOut->iTopRight[iCnt])
                        rgnRectOut->iTopRight[iCnt] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[iCnt];
                }
                lstNdTemp = lstNdTemp->ptrNextNd;
		        if(bIsFirst)
                    bIsFirst = FALSE;
            }	
	                    
	        for(iCnt = 0; iCnt < DIMENSION; iCnt++)
            {     

                dNormSep = fabs(rgnRectTemp->iBottomLeft[iCnt] - rgnRectTemp->iTopRight[iCnt]) / fabs(rgnRectOut->iTopRight[iCnt] - rgnRectOut->iBottomLeft[iCnt]);
                if(dNormSep > dMaxNormSep)
                {   
                 	dMaxNormSep = dNormSep;
			        dimMaxNormSep = iCnt;
                               }
                }
                if(tnInfoMin[(int)dimMaxNormSep] == tnInfoMax[(int)dimMaxNormSep])
                {
            	   	lstNdTemp = ptrChildLst->ptrFirstNd;
                   	temp=tnInfoMax[(int)dimMaxNormSep];
                   	if(temp != lstNdTemp->tnInfo)
                   	{
                   		rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
                		tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->tnInfo;
                        lstNdTemp = lstNdTemp->ptrNextNd;
                    }
                    else
                    {
                    	rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->ptrNextNd->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
                        tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->ptrNextNd->tnInfo;
                        lstNdTemp = lstNdTemp->ptrNextNd->ptrNextNd;
                    }
                    while(lstNdTemp != NULL)
                    {
                    	if(lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep] < rgnRectTemp->iTopRight[(int)dimMaxNormSep] && temp != lstNdTemp->tnInfo)
                    	{
                        	rgnRectTemp->iTopRight[(int)dimMaxNormSep] = lstNdTemp->tnInfo->tdInfo->rgnRect->iTopRight[(int)dimMaxNormSep];
				            tnInfoMax[(int)dimMaxNormSep] = lstNdTemp->tnInfo;
                        }
                        lstNdTemp = lstNdTemp->ptrNextNd;
                    } 
                }
                
                /*if(tnInfoMin[(int)dimMaxNormSep]==tnInfoMax[(int)dimMaxNormSep])
                {
                   	printf("error in the code\n");
                   	exit(-1);
                }*/
                        
	            *lstNdChildOne = RdeleteLstElem(ptrChildLst, tnInfoMin[(int)dimMaxNormSep]);
	  
	            *lstNdChildTwo = RdeleteLstElem(ptrChildLst, tnInfoMax[(int)dimMaxNormSep]);
	                     
	            free(rgnRectTemp->iBottomLeft);
	            free(rgnRectTemp->iTopRight);
	            free(rgnRectTemp);
	            free(rgnRectOut->iBottomLeft);
	            free(rgnRectOut->iTopRight);
	            free(rgnRectOut);
                break;

		case EXTNODE:        
	        lstNdTemp = ptrChildLst->ptrFirstNd;
	        DataPoint iDataMin = (DataPoint)calloc(DIMENSION, sizeof(dataPoint));
	        // assert( iDataMin != NULL);

	        DataPoint iDataMax = (DataPoint)calloc(DIMENSION, sizeof(dataPoint));
	        // assert(iDataMax != NULL);

            bIsFirst = TRUE;
	        while(lstNdTemp != NULL)
            {     
            	for(iCnt = 0; iCnt < DIMENSION; iCnt++)
                {   
                	if(bIsFirst)
                    {    
                        iDataMin[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
				        iDataMax[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
                        tnInfoMin[iCnt] = lstNdTemp->tnInfo;
				        tnInfoMax[iCnt] = lstNdTemp->tnInfo;
			            continue;
                    }

			        if(lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt] <= iDataMin[iCnt])
                    {   
                    	iDataMin[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
				        tnInfoMin[iCnt] = lstNdTemp->tnInfo;
                    }
		            
		            if(lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt] > iDataMax[iCnt])
                    {    
                    	iDataMax[iCnt] = lstNdTemp->tnInfo->tdInfo->dataClstElem->iData[iCnt];
                        tnInfoMax[iCnt]  = lstNdTemp->tnInfo;
                    }
                }
                lstNdTemp = lstNdTemp->ptrNextNd;
                if(bIsFirst)
                    bIsFirst = FALSE;
            } 
	        double dSep = 0;
	        double dMaxSep = 0;
	        dimension dimMaxSep = 0;

	        for(iCnt = 0; iCnt < DIMENSION; iCnt++)
            {      
            	dSep = fabs(iDataMax[iCnt] - iDataMin[iCnt]);
		        if(dSep > dMaxSep)
                {   
                	dMaxSep = dSep;
			        dimMaxSep = iCnt;
	            }
            }
	        
	        *lstNdChildOne = RdeleteLstElem(ptrChildLst, tnInfoMin[(int)dimMaxSep]);
	        
	        *lstNdChildTwo = RdeleteLstElem(ptrChildLst, tnInfoMax[(int)dimMaxSep]);
	        free(rgnRectTemp->iBottomLeft);
	        free(rgnRectTemp->iTopRight);
	        free(rgnRectTemp);
	        free(rgnRectOut->iBottomLeft);
	        free(rgnRectOut->iTopRight);
	        free(rgnRectOut);
	        free(iDataMin);
	        free(iDataMax);
            break;
	}

	free(tnInfoMin);
	free(tnInfoMax);

	return;
}

// Assembly:
testq %rdi, %rdi
je 0x5a40
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
movq %rsi, -0x50(%rbp)
movq %rdx, -0x58(%rbp)
leaq 0x11aa8(%rip), %r15     # 0x17364
movslq (%r15), %r14
movl $0x10, %esi
movq %r14, %rdi
callq 0x31b0
movq %rax, %r12
movl $0x10, %esi
movq %r14, %rdi
callq 0x31b0
movq %rax, -0x30(%rbp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x513c
movq %rax, -0x38(%rbp)
xorl %edi, %edi
xorl %esi, %esi
callq 0x513c
movq %rax, %r13
movq %rbx, -0x48(%rbp)
movq 0x8(%rbx), %rbx
movq (%rbx), %rax
movl (%rax), %ecx
cmpl $0x1, %ecx
je 0x5a41
testl %ecx, %ecx
jne 0x5cf9
movl (%r15), %edx
xorl %ecx, %ecx
movq %rbx, %rsi
xorl %edi, %edi
testl %edx, %edx
jle 0x5a2b
movq -0x38(%rbp), %r8
movq (%r8), %r8
xorl %r9d, %r9d
movq (%rsi), %r10
movq 0x8(%r10), %r11
movq (%r11), %r14
movq (%r14), %r11
movsd (%r11,%r9,8), %xmm0
testb $0x1, %dil
je 0x59c0
ucomisd (%r8,%r9,8), %xmm0
jbe 0x595e
movsd %xmm0, (%r8,%r9,8)
movq %r10, (%r12,%r9,8)
movq 0x8(%r14), %r14
movsd (%r14,%r9,8), %xmm0
movq -0x38(%rbp), %r15
movq 0x8(%r15), %r15
movsd (%r15,%r9,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x598a
movsd %xmm0, (%r15,%r9,8)
movq -0x30(%rbp), %r15
movq %r10, (%r15,%r9,8)
movsd (%r11,%r9,8), %xmm0
movq (%r13), %r10
movsd (%r10,%r9,8), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x59a6
movsd %xmm0, (%r10,%r9,8)
movsd (%r14,%r9,8), %xmm0
movq 0x8(%r13), %r10
ucomisd (%r10,%r9,8), %xmm0
jbe 0x5a0a
movsd %xmm0, (%r10,%r9,8)
jmp 0x5a0a
movsd %xmm0, (%r8,%r9,8)
movq 0x8(%r14), %r14
movsd (%r14,%r9,8), %xmm0
movq -0x38(%rbp), %r15
movq 0x8(%r15), %r15
movsd %xmm0, (%r15,%r9,8)
movsd (%r11,%r9,8), %xmm0
movq (%r13), %r11
movsd %xmm0, (%r11,%r9,8)
movsd (%r14,%r9,8), %xmm0
movq 0x8(%r13), %r11
movsd %xmm0, (%r11,%r9,8)
movq %r10, (%r12,%r9,8)
movq -0x30(%rbp), %r11
movq %r10, (%r11,%r9,8)
incq %r9
cmpq %r9, %rdx
jne 0x5933
movq 0x10(%rsi), %rsi
movb $0x1, %dil
testq %rsi, %rsi
jne 0x5921
jmp 0x5b00
movq 0x10(%rsi), %rsi
movb $0x1, %dil
testq %rsi, %rsi
jne 0x5921
jmp 0x5bf9
retq
movq %r12, -0x40(%rbp)
movslq (%r15), %r15
movl %r15d, %r14d
movl $0x8, %esi
movq %r15, %rdi
callq 0x31b0
movq %rax, %r12
movl $0x8, %esi
movq %r15, %rdi
callq 0x31b0
movq %rax, %r15
xorl %r8d, %r8d
xorl %eax, %eax
movq -0x30(%rbp), %rdi
testl %r14d, %r14d
jle 0x5aec
xorl %ecx, %ecx
movq (%rbx), %rdx
movq 0x8(%rdx), %rsi
movq 0x8(%rsi), %rsi
movq (%rsi), %rsi
movsd (%rsi,%rcx,8), %xmm0
testb $0x1, %al
je 0x5abc
movsd (%r12,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jb 0x5aac
movsd %xmm0, (%r12,%rcx,8)
movq -0x40(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
ucomisd (%r15,%rcx,8), %xmm0
jbe 0x5ad4
movsd %xmm0, (%r15,%rcx,8)
jmp 0x5ad0
movsd %xmm0, (%r12,%rcx,8)
movsd %xmm0, (%r15,%rcx,8)
movq -0x40(%rbp), %rsi
movq %rdx, (%rsi,%rcx,8)
movq %rdx, (%rdi,%rcx,8)
incq %rcx
cmpq %rcx, %r14
jne 0x5a7b
movq 0x10(%rbx), %rbx
movb $0x1, %al
testq %rbx, %rbx
jne 0x5a74
jmp 0x5b8c
movq 0x10(%rbx), %rbx
movb $0x1, %al
testq %rbx, %rbx
jne 0x5a74
jmp 0x5c76
testl %edx, %edx
jle 0x5bf7
movq -0x38(%rbp), %rsi
movq (%rsi), %rcx
movq 0x8(%rsi), %rsi
movq (%r13), %rdi
movq 0x8(%r13), %r8
xorpd %xmm0, %xmm0
xorl %r9d, %r9d
movapd 0xc5d6(%rip), %xmm1     # 0x12100
xorpd %xmm2, %xmm2
movq -0x30(%rbp), %r10
movsd (%rcx,%r9,8), %xmm3
subsd (%rsi,%r9,8), %xmm3
movsd (%r8,%r9,8), %xmm4
subsd (%rdi,%r9,8), %xmm4
divsd %xmm4, %xmm3
andpd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r9d, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm2, %xmm5
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm0, %xmm2
orpd %xmm4, %xmm2
movapd %xmm2, %xmm0
incq %r9
movapd %xmm5, %xmm2
cmpq %r9, %rdx
jne 0x5b32
cvttsd2si %xmm0, %ecx
movslq %ecx, %rcx
jmp 0x5bfd
testl %r14d, %r14d
jle 0x5c73
xorpd %xmm0, %xmm0
xorl %eax, %eax
movapd 0xc55d(%rip), %xmm1     # 0x12100
xorpd %xmm2, %xmm2
movq -0x48(%rbp), %rbx
movsd (%r15,%rax,8), %xmm3
subsd (%r12,%rax,8), %xmm3
andpd %xmm1, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %eax, %xmm4
movapd %xmm3, %xmm5
maxsd %xmm2, %xmm5
cmpltsd %xmm3, %xmm2
andpd %xmm2, %xmm4
andnpd %xmm0, %xmm2
orpd %xmm4, %xmm2
movapd %xmm2, %xmm0
incq %rax
movapd %xmm5, %xmm2
cmpq %rax, %r14
jne 0x5bab
cvttsd2si %xmm0, %eax
movslq %eax, %r8
jmp 0x5c7a
xorl %ecx, %ecx
movq -0x30(%rbp), %r10
movq (%r12,%rcx,8), %rsi
movq (%r10,%rcx,8), %r15
cmpq %r15, %rsi
je 0x5c62
movq %r12, -0x40(%rbp)
movq -0x48(%rbp), %rbx
movq %rbx, %rdi
callq 0x442c
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x442c
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq -0x38(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
movq (%r13), %rdi
callq 0x3310
movq 0x8(%r13), %r12
jmp 0x5ce5
cmpq %rax, %rsi
je 0x5d18
movq %rax, %r15
jmp 0x5d1f
xorl %r8d, %r8d
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %rax
movq (%rax,%r8,8), %rsi
movq %rbx, %rdi
movq %r8, %r14
callq 0x442c
movq -0x50(%rbp), %rcx
movq %rax, (%rcx)
movq -0x30(%rbp), %rax
movq (%rax,%r14,8), %rsi
movq %rbx, %rdi
callq 0x442c
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq -0x38(%rbp), %rbx
movq (%rbx), %rdi
callq 0x3310
movq 0x8(%rbx), %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3310
movq (%r13), %rdi
callq 0x3310
movq 0x8(%r13), %rdi
callq 0x3310
movq %r13, %rdi
callq 0x3310
movq %r15, %r13
movq %r12, %rdi
callq 0x3310
movq %r13, %rdi
callq 0x3310
movq -0x40(%rbp), %r12
movq %r12, %rdi
callq 0x3310
movq -0x30(%rbp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x3310
movq 0x10(%rbx), %rbx
movq (%rbx), %r15
movq 0x8(%r15), %rax
movq (%rax), %rax
movq 0x8(%rax), %rax
movsd (%rax,%rcx,8), %xmm0
movq -0x38(%rbp), %rax
movq 0x8(%rax), %rax
movsd %xmm0, (%rax,%rcx,8)
movq %r15, (%r10,%rcx,8)
movq 0x10(%rbx), %rdx
testq %rdx, %rdx
je 0x5c0a
movq (%rdx), %rdi
cmpq %rdi, %rsi
je 0x5d7c
movq 0x8(%rdi), %r8
movq (%r8), %r8
movq 0x8(%r8), %r8
movsd (%r8,%rcx,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5d7c
movsd %xmm1, (%rax,%rcx,8)
movq %rdi, (%r10,%rcx,8)
movq %rdi, %r15
movapd %xmm1, %xmm0
movq 0x10(%rdx), %rdx
jmp 0x5d44