// File: /workspace/github_cpp_makefile_star2_O2/Luthaf[P]zeopp/networkstorage.cc
// Function: read_xyz(_IO_FILE*, MOLECULE*, char const*)
void read_xyz(FILE *input, MOLECULE *mol, const char *filename) {
  int length = 100;
  char ch1[length];
  int status = 0;
  int num_atoms = 0;
  if(fgets(ch1, length, input)!=NULL) {
    string str = string(ch1);
//    printf("DEBUG: read char array as \"%s\", parsed to string as \"%s\"\n", ch, str.c_str());
    int pos=0;
    char c = str[pos];

//@@@ WARNING - there is a memory leak here and I cannot tell why

    while(c<=0) { //for some reason, xyz files from marvinsketch have three characters at the beginning with ascii codes <0 - very strange, but we need to remove these to succesfully parse any data
      pos++;
      c = str[pos];
    }
    char ch2[length];
    str.copy(ch2, str.size()-pos, pos);
    status = sscanf(ch2, "%d", &num_atoms);
    //printf("DEBUG: parsed num_atoms = %d from string %s (status = %d)\n", num_atoms, ch2, status);
  } else {
    printf("ERROR: could not read string\n");
    exit(EXIT_FAILURE);
  }
  //printf("(status = %d) DEBUG: read that there will be %d atoms\n", status, num_atoms);
  search_for_char(input, '\n'); //now we are on a line containing an atom
  for(int i=0; i<num_atoms; i++) {
    XYZ a;
    char e[length];
    char ch[length];
    if(fgets(ch, length, input)!=NULL) {
      string str = string(ch);
      //printf("DEBUG: read char array as \"%s\", parsed to string as \"%s\"\n", ch, str.c_str());
      int pos=0;
      char c = str[pos];
      while(c<=0) { //for some reason, xyz files from marvinsketch have three characters at the beginning with ascii codes <0 - very strange, but we need to remove these to succesfully parse any data
        pos++;
        c = str[pos];
      }
      char ch2[length];
      str.copy(ch2, str.size()-pos, pos);
      status = sscanf(ch2, "%s %lf %lf %lf", e, &a.x, &a.y, &a.z);
    } else {
      printf("ERROR: could not read expected atom coord string from %s - %d out of %d atom coords were read\n", filename, i, num_atoms);
      exit(EXIT_FAILURE);
    }

    mol->atoms_xyz.push_back(a);
    string name_string(e);
    mol->atoms_label.push_back(name_string);
    //parse out any digits from the name of the element
    int name_length = name_string.length();
    int digit_pos = -1;
    for(int i=0; i<name_length && digit_pos==-1; i++) {
      if(isdigit(name_string[i])) digit_pos = i;
    }
    if(digit_pos==0) { //first character was a digit - this is no good
      printf("ERROR: could not parse label from atom beginning with a digit in read_xyz: %s: %s\n", filename, name_string.c_str());
      exit(EXIT_FAILURE);
    } else {
      string element(e);
      if(digit_pos>0) {
        element = name_string.substr(0,digit_pos);
      }
      mol->atoms_type.push_back(element);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
movq %rdx, %rbx
movq %rsi, -0x40(%rbp)
movq %rdi, %rdx
andl $0x0, -0x2c(%rbp)
leaq -0x140(%rbp), %rdi
pushq $0x64
popq %rsi
movq %rdx, -0x48(%rbp)
callq 0x4690
testq %rax, %rax
je 0x4b245
leaq -0x68(%rbp), %rdi
leaq -0x140(%rbp), %rsi
leaq -0xb8(%rbp), %rdx
callq 0x1b80e
movq %rbx, -0x38(%rbp)
pushq $-0x1
popq %rax
movq -0x68(%rbp), %rcx
cmpb $0x0, 0x1(%rcx,%rax)
leaq 0x1(%rax), %rax
jle 0x4aff0
movq %rsp, %r14
movq %rsp, %rbx
addq $-0x70, %rbx
movq %rbx, %rsp
leaq -0x68(%rbp), %rdi
movq 0x8(%rdi), %rdx
movl %eax, %ecx
subq %rcx, %rdx
movq %rbx, %rsi
callq 0x40a0
leaq 0xc4227(%rip), %rsi     # 0x10f24b
leaq -0x2c(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4470
movq %r14, %rsp
leaq -0x68(%rbp), %r12
movq %r12, %rdi
callq 0x4210
pushq $0xa
popq %rsi
movq -0x48(%rbp), %rdi
callq 0x78a19
movq -0x40(%rbp), %rax
leaq 0x30(%rax), %rcx
movq %rcx, -0x78(%rbp)
addq $0x18, %rax
movq %rax, -0x70(%rbp)
leaq -0xb8(%rbp), %r14
pushq $-0x1
popq %r13
xorl %eax, %eax
cmpl -0x2c(%rbp), %eax
jge 0x4b202
movl %eax, -0x30(%rbp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
leaq -0x98(%rbp), %rdi
callq 0x6402e
movq %rsp, -0x80(%rbp)
movq %rsp, %rbx
addq $-0x70, %rbx
movq %rbx, %rsp
movq %rsp, %r15
addq $-0x70, %r15
movq %r15, %rsp
movq %r15, %rdi
pushq $0x64
popq %rsi
movq -0x48(%rbp), %rdx
callq 0x4690
testq %rax, %rax
je 0x4b211
movq %r12, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x1b80e
movq -0x68(%rbp), %rcx
pushq $-0x1
popq %rax
cmpb $0x0, 0x1(%rcx,%rax)
leaq 0x1(%rax), %rax
jle 0x4b0d4
movq %rsp, %r14
movq %rsp, %r15
addq $-0x70, %r15
movq %r15, %rsp
movq -0x60(%rbp), %rdx
movl %eax, %ecx
subq %rcx, %rdx
movq %r12, %rdi
movq %r15, %rsi
callq 0x40a0
movq %r15, %rdi
leaq 0xb7ccd(%rip), %rsi     # 0x102dd7
movq %rbx, %rdx
leaq -0x98(%rbp), %r15
movq %r15, %rcx
leaq -0x90(%rbp), %r8
leaq -0x88(%rbp), %r9
xorl %eax, %eax
callq 0x4470
movq %r14, %rsp
movq %r12, %rdi
callq 0x4210
movq -0x40(%rbp), %rdi
movq %r15, %rsi
callq 0x4beaa
movq %r12, %rdi
movq %rbx, %rsi
leaq -0xb8(%rbp), %r14
movq %r14, %rdx
callq 0x1b80e
movq -0x78(%rbp), %rdi
movq %r12, %rsi
callq 0x2451e
movslq -0x60(%rbp), %rax
movq -0x68(%rbp), %rdx
xorl %ecx, %ecx
movl %r13d, %r15d
cmpl $-0x1, %r15d
jne 0x4b192
cmpq %rax, %rcx
jge 0x4b192
movsbl (%rdx,%rcx), %esi
addl $-0x30, %esi
cmpl $0xa, %esi
movl %r13d, %r15d
cmovbl %ecx, %r15d
incq %rcx
jmp 0x4b171
testl %r15d, %r15d
je 0x4b22b
movq %r14, %rdi
movq %rbx, %rsi
leaq -0xd8(%rbp), %rbx
movq %rbx, %rdx
callq 0x1b80e
testl %r15d, %r15d
jle 0x4b1d8
movl %r15d, %ecx
movq %rbx, %rdi
movq %r12, %rsi
xorl %edx, %edx
callq 0x44d0
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4560
movq %rbx, %rdi
callq 0x4210
movq -0x70(%rbp), %rdi
movq %r14, %rsi
callq 0x2451e
movq %r14, %rdi
callq 0x4210
movq %r12, %rdi
callq 0x4210
movq -0x80(%rbp), %rsp
movl -0x30(%rbp), %eax
incl %eax
jmp 0x4b06e
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x2c(%rbp), %ecx
leaq 0xb7bcb(%rip), %rdi     # 0x102de6
movq -0x38(%rbp), %rsi
movl -0x30(%rbp), %edx
xorl %eax, %eax
callq 0x4050
jmp 0x4b23d
leaq 0xb7c13(%rip), %rdi     # 0x102e45
movq -0x38(%rbp), %rsi
xorl %eax, %eax
callq 0x4050
pushq $0x1
popq %rdi
callq 0x524c
leaq 0xb7fc3(%rip), %rdi     # 0x10320f
callq 0x4670
jmp 0x4b23d
jmp 0x4b275
jmp 0x4b270
jmp 0x4b25b
jmp 0x4b270
movq %rax, %rbx
leaq -0xb8(%rbp), %rdi
callq 0x4210
jmp 0x4b278
jmp 0x4b275
jmp 0x4b275
movq %rax, %rbx
jmp 0x4b281
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x4210
movq %rbx, %rdi
callq 0x4780
nop