// File: ADAPTLab[P]MuDBSCAN/partition.cpp
// Function: fileReadMulti(char*, int*, int*, std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>&)
int fileReadMulti(char* infilename, int* numObjs, int* numCoords, vector< vector<double> >& objects)     /* no. coordinates */
{    
	int i, j, rank, nproc;
	int num_points,  dims;
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &nproc);

	//cout << "Process " << rank << " entered fileReadMulti\n";

	string out("out_");
	string infolder(infilename);
	//cout<<"infolder "<<infolder<<"\n";
	string file_acc_to_rank = out + patch::to_string(rank);
	//cout<<"file_acc_to_rank "<<file_acc_to_rank<<"\n";
	file_acc_to_rank = infolder + "/" + file_acc_to_rank;
	//cout<<"file_acc_to_rank "<<file_acc_to_rank<<"\n";
	ifstream file(file_acc_to_rank.c_str(), ios::in);
	int local_points = 0;

	if (file.is_open())
	{
		file >> num_points;
		file >> dims;
		long long sch, lower, upper;

		// if(num_points % nproc == 0)
		//     sch = num_points/nproc;
		// else
		//     sch = num_points/nproc + 1;

		lower = 0;
		upper = num_points;

		// if(lower > num_points)
		// 	lower = num_points;

		// if(upper > num_points)
		//     upper = num_points;

		*numObjs = num_points;
		*numCoords = dims;

		DIMENSION = dims;
		local_points = upper - lower;

		objects.resize(local_points);
		for(int ll = 0; ll < local_points; ll++)
			objects[ll].resize(dims);

		MINGRIDSIZEglobal = (double*) calloc(DIMENSION, sizeof(double));
		// assert(MINGRIDSIZEglobal != NULL);

		MAXGRIDSIZEglobal = (double*) calloc(DIMENSION, sizeof(double));
		// assert(MAXGRIDSIZEglobal != NULL);

		MINGRIDSIZE = (double*) calloc(DIMENSION, sizeof(double));
		// assert(MINGRIDSIZE != NULL);

		MAXGRIDSIZE = (double*) calloc(DIMENSION, sizeof(double));
		// assert(MAXGRIDSIZE != NULL);

		int iNum=0;
		double* pt;

		string s;
		s.resize(1000, 0);

		for(i=0;i<lower;i++)
			getline(file, s);

		pt = (double*) calloc(dims, sizeof(double));
		// assert(pt != NULL);

		for (i = 0; i < local_points; i++)
		{
			for (j = 0; j < dims; j++)
			{
				file >> pt[j];
				(objects)[i][j] = pt[j];

				if(i==0)
				{
					MINGRIDSIZEglobal[j] = (objects)[0][0];
					MAXGRIDSIZEglobal[j] = (objects)[0][0];    
				}
				else
				{
					if(MINGRIDSIZEglobal[j] > (objects)[i][j])
					{
						MINGRIDSIZEglobal[j] = (objects)[i][j]; 
					}
					if(MAXGRIDSIZEglobal[j] < (objects)[i][j])
					{
						MAXGRIDSIZEglobal[j] = (objects)[i][j];
					}
				}
			}
		}

		free(pt);
		pt = NULL;
		
		file.close();
		return local_points;
	}
	else
	{
		cout << "rank " << rank << " Error: no such file: " << infilename << endl;
		exit(-1);
	}
	return -1;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x400, %rsp            # imm = 0x400
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq 0x1f0d6(%rip), %rdi     # 0x37fa8
movq %rdi, -0x380(%rbp)
leaq -0x2c(%rbp), %rsi
callq 0x99e0
movq -0x380(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x9790
leaq -0x59(%rbp), %rdi
movq %rdi, -0x378(%rbp)
callq 0x9c80
movq -0x378(%rbp), %rdx
leaq 0x1335b(%rip), %rsi     # 0x2c26b
leaq -0x58(%rbp), %rdi
callq 0x9960
jmp 0x18f1b
leaq -0x59(%rbp), %rdi
callq 0x9720
movq -0x8(%rbp), %rax
movq %rax, -0x390(%rbp)
leaq -0x91(%rbp), %rdi
movq %rdi, -0x388(%rbp)
callq 0x9c80
movq -0x390(%rbp), %rsi
movq -0x388(%rbp), %rdx
leaq -0x90(%rbp), %rdi
callq 0x9960
jmp 0x18f5e
leaq -0x91(%rbp), %rdi
callq 0x9720
leaq -0xd8(%rbp), %rdi
leaq -0x2c(%rbp), %rsi
callq 0x1be20
jmp 0x18f7c
leaq -0xb8(%rbp), %rdi
leaq -0x58(%rbp), %rsi
leaq -0xd8(%rbp), %rdx
callq 0x1bdd0
jmp 0x18f95
leaq -0xd8(%rbp), %rdi
callq 0x93d0
leaq 0x132c8(%rip), %rdx     # 0x2c270
leaq -0x118(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x1bf20
jmp 0x18fbd
leaq -0xf8(%rbp), %rdi
leaq -0x118(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x1bed0
jmp 0x18fd9
leaq -0xb8(%rbp), %rdi
movq %rdi, -0x398(%rbp)
leaq -0xf8(%rbp), %rsi
movq %rsi, -0x3a0(%rbp)
callq 0x9820
movq -0x3a0(%rbp), %rdi
callq 0x93d0
leaq -0x118(%rbp), %rdi
callq 0x93d0
movq -0x398(%rbp), %rdi
callq 0x9240
movq %rax, %rsi
leaq -0x320(%rbp), %rdi
movl $0x8, %edx
callq 0x9cf0
jmp 0x19034
movl $0x0, -0x324(%rbp)
leaq -0x320(%rbp), %rdi
callq 0x9b20
movb %al, -0x3a1(%rbp)
jmp 0x19052
movb -0x3a1(%rbp), %al
testb $0x1, %al
jne 0x19061
jmp 0x19573
leaq -0x320(%rbp), %rdi
leaq -0x34(%rbp), %rsi
callq 0x93c0
jmp 0x19073
leaq -0x320(%rbp), %rdi
leaq -0x38(%rbp), %rsi
callq 0x93c0
jmp 0x19085
movq $0x0, -0x338(%rbp)
movslq -0x34(%rbp), %rax
movq %rax, -0x340(%rbp)
movl -0x34(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, (%rax)
movl -0x38(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movl -0x38(%rbp), %ecx
leaq 0x1f71d(%rip), %rax     # 0x387d4
movl %ecx, (%rax)
movl -0x340(%rbp), %eax
movl -0x338(%rbp), %ecx
subl %ecx, %eax
movl %eax, -0x324(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x324(%rbp), %rsi
callq 0x1bcb0
jmp 0x190df
movl $0x0, -0x344(%rbp)
movl -0x344(%rbp), %eax
cmpl -0x324(%rbp), %eax
jge 0x191d0
movq -0x20(%rbp), %rdi
movslq -0x344(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x38(%rbp), %rsi
callq 0x1bd40
jmp 0x19119
jmp 0x1911b
movl -0x344(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x344(%rbp)
jmp 0x190e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x9720
jmp 0x19628
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x91(%rbp), %rdi
callq 0x9720
jmp 0x1961f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x19613
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x93d0
jmp 0x19613
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x19607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x118(%rbp), %rdi
callq 0x93d0
jmp 0x19607
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0x195fb
leaq 0x1f5fd(%rip), %rax     # 0x387d4
movq %rax, -0x3b8(%rbp)
movslq (%rax), %rdi
movl $0x8, %esi
movq %rsi, -0x3c0(%rbp)
callq 0x9400
movq -0x3c0(%rbp), %rsi
movq %rax, %rdx
movq -0x3b8(%rbp), %rax
leaq 0x1f65e(%rip), %rcx     # 0x38868
movq %rdx, (%rcx)
movslq (%rax), %rdi
callq 0x9400
movq -0x3c0(%rbp), %rsi
movq %rax, %rdx
movq -0x3b8(%rbp), %rax
leaq 0x1f643(%rip), %rcx     # 0x38870
movq %rdx, (%rcx)
movslq (%rax), %rdi
callq 0x9400
movq -0x3c0(%rbp), %rsi
movq %rax, %rdx
movq -0x3b8(%rbp), %rax
leaq 0x1f608(%rip), %rcx     # 0x38858
movq %rdx, (%rcx)
movslq (%rax), %rdi
callq 0x9400
movq %rax, %rcx
leaq 0x1f5fb(%rip), %rax     # 0x38860
movq %rcx, (%rax)
movl $0x0, -0x348(%rbp)
leaq -0x370(%rbp), %rdi
movq %rdi, -0x3b0(%rbp)
callq 0x9990
movq -0x3b0(%rbp), %rdi
movl $0x3e8, %esi            # imm = 0x3E8
xorl %edx, %edx
callq 0x91d0
jmp 0x1929a
movl $0x0, -0x24(%rbp)
movslq -0x24(%rbp), %rax
cmpq -0x338(%rbp), %rax
jge 0x192ed
leaq -0x320(%rbp), %rdi
leaq -0x370(%rbp), %rsi
callq 0x90e0
jmp 0x192c3
jmp 0x192c5
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x192a1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x370(%rbp), %rdi
callq 0x93d0
jmp 0x195fb
movslq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x9400
movq %rax, -0x350(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0x324(%rbp), %eax
jge 0x194fa
movl $0x0, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0x194ea
movq -0x350(%rbp), %rax
movslq -0x28(%rbp), %rcx
leaq (%rax,%rcx,8), %rsi
leaq -0x320(%rbp), %rdi
callq 0x9550
jmp 0x19348
movq -0x350(%rbp), %rax
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x3c8(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x28(%rbp), %rsi
callq 0xf870
movsd -0x3c8(%rbp), %xmm0
movsd %xmm0, (%rax)
cmpl $0x0, -0x24(%rbp)
jne 0x193f0
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf850
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf870
movsd (%rax), %xmm0
leaq 0x1f4b9(%rip), %rax     # 0x38868
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movq -0x20(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf850
movq %rax, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xf870
movsd (%rax), %xmm0
leaq 0x1f491(%rip), %rax     # 0x38870
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x194da
leaq 0x1f471(%rip), %rax     # 0x38868
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x3d0(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x28(%rbp), %rsi
callq 0xf870
movsd -0x3d0(%rbp), %xmm0
ucomisd (%rax), %xmm0
jbe 0x19462
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x28(%rbp), %rsi
callq 0xf870
movsd (%rax), %xmm0
leaq 0x1f412(%rip), %rax     # 0x38868
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
leaq 0x1f407(%rip), %rax     # 0x38870
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x3d8(%rbp)
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x28(%rbp), %rsi
callq 0xf870
movsd -0x3d8(%rbp), %xmm1
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x194d8
movq -0x20(%rbp), %rdi
movslq -0x24(%rbp), %rsi
callq 0xf850
movq %rax, %rdi
movslq -0x28(%rbp), %rsi
callq 0xf870
movsd (%rax), %xmm0
leaq 0x1f3a4(%rip), %rax     # 0x38870
movq (%rax), %rax
movslq -0x28(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
jmp 0x194da
jmp 0x194dc
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x1931f
jmp 0x194ec
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x19309
movq -0x350(%rbp), %rdi
callq 0x97c0
movq $0x0, -0x350(%rbp)
leaq -0x320(%rbp), %rdi
callq 0x9110
jmp 0x1951f
movl -0x324(%rbp), %eax
movl %eax, -0x3dc(%rbp)
leaq -0x370(%rbp), %rdi
callq 0x93d0
leaq -0x320(%rbp), %rdi
callq 0x9180
leaq -0xb8(%rbp), %rdi
callq 0x93d0
leaq -0x90(%rbp), %rdi
callq 0x93d0
leaq -0x58(%rbp), %rdi
callq 0x93d0
movl -0x3dc(%rbp), %eax
addq $0x400, %rsp            # imm = 0x400
popq %rbp
retq
movq 0x1ea4e(%rip), %rdi     # 0x37fc8
leaq 0x12cf1(%rip), %rsi     # 0x2c272
callq 0x9670
movq %rax, -0x3e8(%rbp)
jmp 0x1958f
movq -0x3e8(%rbp), %rdi
movl -0x2c(%rbp), %esi
callq 0x9c30
movq %rax, -0x3f0(%rbp)
jmp 0x195a7
movq -0x3f0(%rbp), %rdi
leaq 0x12cc3(%rip), %rsi     # 0x2c278
callq 0x9670
movq %rax, -0x3f8(%rbp)
jmp 0x195c3
movq -0x3f8(%rbp), %rdi
movq -0x8(%rbp), %rsi
callq 0x9670
movq %rax, -0x400(%rbp)
jmp 0x195dc
movq -0x400(%rbp), %rdi
movq 0x1e9b6(%rip), %rsi     # 0x37fa0
callq 0x96c0
jmp 0x195f1
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x9810
leaq -0x320(%rbp), %rdi
callq 0x9180
leaq -0xb8(%rbp), %rdi
callq 0x93d0
leaq -0x90(%rbp), %rdi
callq 0x93d0
leaq -0x58(%rbp), %rdi
callq 0x93d0
movq -0x68(%rbp), %rdi
callq 0x9c70
nopw %cs:(%rax,%rax)