// File: 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
movq %rsp, %rbp
subq $0xf0, %rsp
movb 0x18(%rbp), %al
movl 0x10(%rbp), %eax
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movsd %xmm0, -0x20(%rbp)
movsd %xmm1, -0x28(%rbp)
movsd %xmm2, -0x30(%rbp)
movsd %xmm3, -0x38(%rbp)
movsd %xmm4, -0x40(%rbp)
movsd %xmm5, -0x48(%rbp)
movl %r8d, -0x4c(%rbp)
movl %r9d, -0x50(%rbp)
movsd -0x20(%rbp), %xmm1
movsd -0x38(%rbp), %xmm0
cvtsi2sdl -0xc(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x58(%rbp)
movsd -0x28(%rbp), %xmm1
movsd -0x40(%rbp), %xmm0
cvtsi2sdl -0x10(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x60(%rbp)
movsd -0x30(%rbp), %xmm1
movsd -0x48(%rbp), %xmm0
cvtsi2sdl -0x14(%rbp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x8(%rbp), %rsi
movsd -0x58(%rbp), %xmm0
movsd -0x60(%rbp), %xmm1
movsd -0x68(%rbp), %xmm2
leaq -0x88(%rbp), %rdi
callq 0x97360
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xe9d70
movsd (%rax), %xmm0
xorl %eax, %eax
movsd 0x10bb25(%rip), %xmm1   # 0x1fc408
ucomisd %xmm1, %xmm0
movb %al, -0xb1(%rbp)
jb 0xf09c4
leaq -0x88(%rbp), %rdi
xorl %esi, %esi
callq 0xe9d70
movq %rax, %rcx
xorl %eax, %eax
movsd 0x10bb02(%rip), %xmm0   # 0x1fc410
ucomisd (%rcx), %xmm0
movb %al, -0xb1(%rbp)
jb 0xf09c4
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0xe9d70
movsd (%rax), %xmm0
xorl %eax, %eax
movsd 0x10bacb(%rip), %xmm1   # 0x1fc408
ucomisd %xmm1, %xmm0
movb %al, -0xb1(%rbp)
jb 0xf09c4
leaq -0x88(%rbp), %rdi
movl $0x1, %esi
callq 0xe9d70
movq %rax, %rcx
xorl %eax, %eax
movsd 0x10baa9(%rip), %xmm0   # 0x1fc410
ucomisd (%rcx), %xmm0
movb %al, -0xb1(%rbp)
jb 0xf09c4
leaq -0x88(%rbp), %rdi
movl $0x2, %esi
callq 0xe9d70
movsd (%rax), %xmm0
xorl %eax, %eax
movsd 0x10ba76(%rip), %xmm1   # 0x1fc408
ucomisd %xmm1, %xmm0
movb %al, -0xb1(%rbp)
jb 0xf09c4
leaq -0x88(%rbp), %rdi
movl $0x2, %esi
callq 0xe9d70
movsd 0x10ba59(%rip), %xmm0   # 0x1fc410
ucomisd (%rax), %xmm0
setae %al
movb %al, -0xb1(%rbp)
movb -0xb1(%rbp), %al
andb $0x1, %al
movb %al, -0x89(%rbp)
testb $0x1, -0x89(%rbp)
jne 0xf09e8
xorps %xmm0, %xmm0
movsd %xmm0, -0x70(%rbp)
jmp 0xf0bef
movsd 0x106eb8(%rip), %xmm0   # 0x1f78a8
movsd %xmm0, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl 0x110(%rcx), %eax
jge 0xf0be2
movq -0x8(%rbp), %rax
movq %rax, -0xe8(%rbp)
movsd -0x58(%rbp), %xmm0
movsd %xmm0, -0xe0(%rbp)
movsd -0x60(%rbp), %xmm0
movsd %xmm0, -0xd8(%rbp)
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0xd0(%rbp)
movq -0x8(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x9c(%rbp), %rsi
callq 0xfcc0
movsd (%rax), %xmm0
movsd %xmm0, -0xc8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x9c(%rbp), %rsi
callq 0xfcc0
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0xc0(%rbp)
movq -0x8(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x9c(%rbp), %rsi
callq 0xfcc0
movq -0xe8(%rbp), %rdi
movsd -0xe0(%rbp), %xmm0
movsd -0xd8(%rbp), %xmm1
movsd -0xd0(%rbp), %xmm2
movsd -0xc8(%rbp), %xmm3
movsd -0xc0(%rbp), %xmm4
movsd 0x10(%rax), %xmm5
callq 0x97a10
movsd %xmm0, -0xa8(%rbp)
movq -0x8(%rbp), %rdi
addq $0x2f0, %rdi            # imm = 0x2F0
movslq -0x9c(%rbp), %rsi
callq 0xfcc0
movsd 0x30(%rax), %xmm0
movsd %xmm0, -0xb0(%rbp)
movsbl 0x18(%rbp), %eax
cmpl $0x66, %eax
jne 0xf0b34
movsd -0xb0(%rbp), %xmm1
movsd -0xa8(%rbp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0xf0baf
movsd -0xa8(%rbp), %xmm0
movsd -0xa8(%rbp), %xmm2
movsd -0xb0(%rbp), %xmm1
mulsd -0xb0(%rbp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movsbl 0x18(%rbp), %eax
cmpl $0x68, %eax
jne 0xf0bad
movsd 0x10b894(%rip), %xmm0   # 0x1fc420
addsd -0xa8(%rbp), %xmm0
callq 0x53f0
movsd 0x10b877(%rip), %xmm1   # 0x1fc418
subsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
jmp 0xf0baf
leaq -0x98(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0x64f50
movsd (%rax), %xmm0
movsd %xmm0, -0x98(%rbp)
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0xf0a02
movsd -0x98(%rbp), %xmm0
movsd %xmm0, -0x70(%rbp)
movsd -0x70(%rbp), %xmm0
addq $0xf0, %rsp
popq %rbp
retq
nopl (%rax)