// File: /workspace/github_c_makefile_star2_O2/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 $0x58, %rsp
movsd %xmm7, 0x40(%rsp)
movsd %xmm5, 0x30(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm3, 0x28(%rsp)
movsd %xmm2, 0x10(%rsp)
movq %rsi, %rbx
movq %rdi, 0x50(%rsp)
movapd %xmm1, %xmm3
movapd %xmm1, %xmm2
movsd %xmm1, 0x38(%rsp)
subsd %xmm0, %xmm3
movapd %xmm0, %xmm1
movsd %xmm0, 0x20(%rsp)
leaq -0x1(%rsi), %rax
cvtsi2sd %rax, %xmm0
divsd %xmm0, %xmm3
movsd %xmm3, (%rsp)
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
movsd %xmm0, 0x48(%rsp)
mulsd 0x1da1(%rip), %xmm6     # 0xb418
movsd %xmm6, 0x8(%rsp)
movapd %xmm7, %xmm0
subsd %xmm6, %xmm0
callq 0x7b08
subsd 0x20(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r14
movsd 0x40(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
callq 0x7b08
subsd 0x20(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r15
movsd 0x90(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
callq 0x7b08
subsd 0x10(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r13
movsd 0x90(%rsp), %xmm0
addsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
callq 0x7b08
subsd 0x10(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %rbp
movsd 0x98(%rsp), %xmm0
subsd 0x8(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x7b08
subsd 0x18(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movq %rax, %r12
movsd 0x8(%rsp), %xmm0
addsd 0x98(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x7b08
subsd 0x18(%rsp), %xmm0
divsd (%rsp), %xmm0
callq 0x2030
movsd 0x48(%rsp), %xmm2
movq %r14, %rcx
imulq %rbx, %rcx
movq %r13, %rdx
shlq $0x3, %rdx
leaq (%rdx,%rcx,8), %rcx
imulq %rbx, %rcx
addq 0x50(%rsp), %rcx
movq %rbx, %rdx
imulq %rbx, %rdx
shlq $0x3, %rdx
shlq $0x3, %rbx
xorpd %xmm0, %xmm0
cmpq %r15, %r14
jg 0x9805
movq %rcx, %rsi
movq %r13, %rdi
cmpq %rbp, %rdi
jg 0x97fd
movq %r12, %r8
cmpq %rax, %r8
jg 0x97f5
movsd (%rsi,%r8,8), %xmm1
mulsd %xmm2, %xmm1
addsd %xmm1, %xmm0
incq %r8
jmp 0x97dd
incq %rdi
addq %rbx, %rsi
jmp 0x97d5
incq %r14
addq %rdx, %rcx
jmp 0x97ca
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq