// File: /workspace/github_c_makefile_star2_O1/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 %r8, %r15
movq %rcx, %r14
movq %rdx, 0x30(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %r12
movb 0xc(%rcx), %bl
movslq (%r8), %rbp
leaq (,%rbp,4), %rdi
callq 0x21d0
testq %rbp, %rbp
movq %rax, 0x10(%rsp)
movq %r15, 0x38(%rsp)
movq %r14, 0x20(%rsp)
jle 0xdde9
movl 0x5a8cc(%rip), %edx     # 0x68614
leaq 0xc(%r14), %r15
xorl %r13d, %r13d
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %edi, %edi
xorl %esi, %esi
movq %rbp, 0x18(%rsp)
movl %edx, 0x2c(%rsp)
movl $0x1, (%rax,%r13,4)
movb (%r15), %r14b
cmpb %r14b, %bl
je 0xddcd
cmpl %edx, %edi
jge 0xddbe
movslq %r9d, %rcx
movl $0x1, %ebx
cmpq %rcx, %r13
jle 0xddc2
movq 0x10(%rsp), %rax
leaq (%rax,%rcx,4), %rdi
notl %ecx
addl %r13d, %ecx
leaq 0x4(,%rcx,4), %rdx
movl %esi, 0xc(%rsp)
xorl %esi, %esi
movl %r8d, %ebp
callq 0x2110
movl 0x2c(%rsp), %edx
movl %ebp, %r8d
movq 0x18(%rsp), %rbp
movl 0xc(%rsp), %esi
movq 0x10(%rsp), %rax
jmp 0xddc2
incl %esi
xorl %ebx, %ebx
addl %ebx, %r8d
xorl %edi, %edi
movl %r13d, %r9d
movl %r14d, %ebx
addl -0x4(%r15), %edi
incq %r13
addq $0x20, %r15
cmpq %r13, %rbp
jne 0xdd62
movq 0x38(%rsp), %r15
movq 0x20(%rsp), %r14
jmp 0xddf6
xorl %esi, %esi
xorl %edi, %edi
xorl %r13d, %r13d
xorl %r9d, %r9d
xorl %r8d, %r8d
movl 0x5a818(%rip), %ecx     # 0x68614
cmpl %ecx, %edi
jge 0xde45
movl %esi, 0xc(%rsp)
movl $0x1, %edx
cmpl %r13d, %r9d
jge 0xde4d
movslq %r9d, %rsi
movq 0x10(%rsp), %rax
leaq (%rax,%rsi,4), %rdi
notl %r9d
addl %r9d, %r13d
leaq 0x4(,%r13,4), %rdx
xorl %esi, %esi
movl %r8d, %ebx
movl %ecx, %ebp
callq 0x2110
movl $0x1, %edx
movl %ebp, %ecx
movl %ebx, %r8d
movq 0x10(%rsp), %rax
jmp 0xde4d
incl %esi
movl %esi, 0xc(%rsp)
xorl %edx, %edx
movl (%r15), %esi
testl %esi, %esi
jle 0xde9c
leaq 0x8(%r14), %rdi
xorl %r9d, %r9d
xorl %ebp, %ebp
xorl %r13d, %r13d
cmpl $0x0, (%rax,%r9,4)
je 0xde8b
movl %ebp, %esi
cmpq %rsi, %r9
je 0xde82
leaq -0x8(%rdi), %rsi
movups (%rsi), %xmm0
movups 0x10(%rsi), %xmm1
movups %xmm1, 0x10(%r14)
movups %xmm0, (%r14)
addl (%rdi), %r13d
addq $0x20, %r14
incl %ebp
incq %r9
movslq (%r15), %rsi
addq $0x20, %rdi
cmpq %rsi, %r9
jl 0xde60
jmp 0xdea1
xorl %r13d, %r13d
xorl %ebp, %ebp
addl %r8d, %edx
subl %ebp, %esi
jne 0xdeac
testl %edx, %edx
je 0xdeba
leaq 0x48009(%rip), %rdi     # 0x55ebc
xorl %eax, %eax
callq 0x20f0
leaq 0x4802f(%rip), %rdi     # 0x55ef0
xorl %ebx, %ebx
movl %r13d, %esi
movl 0xc(%rsp), %r14d
movl %r14d, %edx
xorl %eax, %eax
callq 0x20f0
movl %ebp, (%r15)
movl %r14d, %ebp
movq 0x30(%rsp), %rax
movl %r13d, (%rax)
movslq %r14d, %r14
imulq $0x70, %r14, %rdi
callq 0x21d0
movq %rax, (%r12)
testl %r14d, %r14d
jle 0xdf16
movl %ebp, %eax
imulq $0x70, %rax, %rax
xorl %ecx, %ecx
movq (%r12), %rdx
movq $0x0, 0x38(%rdx,%rcx)
addq $0x70, %rcx
cmpq %rcx, %rax
jne 0xdf00
movq (%r12), %rax
cmpl $0x0, (%r15)
movq 0x20(%rsp), %rcx
jle 0xe020
movb 0xc(%rcx), %sil
xorl %r8d, %r8d
xorl %r10d, %r10d
xorl %edx, %edx
xorl %ebx, %ebx
movq %r12, 0x18(%rsp)
movq %r8, %r11
shlq $0x5, %r11
addq %rcx, %r11
testq %r8, %r8
je 0xdf51
cmpb %sil, 0xc(%r11)
je 0xdf83
movb 0xc(%r11), %sil
testl %edx, %edx
je 0xdf6c
incl %ebx
movl %r10d, %ecx
subl (%rax), %ecx
movl %ecx, 0x4(%rax)
movl %edx, 0x1c(%rax)
addq $0x70, %rax
xorl %edx, %edx
movl %r10d, (%rax)
movl %r8d, 0x18(%rax)
movswl 0x18(%r11), %ecx
movl %ecx, 0x28(%rax)
movb %sil, 0x34(%rax)
movq %r11, 0x48(%rax)
movb %sil, 0xc(%rsp)
movslq %r10d, %r14
movl 0x8(%r11), %ebp
testl %ebp, %ebp
jle 0xdfe4
imulq $0x70, %r14, %r15
movq 0x40(%rsp), %rcx
addq (%rcx), %r15
movq (%r11), %r13
xorl %r12d, %r12d
xorl %r9d, %r9d
leaq (%r15,%r12), %rdi
leaq (%r12,%r13), %rsi
movl $0xe, %ecx
rep  movsq (%rsi), %es:(%rdi)
leal (%r10,%r9), %ecx
movl %r8d, 0x64(%r15,%r12)
movl %ecx, 0x50(%r15,%r12)
movl %ebx, 0x6c(%r15,%r12)
movb 0x13(%r11), %cl
movb %cl, 0x68(%r15,%r12)
incl %r9d
movl 0x8(%r11), %ebp
addq $0x70, %r12
cmpl %ebp, %r9d
jl 0xdfa8
incl %edx
imulq $0x70, %r14, %rcx
movq 0x40(%rsp), %rsi
addq (%rsi), %rcx
movq %rcx, (%r11)
addl %r10d, %ebp
incq %r8
movq 0x38(%rsp), %rcx
movslq (%rcx), %rcx
movl %ebp, %r10d
cmpq %rcx, %r8
movq 0x18(%rsp), %r12
movq 0x20(%rsp), %rcx
movb 0xc(%rsp), %sil
jl 0xdf3c
jmp 0xe024
xorl %edx, %edx
xorl %ebp, %ebp
testl %edx, %edx
je 0xe034
incl %ebx
movl %ebp, %ecx
subl (%rax), %ecx
movl %ecx, 0x4(%rax)
movl %edx, 0x1c(%rax)
leaq 0x47ed4(%rip), %rdi     # 0x55f0f
movl %ebx, %esi
xorl %eax, %eax
callq 0x20f0
testl %ebx, %ebx
jle 0xe069
movl %ebx, %eax
imulq $0x70, %rax, %r14
xorl %r15d, %r15d
movq (%r12), %rax
movsbl 0x34(%rax,%r15), %edi
callq 0x2060
addq $0x70, %r15
cmpq %r15, %r14
jne 0xe051
movl $0xa, %edi
callq 0x2060
movq 0x30(%rsp), %rax
movl %ebp, (%rax)
movq 0x10(%rsp), %rdi
callq 0x2050
movl %ebx, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq