// File: /workspace/github_cpp_makefile_star2_O1/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 0x531ea
movq %r14, %rdi
xorl %esi, %esi
callq 0x85478
movsd (%rax), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0xce28b(%rip), %xmm1    # 0x157688
jb 0x89474
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x85478
movsd 0xce27d(%rip), %xmm0    # 0x157690
ucomisd (%rax), %xmm0
jb 0x89470
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x85478
movsd (%rax), %xmm0
ucomisd 0xce254(%rip), %xmm0    # 0x157688
jb 0x89470
leaq 0x20(%rsp), %rdi
movl $0x1, %esi
callq 0x85478
movsd 0xce243(%rip), %xmm0    # 0x157690
ucomisd (%rax), %xmm0
jb 0x89470
leaq 0x20(%rsp), %rdi
movl $0x2, %esi
callq 0x85478
movsd (%rax), %xmm0
ucomisd 0xce21a(%rip), %xmm0    # 0x157688
jae 0x8947f
xorpd %xmm0, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
movl $0x2, %esi
callq 0x85478
movsd 0xce1fa(%rip), %xmm0    # 0x157690
ucomisd (%rax), %xmm0
xorpd %xmm0, %xmm0
jb 0x89474
cmpl $0x0, 0x110(%rbx)
jle 0x8957e
movb 0x68(%rsp), %bpl
movsd 0xcb246(%rip), %xmm1    # 0x154700
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 0x5394a
movq 0x2f0(%rbx), %rax
movsd 0x30(%rax,%r14), %xmm1
cmpb $0x66, %bpl
jne 0x89514
subsd %xmm1, %xmm0
jmp 0x8953e
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
cmpb $0x68, %bpl
jne 0x8953e
addsd 0xce16a(%rip), %xmm0    # 0x157698
ucomisd 0xc9d8a(%rip), %xmm0    # 0x1532c0
jb 0x89545
sqrtsd %xmm0, %xmm0
jmp 0x8954a
movsd (%rsp), %xmm1
jmp 0x89557
callq 0x4350
movsd (%rsp), %xmm1
addsd 0xce149(%rip), %xmm0    # 0x1576a0
minsd %xmm1, %xmm0
incq %r15
movslq 0x110(%rbx), %rax
addq $0x98, %r14
movapd %xmm0, %xmm1
cmpq %rax, %r15
jl 0x894c0
jmp 0x89474
movsd 0xcb17a(%rip), %xmm0    # 0x154700
jmp 0x89474