// File: /workspace/github_cpp_makefile_star2_O3/Luthaf[P]zeopp/grid.cc
// Function: calculate_distance_function(ATOM_NETWORK*, int, int, int, double, double, double, double, double, double, int, int, int, char)
double calculate_distance_function(ATOM_NETWORK *network, int i, int j, int k, double minX, double minY, double minZ, double xGridRes, double yGridRes, double zGridRes, int x_grid_steps, int y_grid_steps, int z_grid_steps, char gridtype) {

  double xPosition = minX + xGridRes*(i);
  double yPosition = minY + yGridRes*(j);
  double zPosition = minZ + zGridRes*(k);
  double dist;

  Point fractionalCoord = network->xyz_to_abc(xPosition, yPosition, zPosition);

  bool inStructure = (fractionalCoord[0]>=-0.01 && fractionalCoord[0]<=1.01 && fractionalCoord[1]>=-0.01 && fractionalCoord[1]<=1.01 && fractionalCoord[2]>=-0.01 && fractionalCoord[2]<=1.01);
  if (!inStructure) dist = 0;
  else {
    double minAtomDist = 1000;
    for (int i=0; i<network->numAtoms; i++){
      double atmDist = network->calcDistanceXYZ(xPosition, yPosition, zPosition, network->atoms[i].x, network->atoms[i].y, network->atoms[i].z);
      double radius = network->atoms[i].radius;
      if (gridtype=='f') atmDist -= radius;
      else{
        atmDist = atmDist*atmDist - radius*radius ;
        if (gridtype=='h') atmDist = sqrt(atmDist + 1.35*1.35)-1.35;
      }
      minAtomDist = min(minAtomDist, atmDist);
    }
    dist = minAtomDist;
  }
  return dist;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x38, %rsp
movq %rdi, %rbx
cvtsi2sd %esi, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm6, %xmm0
xorps %xmm3, %xmm3
cvtsi2sd %edx, %xmm3
mulsd %xmm4, %xmm3
addsd %xmm3, %xmm1
xorps %xmm3, %xmm3
cvtsi2sd %ecx, %xmm3
mulsd %xmm5, %xmm3
addsd %xmm3, %xmm2
leaq 0x20(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movsd %xmm0, 0x8(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm2, 0x18(%rsp)
callq 0x52eec
movq %r14, %rdi
xorl %esi, %esi
callq 0x83f9a
movsd (%rax), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0xce368(%rip), %xmm1    # 0x1557d8
jb 0x874e7
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x83f9a
movsd 0xce35a(%rip), %xmm0    # 0x1557e0
ucomisd (%rax), %xmm0
jb 0x874e3
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x83f9a
movsd (%rax), %xmm0
ucomisd 0xce331(%rip), %xmm0    # 0x1557d8
jb 0x874e3
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x83f9a
movsd 0xce320(%rip), %xmm0    # 0x1557e0
ucomisd (%rax), %xmm0
jb 0x874e3
leaq 0x20(%rsp), %rdi
movl $0x2, %esi
callq 0x83f9a
movsd (%rax), %xmm0
ucomisd 0xce2f7(%rip), %xmm0    # 0x1557d8
jae 0x874f2
xorpd %xmm0, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
movl $0x2, %esi
callq 0x83f9a
movsd 0xce2d7(%rip), %xmm0    # 0x1557e0
ucomisd (%rax), %xmm0
xorpd %xmm0, %xmm0
jb 0x874e7
cmpl $0x0, 0x110(%rbx)
jle 0x875f1
movb 0x68(%rsp), %bpl
movsd 0xcb27b(%rip), %xmm1    # 0x1527a8
xorl %r14d, %r14d
xorl %r15d, %r15d
movsd %xmm1, (%rsp)
movq 0x2f0(%rbx), %rax
movsd (%rax,%r14), %xmm3
movsd 0x8(%rax,%r14), %xmm4
movsd 0x10(%rax,%r14), %xmm5
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
callq 0x5364c
movq 0x2f0(%rbx), %rax
movsd 0x30(%rax,%r14), %xmm1
cmpb $0x66, %bpl
jne 0x87587
subsd %xmm1, %xmm0
jmp 0x875b1
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
cmpb $0x68, %bpl
jne 0x875b1
addsd 0xce247(%rip), %xmm0    # 0x1557e8
ucomisd 0xc9da7(%rip), %xmm0    # 0x151350
jb 0x875b8
sqrtsd %xmm0, %xmm0
jmp 0x875bd
movsd (%rsp), %xmm1
jmp 0x875ca
callq 0x4350
movsd (%rsp), %xmm1
addsd 0xce226(%rip), %xmm0    # 0x1557f0
minsd %xmm1, %xmm0
incq %r15
movslq 0x110(%rbx), %rax
addq $0x98, %r14
movapd %xmm0, %xmm1
cmpq %rax, %r15
jl 0x87533
jmp 0x874e7
movsd 0xcb1af(%rip), %xmm0    # 0x1527a8
jmp 0x874e7