// File: /workspace/github_c_makefile_star2_O0/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
subq $0xb0, %rsp
movq 0x58(%rbp), %rax
movq 0x50(%rbp), %rax
movq 0x48(%rbp), %rax
movq 0x40(%rbp), %rax
movq 0x38(%rbp), %rax
movq 0x30(%rbp), %rax
movq 0x28(%rbp), %rax
movq 0x20(%rbp), %rax
movq 0x18(%rbp), %rax
movq 0x10(%rbp), %rax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq %r9, -0x30(%rbp)
leaq 0x7dbfe(%rip), %rdi     # 0x8ce70
leaq 0x6b9ab(%rip), %rsi     # 0x7ac24
callq 0x1060
movq 0x58(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0xf7a0
movl %eax, -0x38(%rbp)
movl -0x38(%rbp), %ecx
leaq 0x7dbb8(%rip), %rax     # 0x8ce50
movl %ecx, (%rax)
imull $0x32, -0x38(%rbp), %eax
movl %eax, -0x3c(%rbp)
movslq -0x3c(%rbp), %rax
imulq $0x70, %rax, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movslq -0x38(%rbp), %rdi
shlq $0x5, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq 0x58(%rbp), %rdi
movq 0x10(%rbp), %rsi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x48(%rbp), %rcx
movq -0x28(%rbp), %r8
movq -0x30(%rbp), %r9
movq 0x48(%rbp), %rax
subq $0x10, %rsp
movq %rax, (%rsp)
callq 0xfe90
addq $0x10, %rsp
movl %eax, %ecx
movq -0x8(%rbp), %rax
movl %ecx, (%rax)
movq -0x8(%rbp), %rax
cmpl $0x0, (%rax)
jg 0xf321
movq 0x58(%rbp), %rsi
leaq 0x6b994(%rip), %rdi     # 0x7aca9
movb $0x0, %al
callq 0x10d0
jmp 0xf79b
movq 0x58(%rbp), %rdi
movq 0x50(%rbp), %rsi
callq 0x11000
movq 0x18(%rbp), %rax
movl $0x0, (%rax)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xf372
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %ecx
movq 0x18(%rbp), %rax
addl (%rax), %ecx
movl %ecx, (%rax)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf33f
movq 0x18(%rbp), %rax
movl (%rax), %eax
movq 0x28(%rbp), %rcx
addl (%rcx), %eax
cltq
imulq $0x70, %rax, %rdi
callq 0x1180
movq %rax, %rcx
movq 0x20(%rbp), %rax
movq %rcx, (%rax)
movq 0x50(%rbp), %rsi
movq -0x28(%rbp), %rdx
movq -0x8(%rbp), %rax
movl (%rax), %ecx
movq 0x18(%rbp), %rax
movl (%rax), %r8d
leaq 0x6b91e(%rip), %rdi     # 0x7accd
movb $0x0, %al
callq 0x10d0
movq 0x38(%rbp), %rdi
movq 0x20(%rbp), %rsi
movq 0x18(%rbp), %rdx
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movq -0x8(%rbp), %r8
callq 0x110d0
movl %eax, %ecx
movq 0x40(%rbp), %rax
movl %ecx, (%rax)
movq 0x40(%rbp), %rax
movl (%rax), %esi
leaq 0x6b909(%rip), %rdi     # 0x7acf0
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq 0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xf42f
movq 0x38(%rbp), %rax
movq (%rax), %rax
movslq -0x34(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movsbl 0x34(%rax), %esi
leaq 0x73819(%rip), %rdi     # 0x82c36
movb $0x0, %al
callq 0x10d0
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf3f5
leaq 0x6be01(%rip), %rdi     # 0x7b237
xorl %eax, %eax
callq 0x10d0
movq -0x48(%rbp), %rdi
callq 0x1040
movq 0x40(%rbp), %rax
movl (%rax), %eax
movq %rsp, %rcx
movq %rcx, -0x50(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xb0(%rbp)
movq %rcx, %rsp
movq %rax, -0x58(%rbp)
movq 0x40(%rbp), %rax
movl (%rax), %eax
leaq 0xf(,%rax,8), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa8(%rbp)
movq %rcx, %rsp
movq %rax, -0x60(%rbp)
movq 0x40(%rbp), %rax
movl (%rax), %eax
incl %eax
movq %rax, %rdx
addq $0xf, %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0xa0(%rbp)
movq %rcx, %rsp
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq 0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xf500
movq -0xa0(%rbp), %rax
movq 0x38(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x34(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rcx
movb 0x34(%rcx), %dl
movslq -0x34(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf4c7
movq -0xa0(%rbp), %r8
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdi
movq 0x40(%rbp), %rax
movslq (%rax), %rax
movb $0x0, (%r8,%rax)
movq 0x40(%rbp), %rax
movl (%rax), %edx
movq 0x58(%rbp), %rcx
callq 0x14480
cmpl $0x0, %eax
jge 0xf60a
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq 0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xf608
movq -0xb0(%rbp), %rax
movq 0x38(%rbp), %rcx
movq (%rcx), %rcx
movslq -0x34(%rbp), %rdx
imulq $0x70, %rdx, %rdx
addq %rdx, %rcx
movq %rcx, -0x70(%rbp)
movq -0x70(%rbp), %rcx
movl 0x1c(%rcx), %ecx
movl %ecx, -0x74(%rbp)
movl -0x74(%rbp), %edx
movslq -0x34(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movslq -0x74(%rbp), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rdx
movq -0xa8(%rbp), %rax
movslq -0x34(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movslq -0x34(%rbp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, -0x80(%rbp)
movl $0x0, -0x84(%rbp)
movl -0x84(%rbp), %eax
cmpl -0x74(%rbp), %eax
jge 0xf5f8
movq -0x70(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x84(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb 0x13(%rax), %dl
movq -0x80(%rbp), %rax
movslq -0x84(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x84(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x84(%rbp)
jmp 0xf5b5
jmp 0xf5fa
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf540
jmp 0xf60a
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq 0x40(%rbp), %rcx
cmpl (%rcx), %eax
jge 0xf75a
movq -0xb0(%rbp), %rax
movq -0xa8(%rbp), %rcx
movq 0x38(%rbp), %rdx
movq (%rdx), %rdx
movslq -0x34(%rbp), %rsi
imulq $0x70, %rsi, %rsi
addq %rsi, %rdx
movq %rdx, -0x90(%rbp)
movslq -0x34(%rbp), %rdx
movq (%rcx,%rdx,8), %rdx
movq -0x90(%rbp), %rcx
movq %rdx, 0x58(%rcx)
movslq -0x34(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x90(%rbp), %rax
movl %ecx, 0x60(%rax)
movq -0x90(%rbp), %rax
movslq 0x1c(%rax), %rdi
shlq $0x0, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x50(%rax)
movl $0x0, -0x94(%rbp)
movl -0x94(%rbp), %eax
movq -0x90(%rbp), %rcx
cmpl 0x1c(%rcx), %eax
jge 0xf6eb
movq -0x90(%rbp), %rax
movq 0x48(%rax), %rax
movslq -0x94(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb 0x13(%rax), %dl
movq -0x90(%rbp), %rax
movq 0x50(%rax), %rax
movslq -0x94(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x94(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x94(%rbp)
jmp 0xf697
movq -0xb0(%rbp), %rax
movslq -0x34(%rbp), %rcx
movslq (%rax,%rcx,4), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x90(%rbp), %rax
movq %rcx, 0x68(%rax)
movq -0x90(%rbp), %rax
movq 0x68(%rax), %rdi
movq -0x90(%rbp), %rax
movq 0x58(%rax), %rsi
movq -0x90(%rbp), %rax
movl 0x60(%rax), %edx
movq -0x90(%rbp), %rax
movq 0x50(%rax), %rcx
movq -0x90(%rbp), %rax
movl 0x1c(%rax), %r8d
callq 0x14820
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0xf611
cmpl $0x0, 0x7be6f(%rip)     # 0x8b5d0
je 0xf794
movq 0x20(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movl (%rax), %esi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
movq -0x8(%rbp), %rax
movl (%rax), %ecx
callq 0x115c0
movq 0x20(%rbp), %rax
movq (%rax), %rdi
movq 0x18(%rbp), %rax
movl (%rax), %esi
callq 0x11640
movq -0x50(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq