// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movl %r8d, %r14d
movl %ecx, %ebp
movq %rsi, 0x18(%rsp)
movq %rdi, %rbx
movq (%rdx), %rdi
leaq 0x99bc1(%rip), %rsi     # 0x100bb4
callq 0x4530
movq %rax, 0x10(%rsp)
testl %ebp, %ebp
jne 0x6700e
movl %r14d, %ebp
movsd 0x9504c(%rip), %xmm0    # 0xfc058
jmp 0x67019
movl %r14d, %ebp
movsd 0x9d71f(%rip), %xmm0    # 0x104738
movsd %xmm0, 0x8(%rsp)
leaq 0x9d817(%rip), %rdi     # 0x10483d
pushq $0x17
popq %rsi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %r14
movq %r14, %rcx
callq 0x4840
movq 0x18(%rsp), %r15
movl 0x110(%r15), %edx
leaq 0x9d809(%rip), %r12     # 0x104855
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
movq %r14, %rdi
movq %r12, %rsi
movb $0x3, %al
callq 0x4610
movl 0x78(%rbx), %edx
movsd 0x18(%rbx), %xmm0
movsd 0x8(%rsp), %xmm3
movsd %xmm3, 0x8(%rsp)
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 0x4610
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 0x4610
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 %r15, %r14
movq %r12, %rsi
movb $0x3, %al
callq 0x4610
pushq $0x40
popq %r12
xorl %r13d, %r13d
movl %ebp, 0x24(%rsp)
movslq 0x110(%r14), %rax
cmpq %rax, %r13
jge 0x67227
movq 0x2f0(%r14), %rsi
testb %bpl, %bpl
je 0x671d3
addq %r12, %rsi
leaq 0x28(%rsp), %rbp
movq %rbp, %rdi
callq 0x45d0
movq %rbp, %rdi
callq 0x1b590
leaq 0x48(%rsp), %r14
movl %eax, %r15d
movq 0x18(%rsp), %rax
movq 0x2f0(%rax), %rsi
addq %r12, %rsi
movq %r14, %rdi
callq 0x45d0
movq %r14, %rdi
callq 0x1b486
movq 0x18(%rsp), %rbp
movq 0x2f0(%rbp), %rax
movsd -0x40(%rax,%r12), %xmm1
movsd 0x8(%rsp), %xmm4
mulsd %xmm4, %xmm1
movsd -0x38(%rax,%r12), %xmm2
mulsd %xmm4, %xmm2
movsd -0x30(%rax,%r12), %xmm3
mulsd %xmm4, %xmm3
movq 0x10(%rsp), %rdi
leaq 0x9d6c6(%rip), %rsi     # 0x104874
movl %r15d, %edx
movb $0x4, %al
callq 0x4610
movq %r14, %rdi
movq %rbp, %r14
callq 0x4210
leaq 0x28(%rsp), %rdi
callq 0x4210
movl 0x24(%rsp), %ebp
jmp 0x67218
movsd -0x40(%rsi,%r12), %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm1
movsd -0x38(%rsi,%r12), %xmm2
mulsd %xmm0, %xmm2
movsd -0x30(%rsi,%r12), %xmm3
mulsd %xmm0, %xmm3
movq 0x10(%rsp), %rdi
leaq 0x9d66e(%rip), %rsi     # 0x104874
pushq $0x1
popq %rdx
movsd 0x94e47(%rip), %xmm0    # 0xfc058
movb $0x4, %al
callq 0x4610
incq %r13
addq $0x98, %r12
jmp 0x67110
leaq 0x9d66e(%rip), %rdi     # 0x10489c
pushq $0x8
popq %rsi
pushq $0x1
popq %rdx
movq 0x10(%rsp), %rcx
callq 0x4840
xorl %r12d, %r12d
leaq 0x9d65d(%rip), %r15     # 0x1048a5
xorl %r13d, %r13d
movslq 0x78(%rbx), %rax
cmpq %rax, %r12
jge 0x672d3
xorl %ebp, %ebp
movslq 0x7c(%rbx), %rax
cmpq %rax, %rbp
jge 0x672cb
xorl %r14d, %r14d
movslq 0x80(%rbx), %rax
cmpq %rax, %r14
jge 0x672c6
movq 0x88(%rbx), %rax
movq (%rax,%r12,8), %rax
movq (%rax,%rbp,8), %rax
movsd (%rax,%r14,8), %xmm0
movq 0x10(%rsp), %rdi
movq %r15, %rsi
movb $0x1, %al
callq 0x4610
movl 0x80(%rbx), %eax
decl %eax
cmpq %rax, %r14
sete %al
incl %r13d
cmpl $0x6, %r13d
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x672c1
pushq $0xa
popq %rdi
movq 0x10(%rsp), %rsi
callq 0x4570
xorl %r13d, %r13d
incq %r14
jmp 0x67262
incq %rbp
jmp 0x67256
incq %r12
jmp 0x6724b
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x4210
jmp 0x672f4
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x4210
movq %rbx, %rdi
callq 0x4780