// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/networkio.cc
// Function: readARCFile(char*, ATOM_NETWORK*, bool)
bool readARCFile(char *filename, ATOM_NETWORK *cell, bool radial){
  FILE * input;
  input = fopen(filename, "r");
  int numAtoms=0;

  if(input==NULL){
    cout << "\n" << "Failed to open .arc input file " << filename << "\n";
    cout << "Exiting ..." << "\n";
//    exit(1);
    return false;
  }
  else{
    cout << "Reading input file " << filename << "\n";

    // Parse down to the FINAL GEOMETRY OBTAINED line
    char this_line[500];
    char found_final = 0;
    while(found_final == 0) {
      if(fgets(this_line, 500, input)!=NULL) {
        char str1[100], str2[100], str3[100];
        int status = sscanf(this_line, "%s %s %s", str1, str2, str3);
        if(status!=-1) {
          if(strcmp(str1,"FINAL")==0 && strcmp(str2,"GEOMETRY")==0 && strcmp(str3,"OBTAINED")==0) {
            found_final = 1;
          }
        }
      } else {
        printf("ERROR: finished parsing ARC file before finding geometry section\n");
//        exit(EXIT_FAILURE);
        fclose(input);
        return false;
      }
    }

    // Now parse following lines, trying to extract atom info
    int found_atoms = 0;
    double x, y, z, charge;
    char element[100], str1[100], str2[100], str3[100];
    while(found_atoms==0) {
      if(fgets(this_line, 500, input)!=NULL) {
        int status = sscanf(this_line, "%s %lf %s %lf %s %lf %s %lf", element, &x, str1, &y, str2, &z, str3, &charge);
        if(status==8) { //i.e. exactly 8 fields are required to be read
          found_atoms = 1;
        }
      } else {
        printf("ERROR: finished parsing ARC file before finding individual atom information\n");
//        exit(EXIT_FAILURE);
        fclose(input);
        return false;
      }
    }

    // At this point, we have the first atom info in memory
    ATOM newAtom;
    while(found_atoms==1) {
      //save previously discovered atom data - just xyz data for now
      newAtom.x = x;
      newAtom.y = y;
      newAtom.z = z;
      newAtom.type = string(element);
      newAtom.radius = lookupRadius(newAtom.type, radial);
      newAtom.charge = charge;
      cell->atoms.push_back(newAtom);
      numAtoms++;
      //try to find next atom data
      if(fgets(this_line, 500, input)!=NULL) {
        int status = sscanf(this_line, "%s %lf %s %lf %s %lf %s %lf", element, &x, str1, &y, str2, &z, str3, &charge);
        if(status!=8) found_atoms = 0; //if we can't read all 8 fields, we have, presumably, reached the unit cell params
      } else {
        printf("ERROR: finished parsing ARC file before finding unit cell info\n");
//        exit(EXIT_FAILURE);
        fclose(input);
        return false;
      }
    }

    // Now we have read all the atoms, we read the unit cell vectors
    XYZ v_a, v_b, v_c;
    for(int i=0; i<3; i++) {
      if(i==0) {
        v_a.x = x; v_a.y = y; v_a.z = z;
      } else if(i==1) {
        v_b.x = x; v_b.y = y; v_b.z = z;
      } else if(i==2) {
        v_c.x = x; v_c.y = y; v_c.z = z;
      }
      if(i!=2) {
        if(fgets(this_line, 500, input)!=NULL) {
          int status = sscanf(this_line, "%s %lf %s %lf %s %lf %s", element, &x, str1, &y, str2, &z, str3);
          if(status!=7) { //didn't read exactly 7 fields
            printf("ERROR: could not read exactly three unit cell vectors\n");
//            exit(EXIT_FAILURE);
            fclose(input);
            return false;
          }
        }
      }
    }
    cell->numAtoms = numAtoms;
    fclose(input);

    // Set up cell
    cell->v_a = v_a;
    cell->v_b = v_b;
    cell->v_c = v_c;
    double alpha = v_b.angle_between(v_c);
    double beta = v_a.angle_between(v_c);
    double gamma = v_a.angle_between(v_b);
    cell->alpha = alpha*360.0/(2.0*PI);
    cell->beta = beta*360.0/(2.0*PI);
    cell->gamma = gamma*360.0/(2.0*PI); //required since it expects to store degrees
    cell->a = v_a.magnitude();
    cell->b = v_b.magnitude();
    cell->c = v_c.magnitude();
    cell->initMatrices();
    cell->name = filename;
    cell->name.erase(cell->name.end()-4, cell->name.end());

//   cout << "number of atoms read " << numAtoms << "\n";

    // Convert atom coords to abc and update the xyz values to be within the UC
    for(int i=0; i<numAtoms; i++) {
      Point newCoords = cell->xyz_to_abc(cell->atoms.at(i).x,cell->atoms.at(i).y,cell->atoms.at(i).z);
      cell->atoms.at(i).a_coord = trans_to_origuc(newCoords[0]); cell->atoms.at(i).b_coord = trans_to_origuc(newCoords[1]); cell->atoms.at(i).c_coord = trans_to_origuc(newCoords[2]);
      newCoords = cell->abc_to_xyz(cell->atoms.at(i).a_coord,cell->atoms.at(i).b_coord,cell->atoms.at(i).c_coord);
      cell->atoms.at(i).x = newCoords[0]; cell->atoms.at(i).y = newCoords[1]; cell->atoms.at(i).z = newCoords[2];
      //cout << i << "  " << cell->atoms.at(i).type << "  " << newCoords[0] << "  " << newCoords[1] << "  " << newCoords[2] <<"\n";
    }
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x558, %rsp            # imm = 0x558
movl %edx, %ebp
movq %rsi, %rbx
movq %rdi, %r15
leaq 0xef826(%rip), %rsi     # 0x10d9f0
callq 0x4530
movq %rax, (%rsp)
testq %rax, %rax
je 0x1e2d7
movl %ebp, 0x30(%rsp)
movq %rbx, 0x38(%rsp)
movq 0x131dd4(%rip), %rdi    # 0x14ffc0
leaq 0xdeed2(%rip), %rsi     # 0xfd0c5
callq 0x4480
movq %rax, %rdi
movq %r15, 0xa8(%rsp)
movq %r15, %rsi
callq 0x4480
leaq 0xe436c(%rip), %rsi     # 0x10257e
movq %rax, %rdi
callq 0x4480
xorl %r12d, %r12d
leaq 0x360(%rsp), %rbx
leaq 0xdeead(%rip), %r14     # 0xfd0d9
leaq 0xe0(%rsp), %r15
leaq 0x210(%rsp), %r13
leaq 0x1a0(%rsp), %rbp
testb %r12b, %r12b
jne 0x1e334
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq (%rsp), %rdx
callq 0x4690
testq %rax, %rax
je 0x1e3c8
xorl %r12d, %r12d
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
xorl %eax, %eax
callq 0x4470
cmpl $-0x1, %eax
je 0x1e244
movq %r15, %rdi
leaq 0xdee53(%rip), %rsi     # 0xfd0e2
pushq $0x6
popq %rdx
callq 0x44c0
testl %eax, %eax
jne 0x1e2b5
movq %r13, %rdi
leaq 0xdee43(%rip), %rsi     # 0xfd0e8
pushq $0x9
popq %r12
movq %r12, %rdx
callq 0x44c0
testl %eax, %eax
je 0x1e2ba
xorl %r12d, %r12d
jmp 0x1e244
movq %rbp, %rdi
leaq 0xdee2d(%rip), %rsi     # 0xfd0f1
movq %r12, %rdx
callq 0x44c0
testl %eax, %eax
sete %r12b
jmp 0x1e244
movq 0x131ce2(%rip), %r14    # 0x14ffc0
leaq 0xe4299(%rip), %rbx     # 0x10257e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4480
leaq 0xdedae(%rip), %rsi     # 0xfd0a5
movq %rax, %rdi
callq 0x4480
movq %rax, %rdi
movq %r15, %rsi
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
leaq 0xde897(%rip), %rsi     # 0xfcbb3
movq %r14, %rdi
callq 0x4480
movq %rax, %rdi
movq %rbx, %rsi
callq 0x4480
jmp 0x1e555
leaq 0x360(%rsp), %rbx
leaq 0x210(%rsp), %r15
leaq 0x28(%rsp), %r13
leaq 0x1a0(%rsp), %rbp
leaq 0x20(%rsp), %r14
xorl %ecx, %ecx
movq 0x38(%rsp), %r12
testl %ecx, %ecx
jne 0x1e3d4
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq (%rsp), %rdx
callq 0x4690
testq %rax, %rax
je 0x1e540
movq %rbx, %rdi
leaq 0xded75(%rip), %rsi     # 0xfd0fa
movq %r15, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
leaq 0x50(%rsp), %r10
pushq %r10
leaq 0x288(%rsp), %r10
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x308(%rsp), %r10
pushq %r10
callq 0x4470
addq $0x20, %rsp
xorl %ecx, %ecx
cmpl $0x8, %eax
sete %cl
jmp 0x1e35d
leaq 0xdf5df(%rip), %rdi     # 0xfd9ae
jmp 0x1e547
leaq 0xe0(%rsp), %rdi
callq 0x3dc5a
leaq 0x2f0(%r12), %r14
pushq $0x1
popq %rcx
leaq 0x70(%rsp), %r15
leaq 0x210(%rsp), %r12
leaq 0x180(%rsp), %r13
movzbl 0x30(%rsp), %eax
movl %eax, 0x40(%rsp)
leaq 0x360(%rsp), %rbx
movq $0x0, 0x30(%rsp)
testl %ecx, %ecx
je 0x1e56b
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0xe8(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xf0(%rsp)
movq %r15, %rdi
movq %r12, %rbp
movq %r12, %rsi
leaq 0x58(%rsp), %rdx
callq 0x1b80e
leaq 0x120(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
callq 0x4560
movq %r15, %rdi
callq 0x4210
movq %r13, %rdi
movq %r12, %rsi
callq 0x45d0
movq %r13, %rdi
movl 0x40(%rsp), %esi
callq 0x1b1bb
movsd %xmm0, 0x110(%rsp)
movq %r13, %rdi
callq 0x4210
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
movq %r14, %rdi
leaq 0xe0(%rsp), %rsi
callq 0xa7d8
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq (%rsp), %rdx
callq 0x4690
testq %rax, %rax
je 0x1e5c6
movq %rbp, %r12
movq %rbx, %rdi
leaq 0xdec0e(%rip), %rsi     # 0xfd0fa
movq %rbp, %rdx
leaq 0x28(%rsp), %rcx
leaq 0x1a0(%rsp), %r8
leaq 0x20(%rsp), %r9
xorl %eax, %eax
leaq 0x50(%rsp), %r10
pushq %r10
leaq 0x288(%rsp), %r10
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x308(%rsp), %r10
pushq %r10
callq 0x4470
addq $0x20, %rsp
xorl %ecx, %ecx
cmpl $0x8, %eax
sete %cl
incq 0x30(%rsp)
jmp 0x1e41b
leaq 0xdf41b(%rip), %rdi     # 0xfd962
callq 0x4670
movq (%rsp), %rdi
callq 0x4490
xorl %ebx, %ebx
movl %ebx, %eax
addq $0x558, %rsp            # imm = 0x558
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x70(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x6402e
leaq 0x58(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x6402e
movq 0x38(%rsp), %r12
leaq 0x90(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x6402e
xorl %ebx, %ebx
movl %ebx, %eax
leaq 0xde5b7(%rip), %r15     # 0xfcb74
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
leaq 0xdf356(%rip), %rdi     # 0xfd923
callq 0x4670
movq (%rsp), %rdi
callq 0x4490
xorl %ebx, %ebx
jmp 0x1e60d
jmp 0x1ea8b
jmp 0x1ea8b
jmp 0x1ea8b
movq %rax, %rbx
leaq 0x180(%rsp), %rdi
callq 0x4210
jmp 0x1ea8e
jmp 0x1ea8b
jmp 0x1ea8b
leaq 0xe0(%rsp), %rdi
callq 0xa808
jmp 0x1e557
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x1e6a6
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
incl %ebx
movl %ebx, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
leaq 0x360(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq (%rsp), %rdx
callq 0x4690
testq %rax, %rax
je 0x1e71d
subq $0x8, %rsp
leaq 0xdea4a(%rip), %rsi     # 0xfd116
leaq 0x368(%rsp), %rdi
leaq 0x218(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x1a8(%rsp), %r8
leaq 0x28(%rsp), %r9
xorl %eax, %eax
leaq 0x288(%rsp), %r10
pushq %r10
leaq 0x28(%rsp), %r10
pushq %r10
leaq 0x308(%rsp), %r10
pushq %r10
callq 0x4470
addq $0x20, %rsp
cmpl $0x7, %eax
jne 0x1ea61
incl %ebx
movl %ebx, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
movq 0x30(%rsp), %rax
movl %eax, 0x110(%r12)
movq (%rsp), %rdi
callq 0x4490
movq 0x80(%rsp), %rax
movq %rax, 0x48(%r12)
movups 0x70(%rsp), %xmm0
movups %xmm0, 0x38(%r12)
leaq 0x58(%rsp), %rdi
movups (%rdi), %xmm0
movups %xmm0, 0x50(%r12)
movq 0x10(%rdi), %rax
movq %rax, 0x60(%r12)
leaq 0x90(%rsp), %rsi
movupd (%rsi), %xmm0
movupd %xmm0, 0x68(%r12)
movq 0x10(%rsi), %rax
movq %rax, 0x78(%r12)
callq 0x641b2
movapd %xmm0, (%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x641b2
movapd %xmm0, 0x40(%rsp)
leaq 0x70(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x641b2
movapd (%rsp), %xmm1
unpcklpd 0x40(%rsp), %xmm1       # xmm1 = xmm1[0],mem[0]
mulpd 0xde351(%rip), %xmm1    # 0xfcb20
divpd 0xde359(%rip), %xmm1    # 0xfcb30
mulsd 0xde311(%rip), %xmm0    # 0xfcaf0
movupd %xmm1, 0x20(%r12)
divsd 0xde30a(%rip), %xmm0    # 0xfcaf8
movsd %xmm0, 0x30(%r12)
leaq 0x70(%rsp), %rdi
callq 0x63758
movsd %xmm0, 0x8(%r12)
leaq 0x58(%rsp), %rdi
callq 0x63758
movsd %xmm0, 0x10(%r12)
leaq 0x90(%rsp), %rdi
callq 0x63758
movsd %xmm0, 0x18(%r12)
movq %r12, %rdi
callq 0x3e88c
leaq 0x320(%r12), %r15
movq %r15, %rdi
movq 0xa8(%rsp), %rsi
callq 0x4810
movq 0x320(%r12), %rax
movq 0x328(%r12), %rcx
leaq (%rax,%rcx), %rdx
leaq (%rax,%rcx), %rsi
addq $-0x4, %rsi
movq %r15, %rdi
callq 0x4100
xorl %r15d, %r15d
leaq 0xb0(%rsp), %r12
pushq $0x1
popq %rbp
pushq $0x2
popq %r13
subq $0x1, 0x30(%rsp)
jb 0x1ea76
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd 0x8(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd 0x10(%rax), %xmm2
movq %r12, %rdi
movq 0x38(%rsp), %rsi
movsd (%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
callq 0x3f1ce
movq %r12, %rdi
xorl %esi, %esi
callq 0x63c78
movsd (%rax), %xmm0
callq 0x6377f
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, 0x18(%rax)
movq %r12, %rdi
movl %ebp, %esi
callq 0x63c78
movsd (%rax), %xmm0
callq 0x6377f
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, 0x20(%rax)
movq %r12, %rdi
movl %r13d, %esi
callq 0x63c78
movsd (%rax), %xmm0
callq 0x6377f
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, 0x28(%rax)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd 0x18(%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd 0x20(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd 0x28(%rax), %xmm2
leaq 0xc8(%rsp), %rdi
movq 0x38(%rsp), %rsi
movsd (%rsp), %xmm0
movsd 0x40(%rsp), %xmm1
callq 0x3f0e6
movq 0xd8(%rsp), %rax
movq %rax, 0xc0(%rsp)
movupd 0xc8(%rsp), %xmm0
movapd %xmm0, 0xb0(%rsp)
movq %r12, %rdi
xorl %esi, %esi
callq 0x63c78
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, (%rax)
movq %r12, %rdi
movl %ebp, %esi
callq 0x63c78
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, 0x8(%rax)
movq %r12, %rdi
movl %r13d, %esi
callq 0x63c78
movsd (%rax), %xmm0
movsd %xmm0, (%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa73a
movsd (%rsp), %xmm0
movsd %xmm0, 0x10(%rax)
incq %r15
jmp 0x1e881
leaq 0xdee85(%rip), %rdi     # 0xfd8ed
callq 0x4670
movq (%rsp), %rdi
callq 0x4490
cmpl $0x3, %ebx
setae %bl
jmp 0x1e60d
jmp 0x1ea8b
jmp 0x1ea8b
jmp 0x1ea8b
jmp 0x1ea8b
jmp 0x1ea8b
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
callq 0xa808
movq %rbx, %rdi
callq 0x4780