// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xb8, %rsp
movsd 0xc8(%rsp), %xmm8
movsd 0xc0(%rsp), %xmm8
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movsd %xmm0, 0xa0(%rsp)
movsd %xmm1, 0x98(%rsp)
movsd %xmm2, 0x90(%rsp)
movsd %xmm3, 0x88(%rsp)
movsd %xmm4, 0x80(%rsp)
movsd %xmm5, 0x78(%rsp)
movsd %xmm6, 0x70(%rsp)
movsd %xmm7, 0x68(%rsp)
movq 0xa8(%rsp), %rax
imulq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
movsd 0x98(%rsp), %xmm0
subsd 0xa0(%rsp), %xmm0
movq 0xa8(%rsp), %rax
subq $0x1, %rax
cvtsi2sd %rax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x8(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x22c7(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
callq 0x17630
subsd 0xa0(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x40(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x227b(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0xa0(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
callq 0x17630
subsd 0xa0(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x38(%rsp)
movsd 0xc0(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x222c(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0x90(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
callq 0x17630
subsd 0x90(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x30(%rsp)
movsd 0xc0(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x21dd(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x90(%rsp), %xmm1
movsd 0x88(%rsp), %xmm2
callq 0x17630
subsd 0x90(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x28(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x218e(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
callq 0x17630
subsd 0x80(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x20(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x2142(%rip), %xmm2     # 0x1c4d8
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd 0x80(%rsp), %xmm1
movsd 0x78(%rsp), %xmm2
callq 0x17630
subsd 0x80(%rsp), %xmm0
divsd 0x8(%rsp), %xmm0
callq 0x8040
movq %rax, 0x18(%rsp)
movq 0x40(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
movq 0x38(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a4ae
movq 0x30(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
movq 0x28(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a499
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rax
movq 0x18(%rsp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jge 0x1a484
movq 0xb0(%rsp), %rax
movq 0x48(%rsp), %rcx
movq 0x50(%rsp), %rdx
imulq 0xa8(%rsp), %rdx
addq %rdx, %rcx
movq 0x58(%rsp), %rdx
imulq 0x10(%rsp), %rdx
addq %rdx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd (%rsp), %xmm2
movsd 0x60(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x1a417
jmp 0x1a486
movq 0x50(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x50(%rsp)
jmp 0x1a3f6
jmp 0x1a49b
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x1a3d5
movsd 0x60(%rsp), %xmm0
addq $0xb8, %rsp
retq
nopl (%rax)