// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/read.c
// Function: Read_PDB
void Read_PDB(int *nres,   // Number of amino acid residues
	      struct residue **seq, // Where ligand and aa res are stored
	      int *n_lig,  // Number of ligand residues
	      struct residue **ligres, // Pointer to first ligand residue
	      char *chain, // Chain identifiers
	      int *ANISOU, // Anisotropic B factors?
	      int *nmr,    // NMR structure?
	      int *natoms, // Number of protein atoms
	      atom **atoms,// Pointer to protein atoms
	      int *na_lig, // Number of ligand atoms
	      atom **ligatoms, // Pointer to ligand atoms
	      struct chain **chains, // Pointer to protein chains
	      int *Nchain, // Number of protein chains (1-Nchain)
	      float *TEMP, // Temperature of the experiment, if given
	      char *pdbid, // PDB identifier
	      char *file_pdb) // INPUT: Path to PDB file
{
  strcpy(CHAIN_CODE,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz:=-_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz:=-_");

  /****************** Reading atoms: ************************/
  int i; 
  int numres=Count_residues(file_pdb, chain); L_MAX=numres;
  int numatoms=ATOM_MAX*numres;
  atom *atom_read=malloc(numatoms*sizeof(atom));
  *seq=malloc(numres*sizeof(struct residue)); 
  //for(i=0;i<numres;i++){(*seq)[i].chain='-';}
  
  *nres=Read_coord(file_pdb, nmr, *seq, atom_read, chain, ANISOU, TEMP);
  if(*nres<=0){
    printf("WARNING, file %s, no residue found\n", file_pdb); return;
  }
  GetPdbId(file_pdb,pdbid); //if(*chain==' ')*chain='_';

  // *n_lig=Read_ligand(file_pdb, *seq+*nres, atom_read+*natoms, na_lig);
  // Ligand atoms are stored at the end

  
  /****************** Storing atoms: ************************/
  *natoms=0;  
  for(i=0; i<*nres; i++)*natoms+=(*seq)[i].n_atom;
  *atoms=malloc((*natoms+*na_lig)*sizeof(atom));
  printf("PDB %s chain %s nres=%d natoms=%d\n",pdbid, chain, *nres, *natoms);

  /**************** Set chains *********************/
  // Eliminate chains with fewer than MIN_ATOM atoms and their residues
  // Allocate and store chains
  // Allocate and store atoms
  *Nchain=Set_chains(chains, atoms, natoms, *seq, nres);
  printf("%d chains found: ", *Nchain);
  for(i=0; i<*Nchain; i++)printf("%c", (*chains)[i].label);
  printf(".\n");
  free(atom_read); //modyves: this was never freed

  int N_seqres[*Nchain];
  char *seqres[*Nchain], chainlabel[*Nchain+1];
  for(i=0; i<*Nchain; i++)chainlabel[i]=(*chains)[i].label;
  chainlabel[*Nchain]='\0';
  if(Read_seqres(seqres, N_seqres, *Nchain, file_pdb, chainlabel)<0){
    // Seqres not found, copy from atoms
    for(i=0; i<*Nchain; i++){
      struct chain *ch=*chains+i;
      int L=ch->nres;
      N_seqres[i]=L;
      seqres[i]=malloc(L*sizeof(char)); char *seq=seqres[i];
      for(int j=0; j<L; j++)seq[j]=ch->res[j].amm;
    }
  }
  for(i=0; i<*Nchain; i++){
    struct chain *ch=*chains+i;
    ch->seqres=seqres[i];
    ch->N_seqres=N_seqres[i];
    ch->seq=malloc(ch->nres*sizeof(char));
    for(int j=0; j<ch->nres; j++)ch->seq[j]=ch->res[j].amm;
    ch->ali_seqres=malloc(N_seqres[i]*sizeof(int));
    Align_seqres(ch->ali_seqres, ch->seqres, ch->N_seqres, ch->seq, ch->nres);
  }
  
  if(SC_BEFORE){
    // Change the order of protein atoms setting side chain before CO
    // and testing that CA is before C
    Order_atoms(*atoms, *natoms, *seq, *nres);
    // Find next atom in original PDB order, i_next
    Find_PDB_order(*atoms, *natoms);
  }

  return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r9, -0x30(%rbp)
movq %r8, %r14
movq %rsi, -0x78(%rbp)
movq %rdi, %r13
movq 0x58(%rbp), %rbx
movq 0x50(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x40(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x48(%rbp), %rax
movq %rax, -0x60(%rbp)
leaq 0x5d6ce(%rip), %rdi     # 0x69eb0
leaq 0x4939f(%rip), %rsi     # 0x55b88
movl $0x85, %edx
callq 0x21a0
movq %rbx, %rdi
movq %r14, %rsi
callq 0xcb68
movl %eax, %r12d
leaq 0x5d688(%rip), %rax     # 0x69e90
movl %r12d, (%rax)
imull $0x32, %r12d, %eax
cltq
imulq $0x70, %rax, %rdi
callq 0x21d0
movq %rax, %r15
movslq %r12d, %rdi
movq -0x78(%rbp), %r12
shlq $0x5, %rdi
callq 0x21d0
movq %rax, (%r12)
subq $0x8, %rsp
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
movq %rax, %rdx
movq %r15, -0x40(%rbp)
movq %r15, %rcx
movq %r14, -0x58(%rbp)
movq %r14, %r8
movq -0x30(%rbp), %r9
pushq -0x60(%rbp)
callq 0xd00d
addq $0x10, %rsp
movl %eax, (%r13)
testl %eax, %eax
jle 0xcb49
movq %rbx, -0x60(%rbp)
movq %rbx, %rdi
movq -0x38(%rbp), %rsi
callq 0xdc8a
movq -0x70(%rbp), %rbx
movl $0x0, (%rbx)
movl (%r13), %r14d
testl %r14d, %r14d
jle 0xc8aa
movq (%r12), %rax
addq $0x8, %rax
xorl %ecx, %ecx
movl (%rax), %edx
addl %edx, (%rbx)
incq %rcx
movslq (%r13), %r14
addq $0x20, %rax
cmpq %r14, %rcx
jl 0xc896
movslq (%rbx), %rcx
movq %rcx, -0x30(%rbp)
movq -0x80(%rbp), %rax
movslq (%rax), %rax
addq %rcx, %rax
imulq $0x70, %rax, %rdi
callq 0x21d0
movq -0x68(%rbp), %r15
movq %rax, (%r15)
leaq 0x4935f(%rip), %rdi     # 0x55c31
movq -0x38(%rbp), %rsi
movq -0x58(%rbp), %rdx
movl %r14d, %ecx
movq -0x30(%rbp), %r8
xorl %eax, %eax
callq 0x20f0
movq (%r12), %rcx
movq -0x48(%rbp), %rdi
movq %r15, %rsi
movq %rdi, %r12
movq %rbx, %rdx
movq %r13, %r8
callq 0xdcf6
movq -0x50(%rbp), %r15
movl %eax, (%r15)
leaq 0x49345(%rip), %rdi     # 0x55c54
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, (%r15)
jle 0xc942
movl $0x34, %ebx
xorl %r14d, %r14d
movq (%r12), %rax
movsbl (%rax,%rbx), %edi
callq 0x2060
incq %r14
movslq (%r15), %rax
addq $0x70, %rbx
cmpq %rax, %r14
jl 0xc926
leaq 0x49828(%rip), %rdi     # 0x56171
callq 0x2090
movq -0x40(%rbp), %rdi
callq 0x2050
movq %rsp, -0x40(%rbp)
movl (%r15), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x38(%rbp)
movq %rcx, %rsp
movslq (%r15), %rax
movl %eax, %edx
movq %rsp, %rsi
leaq 0xf(,%rdx,8), %rcx
andq $-0x10, %rcx
subq %rcx, %rsi
movq %rsi, -0x30(%rbp)
movq %rsi, %rsp
leal 0x1(%rax), %ecx
movq %rsp, %r8
addq $0xf, %rcx
andq $-0x10, %rcx
subq %rcx, %r8
movq %r8, %rsp
testq %rax, %rax
jle 0xc9cb
movq (%r12), %rcx
addq $0x34, %rcx
xorl %esi, %esi
movb (%rcx), %dil
movb %dil, (%r8,%rsi)
incq %rsi
addq $0x70, %rcx
cmpq %rsi, %rdx
jne 0xc9b8
movq %r13, -0x58(%rbp)
movb $0x0, (%r8,%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x60(%rbp), %rcx
callq 0x1047c
testl %eax, %eax
jns 0xca4f
movq -0x50(%rbp), %rax
movslq (%rax), %rax
testq %rax, %rax
jle 0xca4f
movq %rax, %r14
movq -0x48(%rbp), %rax
movq (%rax), %r13
xorl %ebx, %ebx
imulq $0x70, %rbx, %r12
movslq 0x1c(%r13,%r12), %r15
movq -0x38(%rbp), %rax
movl %r15d, (%rax,%rbx,4)
movq %r15, %rdi
callq 0x21d0
movq -0x30(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
testq %r15, %r15
jle 0xca47
addq %r13, %r12
shlq $0x5, %r15
xorl %ecx, %ecx
movq 0x48(%r12), %rdx
movb 0x13(%rdx,%rcx), %dl
movb %dl, (%rax)
incq %rax
addq $0x20, %rcx
cmpq %rcx, %r15
jne 0xca30
incq %rbx
cmpq %r14, %rbx
jne 0xca01
movq -0x50(%rbp), %rax
cmpl $0x0, (%rax)
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %r14
jle 0xcb09
xorl %r13d, %r13d
movq (%r14), %r12
imulq $0x70, %r13, %r14
leaq (%r12,%r14), %rbx
movq -0x30(%rbp), %rax
movq (%rax,%r13,8), %rax
movq %rax, 0x58(%r12,%r14)
movl (%rcx,%r13,4), %eax
movl %eax, 0x60(%r12,%r14)
movslq 0x1c(%r12,%r14), %r15
movq %r15, %rdi
callq 0x21d0
movq %rax, 0x50(%r12,%r14)
testq %r15, %r15
jle 0xcac4
movl $0x13, %eax
xorl %ecx, %ecx
movq 0x48(%rbx), %rdx
movq 0x50(%rbx), %rsi
movb (%rdx,%rax), %dl
movb %dl, (%rsi,%rcx)
incq %rcx
movslq 0x1c(%rbx), %r15
addq $0x20, %rax
cmpq %r15, %rcx
jl 0xcaa6
movq -0x48(%rbp), %r14
movq -0x38(%rbp), %r12
movslq (%r12,%r13,4), %rdi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, 0x68(%rbx)
movq 0x50(%rbx), %rcx
movq 0x58(%rbx), %rsi
movl 0x60(%rbx), %edx
movq %rax, %rdi
movl %r15d, %r8d
callq 0x10754
movq %r12, %rcx
incq %r13
movq -0x50(%rbp), %rax
movslq (%rax), %rax
cmpq %rax, %r13
jl 0xca67
cmpl $0x0, 0x5bb00(%rip)     # 0x68610
movq -0x78(%rbp), %rax
je 0xcb36
movq (%rax), %rdx
movq -0x58(%rbp), %rax
movl (%rax), %ecx
callq 0xe095
movq -0x68(%rbp), %rax
movq (%rax), %rdi
movq -0x70(%rbp), %rax
movl (%rax), %esi
callq 0xe0df
movq -0x40(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x490bd(%rip), %rdi     # 0x55c0d
movq %rbx, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0