// File: /workspace/github_c_makefile_star2_O0/ugobas[P]tnm/read.c
// Function: Set_chains
int Set_chains(struct chain **chains, atom **atoms, int *N_atoms, struct residue *seq, int *nres)
{
  int Nchain=0, Nc_del=0, ires, ini_r=0, na=0;
  int *sel;
  char chain=seq[0].chain;

  sel=(int*)malloc((*nres)*sizeof(int));
  
  // Eliminate chains with < MIN_ATOM atoms
  for(ires=0; ires<(*nres); ires++)
  	{sel[ires]=1;
     if(chain!=seq[ires].chain)
     	{Nc_del+=Check_chain(&Nchain, sel, &na, ini_r, ires);
         chain=seq[ires].chain;
      	 ini_r=ires; 
    	}
     na+=seq[ires].n_atom;
  	}
  Nc_del+=Check_chain(&Nchain, sel, &na, ini_r, ires);

  // Eliminate residues in empty chains
  int natoms=0,ires2=0;
  struct residue *res1=seq, *res2=seq;
  for(ires=0; ires<(*nres); ires++)
  	{if(sel[ires])
  		{if(ires2!=ires)*res2=*res1;
      	 natoms+=res1->n_atom;
      	 res2++; ires2++;
    	}
     res1++;
  	}
  
  if((*nres!=ires2)||(Nc_del))
  	{printf("%d residues eliminated in %d chains with < %d atoms",*nres-ires2, Nc_del, MIN_ATOM);
  	}
  printf("Storing %d atoms in %d chains\n", natoms, Nchain);

  *nres=ires2;
 // *atoms=malloc(natoms*sizeof(atom)); //modyves: already allocated
  *N_atoms=natoms;
  *chains=malloc(Nchain*sizeof(struct chain)); int i;
  for(i=0; i<Nchain; i++)(*chains)[i].alignres=NULL;

  // ini_atom, ini_res etc.
  //int ichain=-1, resold=-1, nr=0;
  int ichain=0, nr=0;
  struct chain *chp=*chains;
  chain=seq[0].chain;
  natoms=0;
  
  for(ires=0; ires<*nres; ires++){
    struct residue *res=seq+ires;
    if((ires==0)||(res->chain!=chain)){
      chain=res->chain;
      if(nr){Store_chain(chp, &ichain, natoms, &nr); chp++;}
      chp->ini_atom=natoms;
      chp->ini_res=ires;
      chp->type=res->type; // Set type: aa=1, nuc=2,3, other=0
      chp->label=chain;
      chp->res=res;
    }
    nr++;
    atom *atom1=res->atom_ptr, *atom2=*atoms+natoms;
    for(i=0; i<res->n_atom; i++){
      *(atom2)=*(atom1);      
      atom2->res=ires;
      atom2->i_num=natoms+i;
      atom2->chain=ichain;
      atom2->aa=res->amm;
      atom2++; atom1++; 
    }
    res->atom_ptr=*atoms+natoms;
    natoms+=res->n_atom;
  }

  if(nr)Store_chain(chp, &ichain, natoms, &nr);
  printf("Storing %d chains: ", ichain);
  for(i=0; i<ichain; i++)printf("%c",(*chains)[i].label);
  printf("\n");

  *N_atoms=natoms;
  free(sel);sel=NULL; //modyves: this was never freed
  return(ichain);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl $0x0, -0x38(%rbp)
movl $0x0, -0x3c(%rbp)
movq -0x20(%rbp), %rax
movb 0xc(%rax), %al
movb %al, -0x49(%rbp)
movq -0x28(%rbp), %rax
movslq (%rax), %rdi
shlq $0x2, %rdi
callq 0x1180
movq %rax, -0x48(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x111c8
movq -0x48(%rbp), %rax
movslq -0x34(%rbp), %rcx
movl $0x1, (%rax,%rcx,4)
movsbl -0x49(%rbp), %eax
movq -0x20(%rbp), %rcx
movslq -0x34(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rcx
movsbl 0xc(%rcx), %ecx
cmpl %ecx, %eax
je 0x111a2
movq -0x48(%rbp), %rsi
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq -0x2c(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x137a0
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movb 0xc(%rax), %al
movb %al, -0x49(%rbp)
movl -0x34(%rbp), %eax
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movl 0x8(%rax), %eax
addl -0x3c(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x11130
movq -0x48(%rbp), %rsi
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r8d
leaq -0x2c(%rbp), %rdi
leaq -0x3c(%rbp), %rdx
callq 0x137a0
addl -0x30(%rbp), %eax
movl %eax, -0x30(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x54(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x68(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1128b
movq -0x48(%rbp), %rax
movslq -0x34(%rbp), %rcx
cmpl $0x0, (%rax,%rcx,4)
je 0x11274
movl -0x54(%rbp), %eax
cmpl -0x34(%rbp), %eax
je 0x11252
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rdx
movq %rdx, 0x8(%rax)
movq 0x10(%rcx), %rdx
movq %rdx, 0x10(%rax)
movq 0x18(%rcx), %rcx
movq %rcx, 0x18(%rax)
movq -0x60(%rbp), %rax
movl 0x8(%rax), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movq -0x68(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x68(%rbp)
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
movq -0x60(%rbp), %rax
addq $0x20, %rax
movq %rax, -0x60(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1120b
movq -0x28(%rbp), %rax
movl (%rax), %eax
cmpl -0x54(%rbp), %eax
jne 0x1129c
cmpl $0x0, -0x30(%rbp)
je 0x112bc
movq -0x28(%rbp), %rax
movl (%rax), %esi
subl -0x54(%rbp), %esi
movl -0x30(%rbp), %edx
movl 0x7a326(%rip), %ecx     # 0x8b5d4
leaq 0x69d11(%rip), %rdi     # 0x7afc6
movb $0x0, %al
callq 0x10d0
movl -0x50(%rbp), %esi
movl -0x2c(%rbp), %edx
leaq 0x69d31(%rip), %rdi     # 0x7affa
movb $0x0, %al
callq 0x10d0
movl -0x54(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, (%rax)
movl -0x50(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movslq -0x2c(%rbp), %rax
imulq $0x70, %rax, %rdi
callq 0x1180
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x2c(%rbp), %eax
jge 0x1132d
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq $0x0, 0x38(%rax)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x11300
movl $0x0, -0x70(%rbp)
movl $0x0, -0x74(%rbp)
movq -0x8(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x20(%rbp), %rax
movb 0xc(%rax), %al
movb %al, -0x49(%rbp)
movl $0x0, -0x50(%rbp)
movl $0x0, -0x34(%rbp)
movl -0x34(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x1151c
movq -0x20(%rbp), %rax
movslq -0x34(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0x1139c
movq -0x88(%rbp), %rax
movsbl 0xc(%rax), %eax
movsbl -0x49(%rbp), %ecx
cmpl %ecx, %eax
je 0x1140d
movq -0x88(%rbp), %rax
movb 0xc(%rax), %al
movb %al, -0x49(%rbp)
cmpl $0x0, -0x74(%rbp)
je 0x113cf
movq -0x80(%rbp), %rdi
movl -0x50(%rbp), %edx
leaq -0x70(%rbp), %rsi
leaq -0x74(%rbp), %rcx
callq 0x13820
movq -0x80(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x80(%rbp)
movl -0x50(%rbp), %ecx
movq -0x80(%rbp), %rax
movl %ecx, (%rax)
movl -0x34(%rbp), %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x18(%rax)
movq -0x88(%rbp), %rax
movswl 0x18(%rax), %ecx
movq -0x80(%rbp), %rax
movl %ecx, 0x28(%rax)
movb -0x49(%rbp), %cl
movq -0x80(%rbp), %rax
movb %cl, 0x34(%rax)
movq -0x88(%rbp), %rcx
movq -0x80(%rbp), %rax
movq %rcx, 0x48(%rax)
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
movq -0x88(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x90(%rbp)
movq -0x10(%rbp), %rax
movq (%rax), %rax
movslq -0x50(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
movq -0x88(%rbp), %rcx
cmpl 0x8(%rcx), %eax
jge 0x114e2
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rsi
movl $0x70, %edx
callq 0x1160
movl -0x34(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x64(%rax)
movl -0x50(%rbp), %ecx
addl -0x6c(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x50(%rax)
movl -0x70(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, 0x6c(%rax)
movq -0x88(%rbp), %rax
movb 0x13(%rax), %cl
movq -0x98(%rbp), %rax
movb %cl, 0x68(%rax)
movq -0x98(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
addq $0x70, %rax
movq %rax, -0x90(%rbp)
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x11447
movq -0x10(%rbp), %rax
movq (%rax), %rcx
movslq -0x50(%rbp), %rax
imulq $0x70, %rax, %rax
addq %rax, %rcx
movq -0x88(%rbp), %rax
movq %rcx, (%rax)
movq -0x88(%rbp), %rax
movl 0x8(%rax), %eax
addl -0x50(%rbp), %eax
movl %eax, -0x50(%rbp)
movl -0x34(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x34(%rbp)
jmp 0x1135e
cmpl $0x0, -0x74(%rbp)
je 0x11536
movq -0x80(%rbp), %rdi
movl -0x50(%rbp), %edx
leaq -0x70(%rbp), %rsi
leaq -0x74(%rbp), %rcx
callq 0x13820
movl -0x70(%rbp), %esi
leaq 0x69ad9(%rip), %rdi     # 0x7b019
movb $0x0, %al
callq 0x10d0
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x70(%rbp), %eax
jge 0x11585
movq -0x8(%rbp), %rax
movq (%rax), %rax
movslq -0x6c(%rbp), %rcx
imulq $0x70, %rcx, %rcx
addq %rcx, %rax
movsbl 0x34(%rax), %esi
leaq 0x716c3(%rip), %rdi     # 0x82c36
movb $0x0, %al
callq 0x10d0
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x1154e
leaq 0x6caec(%rip), %rdi     # 0x7e078
movb $0x0, %al
callq 0x10d0
movl -0x50(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, (%rax)
movq -0x48(%rbp), %rdi
callq 0x1040
movq $0x0, -0x48(%rbp)
movl -0x70(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax)