// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::WritePDBFile(char const*) const
int R3Grid::
WritePDBFile(const char *filename) const
{
  // Open file
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    RNFail("Unable to open file %s", filename);
    return 0;
  }

  // Print values
  int n = 0;
  for (int k = 0; k < ZResolution(); k++) {
    for (int j = 0; j < YResolution(); j++) {
      for (int i = 0; i < XResolution(); i++) {
	if (GridValue(i,j, k) > 0){
	  R3Point world_position = WorldPosition(i, j, k);	  
	  fprintf(fp, "%-6s%5d %-4s%c%3s %1s%4d%c   %8.3f%8.3f%8.3f%6.2f%6.2f          %2s\n", "HETATM", n, "H", ' ', "POC", "1", n, ' ', world_position.X(), world_position.Y(), world_position.Z(), GridValue(i, j, k), GridValue(i, j, k), "");
	  n++;
	}
      }
    }
  }

  // Close file
  fclose(fp);

  // Return OK status
  return grid_size;
}

// Assembly:
pushq %rbx
subq $0xc0, %rsp
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0xa8(%rsp), %rdi
leaq 0xcd6ff(%rip), %rsi     # 0x1342f3
callq 0xf5e0
movq %rax, 0xa0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x66c32
movq 0xa8(%rsp), %rsi
leaq 0xcc7c5(%rip), %rdi     # 0x1333e0
movb $0x0, %al
callq 0xd4f30
movl $0x0, 0xbc(%rsp)
jmp 0x66ea7
movl $0x0, 0x9c(%rsp)
movl $0x0, 0x98(%rsp)
movq 0x70(%rsp), %rdi
movl 0x98(%rsp), %eax
movl %eax, 0x6c(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x66e88
movl $0x0, 0x94(%rsp)
movq 0x70(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x68(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x68(%rsp), %eax
cmpl %ecx, %eax
jge 0x66e70
movl $0x0, 0x90(%rsp)
movq 0x70(%rsp), %rdi
movl 0x90(%rsp), %eax
movl %eax, 0x64(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x64(%rsp), %eax
cmpl %ecx, %eax
jge 0x66e58
movq 0x70(%rsp), %rdi
movl 0x90(%rsp), %esi
movl 0x94(%rsp), %edx
movl 0x98(%rsp), %ecx
callq 0x6bad0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x66e40
movq 0x70(%rsp), %rsi
cvtsi2sdl 0x90(%rsp), %xmm0
cvtsi2sdl 0x94(%rsp), %xmm1
cvtsi2sdl 0x98(%rsp), %xmm2
leaq 0x78(%rsp), %rdi
callq 0x62cd0
movq 0xa0(%rsp), %rax
movq %rax, 0x30(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x3c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x58(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x40(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x378c0
movq 0x70(%rsp), %rdi
movsd %xmm0, 0x48(%rsp)
movl 0x90(%rsp), %esi
movl 0x94(%rsp), %edx
movl 0x98(%rsp), %ecx
callq 0x6bad0
movq 0x70(%rsp), %rdi
movsd %xmm0, 0x50(%rsp)
movl 0x90(%rsp), %esi
movl 0x94(%rsp), %edx
movl 0x98(%rsp), %ecx
callq 0x6bad0
movq 0x30(%rsp), %rdi
movl 0x38(%rsp), %ecx
movl 0x3c(%rsp), %r10d
movsd 0x40(%rsp), %xmm1
movsd 0x48(%rsp), %xmm2
movsd 0x50(%rsp), %xmm3
movaps %xmm0, %xmm4
movsd 0x58(%rsp), %xmm0
leaq 0xce20e(%rip), %rsi     # 0x134ff2
leaq 0xce24c(%rip), %rdx     # 0x135037
leaq 0xce24c(%rip), %r8      # 0x13503e
movl $0x20, %r9d
leaq 0xce241(%rip), %rbx     # 0x135040
leaq 0xce23e(%rip), %r11     # 0x135044
leaq 0xcc873(%rip), %rax     # 0x133680
movq %rbx, (%rsp)
movq %r11, 0x8(%rsp)
movl %r10d, 0x10(%rsp)
movl $0x20, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movb $0x5, %al
callq 0xf730
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x66e42
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x66ca4
jmp 0x66e5a
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x66c76
jmp 0x66e72
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x66c48
movq 0xa0(%rsp), %rdi
callq 0xf4b0
movq 0x70(%rsp), %rax
movl 0x24c(%rax), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
addq $0xc0, %rsp
popq %rbx
retq
nopw (%rax,%rax)