// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x108, %rsp            # imm = 0x108
movsd 0x118(%rsp), %xmm8
movsd 0x110(%rsp), %xmm8
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movsd %xmm0, 0xe8(%rsp)
movsd %xmm1, 0xe0(%rsp)
movsd %xmm2, 0xd8(%rsp)
movsd %xmm3, 0xd0(%rsp)
movsd %xmm4, 0xc8(%rsp)
movsd %xmm5, 0xc0(%rsp)
movsd %xmm6, 0xb8(%rsp)
movsd %xmm7, 0xb0(%rsp)
movq $0x0, 0x88(%rsp)
movq 0xf0(%rsp), %rax
imulq 0xf0(%rsp), %rax
movq %rax, 0x30(%rsp)
movsd 0xe0(%rsp), %xmm0
subsd 0xe8(%rsp), %xmm0
movq 0xf0(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0xb8(%rsp), %xmm0
mulsd 0xb8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
mulsd 0x28(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0xb0(%rsp), %xmm0
subsd 0xe8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x110(%rsp), %xmm0
subsd 0xd8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x118(%rsp), %xmm0
subsd 0xc8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xb0(%rsp), %xmm0
subsd 0xb8(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm2
callq 0x17630
subsd 0xe8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
callq 0x8040
movq %rax, 0x80(%rsp)
movsd 0xb0(%rsp), %xmm0
addsd 0xb8(%rsp), %xmm0
movsd 0xe8(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm2
callq 0x17630
subsd 0xe8(%rsp), %xmm0
divsd 0x28(%rsp), %xmm0
callq 0x8040
movq %rax, 0x78(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq 0x78(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a0e6
cvtsi2sdq 0xa0(%rsp), %xmm0
subsd 0x18(%rsp), %xmm0
cvtsi2sdq 0xa0(%rsp), %xmm1
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x20(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
callq 0x84b0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a0cb
movsd 0x10(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
callq 0x80f0
cvttsd2si %xmm0, %rax
movq %rax, 0x70(%rsp)
movsd 0x10(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
callq 0x8b50
cvttsd2si %xmm0, %rax
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movq 0x68(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a0c9
cvtsi2sdq 0x98(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
cvtsi2sdq 0x98(%rsp), %xmm1
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x20(%rsp), %xmm0
subsd 0x50(%rsp), %xmm0
subsd 0x48(%rsp), %xmm0
callq 0x84b0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a0ae
movsd 0x8(%rsp), %xmm0
subsd 0x38(%rsp), %xmm0
callq 0x80f0
cvttsd2si %xmm0, %rax
movq %rax, 0x60(%rsp)
movsd 0x8(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
callq 0x8b50
cvttsd2si %xmm0, %rax
movq %rax, 0x58(%rsp)
movq 0x60(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq 0x58(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a0ac
cvtsi2sdq 0x90(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
cvtsi2sdq 0x90(%rsp), %xmm1
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x20(%rsp), %xmm0
movsd 0x50(%rsp), %xmm1
addsd 0x48(%rsp), %xmm1
addsd 0x40(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1a091
movq 0x88(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x88(%rsp)
movq 0xf8(%rsp), %rax
movq 0x90(%rsp), %rcx
movq 0x98(%rsp), %rdx
imulq 0xf0(%rsp), %rdx
addq %rdx, %rcx
movq 0xa0(%rsp), %rdx
imulq 0x30(%rsp), %rdx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
jmp 0x1a093
movq 0x90(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x90(%rsp)
jmp 0x19fcf
jmp 0x1a0ae
jmp 0x1a0b0
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x19f18
jmp 0x1a0cb
jmp 0x1a0cd
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x19e67
cmpq $0x0, 0x88(%rsp)
je 0x1a113
movsd 0xa8(%rsp), %xmm0
cvtsi2sdq 0x88(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
jmp 0x1a11f
xorps %xmm0, %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0x100(%rsp), %xmm0
addq $0x108, %rsp            # imm = 0x108
retq