// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rsi, -0x60(%rbp)
movq %rdi, %rdx
movl $0x0, -0x2c(%rbp)
leaq -0x140(%rbp), %rdi
movl $0x64, %esi
movq %rdx, -0x68(%rbp)
callq 0x4690
testq %rax, %rax
je 0x66d85
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x140(%rbp), %rbx
movq %rbx, %rdi
callq 0x41e0
leaq (%rax,%rbp), %rdx
addq $-0x140, %rdx           # imm = 0xFEC0
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x22538
movq -0x50(%rbp), %rax
cmpb $0x0, (%rax)
jle 0x66a32
xorl %ecx, %ecx
jmp 0x66a41
xorl %ecx, %ecx
cmpb $0x0, 0x1(%rax,%rcx)
leaq 0x1(%rcx), %rcx
jle 0x66a34
movl %ecx, %ecx
movq %rsp, %r14
movq %rsp, %rbx
addq $-0x70, %rbx
movq %rbx, %rsp
leaq -0x50(%rbp), %rdi
movq 0x8(%rdi), %rdx
subq %rcx, %rdx
movq %rbx, %rsi
callq 0x40b0
movq %r15, -0x58(%rbp)
leaq 0xf791f(%rip), %rsi     # 0x15e38b
leaq -0x2c(%rbp), %rdx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x4430
movq %r14, %rsp
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x66a96
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x4470
movq -0x68(%rbp), %rdi
movl $0xa, %esi
callq 0xa156f
cmpl $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rax
jle 0x66d3c
leaq 0x30(%rax), %rcx
movq %rcx, -0xb0(%rbp)
addq $0x18, %rax
movq %rax, -0xa8(%rbp)
leaq -0x50(%rbp), %rbx
leaq -0xd8(%rbp), %r14
xorl %r13d, %r13d
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
leaq -0xa0(%rbp), %rdi
callq 0x84364
movq %rsp, -0xb8(%rbp)
movq %rsp, %r15
addq $-0x70, %r15
movq %r15, %rsp
movq %rsp, %r12
addq $-0x70, %r12
movq %r12, %rsp
movq %r12, %rdi
movl $0x64, %esi
movq -0x68(%rbp), %rdx
callq 0x4690
testq %rax, %rax
je 0x66d4b
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %r12, %rdi
callq 0x41e0
leaq (%rax,%r12), %rdx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x22538
movq -0x50(%rbp), %rax
cmpb $0x0, (%rax)
jle 0x66b4c
xorl %ecx, %ecx
jmp 0x66b5b
xorl %ecx, %ecx
cmpb $0x0, 0x1(%rax,%rcx)
leaq 0x1(%rcx), %rcx
jle 0x66b4e
movl %ecx, %ecx
movq %rsp, %rbx
movq %rsp, %r12
addq $-0x70, %r12
movq %r12, %rsp
movq -0x48(%rbp), %rdx
subq %rcx, %rdx
leaq -0x50(%rbp), %rdi
movq %r12, %rsi
callq 0x40b0
movq %r12, %rdi
leaq 0xed2f2(%rip), %rsi     # 0x153e77
movq %r15, %rdx
leaq -0xa0(%rbp), %rcx
leaq -0x98(%rbp), %r8
leaq -0x90(%rbp), %r9
xorl %eax, %eax
callq 0x4430
movq %rbx, %rsp
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x66bc0
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x4470
movq -0x60(%rbp), %rdi
movq 0x8(%rdi), %rsi
cmpq 0x10(%rdi), %rsi
leaq -0x50(%rbp), %rbx
je 0x66bee
movq -0x90(%rbp), %rax
movq %rax, 0x10(%rsi)
movups -0xa0(%rbp), %xmm0
movups %xmm0, (%rsi)
addq $0x18, 0x8(%rdi)
jmp 0x66bfa
leaq -0xa0(%rbp), %rdx
callq 0x69b82
leaq -0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq %r15, %rdi
callq 0x41e0
leaq (%rax,%r15), %rdx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x22538
movq -0xb0(%rbp), %rdi
movq %rbx, %rsi
callq 0x2f520
movq -0x48(%rbp), %rax
movl $0xffffffff, %r12d      # imm = 0xFFFFFFFF
testl %eax, %eax
jle 0x66c74
movq -0x50(%rbp), %rcx
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq $-0x1, %rsi
movsbl 0x1(%rcx,%rsi), %edi
addl $-0x3a, %edi
leaq 0x1(%rsi), %rdx
cmpl $-0xb, %edi
ja 0x66c63
addq $0x2, %rsi
cmpq %rax, %rsi
movq %rdx, %rsi
jb 0x66c46
cmpl $-0xa, %edi
jb 0x66c74
testq %rdx, %rdx
je 0x66d65
movl %edx, %r12d
leaq -0x78(%rbp), %rax
movq %rax, -0x88(%rbp)
movq %r15, %rdi
callq 0x41e0
leaq (%rax,%r15), %rdx
leaq -0x88(%rbp), %rdi
movq %r15, %rsi
callq 0x22538
testl %r12d, %r12d
jle 0x66ce0
movl %r12d, %ecx
movq %r14, %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x4490
leaq -0x88(%rbp), %rdi
movq %r14, %rsi
callq 0x4540
movq -0xd8(%rbp), %rdi
leaq -0xc8(%rbp), %rax
cmpq %rax, %rdi
je 0x66ce0
movq -0xc8(%rbp), %rsi
incq %rsi
callq 0x4470
movq -0xa8(%rbp), %rdi
leaq -0x88(%rbp), %rsi
callq 0x2f520
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x66d0f
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x4470
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x66d28
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x4470
movq -0xb8(%rbp), %rsp
incl %r13d
cmpl -0x2c(%rbp), %r13d
jl 0x66ad6
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl -0x2c(%rbp), %ecx
leaq 0xed131(%rip), %rdi     # 0x153e86
movq -0x58(%rbp), %rsi
movl %r13d, %edx
xorl %eax, %eax
callq 0x4060
jmp 0x66d7b
movq -0x50(%rbp), %rdx
leaq 0xed175(%rip), %rdi     # 0x153ee5
movq -0x58(%rbp), %rsi
xorl %eax, %eax
callq 0x4060
movl $0x1, %edi
callq 0x72bc
leaq 0xed523(%rip), %rdi     # 0x1542af
callq 0x4660
jmp 0x66d7b
jmp 0x66dc5
jmp 0x66dc0
jmp 0x66d99
movq %rax, %rbx
movq -0x88(%rbp), %rdi
leaq -0x78(%rbp), %rax
cmpq %rax, %rdi
je 0x66dc8
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x4470
jmp 0x66dc8
jmp 0x66dc5
jmp 0x66dc0
jmp 0x66dc5
movq %rax, %rbx
jmp 0x66de1
movq %rax, %rbx
movq -0x50(%rbp), %rdi
leaq -0x40(%rbp), %rax
cmpq %rax, %rdi
je 0x66de1
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x4470
movq %rbx, %rdi
callq 0x4780
nop