// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movsd %xmm7, 0x48(%rsp)
movsd %xmm5, 0x38(%rsp)
movsd %xmm4, 0x20(%rsp)
movsd %xmm3, 0x30(%rsp)
movsd %xmm2, 0x18(%rsp)
movapd %xmm0, %xmm4
movsd %xmm0, 0x28(%rsp)
movq %rsi, %rbx
movq %rdi, 0x50(%rsp)
movapd %xmm1, %xmm3
movapd %xmm1, %xmm2
movsd %xmm1, 0x40(%rsp)
subsd %xmm0, %xmm3
leaq -0x1(%rsi), %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm3
movsd %xmm3, 0x8(%rsp)
mulsd 0x1b99(%rip), %xmm6     # 0xb418
movsd %xmm6, 0x10(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
movapd %xmm4, %xmm1
callq 0x7b08
subsd 0x28(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r14
movsd 0x48(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x40(%rsp), %xmm2
callq 0x7b08
subsd 0x28(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r15
movsd 0x90(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x7b08
subsd 0x18(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r12
movsd 0x90(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x7b08
subsd 0x18(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %rbp
movsd 0x98(%rsp), %xmm0
subsd 0x10(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
callq 0x7b08
subsd 0x20(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %rax, %r13
movsd 0x10(%rsp), %xmm0
addsd 0x98(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
callq 0x7b08
subsd 0x20(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x2030
movq %r14, %rcx
imulq %rbx, %rcx
movq %r12, %rdx
shlq $0x3, %rdx
leaq (%rdx,%rcx,8), %rcx
imulq %rbx, %rcx
addq 0x50(%rsp), %rcx
movq %rbx, %rsi
imulq %rbx, %rsi
shlq $0x3, %rsi
shlq $0x3, %rbx
xorpd %xmm1, %xmm1
xorl %edx, %edx
cmpq %r15, %r14
jg 0x9a0e
movq %rcx, %rdi
movq %r12, %r8
cmpq %rbp, %r8
jg 0x9a06
movq %r13, %r9
cmpq %rax, %r9
jg 0x99fe
incq %rdx
addsd (%rdi,%r9,8), %xmm1
incq %r9
jmp 0x99eb
incq %r8
addq %rbx, %rdi
jmp 0x99e3
incq %r14
addq %rsi, %rcx
jmp 0x99d8
xorpd %xmm0, %xmm0
testq %rdx, %rdx
je 0x9a24
cvtsi2sd %rdx, %xmm0
divsd %xmm0, %xmm1
movapd %xmm1, %xmm0
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop