// File: /workspace/github_cpp_makefile_star2_O3/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), %r12
movq %r12, %rax
subq %rbx, %rax
sarq $0x3, %rax
movabsq $-0x5555555555555555, %r14 # imm = 0xAAAAAAAAAAAAAAAB
imulq %rax, %r14
movslq %r14d, %rbp
leaq -0x8(,%rbp,8), %rdi
callq 0x4580
movq %rax, 0x8(%rsp)
movl $0x18, %edi
callq 0x4580
cmpq %rbx, %r12
je 0x10bf3a
movq %rax, %r15
decq %rbp
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
subq %rax, %rdx
sarq $0x3, %rdx
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x10bf1a
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, (%r15,%rcx,8)
incq %rcx
cmpq $0x3, %rcx
jne 0x10bca0
cmpl $0x2, %r14d
jl 0x10bf01
movl %r14d, %r12d
andl $0x7fffffff, %r12d      # imm = 0x7FFFFFFF
movl $0x1, %r13d
movl $0x18, %edi
callq 0x4580
movq 0x8(%rsp), %rcx
movq %rax, -0x8(%rcx,%r13,8)
cmpq %r14, %r13
je 0x10bf2b
leaq (,%r13,2), %rdx
addq %r13, %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 0x10bf1a
movsd (%rcx,%rsi,8), %xmm0
subsd (%r15,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq $0x3, %rsi
jne 0x10bd11
incq %r13
cmpq %r12, %r13
jne 0x10bcd7
cmpl $0x2, %r14d
jl 0x10bf01
movq %rbp, 0x48(%rsp)
movl %ebp, %ebp
movb $0x1, %al
movl $0x1, %ecx
movq %rcx, 0x40(%rsp)
xorl %r14d, %r14d
movb %al, 0x7(%rsp)
leaq 0x1(%r14), %rax
movq %rax, 0x50(%rsp)
cmpq 0x48(%rsp), %rax
setl %al
movl %eax, 0x3c(%rsp)
jge 0x10bee8
movq 0x8(%rsp), %rax
movq (%rax,%r14,8), %r15
movq 0x40(%rsp), %rdi
movq %r15, 0x10(%rsp)
movq 0x8(%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 0x45557(%rip), %xmm0    # 0x151350
jne 0x10be01
jnp 0x10bedc
xorpd %xmm0, %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %r13d, %r13d
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x18(%rsp)
movq %rdi, 0x30(%rsp)
cmpq %r13, %r14
sete %al
cmpq %r13, %rdi
sete %cl
orb %al, %cl
jne 0x10beb8
movq 0x8(%rsp), %rax
movq (%rax,%r13,8), %r12
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x10b637
movapd %xmm0, %xmm1
ucomisd 0x454fb(%rip), %xmm0    # 0x151350
jbe 0x10be7f
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
movsd %xmm1, 0x60(%rsp)
callq 0x10ba5a
movsd 0x60(%rsp), %xmm1
ucomisd 0x20(%rsp), %xmm0
jbe 0x10be7f
movsd %xmm0, 0x20(%rsp)
ucomisd 0x454c9(%rip), %xmm1    # 0x151350
jae 0x10bea9
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %r12, %rdx
callq 0x10ba5a
ucomisd 0x28(%rsp), %xmm0
jbe 0x10bea9
movsd %xmm0, 0x28(%rsp)
movq 0x10(%rsp), %r15
movq 0x18(%rsp), %rbx
movq 0x30(%rsp), %rdi
incq %r13
cmpq %r13, %rbp
jne 0x10be1e
movsd 0x28(%rsp), %xmm1
minsd 0x20(%rsp), %xmm1
movsd 0x58(%rsp), %xmm0
ucomisd %xmm1, %xmm0
ja 0x10bf05
incq %rdi
cmpq %rbp, %rdi
jne 0x10bd8d
incq 0x40(%rsp)
movq 0x50(%rsp), %r14
cmpq %rbp, %r14
movl 0x3c(%rsp), %eax
jne 0x10bd5b
jmp 0x10bf09
xorl %eax, %eax
jmp 0x10bf09
movb 0x7(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x417d4(%rip), %rdi     # 0x14d6f5
movq %rdx, %rsi
xorl %eax, %eax
callq 0x4630
leaq 0x417c3(%rip), %rdi     # 0x14d6f5
movq %r14, %rsi
movq %r14, %rdx
jmp 0x10bf24
leaq 0x417b4(%rip), %rdi     # 0x14d6f5
xorl %esi, %esi
jmp 0x10bf35