// File: Luthaf[P]zeopp/OMS.cc
// Function: IsExposedMolecule(std::vector<std::vector<double, std::allocator<double>>, std::allocator<std::vector<double, std::allocator<double>>>>)
bool IsExposedMolecule (vector < vector <double> > MoleculeCoordinates  )
{

	int NPoints = (int) MoleculeCoordinates.size();
	//vector < vector <double> > MoleculeSphericalCoordinates(Size, vector<double>(DIM) );
	//vector < vector <double> > MoleculeSphericalCoordinates(Size);

	double **TranslatedCoordinates = (double **) malloc((NPoints-1)*sizeof(double *));
	double *OriginCoordinates = (double *) malloc (DIM*sizeof(double));
	
	int i,j,k;

	double Aux;

	double Lambda = 0.;
	int S_lambda_old = 0, S_lambda;

	bool PlaneFound;


	// Assign initial coordinates
	for (i = 0; i < DIM; i++)
		OriginCoordinates[i] = MoleculeCoordinates.at(0).at(i);


	// Displace all the coordinates in order to set the metal molecule at the center
	for(i = 1; i < NPoints; i++)
	{

		TranslatedCoordinates[i-1] = (double *) malloc(sizeof(double)*DIM);
		// Assign values from input to the spherical coordinates vector and translate it to origin
		for (j = 0; j < DIM; j++)
		{
			Aux = MoleculeCoordinates.at(i).at(j);
			TranslatedCoordinates[i-1][j] = Aux - OriginCoordinates[j];
		}


	}

	/* For every two non-metal atoms in the molecule, form a vector (M-O), then a plane, then decide whether the rest of the poins are
	 * - Either in the same plane
 	 * - Either at the same side of the plane
	 */
	for (i = 0; i < NPoints-1; i++)
		for (j = i+1; j < NPoints-1; j++)
		{

			// Reinitialize the variables to avoid error
			S_lambda_old = 0;
			S_lambda = 0;
			Lambda = 0;
			PlaneFound = true;
			// For every point except the two composing the plane, compute
			if (!LinearlyDependent(TranslatedCoordinates[i], TranslatedCoordinates[j]))
			{		
				for (k = 0; k < NPoints-1; k++)
					if (k != i && k != j ) 
					{
						Lambda = PlaneSide (TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);

						//cout << "k: " << k << "\nLambda: " << Lambda << "\n";
			
						S_lambda = Sign(Lambda);

						// Update S_lambda_old only the first time that the point is not in the plane				
						if (Lambda != 0 && S_lambda_old == 0)
							S_lambda_old = Sign(Lambda);

						// If the sign changes anytime, stop this loop (the plane has points on both sides)
						if (S_lambda != S_lambda_old)
						{
							PlaneFound = false;
							//cout << "This plane separates the points \n";
							break;
						}
					}
			}
			else 	// If the vectors are linearly dependent the iteration is ignored
			{
				PlaneFound = false;
				//cout << "Setting PlaneFound false \n";
			}

			// Check if a plane with all the points on one side was found
			if (PlaneFound)
				return true;
		}
						
					

	return false;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x60(%rbp)
movq %rdi, -0x10(%rbp)
callq 0x22b10
movl %eax, -0x14(%rbp)
movl -0x14(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdi
shlq $0x3, %rdi
callq 0x56e0
movq %rax, -0x20(%rbp)
movl $0x18, %edi
callq 0x56e0
movq %rax, -0x28(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x2c(%rbp)
cmpl $0x3, -0x2c(%rbp)
jge 0x17426d
movq -0x60(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0xce5f0
movq %rax, %rdi
movslq -0x2c(%rbp), %rsi
callq 0xfc80
movsd (%rax), %xmm0
movq -0x28(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x174232
movl $0x1, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x174314
movl $0x18, %edi
callq 0x56e0
movq %rax, %rdx
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movl $0x0, -0x30(%rbp)
cmpl $0x3, -0x30(%rbp)
jge 0x174304
movq -0x60(%rbp), %rdi
movslq -0x2c(%rbp), %rsi
callq 0xce5f0
movq %rax, %rdi
movslq -0x30(%rbp), %rsi
callq 0xfc80
movsd (%rax), %xmm0
movsd %xmm0, -0x40(%rbp)
movsd -0x40(%rbp), %xmm0
movq -0x28(%rbp), %rax
movslq -0x30(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movl -0x2c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movq (%rax,%rcx,8), %rax
movslq -0x30(%rbp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x1742a5
jmp 0x174306
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x174274
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x17445f
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x17444f
movl $0x0, -0x4c(%rbp)
movl $0x0, -0x50(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x48(%rbp)
movb $0x1, -0x51(%rbp)
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
callq 0x173de0
testb $0x1, %al
jne 0x17442f
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movl -0x14(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x17442d
movl -0x34(%rbp), %eax
cmpl -0x2c(%rbp), %eax
je 0x17441d
movl -0x34(%rbp), %eax
cmpl -0x30(%rbp), %eax
je 0x17441d
movq -0x20(%rbp), %rax
movslq -0x2c(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x20(%rbp), %rax
movslq -0x30(%rbp), %rcx
movq (%rax,%rcx,8), %rsi
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
callq 0x173f20
movsd %xmm0, -0x48(%rbp)
movsd -0x48(%rbp), %xmm0
callq 0x174190
movl %eax, -0x50(%rbp)
movsd -0x48(%rbp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1743fa
jp 0x1743fa
jmp 0x17440d
cmpl $0x0, -0x4c(%rbp)
jne 0x17440d
movsd -0x48(%rbp), %xmm0
callq 0x174190
movl %eax, -0x4c(%rbp)
movl -0x50(%rbp), %eax
cmpl -0x4c(%rbp), %eax
je 0x17441b
movb $0x0, -0x51(%rbp)
jmp 0x17442d
jmp 0x17441d
jmp 0x17441f
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x17438c
jmp 0x174433
movb $0x0, -0x51(%rbp)
testb $0x1, -0x51(%rbp)
je 0x17443f
movb $0x1, -0x1(%rbp)
jmp 0x174463
jmp 0x174441
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x174335
jmp 0x174451
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x17431b
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x60, %rsp
popq %rbp
retq
nop