// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/networkstorage.cc
// Function: write_abstract_cif(_IO_FILE*, ATOM_NETWORK*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
void write_abstract_cif(FILE *cif, ATOM_NETWORK *cell, string name) {
  //this function writes out a cif file with abstracted cell information
  //the basic vertices and edges are written, i.e. before symmetry operations, and the symmetry group is written to the file
  //edges are written as vertices - they are shortened to 1/4 length, so that they do not overlap with real vertices, and so that their direction can be preserved
  fprintf(cif, "#******************************************\n#\n# CIF file created by Richard L. Martin, Lawrence Berkeley National Laboratory, 2013\n#\n#*******************************************\n\n");
  fprintf(cif, "_cell_length_a\t\t%.3f(0)\n", cell->a);
  fprintf(cif, "_cell_length_b\t\t%.3f(0)\n", cell->b);
  fprintf(cif, "_cell_length_c\t\t%.3f(0)\n", cell->c);
  fprintf(cif, "_cell_angle_alpha\t\t%.3f(0)\n", cell->alpha);
  fprintf(cif, "_cell_angle_beta\t\t%.3f(0)\n", cell->beta);
  fprintf(cif, "_cell_angle_gamma\t\t%.3f(0)\n", cell->gamma);
//  fprintf(cif, "_symmetry_space_group_name_H-M\t\t%s\n", cell->sym_name.c_str());
fprintf(cif, "_symmetry_space_group_name_H-M\t\tP-1\n");
//  fprintf(cif, "_symmetry_Int_Tables_number\t\t%d\n", cell->sym_ID);
  fprintf(cif, "_symmetry_Int_Tables_number\t\t1\n");
  fprintf(cif, "_symmetry_cell_setting\t\t");
  
  //Determine the Crystal System
  if (cell->alpha == 90 && cell->beta == 90 && cell->gamma == 90){
    if (cell->a == cell->b || cell->b == cell->c || cell->a == cell->c){
      if (cell->a == cell->b && cell->b == cell->c){
        fprintf(cif, "Isometric\n\n");
      }
      else {
        fprintf(cif, "Tetragonal\n\n");
      }
    }
    else{
    	fprintf(cif, "Orthorhombic\n\n");
    }
  }
  else if(cell->alpha == cell->beta || cell->beta == cell->gamma || cell->alpha == cell->gamma){
    fprintf(cif, "Monoclinic\n\n");
  }
  else{
    fprintf(cif, "Triclinic\n\n");
  }
  
  fprintf(cif, "loop_\n");
  fprintf(cif, "_symmetry_equiv_pos_as_xyz\n");
fprintf(cif, "'+x,+y,+z'\n\n"); //can we do something smarter here?
  fprintf(cif, "loop_\n");
  fprintf(cif, "_atom_site_label\n");
  fprintf(cif, "_atom_site_type_symbol\n");
  fprintf(cif, "_atom_site_fract_x\n");
  fprintf(cif, "_atom_site_fract_y\n");
  fprintf(cif, "_atom_site_fract_z\n");
  int num_vertices = cell->vertices.size();
  int count = 1;
  for (int i=0; i<num_vertices; i++){
    VERTEX v = cell->vertices.at(i);
    int num_edges = v.edges.size();
    if(num_edges>2) { //don't write out augmented, 2-c vertices...
//      fprintf(cif, "%d\t%d\t%.6f\t%.6f\t%.6f\n", count, count, v.abc.x, v.abc.y, v.abc.z);
      fprintf(cif, "%s\t%s\t%.6f\t%.6f\t%.6f\n", "C", "C", v.abc.x, v.abc.y, v.abc.z);
      count++;
      for (int j=0; j<num_edges; j++) { //... or dummy edges...
        XYZ edge = v.edges.at(j);
        XYZ quarter_edge = ((edge-v.abc).scale(1.0/4.0))+v.abc;
//        fprintf(cif, "%d\t%d\t%.6f\t%.6f\t%.6f\n", count, count, quarter_edge.x, quarter_edge.y, quarter_edge.z);
        fprintf(cif, "%s\t%s\t%.6f\t%.6f\t%.6f\n", "H", "H", quarter_edge.x, quarter_edge.y, quarter_edge.z);
        count++;        
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r15
movq %rdi, %r13
leaq 0xb89c9(%rip), %rdi     # 0x1029c7
pushq $0x1
popq %rbx
movl $0xb3, %esi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
movsd 0x8(%r15), %xmm0
leaq 0xb8a5d(%rip), %rsi     # 0x102a7b
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
movsd 0x10(%r15), %xmm0
leaq 0xb8a5f(%rip), %rsi     # 0x102a94
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
movsd 0x18(%r15), %xmm0
leaq 0xb8a61(%rip), %rsi     # 0x102aad
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
movsd 0x20(%r15), %xmm0
leaq 0xb8a63(%rip), %rsi     # 0x102ac6
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
movsd 0x28(%r15), %xmm0
leaq 0xb8a68(%rip), %rsi     # 0x102ae2
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
movsd 0x30(%r15), %xmm0
leaq 0xb8a6c(%rip), %rsi     # 0x102afd
movq %r13, %rdi
movb $0x1, %al
callq 0x4610
leaq 0xb8a77(%rip), %rdi     # 0x102b19
pushq $0x24
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb8a87(%rip), %rdi     # 0x102b3e
pushq $0x1f
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb3503(%rip), %rdi     # 0xfd5cf
pushq $0x18
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
movsd 0x20(%r15), %xmm0
movsd 0x28(%r15), %xmm1
ucomisd 0xb2a22(%rip), %xmm0    # 0xfcb10
jne 0x4a14b
jp 0x4a14b
ucomisd 0xb2a16(%rip), %xmm1    # 0xfcb10
jne 0x4a14b
jp 0x4a14b
movsd 0x30(%r15), %xmm2
ucomisd 0xb2a04(%rip), %xmm2    # 0xfcb10
jne 0x4a14b
jp 0x4a14b
movsd 0x8(%r15), %xmm0
movsd 0x10(%r15), %xmm2
ucomisd %xmm2, %xmm0
movsd 0x18(%r15), %xmm1
jne 0x4a3d5
jp 0x4a3d5
ucomisd %xmm1, %xmm2
jne 0x4a3f3
jp 0x4a3f3
leaq 0xb8a15(%rip), %rdi     # 0x102b5e
jmp 0x4a17b
ucomisd %xmm1, %xmm0
jne 0x4a15e
jp 0x4a15e
leaq 0xb8a2c(%rip), %rdi     # 0x102b86
pushq $0xc
jmp 0x4a17d
movsd 0x30(%r15), %xmm2
ucomisd %xmm2, %xmm1
jne 0x4a16c
jnp 0x4a153
ucomisd %xmm2, %xmm0
jne 0x4a174
jnp 0x4a153
leaq 0xb8a18(%rip), %rdi     # 0x102b93
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb8a0f(%rip), %r14     # 0x102b9f
pushq $0x6
popq %r12
pushq $0x1
popq %rbx
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89f7(%rip), %rdi     # 0x102ba6
pushq $0x1b
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89fe(%rip), %rdi     # 0x102bc2
pushq $0xc
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89e5(%rip), %rdi     # 0x102bcf
pushq $0x11
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89e2(%rip), %rdi     # 0x102be1
pushq $0x17
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89e5(%rip), %rdi     # 0x102bf9
pushq $0x13
popq %r14
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89e0(%rip), %rdi     # 0x102c0d
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x4840
leaq 0xb89df(%rip), %rdi     # 0x102c21
movq %r14, %rsi
movq %rbx, %rdx
movq %r13, 0x8(%rsp)
movq %r13, %rcx
callq 0x4840
movq 0x3c8(%r15), %rax
subq 0x3c0(%r15), %rax
leaq 0x3c0(%r15), %rcx
movq %rcx, 0x10(%rsp)
pushq $0x70
popq %rcx
cqto
idivq %rcx
xorl %esi, %esi
testl %eax, %eax
cmovlel %esi, %eax
movq %rax, 0x18(%rsp)
leaq 0x58(%rsp), %r14
leaq 0xc8(%rsp), %r12
leaq 0xe0(%rsp), %rbx
leaq 0xbbe85(%rip), %rbp     # 0x106124
cmpq 0x18(%rsp), %rsi
je 0x4a3c3
movq 0x10(%rsp), %rdi
movq %rsi, 0x20(%rsp)
callq 0x4b63c
movq %r14, %rdi
movq %rax, %rsi
callq 0x4b65a
movq 0x80(%rsp), %rax
subq 0x78(%rsp), %rax
cqto
pushq $0x18
popq %rcx
idivq %rcx
movq %rax, %r15
cmpl $0x3, %r15d
jl 0x4a3ae
movsd 0x58(%rsp), %xmm0
movsd 0x60(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movq 0x8(%rsp), %rdi
leaq 0xb8931(%rip), %rsi     # 0x102c35
leaq 0xbc233(%rip), %rcx     # 0x10653e
movq %rcx, %rdx
movb $0x3, %al
callq 0x4610
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
xorl %r13d, %r13d
cmpq %r13, %r15
je 0x4a3ae
leaq 0x78(%rsp), %rdi
movq %r13, %rsi
callq 0x4b6d6
movq 0x10(%rax), %rcx
movq %rcx, 0x50(%rsp)
movupd (%rax), %xmm0
movapd %xmm0, 0x40(%rsp)
movq %r12, %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x642ae
movq %rbx, %rdi
movq %r12, %rsi
movsd 0xb73b2(%rip), %xmm0    # 0x101718
callq 0x640ba
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x642d2
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm1
movsd 0x38(%rsp), %xmm2
movq 0x8(%rsp), %rdi
leaq 0xb889c(%rip), %rsi     # 0x102c35
movq %rbp, %rdx
movq %rbp, %rcx
movb $0x3, %al
callq 0x4610
incq %r13
jmp 0x4a31f
movq %r14, %rdi
callq 0xb1ba
movq 0x20(%rsp), %rsi
incq %rsi
jmp 0x4a29f
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd %xmm1, %xmm2
jne 0x4a3dd
jnp 0x4a3f3
ucomisd %xmm1, %xmm0
jne 0x4a3e5
jnp 0x4a3f3
leaq 0xb878b(%rip), %rdi     # 0x102b77
pushq $0xe
jmp 0x4a17d
leaq 0xb8770(%rip), %rdi     # 0x102b6a
jmp 0x4a15a
jmp 0x4a401
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0xb1ba
movq %rbx, %rdi
callq 0x4780