// File: /workspace/github_c_makefile_star2_O3/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, -0x38(%rbp)
movq %r8, %rbx
movq %rsi, %r14
movq %rdi, -0x80(%rbp)
movq 0x58(%rbp), %r15
movq 0x50(%rbp), %rax
movq %rax, -0x40(%rbp)
movq 0x40(%rbp), %rax
movq %rax, -0x88(%rbp)
movq 0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
movq 0x28(%rbp), %rax
movq %rax, -0x58(%rbp)
movq 0x20(%rbp), %rax
movq %rax, -0x70(%rbp)
movq 0x18(%rbp), %rax
movq %rax, -0x78(%rbp)
movq 0x10(%rbp), %rax
movq %rax, -0x30(%rbp)
movq 0x48(%rbp), %rax
movq %rax, -0x50(%rbp)
leaq 0x5d6c7(%rip), %rdi     # 0x69eb0
leaq 0x493e0(%rip), %rsi     # 0x55bd0
movl $0x85, %edx
callq 0x21b0
movq %r15, %rdi
movq %rbx, %rsi
callq 0xcba1
movl %eax, %r13d
leaq 0x5d681(%rip), %rax     # 0x69e90
movl %r13d, (%rax)
imull $0x32, %r13d, %eax
cltq
imulq $0x70, %rax, %rdi
callq 0x21e0
movq %rax, %r12
movslq %r13d, %rdi
movq %r14, %r13
shlq $0x5, %rdi
callq 0x21e0
movq %rax, (%r14)
subq $0x8, %rsp
movq %r15, %rdi
movq -0x30(%rbp), %rsi
movq %rax, %rdx
movq %r12, -0x30(%rbp)
movq %r12, %rcx
movq %rbx, -0x60(%rbp)
movq %rbx, %r8
movq -0x38(%rbp), %r9
pushq -0x50(%rbp)
movq -0x80(%rbp), %r14
callq 0xd04a
addq $0x10, %rsp
movl %eax, (%r14)
testl %eax, %eax
jle 0xc8ba
movq %r15, %rdi
movq -0x40(%rbp), %rsi
callq 0xdca2
movq -0x78(%rbp), %r12
movl $0x0, (%r12)
movl (%r14), %ebx
testl %ebx, %ebx
movq %r13, -0x68(%rbp)
movq %r15, -0x90(%rbp)
jle 0xc8d9
movq (%r13), %rcx
addq $0x8, %rcx
xorl %eax, %eax
xorl %edx, %edx
addl (%rcx), %eax
movl %eax, (%r12)
incq %rdx
movslq (%r14), %rbx
addq $0x20, %rcx
cmpq %rbx, %rdx
jl 0xc8a3
jmp 0xc8db
leaq 0x49394(%rip), %rdi     # 0x55c55
movq %r15, %rsi
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x20f0
xorl %eax, %eax
movq -0x58(%rbp), %rcx
movslq (%rcx), %rcx
movslq %eax, %r15
addq %r15, %rcx
imulq $0x70, %rcx, %rdi
callq 0x21e0
movq -0x70(%rbp), %r13
movq %rax, (%r13)
leaq 0x49379(%rip), %rdi     # 0x55c79
movq -0x40(%rbp), %rsi
movq -0x60(%rbp), %rdx
movl %ebx, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x20f0
movq -0x68(%rbp), %rax
movq (%rax), %rcx
movq -0x48(%rbp), %rdi
movq %r13, %rsi
movq %rdi, %r13
movq %r12, %rdx
movq %r14, %r8
callq 0xdd0c
movq -0x88(%rbp), %r15
movl %eax, (%r15)
leaq 0x4935b(%rip), %rdi     # 0x55c9c
movl %eax, %esi
xorl %eax, %eax
callq 0x20f0
cmpl $0x0, (%r15)
jle 0xc974
movl $0x34, %ebx
xorl %r14d, %r14d
movq (%r13), %rax
movsbl (%rax,%rbx), %edi
callq 0x2060
incq %r14
movslq (%r15), %rax
addq $0x70, %rbx
cmpq %rax, %r14
jl 0xc958
leaq 0x497fe(%rip), %rdi     # 0x56179
callq 0x2090
movq -0x30(%rbp), %rdi
callq 0x2050
movq %rsp, -0x58(%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 0xc9fd
movq (%r13), %rcx
addq $0x34, %rcx
xorl %esi, %esi
movb (%rcx), %dil
movb %dil, (%r8,%rsi)
incq %rsi
addq $0x70, %rcx
cmpq %rsi, %rdx
jne 0xc9ea
movb $0x0, (%r8,%rax)
movq -0x30(%rbp), %rdi
movq -0x38(%rbp), %rsi
movq -0x90(%rbp), %rcx
callq 0xfff0
movl (%r15), %r14d
testl %eax, %eax
jns 0xca7e
testl %r14d, %r14d
movq -0x48(%rbp), %rax
jle 0xcb61
movq (%rax), %r15
xorl %r12d, %r12d
imulq $0x70, %r12, %r13
movslq 0x1c(%r15,%r13), %rbx
movq -0x38(%rbp), %rax
movl %ebx, (%rax,%r12,4)
movq %rbx, %rdi
callq 0x21e0
movq -0x30(%rbp), %rcx
movq %rax, (%rcx,%r12,8)
testq %rbx, %rbx
jle 0xca76
addq %r15, %r13
movq 0x48(%r13), %rcx
addq $0x13, %rcx
xorl %edx, %edx
movb (%rcx), %sil
movb %sil, (%rax,%rdx)
incq %rdx
addq $0x20, %rcx
cmpq %rdx, %rbx
jne 0xca63
incq %r12
cmpq %r14, %r12
jne 0xca30
testl %r14d, %r14d
movq -0x48(%rbp), %r12
jle 0xcb61
xorl %r14d, %r14d
movq (%r12), %r12
imulq $0x70, %r14, %r13
leaq (%r12,%r13), %rbx
movq -0x30(%rbp), %rax
movq (%rax,%r14,8), %rax
movq %rax, -0x60(%rbp)
movq %rax, 0x58(%r12,%r13)
movq -0x38(%rbp), %rax
movl (%rax,%r14,4), %eax
movl %eax, -0x40(%rbp)
movl %eax, 0x60(%r12,%r13)
movslq 0x1c(%r12,%r13), %r15
movq %r15, %rdi
callq 0x21e0
movq %rax, 0x50(%r12,%r13)
testq %r15, %r15
jle 0xcb17
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 0xcad9
movq -0x38(%rbp), %rax
movl (%rax,%r14,4), %eax
movq 0x50(%rbx), %rcx
movq %rcx, -0x50(%rbp)
movq 0x58(%rbx), %r13
movl 0x60(%rbx), %ecx
movl %ecx, -0x40(%rbp)
movq -0x48(%rbp), %r12
jmp 0xcb26
movq %rax, -0x50(%rbp)
movl -0x40(%rbp), %eax
movq -0x48(%rbp), %r12
movq -0x60(%rbp), %r13
movslq %eax, %rdi
shlq $0x2, %rdi
callq 0x21e0
movq %rax, 0x68(%rbx)
movq %rax, %rdi
movq %r13, %rsi
movl -0x40(%rbp), %edx
movq -0x50(%rbp), %rcx
movl %r15d, %r8d
callq 0x102c0
incq %r14
movq -0x88(%rbp), %rax
movslq (%rax), %rax
cmpq %rax, %r14
jl 0xca8e
cmpl $0x0, 0x5baa8(%rip)     # 0x68610
je 0xcb8e
movq -0x68(%rbp), %rax
movq (%rax), %rdx
movq -0x80(%rbp), %rax
movl (%rax), %ecx
callq 0xe0d6
movq -0x70(%rbp), %rax
movq (%rax), %rdi
movq -0x78(%rbp), %rax
movl (%rax), %esi
callq 0xe120
movq -0x58(%rbp), %rsp
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq