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

	int NPoints = (int) MoleculeCoordinates.size();

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

	// Angles
	double Theta = 0.;
	double Exposure_pos = 0., Exposure_neg = 0., Exposure_plane = 0.;

	// Plane side
	double Lambda = 0.;
	int S_lambda_old = 0, S_lambda = 0;


	// 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++)
			TranslatedCoordinates[i-1][j] = MoleculeCoordinates.at(i).at(j) - 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++)
		{

			//cout << "Plane " << i << j << "\n";

			// Reinitialize the variables to avoid error
			S_lambda_old = 0;
			S_lambda = 0;
			Lambda = 0;
			Theta = 0.;

			Exposure_pos = 0.;
			Exposure_neg = 0.;

			// For every point except the two composing the plane, compute
			if (!LinearlyDependent(TranslatedCoordinates[i], TranslatedCoordinates[j]))
			{	
				// POINTS ITERATION: Compare the points with the plane selected in previous iterations
				for (k = 0; k < NPoints-1; k++)
					if (k != i && k != j ) 
					{
						// Compute the side of the plane
						Lambda = PlaneSide (TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
						//cout << "k: " << k << " Lambda " << Lambda << "\n";

						S_lambda = Sign(Lambda);

						// Store the angles at both sides of the plane 
						// The exposure angle is the minimum of both
						if (S_lambda > 0)
						{
							Theta = PlaneAngle(TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
							if (Theta > Exposure_pos)							
								Exposure_pos = Theta;
						}
						if (S_lambda < 0)
						{
							Theta = PlaneAngle(TranslatedCoordinates[i], TranslatedCoordinates[j], TranslatedCoordinates[k]);
							if (Theta > Exposure_neg)							
								Exposure_neg = Theta;
						}

					}

				// Compute exposures (only if the vector forms a plane)
				Exposure_plane = min(Exposure_pos, Exposure_neg);
				if (Exposure_plane < Threshold)
					return true;

			}

			

		}
						
					
	// Maximum angle formed by a point not contained within the same side of the rest of the points
	return false;




}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm0, 0x58(%rsp)
movq (%rdi), %rbx
movq 0x8(%rdi), %r14
movq %r14, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %rbp # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %rbp
movslq %ebp, %rax
leaq -0x8(,%rax,8), %rdi
callq 0x4580
movq %rax, 0x18(%rsp)
movl $0x18, %edi
callq 0x4580
cmpq %rbx, %r14
je 0x10d2bb
movq %rax, %r15
leal -0x1(%rbp), %ecx
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x10d29e
movsd (%rax,%rsi,8), %xmm0
movsd %xmm0, (%r15,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x10d022
movl %ecx, 0x10(%rsp)
cmpl $0x2, %ebp
jl 0x10d0c0
cmpq $0x1, %rbp
movq %rbp, %r13
adcq $0x0, %r13
movl %ebp, %r14d
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
movl $0x1, %r12d
movl $0x18, %edi
callq 0x4580
movq 0x18(%rsp), %rcx
movq %rax, -0x8(%rcx,%r12,8)
cmpq %r13, %r12
je 0x10d2ac
leaq (%r12,%r12,2), %rdx
movq (%rbx,%rdx,8), %rcx
movq 0x8(%rbx,%rdx,8), %rdx
subq %rcx, %rdx
sarq $0x3, %rdx
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x10d29e
movsd (%rcx,%rsi,8), %xmm0
subsd (%r15,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x10d096
incq %r12
cmpq %r14, %r12
jne 0x10d063
cmpl $0x2, %ebp
setge %al
jl 0x10d28d
movslq 0x10(%rsp), %rcx
movl %ecx, %ebp
movl $0x1, %esi
movq %rsi, 0x40(%rsp)
xorl %r14d, %r14d
movq %rcx, 0x48(%rsp)
movb %al, 0xf(%rsp)
leaq 0x1(%r14), %rax
movq %rax, 0x50(%rsp)
cmpq %rcx, %rax
jge 0x10d269
movq 0x18(%rsp), %rax
movq (%rax,%r14,8), %r15
movq 0x40(%rsp), %rdi
movq %r15, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq (%rax,%rdi,8), %rbx
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm2
movsd 0x10(%r15), %xmm1
movsd (%rbx), %xmm3
movsd 0x8(%rbx), %xmm4
movsd 0x10(%rbx), %xmm5
movapd %xmm2, %xmm6
mulsd %xmm5, %xmm6
mulsd %xmm0, %xmm5
mulsd %xmm4, %xmm0
mulsd %xmm1, %xmm4
subsd %xmm4, %xmm6
mulsd %xmm3, %xmm1
subsd %xmm5, %xmm1
mulsd %xmm2, %xmm3
subsd %xmm3, %xmm0
mulsd %xmm1, %xmm1
mulsd %xmm6, %xmm6
addsd %xmm1, %xmm6
mulsd %xmm0, %xmm0
addsd %xmm6, %xmm0
ucomisd 0x46146(%rip), %xmm0    # 0x1532c0
jne 0x10d182
jnp 0x10d25d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
movsd %xmm0, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
movq %rdi, 0x38(%rsp)
cmpq %r13, %r14
sete %al
cmpq %r13, %rdi
sete %cl
orb %al, %cl
jne 0x10d239
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x10c938
movapd %xmm0, %xmm1
ucomisd 0x460ea(%rip), %xmm0    # 0x1532c0
jbe 0x10d200
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movsd %xmm1, 0x60(%rsp)
callq 0x10cd71
movsd 0x60(%rsp), %xmm1
ucomisd 0x28(%rsp), %xmm0
jbe 0x10d200
movsd %xmm0, 0x28(%rsp)
ucomisd 0x460b8(%rip), %xmm1    # 0x1532c0
jae 0x10d22a
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
movq %r12, %rdx
callq 0x10cd71
ucomisd 0x30(%rsp), %xmm0
jbe 0x10d22a
movsd %xmm0, 0x30(%rsp)
movq 0x20(%rsp), %r15
movq 0x10(%rsp), %rbx
movq 0x38(%rsp), %rdi
incq %r13
cmpq %r13, %rbp
jne 0x10d19f
movsd 0x30(%rsp), %xmm1
minsd 0x28(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x10d289
incq %rdi
cmpq %rbp, %rdi
jne 0x10d10e
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %r14
cmpq %rcx, %r14
setl %al
incq 0x40(%rsp)
cmpq %rbp, %r14
jne 0x10d0e5
jmp 0x10d28d
movb 0xf(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x42400(%rip), %rdi     # 0x14f6a5
xorl %eax, %eax
callq 0x4630
leaq 0x423f2(%rip), %rdi     # 0x14f6a5
movq %r12, %rsi
movq %rbp, %rdx
jmp 0x10d2a5
leaq 0x423e3(%rip), %rdi     # 0x14f6a5
xorl %esi, %esi
jmp 0x10d2b6