// File: 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
subq $0x210, %rsp            # imm = 0x210
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x64, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x180(%rbp)
movq %rdi, %rsp
movq %rax, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
movl -0x1c(%rbp), %esi
movq -0x8(%rbp), %rdx
callq 0x57e0
cmpq $0x0, %rax
je 0xad508
leaq -0x59(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x5900
movq -0x180(%rbp), %rsi
movq -0x188(%rbp), %rdx
leaq -0x58(%rbp), %rdi
callq 0x47530
jmp 0xad3e4
leaq -0x59(%rbp), %rdi
callq 0x55d0
movl $0x0, -0x70(%rbp)
movslq -0x70(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x5a00
movq %rax, -0x190(%rbp)
jmp 0xad40a
movq -0x190(%rbp), %rax
movb (%rax), %al
movb %al, -0x71(%rbp)
movsbl -0x71(%rbp), %eax
cmpl $0x0, %eax
jg 0xad47f
movl -0x70(%rbp), %eax
incl %eax
movl %eax, -0x70(%rbp)
movslq -0x70(%rbp), %rsi
leaq -0x58(%rbp), %rdi
callq 0x5a00
movq %rax, -0x198(%rbp)
jmp 0xad43d
movq -0x198(%rbp), %rax
movb (%rax), %al
movb %al, -0x71(%rbp)
jmp 0xad416
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x59(%rbp), %rdi
callq 0x55d0
jmp 0xada35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x5300
jmp 0xada35
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x80(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1a0(%rbp)
movq %rcx, %rsp
movq %rax, -0x88(%rbp)
leaq -0x58(%rbp), %rdi
movq %rdi, -0x1a8(%rbp)
callq 0x5360
movq -0x1a8(%rbp), %rdi
movq -0x1a0(%rbp), %rsi
movq %rax, %rdx
movslq -0x70(%rbp), %rcx
subq %rcx, %rdx
callq 0x50c0
jmp 0xad4da
movq -0x1a0(%rbp), %rdi
leaq 0x15ceff(%rip), %rsi    # 0x20a3e7
leaq -0x38(%rbp), %rdx
movb $0x0, %al
callq 0x5520
movl %eax, -0x34(%rbp)
movq -0x80(%rbp), %rax
movq %rax, %rsp
leaq -0x58(%rbp), %rdi
callq 0x5300
jmp 0xad520
leaq 0x14d95b(%rip), %rdi    # 0x1fae6a
movb $0x0, %al
callq 0x5070
movl $0x1, %edi
callq 0x67c0
movq -0x8(%rbp), %rdi
movl $0xa, %esi
callq 0x115240
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jge 0xada29
leaq -0xa8(%rbp), %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0xea8e0
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0xb0(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1b8(%rbp)
movq %rcx, %rsp
movq %rax, -0xb8(%rbp)
movl -0x1c(%rbp), %eax
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x1b0(%rbp)
movq %rdi, %rsp
movq %rax, -0xc0(%rbp)
movl -0x1c(%rbp), %esi
movq -0x8(%rbp), %rdx
callq 0x57e0
cmpq $0x0, %rax
je 0xad76f
leaq -0xe1(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x5900
movq -0x1b0(%rbp), %rsi
movq -0x1c0(%rbp), %rdx
leaq -0xe0(%rbp), %rdi
callq 0x47530
jmp 0xad5f5
leaq -0xe1(%rbp), %rdi
callq 0x55d0
movl $0x0, -0xe8(%rbp)
movslq -0xe8(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x5a00
movq %rax, -0x1c8(%rbp)
jmp 0xad627
movq -0x1c8(%rbp), %rax
movb (%rax), %al
movb %al, -0xe9(%rbp)
movsbl -0xe9(%rbp), %eax
cmpl $0x0, %eax
jg 0xad6b7
movl -0xe8(%rbp), %eax
incl %eax
movl %eax, -0xe8(%rbp)
movslq -0xe8(%rbp), %rsi
leaq -0xe0(%rbp), %rdi
callq 0x5a00
movq %rax, -0x1d0(%rbp)
jmp 0xad66c
movq -0x1d0(%rbp), %rax
movb (%rax), %al
movb %al, -0xe9(%rbp)
jmp 0xad636
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe1(%rbp), %rdi
callq 0x55d0
jmp 0xada35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x5300
jmp 0xada35
movl -0x1c(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0xf8(%rbp)
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1d8(%rbp)
movq %rcx, %rsp
movq %rax, -0x100(%rbp)
leaq -0xe0(%rbp), %rdi
movq %rdi, -0x1e0(%rbp)
callq 0x5360
movq -0x1e0(%rbp), %rdi
movq -0x1d8(%rbp), %rsi
movq %rax, %rdx
movslq -0xe8(%rbp), %rcx
subq %rcx, %rdx
callq 0x50c0
jmp 0xad71b
movq -0x1b8(%rbp), %rdx
movq -0x1d8(%rbp), %rdi
leaq -0xa8(%rbp), %rcx
leaq -0xa8(%rbp), %r8
addq $0x8, %r8
leaq -0xa8(%rbp), %r9
addq $0x10, %r9
leaq 0x14d73b(%rip), %rsi    # 0x1fae88
movb $0x0, %al
callq 0x5520
movl %eax, -0x34(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, %rsp
leaq -0xe0(%rbp), %rdi
callq 0x5300
jmp 0xad794
movq -0x18(%rbp), %rsi
movl -0x8c(%rbp), %edx
movl -0x38(%rbp), %ecx
leaq 0x14d714(%rip), %rdi    # 0x1fae97
movb $0x0, %al
callq 0x5070
movl $0x1, %edi
callq 0x67c0
movq -0x10(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xafc20
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1e8(%rbp)
callq 0x5900
movq -0x1b8(%rbp), %rsi
movq -0x1e8(%rbp), %rdx
leaq -0x120(%rbp), %rdi
callq 0x47530
jmp 0xad7d3
leaq -0x121(%rbp), %rdi
callq 0x55d0
movq -0x10(%rbp), %rdi
addq $0x30, %rdi
leaq -0x120(%rbp), %rsi
callq 0x59330
jmp 0xad7f5
leaq -0x120(%rbp), %rdi
callq 0x5790
movl %eax, -0x128(%rbp)
movl $0xffffffff, -0x12c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x130(%rbp)
movl -0x130(%rbp), %ecx
xorl %eax, %eax
cmpl -0x128(%rbp), %ecx
movb %al, -0x1e9(%rbp)
jge 0xad841
cmpl $-0x1, -0x12c(%rbp)
sete %al
movb %al, -0x1e9(%rbp)
movb -0x1e9(%rbp), %al
testb $0x1, %al
jne 0xad850
jmp 0xad8d2
movslq -0x130(%rbp), %rsi
leaq -0x120(%rbp), %rdi
callq 0x5a00
movq %rax, -0x1f8(%rbp)
jmp 0xad86c
movq -0x1f8(%rbp), %rax
movsbl (%rax), %edi
callq 0x5490
cmpl $0x0, %eax
je 0xad8bc
movl -0x130(%rbp), %eax
movl %eax, -0x12c(%rbp)
jmp 0xad8bc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x121(%rbp), %rdi
callq 0x55d0
jmp 0xada35
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
jmp 0xada1b
jmp 0xad8be
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0xad81b
cmpl $0x0, -0x12c(%rbp)
jne 0xad916
movq -0x18(%rbp), %rax
movq %rax, -0x200(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x5200
movq -0x200(%rbp), %rsi
movq %rax, %rdx
leaq 0x14d5f3(%rip), %rdi    # 0x1faef6
xorl %eax, %eax
callq 0x5070
jmp 0xad90c
movl $0x1, %edi
callq 0x67c0
leaq -0x151(%rbp), %rdi
movq %rdi, -0x208(%rbp)
callq 0x5900
movq -0x1b8(%rbp), %rsi
movq -0x208(%rbp), %rdx
leaq -0x150(%rbp), %rdi
callq 0x47530
jmp 0xad945
leaq -0x151(%rbp), %rdi
callq 0x55d0
cmpl $0x0, -0x12c(%rbp)
jle 0xad9cf
movslq -0x12c(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x178(%rbp), %rdi
leaq -0x120(%rbp), %rsi
callq 0x5590
jmp 0xad97a
leaq -0x150(%rbp), %rdi
leaq -0x178(%rbp), %rsi
callq 0x5680
leaq -0x178(%rbp), %rdi
callq 0x5300
jmp 0xad9cf
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x151(%rbp), %rdi
callq 0x55d0
jmp 0xada1b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x68(%rbp)
movl %eax, -0x6c(%rbp)
leaq -0x150(%rbp), %rdi
callq 0x5300
jmp 0xada1b
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
leaq -0x150(%rbp), %rsi
callq 0x59330
jmp 0xad9e5
leaq -0x150(%rbp), %rdi
callq 0x5300
leaq -0x120(%rbp), %rdi
callq 0x5300
movq -0xb0(%rbp), %rax
movq %rax, %rsp
movl -0x8c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8c(%rbp)
jmp 0xad538
leaq -0x120(%rbp), %rdi
callq 0x5300
jmp 0xada35
movq -0x28(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x68(%rbp), %rdi
callq 0x58f0
nop