// File: /workspace/github_cpp_makefile_star2_O1/Luthaf[P]zeopp/grid.cc
// Function: GaussianCube::writeGrid(ATOM_NETWORK*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, bool)
void GaussianCube::writeGrid(ATOM_NETWORK *atmnet, std::string cubefilename, bool angstrom_to_bohr, bool useMassFlag){

 FILE *fp1;
 fp1=fopen(cubefilename.c_str(),"w");

 double toB=1; //scale factor - update it if we want to scale the output
 if(angstrom_to_bohr) toB=1.8903592 ; // scale factor to convert from A to Bohr

 fprintf(fp1,"\nThis is distance grid\n");

 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",atmnet->numAtoms,0.0,0.0,0.0);

 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",na,va.x*toB,va.y*toB,va.z*toB);
 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",nb,vb.x*toB,vb.y*toB,vb.z*toB);
 fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf\n",nc,vc.x*toB,vc.y*toB,vc.z*toB);

 for(int i=0;i<atmnet->numAtoms;i++)
   {
   if(useMassFlag == true)
     {
     fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf % 13.6lf\n",lookupAtomicNumber(atmnet->atoms[i].type),lookupMass(atmnet->atoms[i].type),
                                                              atmnet->atoms[i].x*toB, atmnet->atoms[i].y*toB, atmnet->atoms[i].z*toB);
     }else{
     fprintf(fp1,"%d % 13.6lf % 13.6lf % 13.6lf % 13.6lf\n", 1, 1.0,
                                                              atmnet->atoms[i].x*toB, atmnet->atoms[i].y*toB, atmnet->atoms[i].z*toB);
     };
   };

 fprintf(fp1," 1    1\n");

 int count2=0; // for formatting only
 for(int x=0;x<na;x++)
   for(int y=0;y<nb;y++)
     for(int z=0;z<nc;z++)
       {
       fprintf(fp1," % 13.6E ",cube[x][y][z]);

       // count2 is for formatting only (max 6 columns with data)
       count2++;
       if(z==(nc-1)) {fprintf(fp1,"\n");count2=0;};
       if(count2==6) {fprintf(fp1,"\n");count2=0;};
       };

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r8d, %r13d
movl %ecx, %r12d
movq %rsi, %r15
movq %rdi, %rbx
movq (%rdx), %rdi
leaq 0xca1da(%rip), %rsi     # 0x153b94
callq 0x4510
movq %rax, %r14
testl %r12d, %r12d
jne 0x899d1
movsd 0xc5689(%rip), %xmm0    # 0x14f058
jmp 0x899d9
movsd 0xcdccf(%rip), %xmm0    # 0x1576a8
movsd %xmm0, 0x8(%rsp)
leaq 0xcddb7(%rip), %rdi     # 0x15779d
movl $0x17, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x4850
movl 0x110(%r15), %edx
leaq 0xcddaf(%rip), %r12     # 0x1577b5
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x78(%rbx), %edx
movsd 0x18(%rbx), %xmm0
movsd 0x8(%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x20(%rbx), %xmm1
mulsd %xmm3, %xmm1
movsd 0x28(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x7c(%rbx), %edx
movsd 0x30(%rbx), %xmm0
movsd 0x8(%rsp), %xmm3
mulsd %xmm3, %xmm0
movsd 0x38(%rbx), %xmm1
mulsd %xmm3, %xmm1
movsd 0x40(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
movl 0x80(%rbx), %edx
movsd 0x48(%rbx), %xmm0
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movsd 0x50(%rbx), %xmm1
mulsd %xmm2, %xmm1
movapd %xmm2, %xmm3
movsd 0x58(%rbx), %xmm2
mulsd %xmm3, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x45f0
cmpl $0x0, 0x110(%r15)
jle 0x89c24
xorl %r12d, %r12d
xorl %ebp, %ebp
movl %r13d, 0x14(%rsp)
testb %r13b, %r13b
je 0x89bbf
movq 0x2f0(%r15), %rax
leaq 0x48(%rsp), %rcx
movq %rcx, 0x38(%rsp)
movq 0x40(%rax,%r12), %rsi
movq 0x48(%rax,%r12), %rdx
addq %rsi, %rdx
leaq 0x38(%rsp), %r13
movq %r13, %rdi
callq 0xf32a
movq %r13, %rdi
callq 0x2231c
movl %eax, %r13d
movq 0x2f0(%r15), %rax
leaq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq 0x40(%rax,%r12), %rsi
movq 0x48(%rax,%r12), %rdx
addq %rsi, %rdx
leaq 0x18(%rsp), %rdi
callq 0xf32a
leaq 0x18(%rsp), %rdi
callq 0x221d4
movq 0x2f0(%r15), %rax
movsd (%rax,%r12), %xmm1
movsd 0x8(%rsp), %xmm4
mulsd %xmm4, %xmm1
movsd 0x8(%rax,%r12), %xmm2
mulsd %xmm4, %xmm2
movsd 0x10(%rax,%r12), %xmm3
mulsd %xmm4, %xmm3
movq %r14, %rdi
leaq 0xcdc5e(%rip), %rsi     # 0x1577d4
movl %r13d, %edx
movb $0x4, %al
callq 0x45f0
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x89b9c
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
movl 0x14(%rsp), %r13d
je 0x89c0a
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x89c0a
movq 0x2f0(%r15), %rax
movsd (%rax,%r12), %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd 0x8(%rax,%r12), %xmm2
mulsd %xmm0, %xmm2
movsd 0x10(%rax,%r12), %xmm3
mulsd %xmm0, %xmm3
movq %r14, %rdi
leaq 0xcdbde(%rip), %rsi     # 0x1577d4
movl $0x1, %edx
movsd 0xc5455(%rip), %xmm0    # 0x14f058
movb $0x4, %al
callq 0x45f0
incq %rbp
movslq 0x110(%r15), %rax
addq $0x98, %r12
cmpq %rax, %rbp
jl 0x89ace
leaq 0xcdbd1(%rip), %rdi     # 0x1577fc
movl $0x8, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x4850
cmpl $0x0, 0x78(%rbx)
jle 0x89cee
xorl %r12d, %r12d
xorl %ebp, %ebp
cmpl $0x0, 0x7c(%rbx)
jle 0x89cde
xorl %r13d, %r13d
cmpl $0x0, 0x80(%rbx)
jle 0x89cce
xorl %r15d, %r15d
movq 0x88(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%r13,8), %rax
movsd (%rax,%r15,8), %xmm0
movq %r14, %rdi
leaq 0xcdb81(%rip), %rsi     # 0x157805
movb $0x1, %al
callq 0x45f0
movl 0x80(%rbx), %eax
decl %eax
cmpq %rax, %r15
jne 0x89ca9
movl $0xa, %edi
movq %r14, %rsi
callq 0x4550
xorl %ebp, %ebp
jmp 0x89cab
incl %ebp
cmpl $0x6, %ebp
jne 0x89cbf
movl $0xa, %edi
movq %r14, %rsi
callq 0x4550
xorl %ebp, %ebp
incq %r15
movslq 0x80(%rbx), %rax
cmpq %rax, %r15
jl 0x89c65
incq %r13
movslq 0x7c(%rbx), %rax
cmpq %rax, %r13
jl 0x89c59
incq %r12
movslq 0x78(%rbx), %rax
cmpq %rax, %r12
jl 0x89c4c
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x89d21
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x4470
jmp 0x89d21
movq %rax, %rbx
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x89d3d
movq 0x48(%rsp), %rsi
incq %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780