// File: /workspace/github_c_makefile_star2_O3/guszejnovdavid[P]MISFIT/SumScalarInRegion.c
// Function: AverageScalar_Sphere(double*, long, double, double, double, double, double, double, double, double, double, double)
double AverageScalar_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;
long i,j,k;
long Counted=0;
long Imin,Imax,Jmin,Jmax,Kmin,Kmax; //indices
double xsq_norm, ysq_norm, zsq_norm; //used in distance calculations
double temp; //temp number

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
// 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=lround((InLimits_double(x0-R, Xmin, Xmax)-Xmin)/d);
Imax=lround((InLimits_double(x0+R, Xmin, Xmax)-Xmin)/d);
              
// Go through all points in subcube
for (i = Imin; i < (Imax+1); i++){
    xsq_norm=(i-x0_norm)*(i-x0_norm);
    temp=sqrt(Rsq-xsq_norm);
	if(temp>0.0){ //check to make sure we don't get negative values
	//Y index
	Jmin=(long)ceil(y0_norm-temp);
	Jmax=(long)floor(y0_norm+temp);
    for (j = Jmin; j < (Jmax+1); j++){
        ysq_norm=(j-y0_norm)*(j-y0_norm);
        temp=sqrt(Rsq-xsq_norm-ysq_norm);
		if(temp>0.0){ //check to make sure we don't get negative values
		//Z index
		Kmin=(long)ceil(z0_norm-temp);
		Kmax=(long)floor(z0_norm+temp);
        for (k = Kmin; k < (Kmax+1); k++){
        	zsq_norm=(k-z0_norm)*(k-z0_norm);
        	if(Rsq>xsq_norm+ysq_norm+zsq_norm){
              				 ++Counted;
                             res+=Field[k+j*N+i*Nsq];
            }
         }
     }//end if
     }
 }//end if
}

if(Counted){        
 return (res/Counted); //return average of scalar field 
}
else{
	return 0;
}
              
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movsd %xmm7, 0x28(%rsp)
movsd %xmm6, 0x8(%rsp)
movsd %xmm4, 0x30(%rsp)
movsd %xmm2, 0x18(%rsp)
movapd %xmm0, %xmm4
movsd %xmm0, (%rsp)
movq %rsi, %rbx
movq %rdi, 0x38(%rsp)
movapd %xmm1, %xmm3
movapd %xmm1, %xmm2
movsd %xmm1, 0x20(%rsp)
leaq -0x1(%rsi), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
subsd %xmm4, %xmm3
divsd %xmm0, %xmm3
movsd %xmm3, 0x10(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
movapd %xmm4, %xmm1
callq 0x8160
subsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x2030
movq %rax, %r14
movsd 0x8(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd (%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq 0x8160
subsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x2030
movq %rax, 0x58(%rsp)
cmpq %rax, %r14
jg 0x9e3c
movsd 0xb8(%rsp), %xmm1
movsd 0xb0(%rsp), %xmm2
movsd 0x8(%rsp), %xmm5
mulsd %xmm5, %xmm5
movsd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
divsd %xmm0, %xmm5
movsd 0x28(%rsp), %xmm0
subsd (%rsp), %xmm0
divsd %xmm3, %xmm0
subsd 0x18(%rsp), %xmm2
divsd %xmm3, %xmm2
movsd %xmm2, 0x18(%rsp)
subsd 0x30(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %rbx, %rax
imulq %rbx, %rax
shlq $0x3, %rax
movq %rax, 0x50(%rsp)
movq %r14, %rax
imulq %rbx, %rax
shlq $0x3, %rax
movq %rax, 0x40(%rsp)
movq %rbx, 0x48(%rsp)
leaq (,%rbx,8), %rbp
xorpd %xmm4, %xmm4
xorl %ebx, %ebx
xorpd %xmm3, %xmm3
movsd %xmm0, 0x28(%rsp)
movapd %xmm5, %xmm1
movsd %xmm5, 0x8(%rsp)
xorps %xmm2, %xmm2
cvtsi2sd %r14, %xmm2
subsd %xmm0, %xmm2
mulsd %xmm2, %xmm2
movapd %xmm1, %xmm0
movsd %xmm2, 0x68(%rsp)
subsd %xmm2, %xmm0
ucomisd %xmm4, %xmm0
movsd %xmm3, (%rsp)
movsd %xmm0, 0x30(%rsp)
jb 0x9c63
xorps %xmm2, %xmm2
sqrtsd %xmm0, %xmm2
jmp 0x9c7b
callq 0x2160
xorpd %xmm4, %xmm4
movsd (%rsp), %xmm3
movsd 0x8(%rsp), %xmm1
movapd %xmm0, %xmm2
movq %r14, 0x60(%rsp)
ucomisd %xmm4, %xmm2
jbe 0x9e05
movsd 0x18(%rsp), %xmm0
subsd %xmm2, %xmm0
movsd %xmm2, 0x20(%rsp)
callq 0x2050
cvttsd2si %xmm0, %r12
movsd 0x20(%rsp), %xmm0
addsd 0x18(%rsp), %xmm0
callq 0x2370
cvttsd2si %xmm0, %r13
cmpq %r13, %r12
jle 0x9cd3
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
jmp 0x9e05
movq 0x40(%rsp), %rax
leaq (%rax,%r12,8), %r14
imulq 0x48(%rsp), %r14
addq 0x38(%rsp), %r14
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
xorps %xmm5, %xmm5
cvtsi2sd %r12, %xmm5
subsd 0x18(%rsp), %xmm5
mulsd %xmm5, %xmm5
movsd 0x30(%rsp), %xmm0
subsd %xmm5, %xmm0
ucomisd %xmm4, %xmm0
movsd %xmm3, (%rsp)
movsd %xmm5, 0x20(%rsp)
jb 0x9d32
xorps %xmm3, %xmm3
sqrtsd %xmm0, %xmm3
jmp 0x9d4b
callq 0x2160
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movapd %xmm0, %xmm3
ucomisd %xmm4, %xmm3
jbe 0x9d9b
movapd %xmm2, %xmm0
subsd %xmm3, %xmm0
movsd %xmm3, 0x70(%rsp)
callq 0x2050
cvttsd2si %xmm0, %r15
movsd 0x70(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
callq 0x2370
cvttsd2si %xmm0, %rax
cmpq %rax, %r15
jle 0x9da2
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
jmp 0x9df1
movsd (%rsp), %xmm3
jmp 0x9df1
movsd 0x20(%rsp), %xmm5
addsd 0x68(%rsp), %xmm5
incq %rax
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
xorps %xmm0, %xmm0
cvtsi2sd %r15, %xmm0
subsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm5, %xmm0
ucomisd %xmm0, %xmm1
jbe 0x9de9
incq %rbx
addsd (%r14,%r15,8), %xmm3
incq %r15
cmpq %r15, %rax
jne 0x9dc6
leaq 0x1(%r12), %rax
addq %rbp, %r14
cmpq %r13, %r12
movq %rax, %r12
jne 0x9cfc
movq 0x60(%rsp), %rcx
leaq 0x1(%rcx), %r14
movq 0x50(%rsp), %rdx
addq %rdx, 0x38(%rsp)
cmpq 0x58(%rsp), %rcx
movsd 0x28(%rsp), %xmm0
jne 0x9c2b
testq %rbx, %rbx
je 0x9e3c
xorps %xmm0, %xmm0
cvtsi2sd %rbx, %xmm0
divsd %xmm0, %xmm3
jmp 0x9e40
xorpd %xmm3, %xmm3
movapd %xmm3, %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq