// File: /workspace/github_c_makefile_star2_O2/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)
movapd %xmm1, %xmm10
movq %rsi, %rbx
movq %rdi, %r14
movsd 0xb8(%rsp), %xmm9
movsd 0xb0(%rsp), %xmm3
movapd %xmm1, %xmm8
movsd %xmm1, 0x20(%rsp)
leaq -0x1(%rsi), %rax
cvtsi2sd %rax, %xmm1
subsd %xmm0, %xmm8
divsd %xmm1, %xmm8
movsd %xmm8, 0x8(%rsp)
movapd %xmm6, %xmm5
movsd %xmm6, 0x18(%rsp)
mulsd %xmm6, %xmm5
movapd %xmm8, %xmm1
mulsd %xmm8, %xmm1
divsd %xmm1, %xmm5
movsd %xmm5, 0x38(%rsp)
movapd %xmm7, %xmm5
movapd %xmm0, %xmm1
movsd %xmm0, (%rsp)
subsd %xmm0, %xmm5
divsd %xmm8, %xmm5
movsd %xmm5, 0x70(%rsp)
subsd %xmm2, %xmm3
divsd %xmm8, %xmm3
movsd %xmm3, 0x30(%rsp)
subsd %xmm4, %xmm9
divsd %xmm8, %xmm9
movsd %xmm9, 0x10(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
movapd %xmm10, %xmm2
callq 0x7b08
subsd (%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r15
movsd 0x18(%rsp), %xmm0
addsd 0x28(%rsp), %xmm0
movsd (%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
callq 0x7b08
subsd (%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r12
movq %rbx, %r13
imulq %rbx, %r13
shlq $0x3, %r13
movq %r15, %rax
imulq %rbx, %rax
shlq $0x3, %rax
movq %rax, 0x40(%rsp)
movq %rbx, 0x58(%rsp)
leaq (,%rbx,8), %rbx
xorpd %xmm2, %xmm2
xorl %ebp, %ebp
xorpd %xmm3, %xmm3
movq %r12, 0x50(%rsp)
movq %r13, 0x48(%rsp)
cmpq %r12, %r15
jg 0x95d6
cvtsi2sd %r15, %xmm1
subsd 0x70(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x38(%rsp), %xmm0
movsd %xmm1, 0x18(%rsp)
subsd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
movsd %xmm3, (%rsp)
movsd %xmm0, 0x28(%rsp)
jb 0x945e
sqrtsd %xmm0, %xmm1
jmp 0x946b
callq 0x2140
xorpd %xmm2, %xmm2
movapd %xmm0, %xmm1
ucomisd %xmm2, %xmm1
jbe 0x95b0
movq %r15, 0x60(%rsp)
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm1, 0x8(%rsp)
callq 0x2050
cvttsd2si %xmm0, %r12
movsd 0x8(%rsp), %xmm0
addsd 0x30(%rsp), %xmm0
callq 0x2330
xorpd %xmm2, %xmm2
cvttsd2si %xmm0, %r13
movq %r14, %rax
movq 0x40(%rsp), %rcx
leaq (%rcx,%r12,8), %r14
imulq 0x58(%rsp), %r14
movq %rax, 0x68(%rsp)
addq %rax, %r14
movsd 0x10(%rsp), %xmm1
movsd (%rsp), %xmm3
cmpq %r13, %r12
jg 0x95b7
cvtsi2sd %r12, %xmm4
subsd 0x30(%rsp), %xmm4
mulsd %xmm4, %xmm4
movsd 0x28(%rsp), %xmm0
subsd %xmm4, %xmm0
ucomisd %xmm2, %xmm0
movsd %xmm3, (%rsp)
movsd %xmm4, 0x8(%rsp)
jb 0x950c
sqrtsd %xmm0, %xmm3
jmp 0x951f
callq 0x2140
xorpd %xmm2, %xmm2
movsd 0x10(%rsp), %xmm1
movapd %xmm0, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x959a
movapd %xmm1, %xmm0
subsd %xmm3, %xmm0
movsd %xmm3, 0x20(%rsp)
callq 0x2050
cvttsd2si %xmm0, %r15
movsd 0x20(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
callq 0x2330
movsd 0x10(%rsp), %xmm1
cvttsd2si %xmm0, %rax
movsd 0x8(%rsp), %xmm4
addsd 0x18(%rsp), %xmm4
movsd 0x38(%rsp), %xmm2
movsd (%rsp), %xmm3
cmpq %rax, %r15
jg 0x95a1
cvtsi2sd %r15, %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd %xmm4, %xmm0
ucomisd %xmm0, %xmm2
jbe 0x9595
incq %rbp
addsd (%r14,%r15,8), %xmm3
incq %r15
jmp 0x9570
movsd (%rsp), %xmm3
jmp 0x95a5
xorpd %xmm2, %xmm2
incq %r12
addq %rbx, %r14
jmp 0x94d3
movsd (%rsp), %xmm3
jmp 0x95cb
movq 0x68(%rsp), %r14
movq 0x60(%rsp), %r15
movq 0x50(%rsp), %r12
movq 0x48(%rsp), %r13
incq %r15
addq %r13, %r14
jmp 0x941f
xorpd %xmm0, %xmm0
testq %rbp, %rbp
je 0x95ec
cvtsi2sd %rbp, %xmm0
divsd %xmm0, %xmm3
movapd %xmm3, %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq