// File: /workspace/github_c_makefile_star2_O2/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 %rdx, 0x30(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, 0x38(%rsp)
leaq 0x14(%rsp), %rax
andl $0x0, (%rax)
leaq 0x10(%rsp), %rax
andl $0x0, (%rax)
leaq 0xc(%rcx), %r13
movq %rcx, 0x20(%rsp)
movb 0xc(%rcx), %bpl
movq %r8, 0x28(%rsp)
movslq (%r8), %rbx
movq %rbx, %rdi
shlq $0x2, %rdi
callq 0x21d0
xorl %edx, %edx
testl %ebx, %ebx
movl $0x0, %r12d
cmovgl %ebx, %r12d
movq %rax, %rsi
xorl %r14d, %r14d
xorl %ecx, %ecx
movl $0x0, 0xc(%rsp)
movq %rax, 0x18(%rsp)
cmpq %r14, %r12
je 0xd486
movl $0x1, (%rsi,%r14,4)
movb (%r13), %r15b
cmpb %r15b, %bpl
je 0xd475
leaq 0x14(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movl %r14d, %r8d
callq 0xef0f
movq 0x18(%rsp), %rsi
addl %eax, 0xc(%rsp)
movl 0x10(%rsp), %edx
movl %r14d, %ecx
movl %r15d, %ebp
addl -0x4(%r13), %edx
movl %edx, 0x10(%rsp)
incq %r14
addq $0x20, %r13
jmp 0xd43a
leaq 0x14(%rsp), %rdi
leaq 0x10(%rsp), %rdx
movl %r12d, %r8d
callq 0xef0f
movq 0x18(%rsp), %r8
movq 0x20(%rsp), %rsi
leaq 0x8(%rsi), %rcx
xorl %edx, %edx
xorl %r12d, %r12d
xorl %r14d, %r14d
movq 0x38(%rsp), %r13
movslq %ebx, %rdi
cmpq %rdi, %rdx
jge 0xd4f6
cmpl $0x0, (%r8,%rdx,4)
je 0xd4ed
movl %r14d, %edi
cmpq %rdi, %rdx
je 0xd4e3
leaq -0x8(%rcx), %rdi
movups (%rdi), %xmm0
movups 0x10(%rdi), %xmm1
movups %xmm1, 0x10(%rsi)
movups %xmm0, (%rsi)
movq 0x28(%rsp), %rdi
movl (%rdi), %ebx
addl (%rcx), %r12d
addq $0x20, %rsi
incl %r14d
incq %rdx
addq $0x20, %rcx
jmp 0xd4b3
addl 0xc(%rsp), %eax
subl %r14d, %ebx
jne 0xd503
testl %eax, %eax
je 0xd51b
movl 0x5610b(%rip), %ecx     # 0x63614
leaq 0x428e6(%rip), %rdi     # 0x4fdf6
movl %ebx, %esi
movl %eax, %edx
xorl %eax, %eax
callq 0x20f0
movslq 0x14(%rsp), %rbx
leaq 0x42903(%rip), %rdi     # 0x4fe2a
xorl %r15d, %r15d
movl %r12d, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x20f0
movq 0x28(%rsp), %rax
movl %r14d, (%rax)
movq 0x30(%rsp), %rax
movl %r12d, (%rax)
imulq $0x70, %rbx, %rdi
callq 0x21d0
movq %rax, (%r13)
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x70, %rax, %rax
movq (%r13), %r8
cmpq %r15, %rax
je 0xd576
andq $0x0, 0x38(%r8,%r15)
addq $0x70, %r15
jmp 0xd561
movq 0x20(%rsp), %rax
movb 0xc(%rax), %cl
xorl %r9d, %r9d
pushq $0xe
popq %rax
xorl %ebx, %ebx
xorl %edi, %edi
xorl %r14d, %r14d
movq 0x28(%rsp), %rdx
movslq (%rdx), %rdx
cmpq %rdx, %r9
jge 0xd67f
movq %r9, %rdx
shlq $0x5, %rdx
movq 0x20(%rsp), %rsi
leaq (%rsi,%rdx), %r12
movb 0xc(%rsi,%rdx), %dl
testq %r9, %r9
je 0xd5c3
cmpb %cl, %dl
jne 0xd5c3
movl %edi, 0xc(%rsp)
movb %cl, 0xb(%rsp)
jmp 0xd600
testl %edi, %edi
je 0xd5db
movl %r14d, %ecx
subl (%r8), %ecx
incl %ebx
movl %ecx, 0x4(%r8)
movl %edi, 0x1c(%r8)
addq $0x70, %r8
movl %r14d, (%r8)
movl %r9d, 0x18(%r8)
movswl 0x18(%r12), %ecx
movl %ecx, 0x28(%r8)
movb %dl, 0xb(%rsp)
movb %dl, 0x34(%r8)
movq %r12, 0x48(%r8)
movl $0x0, 0xc(%rsp)
movq (%r12), %r13
movslq %r14d, %rcx
imulq $0x70, %rcx, %r15
movq 0x40(%rsp), %rcx
movq (%rcx), %rbp
addq %r15, %rbp
xorl %r10d, %r10d
xorl %r11d, %r11d
movl 0x8(%r12), %edx
cmpl %edx, %r11d
jge 0xd656
leaq (%r10,%r13), %rsi
leaq (%r10,%rbp), %rdx
movq %rax, %rcx
movq %rdx, %rdi
rep  movsq (%rsi), %es:(%rdi)
movl %r9d, 0x64(%rdx)
leal (%r14,%r11), %ecx
movl %ecx, 0x50(%rdx)
movl %ebx, 0x6c(%rdx)
movb 0x13(%r12), %cl
movb %cl, 0x68(%rdx)
incl %r11d
addq $0x70, %r10
jmp 0xd61c
movl 0xc(%rsp), %edi
incl %edi
movq 0x40(%rsp), %rcx
addq (%rcx), %r15
movq %r15, (%r12)
addl %r14d, %edx
incq %r9
movb 0xb(%rsp), %cl
movl %edx, %r14d
movq 0x38(%rsp), %r13
jmp 0xd58b
testl %edi, %edi
je 0xd693
incl %ebx
movl %r14d, %eax
subl (%r8), %eax
movl %eax, 0x4(%r8)
movl %edi, 0x1c(%r8)
leaq 0x427af(%rip), %rdi     # 0x4fe49
xorl %r15d, %r15d
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
imulq $0x70, %rax, %r12
cmpq %r15, %r12
je 0xd6ce
movq (%r13), %rax
movsbl 0x34(%rax,%r15), %edi
callq 0x2060
addq $0x70, %r15
jmp 0xd6b4
pushq $0xa
popq %rdi
callq 0x2060
movq 0x30(%rsp), %rax
movl %r14d, (%rax)
movq 0x18(%rsp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq