// File: /workspace/github_c_makefile_star2_O2/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 $0x68, %rsp
movq %r9, -0x48(%rbp)
movq %r8, %r14
movq %rsi, %r12
movq %rdi, -0x68(%rbp)
movq 0x58(%rbp), %r13
movq 0x50(%rbp), %rax
movq %rax, -0x30(%rbp)
movq 0x40(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x80(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x50(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq 0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq 0x58f36(%rip), %rdi     # 0x64eb0
leaq 0x43b9f(%rip), %rsi     # 0x4fb20
movl $0x85, %edx
callq 0x21a0
movq %r13, %rdi
movq %r14, %rsi
callq 0xc312
movl %eax, %r15d
leaq 0x58ef0(%rip), %rax     # 0x64e90
movl %r15d, (%rax)
imull $0x32, %r15d, %eax
cltq
imulq $0x70, %rax, %rdi
callq 0x21d0
movq %rax, %rbx
movslq %r15d, %rdi
movq %r13, %r15
shlq $0x5, %rdi
callq 0x21d0
movq %r12, -0x58(%rbp)
movq %rax, (%r12)
subq $0x8, %rsp
movq %r13, %rdi
movq -0x38(%rbp), %rsi
movq %rax, %rdx
movq %rbx, -0x38(%rbp)
movq %rbx, %rcx
movq %r14, -0x88(%rbp)
movq %r14, %r8
movq -0x48(%rbp), %r9
pushq -0x40(%rbp)
movq -0x68(%rbp), %r13
callq 0xc74e
popq %rcx
popq %rdx
movl %eax, (%r13)
testl %eax, %eax
jle 0xc280
movq %r15, %rdi
movq -0x30(%rbp), %rsi
callq 0xd366
movq -0x50(%rbp), %r12
andl $0x0, (%r12)
xorl %eax, %eax
pushq $0x8
popq %rcx
xorl %edx, %edx
movq -0x58(%rbp), %rbx
movslq (%r13), %r14
cmpq %r14, %rdx
jge 0xc045
movq (%rbx), %rsi
addl (%rsi,%rcx), %eax
movl %eax, (%r12)
incq %rdx
addq $0x20, %rcx
jmp 0xc029
movq %r15, -0x40(%rbp)
movq -0x80(%rbp), %rcx
movslq (%rcx), %rcx
movslq %eax, %r15
addq %r15, %rcx
imulq $0x70, %rcx, %rdi
callq 0x21d0
movq -0x60(%rbp), %r12
movq %rax, (%r12)
leaq 0x43b5b(%rip), %rdi     # 0x4fbc9
xorl %ebx, %ebx
movq -0x30(%rbp), %rsi
movq -0x88(%rbp), %rdx
movl %r14d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x20f0
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movq -0x70(%rbp), %rdi
movq %r12, %rsi
movq -0x50(%rbp), %rdx
movq %r13, %r8
movq %rdi, %r13
callq 0xd3c9
movq -0x78(%rbp), %r12
movl %eax, (%r12)
leaq 0x43b38(%rip), %rdi     # 0x4fbec
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
pushq $0x34
popq %r14
movslq (%r12), %rax
cmpq %rax, %rbx
jge 0xc0e1
movq (%r13), %rax
movsbl (%rax,%r14), %edi
callq 0x2060
incq %rbx
addq $0x70, %r14
jmp 0xc0c1
leaq 0x43fd9(%rip), %rdi     # 0x500c1
callq 0x2090
movq -0x38(%rbp), %rdi
callq 0x2050
movq %rsp, -0x38(%rbp)
movl (%r12), %eax
movq %rsp, %rcx
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x30(%rbp)
movq %rcx, %rsp
movl (%r12), %eax
movslq %eax, %rdx
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x48(%rbp)
movq %rcx, %rsp
leal 0x1(%rdx), %eax
movq %rsp, %r8
addq $0xf, %rax
andq $-0x10, %rax
subq %rax, %r8
movq %r8, %rsp
xorl %eax, %eax
testl %edx, %edx
movl $0x0, %ecx
cmovgl %edx, %ecx
imulq $0x70, %rcx, %r9
movq %r8, %rsi
movq -0x40(%rbp), %rcx
cmpq %rax, %r9
je 0xc17c
movq (%r13), %rdi
movb 0x34(%rdi,%rax), %dil
movb %dil, (%rsi)
addq $0x70, %rax
incq %rsi
jmp 0xc162
movb $0x0, (%r8,%rdx)
movq -0x48(%rbp), %rdi
movq -0x30(%rbp), %rsi
callq 0xf54c
testl %eax, %eax
js 0xc29f
xorl %ebx, %ebx
movslq (%r12), %rax
cmpq %rax, %rbx
jge 0xc240
movq (%r13), %r13
imulq $0x70, %rbx, %r12
leaq (%r12,%r13), %r15
movq -0x48(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq %rax, 0x58(%r13,%r12)
movq -0x30(%rbp), %rax
movl (%rax,%rbx,4), %eax
movl %eax, 0x60(%r13,%r12)
movl 0x1c(%r13,%r12), %r14d
movslq %r14d, %rdi
callq 0x21d0
movq %rax, 0x50(%r13,%r12)
pushq $0x13
popq %rax
xorl %ecx, %ecx
movslq %r14d, %rdx
cmpq %rdx, %rcx
jge 0xc204
movq 0x48(%r15), %rdx
movq 0x50(%r15), %rsi
movb (%rdx,%rax), %dl
movb %dl, (%rsi,%rcx)
incq %rcx
movl 0x1c(%r15), %r14d
addq $0x20, %rax
jmp 0xc1e1
movq -0x30(%rbp), %rax
movslq (%rax,%rbx,4), %rdi
shlq $0x2, %rdi
callq 0x21d0
movq %rax, 0x68(%r15)
movq 0x50(%r15), %rcx
movq 0x58(%r15), %rsi
movl 0x60(%r15), %edx
movq %rax, %rdi
movl %r14d, %r8d
callq 0xf804
incq %rbx
movq -0x78(%rbp), %r12
movq -0x70(%rbp), %r13
jmp 0xc198
cmpl $0x0, 0x573c9(%rip)     # 0x63610
je 0xc26d
movq -0x58(%rbp), %rax
movq (%rax), %rdx
movq -0x68(%rbp), %rax
movl (%rax), %ecx
callq 0xd6f9
movq -0x60(%rbp), %rax
movq (%rax), %rdi
movq -0x50(%rbp), %rax
movl (%rax), %esi
callq 0xd744
movq -0x38(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4391e(%rip), %rdi     # 0x4fba5
movq %r15, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
movl (%r12), %eax
xorl %ebx, %ebx
testl %eax, %eax
cmovlel %ebx, %eax
movq %rax, -0x40(%rbp)
cmpq -0x40(%rbp), %rbx
je 0xc196
movq (%r13), %rax
imulq $0x70, %rbx, %rcx
leaq (%rax,%rcx), %r15
movslq 0x1c(%rax,%rcx), %r14
movq -0x30(%rbp), %rax
movl %r14d, (%rax,%rbx,4)
movq %r14, %rdi
callq 0x21d0
movq -0x48(%rbp), %rcx
movq %rax, (%rcx,%rbx,8)
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
shlq $0x5, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0xc30d
movq 0x48(%r15), %rsi
movb 0x13(%rsi,%rdx), %sil
movb %sil, (%rax)
addq $0x20, %rdx
incq %rax
jmp 0xc2f3
incq %rbx
jmp 0xc2ae