// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xc8, %rsp
movsd 0xd8(%rsp), %xmm8
movsd 0xd0(%rsp), %xmm8
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movsd %xmm0, 0xa8(%rsp)
movsd %xmm1, 0xa0(%rsp)
movsd %xmm2, 0x98(%rsp)
movsd %xmm3, 0x90(%rsp)
movsd %xmm4, 0x88(%rsp)
movsd %xmm5, 0x80(%rsp)
movsd %xmm6, 0x78(%rsp)
movsd %xmm7, 0x70(%rsp)
movq $0x0, 0x48(%rsp)
movq 0xb0(%rsp), %rax
imulq 0xb0(%rsp), %rax
movq %rax, 0x10(%rsp)
movsd 0xa0(%rsp), %xmm0
subsd 0xa8(%rsp), %xmm0
movq 0xb0(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1f42(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0xa8(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
callq 0x17630
subsd 0xa8(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x40(%rsp)
movsd 0x70(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1ef6(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0xa8(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
callq 0x17630
subsd 0xa8(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x38(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1ea7(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0x90(%rsp), %xmm2
callq 0x17630
subsd 0x98(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x30(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1e58(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x98(%rsp), %xmm1
movsd 0x90(%rsp), %xmm2
callq 0x17630
subsd 0x98(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x28(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1e09(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0x88(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
callq 0x17630
subsd 0x88(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x20(%rsp)
movsd 0xd8(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x1dba(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x88(%rsp), %xmm1
movsd 0x80(%rsp), %xmm2
callq 0x17630
subsd 0x88(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x60(%rsp), %rax
movq 0x38(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a83a
movq 0x30(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
movq 0x28(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a825
movq 0x20(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0x18(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a810
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
movq 0xb8(%rsp), %rax
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %rdx
imulq 0xb0(%rsp), %rdx
addq %rdx, %rcx
movq 0x60(%rsp), %rdx
imulq 0x10(%rsp), %rdx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x1a7a2
jmp 0x1a812
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x1a781
jmp 0x1a827
movq 0x60(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x60(%rsp)
jmp 0x1a760
cmpq $0x0, 0x48(%rsp)
je 0x1a85e
movsd 0x68(%rsp), %xmm0
cvtsi2sdq 0x48(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x1a86a
xorps %xmm0, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
addq $0xc8, %rsp
retq
nopl (%rax,%rax)