// File: /workspace/github_cpp_makefile_star2_O2/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
cvtsi2sd %edx, %xmm3
mulsd %xmm4, %xmm3
addsd %xmm3, %xmm1
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 0x3f1ce
movq %r14, %rdi
xorl %esi, %esi
callq 0x63c78
movsd (%rax), %xmm1
xorpd %xmm0, %xmm0
ucomisd 0x9dc07(%rip), %xmm1    # 0x104718
jb 0x66b82
leaq 0x20(%rsp), %rdi
xorl %esi, %esi
callq 0x63c78
movsd 0x9dbf9(%rip), %xmm0    # 0x104720
ucomisd (%rax), %xmm0
jb 0x66b7e
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x63c78
movsd (%rax), %xmm0
ucomisd 0x9dbd2(%rip), %xmm0    # 0x104718
jb 0x66b7e
leaq 0x20(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x63c78
movsd 0x9dbc3(%rip), %xmm0    # 0x104720
ucomisd (%rax), %xmm0
jb 0x66b7e
leaq 0x20(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x63c78
movsd (%rax), %xmm0
ucomisd 0x9db9c(%rip), %xmm0    # 0x104718
jae 0x66b8d
xorpd %xmm0, %xmm0
addq $0x38, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
leaq 0x20(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x63c78
movsd 0x9db7e(%rip), %xmm0    # 0x104720
ucomisd (%rax), %xmm0
xorpd %xmm0, %xmm0
jb 0x66b82
movb 0x68(%rsp), %bpl
movsd 0x9ab57(%rip), %xmm0    # 0x101710
xorl %r14d, %r14d
xorl %r15d, %r15d
movslq 0x110(%rbx), %rax
cmpq %rax, %r15
jge 0x66b82
movsd %xmm0, (%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 0x3f636
movq 0x2f0(%rbx), %rax
movsd 0x30(%rax,%r14), %xmm1
cmpb $0x66, %bpl
jne 0x66c1f
subsd %xmm1, %xmm0
jmp 0x66c49
mulsd %xmm1, %xmm1
mulsd %xmm0, %xmm0
subsd %xmm1, %xmm0
cmpb $0x68, %bpl
jne 0x66c49
addsd 0x9daef(%rip), %xmm0    # 0x104728
ucomisd 0x9969f(%rip), %xmm0    # 0x1002e0
jb 0x66c50
sqrtsd %xmm0, %xmm0
jmp 0x66c55
movsd (%rsp), %xmm1
jmp 0x66c62
callq 0x4390
movsd (%rsp), %xmm1
addsd 0x9dace(%rip), %xmm0    # 0x104730
minsd %xmm1, %xmm0
incq %r15
addq $0x98, %r14
jmp 0x66bbf