// File: /workspace/github_c_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rcx, %r14
movq %rdx, 0x38(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, 0x30(%rsp)
movb 0xc(%rcx), %r13b
movq %r8, 0x18(%rsp)
movslq (%r8), %rbx
leaq (,%rbx,4), %rdi
callq 0x21e0
movl 0x5a8c9(%rip), %ecx     # 0x68614
testq %rbx, %rbx
movq %rax, 0x8(%rsp)
movq %r14, 0x20(%rsp)
jle 0xde45
movq %rbx, 0x28(%rsp)
movl %ebx, %ebx
leaq 0xc(%r14), %rbp
xorl %r14d, %r14d
xorl %r9d, %r9d
xorl %r8d, %r8d
xorl %edi, %edi
xorl %esi, %esi
movl %ecx, 0x14(%rsp)
movl $0x1, (%rax,%r14,4)
movb (%rbp), %r15b
cmpb %r15b, %r13b
je 0xdde3
cmpl %ecx, %edi
jge 0xddd3
movslq %r8d, %rdx
movl $0x1, %r13d
cmpq %rdx, %r14
jle 0xddd8
movq 0x8(%rsp), %rax
leaq (%rax,%rdx,4), %rdi
notl %edx
addl %r14d, %edx
leaq 0x4(,%rdx,4), %rdx
movl %esi, 0x4(%rsp)
xorl %esi, %esi
movl %r9d, %r12d
callq 0x2110
movl %r12d, %r9d
movl 0x14(%rsp), %ecx
movl 0x4(%rsp), %esi
movq 0x8(%rsp), %rax
jmp 0xddd8
incl %esi
xorl %r13d, %r13d
addl %r13d, %r9d
xorl %edi, %edi
movl %r14d, %r8d
movl %r15d, %r13d
addl -0x4(%rbp), %edi
incq %r14
addq $0x20, %rbp
cmpq %r14, %rbx
jne 0xdd7a
cmpl %ecx, %edi
jge 0xde63
movl $0x1, %edx
cmpl %ebx, %r8d
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r13
jge 0xde71
movslq %r8d, %rcx
movq 0x8(%rsp), %rax
leaq (%rax,%rcx,4), %rdi
notl %r8d
addl %r13d, %r8d
leaq 0x4(,%r8,4), %rdx
movl %esi, %ebx
xorl %esi, %esi
movl %r9d, %ebp
callq 0x2110
movl $0x1, %edx
movl %ebp, %r9d
movl 0x14(%rsp), %ecx
movl %ebx, %esi
movq 0x8(%rsp), %rax
jmp 0xde71
movl $0x1, %edx
xorl %r9d, %r9d
movl $0x0, %esi
testl %ecx, %ecx
movq %rbx, %r13
jle 0xde6d
xorl %ebp, %ebp
xorl %r12d, %r12d
xorl %r15d, %r15d
jmp 0xdedd
movq 0x20(%rsp), %r14
movq 0x28(%rsp), %r13
incl %esi
xorl %edx, %edx
movl %esi, %r15d
testl %r13d, %r13d
jle 0xdecc
leaq 0x8(%r14), %rdi
xorl %r8d, %r8d
movq %r14, %r10
xorl %ebp, %ebp
xorl %r12d, %r12d
cmpl $0x0, (%rax,%r8,4)
je 0xdebb
movl %ebp, %esi
cmpq %rsi, %r8
je 0xdeb2
leaq -0x8(%rdi), %rsi
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%r10)
movups %xmm0, (%r10)
movq 0x18(%rsp), %rsi
movl (%rsi), %r13d
addl (%rdi), %r12d
addq $0x20, %r10
incl %ebp
incq %r8
movslq %r13d, %rsi
addq $0x20, %rdi
cmpq %rsi, %r8
jl 0xde88
jmp 0xded1
xorl %r12d, %r12d
xorl %ebp, %ebp
addl %r9d, %edx
cmpl %ebp, %r13d
jne 0xdedd
testl %edx, %edx
je 0xdef1
subl %ebp, %r13d
leaq 0x47fbf(%rip), %rdi     # 0x55ea6
movl %r13d, %esi
xorl %eax, %eax
callq 0x20f0
leaq 0x47fe2(%rip), %rdi     # 0x55eda
movl %r12d, %esi
movl %r15d, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x18(%rsp), %rax
movl %ebp, (%rax)
movq 0x38(%rsp), %rax
movl %r12d, (%rax)
movslq %r15d, %rbx
imulq $0x70, %rbx, %rdi
callq 0x21e0
movq 0x30(%rsp), %rsi
movq %rax, (%rsi)
testl %ebx, %ebx
jle 0xdf4d
movl %r15d, %eax
imulq $0x70, %rax, %rax
xorl %ecx, %ecx
movq (%rsi), %rdx
movq $0x0, 0x38(%rdx,%rcx)
addq $0x70, %rcx
cmpq %rcx, %rax
jne 0xdf35
movq (%rsi), %rax
movq 0x18(%rsp), %rcx
cmpl $0x0, (%rcx)
jle 0xe096
movb 0xc(%r14), %cl
movb %cl, 0x4(%rsp)
xorl %edx, %edx
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %ebx, %ebx
movq %rdx, %rcx
shlq $0x5, %rcx
leaq (%r14,%rcx), %r11
movb 0xc(%r14,%rcx), %cl
testq %rdx, %rdx
je 0xdf88
cmpb 0x4(%rsp), %cl
je 0xdfbb
testl %r9d, %r9d
je 0xdf9f
movl %r8d, %esi
subl (%rax), %esi
incl %ebx
movl %esi, 0x4(%rax)
movl %r9d, 0x1c(%rax)
addq $0x70, %rax
movl %r8d, (%rax)
movl %edx, 0x18(%rax)
movswl 0x18(%r11), %esi
movl %esi, 0x28(%rax)
movb %cl, 0x34(%rax)
movq %r11, 0x48(%rax)
xorl %r9d, %r9d
movb %cl, 0x4(%rsp)
movslq %r8d, %r14
movl 0x8(%r11), %ebp
testl %ebp, %ebp
jle 0xe017
imulq $0x70, %r14, %r15
movq 0x40(%rsp), %rcx
addq (%rcx), %r15
movq (%r11), %r12
xorl %r13d, %r13d
xorl %r10d, %r10d
leaq (%r15,%r13), %rdi
leaq (%r12,%r13), %rsi
movl $0xe, %ecx
rep  movsq (%rsi), %es:(%rdi)
leal (%r8,%r10), %ecx
movl %edx, 0x64(%r15,%r13)
movl %ecx, 0x50(%r15,%r13)
movl %ebx, 0x6c(%r15,%r13)
movb 0x13(%r11), %cl
movb %cl, 0x68(%r15,%r13)
incl %r10d
movl 0x8(%r11), %ebp
addq $0x70, %r13
cmpl %ebp, %r10d
jl 0xdfdb
incl %r9d
imulq $0x70, %r14, %rcx
movq 0x40(%rsp), %rsi
addq (%rsi), %rcx
movq %rcx, (%r11)
addl %r8d, %ebp
incq %rdx
movq 0x18(%rsp), %rcx
movslq (%rcx), %rcx
movl %ebp, %r8d
cmpq %rcx, %rdx
movq 0x20(%rsp), %r14
jl 0xdf6d
testl %r9d, %r9d
je 0xe05a
incl %ebx
movl %ebp, %ecx
subl (%rax), %ecx
movl %ecx, 0x4(%rax)
movl %r9d, 0x1c(%rax)
leaq 0x47e98(%rip), %rdi     # 0x55ef9
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
jle 0xe0aa
movl %ebx, %eax
imulq $0x70, %rax, %r14
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
movq (%r12), %rax
movsbl 0x34(%rax,%r15), %edi
callq 0x2060
addq $0x70, %r15
cmpq %r15, %r14
jne 0xe07c
jmp 0xe0aa
leaq 0x47e5c(%rip), %rdi     # 0x55ef9
xorl %ebx, %ebx
xorl %esi, %esi
xorl %eax, %eax
callq 0x20f0
xorl %ebp, %ebp
movl $0xa, %edi
callq 0x2060
movq 0x38(%rsp), %rax
movl %ebp, (%rax)
movq 0x8(%rsp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq