// File: /workspace/github_c_makefile_star2_O0/guszejnovdavid[P]MISFIT/SumScalarInRegion.c
// Function: SumScalar_Sphere(double*, long, double, double, double, double, double, double, double, double, double, double)
double SumScalar_Sphere(
       double* Field, long N, //field array and length of one dimension
       double Xmin, double Xmax, double Ymin, double Ymax, double Zmin, double Zmax, // limits of grid
       double R, double x0, double y0, double z0) //parameters of sphere
{
double res,temp;
long i,j,k;
long Imin,Imax,Jmin,Jmax,Kmin,Kmax; //indices
double xsq_norm, ysq_norm/*, zsq_norm*/ ; //used in distance calculations
double rsq; //radial distance square of point to origin normalized to d
double contribfactor; //fraction of volume element within the sphere, <1.0 for boundary points
double reducedr; // (R-(r-d/2))/d, used for getting contribfactor

const long Nsq=N*N; //N^2 used for indexing
const double d=(Xmax-Xmin)/(double)(N-1); //spacing
//const double Rsq=R*R/(d*d); //radius squared renormalized to grid
const double dV=d*d*d; //volume element, assuming cubic lattice
const double Rprime=R+d/2.0; //actual radius within which points contribute
const double Rprimesq=Rprime*Rprime/(d*d); //modified radius squared renormalized to grid
const double Rfull=R-d/2.0; //radius within which points contribute at 100%
const double Rfullsq=Rfull*Rfull/(d*d); //modified radius squared renormalized to grid
// Origin coordinates
const double x0_norm=(x0-Xmin)/d;
const double y0_norm=(y0-Ymin)/d;
const double z0_norm=(z0-Zmin)/d;

res=0; //final result

//Get smaller cube where the points can be
  //X index
Imin=(long)Max_2_double(floor((InLimits_double(x0-Rprime, Xmin, Xmax)-Xmin)/d), 0.0);
Imax=(long)Min_2_double(ceil((InLimits_double(x0+Rprime, Xmin, Xmax)-Xmin)/d),(double)(N-1));

 //Go through all points in subcube
for (i = Imin; i <=Imax; i++){
    xsq_norm=(i-x0_norm)*(i-x0_norm);
    temp=sqrt(Rprimesq-xsq_norm);
	if(temp>0){ //check to make sure we don't get negative values
	//Y index
	Jmin=(long)Max_2_double(floor(y0_norm-temp), 0.0);
	Jmax=(long)Min_2_double(ceil(y0_norm+temp), (double)(N-1));
    for (j = Jmin; j <=Jmax; j++){
        ysq_norm=(j-y0_norm)*(j-y0_norm);
        temp=sqrt(Rprimesq-xsq_norm-ysq_norm);
		if(temp>0){ //check to make sure we don't get negative values
		//Z index
		Kmin=(long)Max_2_double(floor(z0_norm-temp), 0.0);
		Kmax=(long)Min_2_double(ceil(z0_norm+temp), (double)(N-1));
        for (k = Kmin; k <=Kmax; k++){
        	//zsq_norm=(k-z0_norm)*(k-z0_norm);
        	rsq=xsq_norm+ysq_norm+(k-z0_norm)*(k-z0_norm);
        	if(Rfullsq>rsq){ //close enough to contribute 100%
        		res+=Field[k+j*N+i*Nsq];
			}
			else{
				if(Rprimesq>rsq){ //close enough to contribute but not 100%
					reducedr=R/d-sqrt(rsq)+0.5; // (R-(r-d/2))/d used for getting contribfactor, between 0 and 1
					contribfactor=pow(reducedr,3.0)*(10.0-15.0*reducedr+6.0*reducedr*reducedr); //10x^3+6x^5-15x^4
        			res+=contribfactor*Field[k+j*N+i*Nsq];
				}	
			}
         }
     }//end if
     }
 }//end if
}

              
 return (res*dV); //return sum of scalar field             
              
}

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movsd 0x138(%rsp), %xmm8
movsd 0x130(%rsp), %xmm8
movq %rdi, 0x120(%rsp)
movq %rsi, 0x118(%rsp)
movsd %xmm0, 0x110(%rsp)
movsd %xmm1, 0x108(%rsp)
movsd %xmm2, 0x100(%rsp)
movsd %xmm3, 0xf8(%rsp)
movsd %xmm4, 0xf0(%rsp)
movsd %xmm5, 0xe8(%rsp)
movsd %xmm6, 0xe0(%rsp)
movsd %xmm7, 0xd8(%rsp)
movq 0x118(%rsp), %rax
imulq 0x118(%rsp), %rax
movq %rax, 0x50(%rsp)
movsd 0x108(%rsp), %xmm0
subsd 0x110(%rsp), %xmm0
movq 0x118(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
mulsd 0x48(%rsp), %xmm0
mulsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0xe0(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x2de3(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
mulsd 0x38(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0xe0(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
movsd 0x2d9c(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
mulsd 0x28(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
mulsd 0x48(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0xd8(%rsp), %xmm0
subsd 0x110(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x130(%rsp), %xmm0
subsd 0x100(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x138(%rsp), %xmm0
subsd 0xf0(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0xd8(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
movsd 0x110(%rsp), %xmm1
movsd 0x108(%rsp), %xmm2
callq 0x17630
subsd 0x110(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x8b50
xorps %xmm1, %xmm1
callq 0x17570
cvttsd2si %xmm0, %rax
movq %rax, 0xa8(%rsp)
movsd 0xd8(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd 0x110(%rsp), %xmm1
movsd 0x108(%rsp), %xmm2
callq 0x17630
subsd 0x110(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
callq 0x80f0
movq 0x118(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
callq 0x17530
cvttsd2si %xmm0, %rax
movq %rax, 0xa0(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jg 0x19c5a
cvtsi2sdq 0xc0(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
cvtsi2sdq 0xc0(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x30(%rsp), %xmm0
subsd 0x78(%rsp), %xmm0
callq 0x84b0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x19c3f
movsd 0x10(%rsp), %xmm0
subsd 0xc8(%rsp), %xmm0
callq 0x8b50
xorps %xmm1, %xmm1
callq 0x17570
cvttsd2si %xmm0, %rax
movq %rax, 0x98(%rsp)
movsd 0x10(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
callq 0x80f0
movq 0x118(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
callq 0x17530
cvttsd2si %xmm0, %rax
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
cmpq 0x90(%rsp), %rax
jg 0x19c3d
cvtsi2sdq 0xb8(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
cvtsi2sdq 0xb8(%rsp), %xmm1
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x30(%rsp), %xmm0
subsd 0x78(%rsp), %xmm0
subsd 0x70(%rsp), %xmm0
callq 0x84b0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x19c22
movsd 0x8(%rsp), %xmm0
subsd 0xc8(%rsp), %xmm0
callq 0x8b50
xorps %xmm1, %xmm1
callq 0x17570
cvttsd2si %xmm0, %rax
movq %rax, 0x88(%rsp)
movsd 0x8(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
callq 0x80f0
movq 0x118(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
callq 0x17530
cvttsd2si %xmm0, %rax
movq %rax, 0x80(%rsp)
movq 0x88(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0x80(%rsp), %rax
jg 0x19c20
movsd 0x78(%rsp), %xmm1
addsd 0x70(%rsp), %xmm1
cvtsi2sdq 0xb0(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
cvtsi2sdq 0xb0(%rsp), %xmm2
subsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x20(%rsp), %xmm0
ucomisd 0x68(%rsp), %xmm0
jbe 0x19b03
movq 0x120(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0xb8(%rsp), %rdx
imulq 0x118(%rsp), %rdx
addq %rdx, %rcx
movq 0xc0(%rsp), %rdx
imulq 0x50(%rsp), %rdx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
jmp 0x19c05
movsd 0x30(%rsp), %xmm0
ucomisd 0x68(%rsp), %xmm0
jbe 0x19c03
movsd 0xe0(%rsp), %xmm0
divsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rsp)
movsd 0x68(%rsp), %xmm0
callq 0x84b0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0x29a8(%rip), %xmm1     # 0x1c4f0
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
movsd 0x2998(%rip), %xmm1     # 0x1c4f8
callq 0x8bc0
movsd 0x58(%rsp), %xmm2
movsd 0x2ded(%rip), %xmm1     # 0x1c960
mulsd %xmm1, %xmm2
movsd 0x29c9(%rip), %xmm1     # 0x1c548
addsd %xmm1, %xmm2
movsd 0x2dcd(%rip), %xmm1     # 0x1c958
mulsd 0x58(%rsp), %xmm1
movsd 0x58(%rsp), %xmm3
mulsd %xmm3, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movq 0x120(%rsp), %rax
movq 0xb0(%rsp), %rcx
movq 0xb8(%rsp), %rdx
imulq 0x118(%rsp), %rdx
addq %rdx, %rcx
movq 0xc0(%rsp), %rdx
imulq 0x50(%rsp), %rdx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0xd0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
jmp 0x19c05
jmp 0x19c07
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x19a54
jmp 0x19c22
jmp 0x19c24
movq 0xb8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb8(%rsp)
jmp 0x1996e
jmp 0x19c3f
jmp 0x19c41
movq 0xc0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xc0(%rsp)
jmp 0x1988e
movsd 0xd0(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
addq $0x128, %rsp            # imm = 0x128
retq
nopw %cs:(%rax,%rax)