// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/output_tnm.c
// Function: Write_coord
int Write_coord(char *name_out, double *atoms_ref,
		atom *atoms, int *atom_num, int N_atom_ref,
		struct residue *seq, char chain, int i_model)
{
  int i, ini=0, m=0, ires=0, iatom=0, print_bb=1, nprint_B, nprint_S;
  int *printed=malloc(N_atom_ref*sizeof(int));
  struct residue *res;
  char aaname3[4]; FILE *fh;
  atom *atom1=NULL;

  // printf("Printing PDB %d\n", i_model);
  if(i_model==0){
    fh=fopen(name_out, "w");
  }else{
    fh=fopen(name_out, "a");
  }
  fprintf(fh, "MODEL %d: ", i_model+1);
  if(i_model==0){
    fprintf(fh, "STR.1\n");
  }else if(i_model > 0){
    fprintf(fh, "%d normal modes\n", i_model);
  }else if(i_model<0){
    fprintf(fh, "STR.2\n");
  }
  for(i=0; i<4; i++)aaname3[i]='\0';


  int jatom=0;
  ires=0; ini=0; print_bb=1; iatom=0;
  for (i=ini;i<N_atom_ref;i++)printed[i]=0;
  while(iatom < N_atom_ref){

    nprint_B=0;  nprint_S=0;
    for (i=ini;i<N_atom_ref;i++){
      if(printed[i])continue;
      atom1=atoms+atom_num[i];
      if(atom1->res > ires)break;
      if((strncmp(atom1->name, "N ", 2)==0)||
	 (strncmp(atom1->name, "CA", 2)==0)||
	 (strncmp(atom1->name, "C ", 2)==0)||
	 (strncmp(atom1->name, "O ", 2)==0)){
	if(print_bb){nprint_B=1; break;}
      }else{
	if(print_bb==0){nprint_S=1; break;}
      }
    }
    if((print_bb) && (nprint_B==0)){print_bb=0; continue;}
    if((print_bb==0) && (nprint_S==0)){
      ires=ires+1; ini=i; print_bb=1; continue;
    }

    printed[i]=1; iatom++;
    if(jatom < 99999)jatom++;
    res=seq+atom1->res;
    m=3*i;

    Name3(aaname3, res->i_aa);
    if (strlen(atom1->name)==4){
      fprintf(fh,"%-6s%5d %-4s%1s%3s %c%4d%1s   %8.3f%8.3f%8.3f%6.2f\n",
	      "ATOM", jatom, atom1->name," ", aaname3,chain,
	      //res->pdbres," ",atoms_ref[m],atoms_ref[m+1],atoms_ref[m+2],
	      atom1->res+1," ",atoms_ref[m],atoms_ref[m+1],atoms_ref[m+2],
	      atom1->occupancy);
    }else{
      fprintf(fh,"%-6s%5d  %-3s%1s%3s %c%4d%s   %8.3f%8.3f%8.3f%6.2f\n",
	      "ATOM", jatom, atom1->name," ",aaname3,chain,
	      //res->pdbres," ",atoms_ref[m],atoms_ref[m+1],atoms_ref[m+2],
	      atom1->res+1," ",atoms_ref[m],atoms_ref[m+1],atoms_ref[m+2],
	      atom1->occupancy);
    }
  }
  fprintf(fh,"TER\n");
  fclose(fh); free(printed);
  return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x48(%rsp)
movq %rcx, 0x28(%rsp)
movq %rdx, %r12
movq %rsi, 0x40(%rsp)
movq %rdi, %rbp
movl 0xa8(%rsp), %r14d
movl %r8d, 0x4(%rsp)
movslq %r8d, %r13
leaq (,%r13,4), %rdi
callq 0x21e0
movq %rax, %rbx
testl %r14d, %r14d
je 0x424e3
leaq 0x176b8(%rip), %rsi     # 0x59b5b
movq %rbp, %rdi
callq 0x2220
movq %rax, %r15
leal 0x1(%r14), %edx
leaq 0x1aa73(%rip), %rsi     # 0x5cf2c
movq %rax, %rdi
xorl %eax, %eax
callq 0x21a0
testl %r14d, %r14d
movq %r15, 0x18(%rsp)
jle 0x42526
leaq 0x1870a(%rip), %rsi     # 0x5abde
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x21a0
jmp 0x4253f
leaq 0x14d16(%rip), %rsi     # 0x57200
movq %rbp, %rdi
callq 0x2220
movq %rax, %r14
leaq 0x1aa30(%rip), %rsi     # 0x5cf2c
movq %rax, %rdi
movl $0x1, %edx
xorl %eax, %eax
callq 0x21a0
leaq 0x1aa25(%rip), %rdi     # 0x5cf37
movl $0x6, %esi
movl $0x1, %edx
movq %r14, 0x18(%rsp)
movq %r14, %rcx
jmp 0x4253a
leaq 0x1aa11(%rip), %rdi     # 0x5cf3e
movl $0x6, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0x2290
movl $0x0, 0x14(%rsp)
cmpl $0x0, 0x4(%rsp)
jle 0x42787
movb 0xa0(%rsp), %bpl
movabsq $0x300000000, %r14      # imm = 0x300000000
movl 0x4(%rsp), %edx
shlq $0x2, %rdx
movl $0x0, 0x8(%rsp)
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2110
movq %r14, %r8
movq 0x28(%rsp), %rdi
movsbl %bpl, %eax
movq %rax, 0x38(%rsp)
movb $0x1, %r9b
movabsq $0x100000000, %r10      # imm = 0x100000000
xorl %eax, %eax
xorl %r11d, %r11d
movl $0x0, 0x10(%rsp)
movl %eax, %ecx
movslq %eax, %rdx
leal 0x1(%rdx), %esi
movl 0x4(%rsp), %eax
cmpl %esi, %eax
cmovgl %eax, %esi
movl %esi, 0x24(%rsp)
movq %rdx, %rax
imulq %r8, %rax
movq %rax, 0x58(%rsp)
movq %rdx, 0x60(%rsp)
shlq $0x20, %rdx
movq %rdx, 0x50(%rsp)
movl $0x1, %r15d
movb %r9b, 0x3(%rsp)
movl %ecx, 0xc(%rsp)
movl %ecx, %eax
testb $0x1, %r9b
je 0x4265d
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r14
movq 0x60(%rsp), %rax
cmpl $0x0, (%rbx,%rax,4)
jne 0x42647
movslq (%rdi,%rax,4), %rdx
imulq $0x70, %rdx, %rbp
movslq 0x64(%r12,%rbp), %rdx
cmpl 0x10(%rsp), %edx
jg 0x42659
addq %r12, %rbp
movzbl 0x18(%rbp), %esi
cmpl $0x43, %esi
je 0x4262f
cmpl $0x4f, %esi
je 0x42635
cmpl $0x4e, %esi
je 0x42635
jmp 0x42642
cmpb $0x41, 0x19(%rbp)
je 0x4263b
cmpb $0x20, 0x19(%rbp)
jne 0x42642
testl %r15d, %r15d
je 0x42647
jmp 0x42674
testl %r15d, %r15d
je 0x42671
incq %rax
addq %r8, %r14
addq %r10, %rcx
cmpq %r13, %rax
jl 0x425fe
movl 0x24(%rsp), %eax
movl 0xc(%rsp), %ecx
testl %r15d, %r15d
movl $0x0, %r15d
jne 0x425e7
jmp 0x42770
xorl %r15d, %r15d
leaq 0x18(%rbp), %rax
movq %rax, 0x30(%rsp)
sarq $0x1e, %rcx
movl $0x1, (%rbx,%rcx)
incl %r11d
movl %r11d, 0x20(%rsp)
xorl %eax, %eax
movl 0x8(%rsp), %ecx
cmpl $0x1869f, %ecx          # imm = 0x1869F
setl %al
addl %eax, %ecx
movl %ecx, 0x8(%rsp)
shlq $0x5, %rdx
movq 0x48(%rsp), %rax
movswl 0x14(%rax,%rdx), %esi
leaq 0x14(%rsp), %rdi
callq 0x26d50
movq 0x30(%rsp), %rdi
callq 0x20d0
movl 0x64(%rbp), %r10d
incl %r10d
sarq $0x1d, %r14
cmpq $0x4, %rax
movq 0x40(%rsp), %rax
movsd (%rax,%r14), %xmm0
movsd 0x8(%rax,%r14), %xmm1
cvtss2sd 0x44(%rbp), %xmm3
movsd 0x10(%rax,%r14), %xmm2
leaq 0x1a883(%rip), %rsi     # 0x5cf7e
leaq 0x1a843(%rip), %rax     # 0x5cf45
cmoveq %rax, %rsi
movq 0x18(%rsp), %rdi
leaq 0x1a867(%rip), %rdx     # 0x5cf79
movl 0x8(%rsp), %ecx
movq 0x30(%rsp), %r8
leaq 0x16c4d(%rip), %r9      # 0x5936f
movb $0x4, %al
pushq %r9
pushq %r10
pushq 0x48(%rsp)
leaq 0x2c(%rsp), %r10
pushq %r10
callq 0x21a0
movl 0x40(%rsp), %r11d
addq $0x20, %rsp
cmpl 0x4(%rsp), %r11d
movq 0x28(%rsp), %rdi
movabsq $0x300000000, %r8       # imm = 0x300000000
movb 0x3(%rsp), %r9b
movabsq $0x100000000, %r10      # imm = 0x100000000
movl 0xc(%rsp), %ecx
jl 0x425e7
jmp 0x42787
incl 0x10(%rsp)
movl 0x4(%rsp), %ecx
cmpl %ecx, %eax
setl %r9b
cmpl %ecx, %r11d
jl 0x425a9
leaq 0x1a780(%rip), %rdi     # 0x5cf0e
movl $0x4, %esi
movl $0x1, %edx
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x2290
movq %r14, %rdi
callq 0x20c0
movq %rbx, %rdi
callq 0x2050
xorl %eax, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq