// File: /workspace/github_c_makefile_star2_O1/guszejnovdavid[P]MISFIT/SumScalarInRegion.c
// Function: SumScalar_Slab(double*, long, double, double, double, double, double, double, double, double, double, double)
double SumScalar_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 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
const double dV=d*d*d; //volume element, assuming cubic lattice

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++){
             res+=Field[k+j*N+i*Nsq]*dV;
         }
     }
}
              
 return res; //return sum of scalar field             
              
}

// 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 0x14d5(%rip), %xmm6     # 0xb428
movsd %xmm6, 0x10(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
movapd %xmm4, %xmm1
callq 0x81d8
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 0x81d8
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 0x81d8
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 0x81d8
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 0x81d8
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 0x81d8
subsd 0x20(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq 0x8(%rsp), %r9
xorpd %xmm0, %xmm0
cmpq %r9, %r15
jg 0xa113
movsd (%rsp), %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
leaq 0x1(%rax), %rcx
movq %r15, %rdx
imulq %rbx, %rdx
leaq (,%r13,8), %rsi
leaq (%rsi,%rdx,8), %rdx
imulq %rbx, %rdx
addq %rdx, %r14
movq %rbx, %rdx
imulq %rbx, %rdx
shlq $0x3, %rdx
shlq $0x3, %rbx
xorpd %xmm0, %xmm0
cmpq %rbp, %r13
jg 0xa104
movq %r14, %rsi
movq %r13, %rdi
cmpq %rax, %r12
jg 0xa0f5
movq %r12, %r8
movsd (%rsi,%r8,8), %xmm2
mulsd %xmm1, %xmm2
addsd %xmm2, %xmm0
incq %r8
cmpq %r8, %rcx
jne 0xa0df
leaq 0x1(%rdi), %r8
addq %rbx, %rsi
cmpq %rbp, %rdi
movq %r8, %rdi
jne 0xa0d7
leaq 0x1(%r15), %rsi
addq %rdx, %r14
cmpq %r9, %r15
movq %rsi, %r15
jne 0xa0cc
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq