// File: /workspace/github_c_makefile_star2_O1/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 0x81d8
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 0x81d8
subsd (%rsp), %xmm0
divsd 0x10(%rsp), %xmm0
callq 0x2030
movq %rax, 0x58(%rsp)
cmpq %rax, %r14
jle 0x9c1d
xorl %r13d, %r13d
xorpd %xmm3, %xmm3
jmp 0x9ec0
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), %rbx
xorpd %xmm4, %xmm4
xorl %r13d, %r13d
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 0x9cfa
xorps %xmm2, %xmm2
sqrtsd %xmm0, %xmm2
jmp 0x9d12
callq 0x2160
xorpd %xmm4, %xmm4
movsd (%rsp), %xmm3
movsd 0x8(%rsp), %xmm1
movapd %xmm0, %xmm2
movq %r14, 0x60(%rsp)
ucomisd %xmm4, %xmm2
jbe 0x9e9c
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, %rbp
cmpq %rbp, %r12
jle 0x9d6a
movsd 0x8(%rsp), %xmm1
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
jmp 0x9e9c
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 0x9dc9
xorps %xmm3, %xmm3
sqrtsd %xmm0, %xmm3
jmp 0x9de2
callq 0x2160
xorpd %xmm4, %xmm4
movsd 0x10(%rsp), %xmm2
movsd 0x8(%rsp), %xmm1
movapd %xmm0, %xmm3
ucomisd %xmm4, %xmm3
jbe 0x9e32
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 0x9e39
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
movsd (%rsp), %xmm3
xorpd %xmm4, %xmm4
jmp 0x9e88
movsd (%rsp), %xmm3
jmp 0x9e88
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 0x9e80
incq %r13
addsd (%r14,%r15,8), %xmm3
incq %r15
cmpq %r15, %rax
jne 0x9e5d
leaq 0x1(%r12), %rax
addq %rbx, %r14
cmpq %rbp, %r12
movq %rax, %r12
jne 0x9d93
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 0x9cc2
testq %r13, %r13
xorpd %xmm0, %xmm0
je 0x9ed9
xorps %xmm0, %xmm0
cvtsi2sd %r13, %xmm0
divsd %xmm0, %xmm3
movapd %xmm3, %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq