// File: /workspace/github_c_makefile_star2_O3/guszejnovdavid[P]MISFIT/SumScalarInRegion.c
// Function: AverageScalar_Slab(double*, long, double, double, double, double, double, double, double, double, double, double)
double AverageScalar_Slab(
       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 a, double x0, double y0, double z0) //parameters of cube
{
double res;
long i,j,k;
long Counted=0;
long Imin,Imax,Jmin,Jmax,Kmin,Kmax; //indices

const long Nsq=N*N; //N^2 used for indexing
const double d=(Xmax-Xmin)/(double)(N-1); //spacing

res=0; //final result


//Get smaller cube where the points can be
  //X index
Imin=lround((InLimits_double(x0-a/2.0, Xmin, Xmax)-Xmin)/d);
Imax=lround((InLimits_double(x0+a/2.0, Xmin, Xmax)-Xmin)/d);
  //Y index
Jmin=lround((InLimits_double(y0-a/2.0, Ymin, Ymax)-Ymin)/d);
Jmax=lround((InLimits_double(y0+a/2.0, Ymin, Ymax)-Ymin)/d);
  //Z index
Kmin=lround((InLimits_double(z0-a/2.0, Zmin, Zmax)-Zmin)/d);
Kmax=lround((InLimits_double(z0+a/2.0, Zmin, Zmax)-Zmin)/d);
              
// Go through all points in subcube
for (i = Imin; i < (Imax+1); i++){
    for (j = Jmin; j < (Jmax+1); j++){
        for (k = Kmin; k < (Kmax+1); k++){
        	 Counted++;
             res+=Field[k+j*N+i*Nsq];
         }
     }
}
              
if(Counted){        
 return (res/(double)Counted); //return average of scalar field 
}
else{
	return 0;
}           
              
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movsd %xmm7, 0x40(%rsp)
movsd %xmm5, 0x30(%rsp)
movsd %xmm4, 0x20(%rsp)
movsd %xmm3, 0x28(%rsp)
movsd %xmm2, 0x18(%rsp)
movapd %xmm0, %xmm4
movsd %xmm0, 0x8(%rsp)
movq %rsi, %rbx
movq %rdi, %r14
movapd %xmm1, %xmm3
movapd %xmm1, %xmm2
movsd %xmm1, 0x38(%rsp)
subsd %xmm0, %xmm3
leaq -0x1(%rsi), %rax
xorps %xmm0, %xmm0
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm3
movsd %xmm3, (%rsp)
mulsd 0x1330(%rip), %xmm6     # 0xb428
movsd %xmm6, 0x10(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
movapd %xmm4, %xmm1
callq 0x8160
subsd 0x8(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r15
movsd 0x40(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
callq 0x8160
subsd 0x8(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, 0x8(%rsp)
movsd 0x80(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
callq 0x8160
subsd 0x18(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r13
movsd 0x80(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
callq 0x8160
subsd 0x18(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %rbp
movsd 0x88(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x8160
subsd 0x20(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r12
movsd 0x10(%rsp), %xmm0
addsd 0x88(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x8160
subsd 0x20(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq 0x8(%rsp), %r10
cmpq %r10, %r15
jg 0xa2bc
movq %rax, %rcx
subq %r12, %rcx
incq %rcx
movq %r15, %rdx
imulq %rbx, %rdx
leaq (,%r13,8), %rsi
leaq (%rsi,%rdx,8), %rdx
imulq %rbx, %rdx
leaq (%rdx,%r12,8), %rdx
addq %rdx, %r14
movq %rbx, %rdx
imulq %rbx, %rdx
shlq $0x3, %rdx
shlq $0x3, %rbx
xorpd %xmm0, %xmm0
xorl %esi, %esi
cmpq %rbp, %r13
jg 0xa29a
movq %r14, %rdi
movq %r13, %r8
cmpq %rax, %r12
jg 0xa28b
xorl %r9d, %r9d
addsd (%rdi,%r9,8), %xmm0
incq %r9
cmpq %r9, %rcx
jne 0xa27a
addq %r9, %rsi
leaq 0x1(%r8), %r9
addq %rbx, %rdi
cmpq %rbp, %r8
movq %r9, %r8
jne 0xa272
leaq 0x1(%r15), %rdi
addq %rdx, %r14
cmpq %r10, %r15
movq %rdi, %r15
jne 0xa267
testq %rsi, %rsi
je 0xa2bc
xorps %xmm1, %xmm1
cvtsi2sd %rsi, %xmm1
divsd %xmm1, %xmm0
jmp 0xa2c0
xorpd %xmm0, %xmm0
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop