// File: /workspace/github_c_makefile_star2_O2/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, 0x20(%rsp)
movq %rsi, 0x40(%rsp)
movq %rdi, %rbp
movl 0xa8(%rsp), %r14d
movl %r8d, (%rsp)
movslq %r8d, %r13
leaq (,%r13,4), %rdi
callq 0x21d0
movq %rax, %rbx
testl %r14d, %r14d
leaq 0x141e8(%rip), %rax     # 0x51150
leaq 0x16b4c(%rip), %rsi     # 0x53abb
cmoveq %rax, %rsi
movq %rbp, %rdi
callq 0x2210
movq %rax, %r15
leal 0x1(%r14), %edx
leaq 0x19f13(%rip), %rsi     # 0x56e9c
movq %rax, %rdi
xorl %eax, %eax
callq 0x2190
testl %r14d, %r14d
je 0x3cfb0
jle 0x3cfb9
leaq 0x17b9d(%rip), %rsi     # 0x54b3e
movq %r15, %rdi
movl %r14d, %edx
xorl %eax, %eax
callq 0x2190
jmp 0x3cfce
leaq 0x19ef0(%rip), %rdi     # 0x56ea7
jmp 0x3cfc0
leaq 0x19eee(%rip), %rdi     # 0x56eae
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x2280
movb 0xa0(%rsp), %al
xorl %ecx, %ecx
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %r8
cmpq $0x4, %rcx
je 0x3cff1
movb $0x0, 0x8(%rsp,%rcx)
incq %rcx
jmp 0x3cfe1
xorl %ecx, %ecx
movl (%rsp), %esi
testl %esi, %esi
movl $0x0, %edx
cmovgl %esi, %edx
cmpq %rcx, %rdx
je 0x3d00e
andl $0x0, (%rbx,%rcx,4)
incq %rcx
jmp 0x3d000
movq %r15, 0x18(%rsp)
movabsq $0x300000000, %r9       # imm = 0x300000000
movsbl %al, %eax
movq %rax, 0x38(%rsp)
xorl %r10d, %r10d
movabsq $0x100000000, %r15      # imm = 0x100000000
xorl %r11d, %r11d
xorl %eax, %eax
movl $0x0, 0x4(%rsp)
movslq %eax, %rcx
movl (%rsp), %eax
cmpl %eax, %ecx
cmovgl %ecx, %eax
movl %eax, 0x14(%rsp)
movq %rcx, %rax
imulq %r9, %rax
movq %rax, 0x58(%rsp)
movq %rcx, 0x60(%rsp)
shlq $0x20, %rcx
movq %rcx, 0x50(%rsp)
pushq $0x1
popq %rbp
movl %r10d, 0xc(%rsp)
cmpl (%rsp), %r11d
jge 0x3d1f0
movq 0x50(%rsp), %rcx
movq 0x58(%rsp), %r14
movq 0x60(%rsp), %rax
cmpq %r13, %rax
jge 0x3d0e5
cmpl $0x0, (%rbx,%rax,4)
jne 0x3d0da
movslq (%rdi,%rax,4), %rdx
imulq $0x70, %rdx, %r12
movslq 0x64(%r8,%r12), %rdx
cmpl %r10d, %edx
jg 0x3d0e9
addq %r8, %r12
movzbl 0x18(%r12), %esi
cmpl $0x43, %esi
je 0x3d0c0
cmpl $0x4f, %esi
je 0x3d0c8
cmpl $0x4e, %esi
je 0x3d0c8
jmp 0x3d0d6
cmpb $0x41, 0x19(%r12)
je 0x3d0d0
cmpb $0x20, 0x19(%r12)
jne 0x3d0d6
testl %ebp, %ebp
je 0x3d0da
jmp 0x3d0f9
testl %ebp, %ebp
je 0x3d0f7
incq %rax
addq %r9, %r14
addq %r15, %rcx
jmp 0x3d089
movl 0x14(%rsp), %eax
testl %ebp, %ebp
movl $0x0, %ebp
jne 0x3d07a
jmp 0x3d1e8
xorl %ebp, %ebp
leaq 0x18(%r12), %rax
movq %rax, 0x30(%rsp)
sarq $0x1e, %rcx
movl $0x1, (%rbx,%rcx)
incl %r11d
movl %r11d, 0x10(%rsp)
xorl %eax, %eax
movl 0x4(%rsp), %ecx
cmpl $0x1869f, %ecx          # imm = 0x1869F
setl %al
addl %eax, %ecx
movl %ecx, 0x4(%rsp)
shlq $0x5, %rdx
movq 0x48(%rsp), %rax
movswl 0x14(%rax,%rdx), %esi
leaq 0x8(%rsp), %rdi
callq 0x24647
movq 0x30(%rsp), %rdi
callq 0x20d0
movl 0x64(%r12), %r10d
incl %r10d
sarq $0x1d, %r14
cmpq $0x4, %rax
movq 0x40(%rsp), %rax
movsd (%rax,%r14), %xmm0
movsd 0x8(%rax,%r14), %xmm1
cvtss2sd 0x44(%r12), %xmm3
movsd 0x10(%rax,%r14), %xmm2
leaq 0x19d6a(%rip), %rsi     # 0x56eee
leaq 0x19d2a(%rip), %rax     # 0x56eb5
cmoveq %rax, %rsi
movq 0x18(%rsp), %rdi
leaq 0x19d4e(%rip), %rdx     # 0x56ee9
movl 0x4(%rsp), %ecx
movq 0x30(%rsp), %r8
leaq 0x16124(%rip), %r9      # 0x532cf
movb $0x4, %al
pushq %r9
pushq %r10
pushq 0x48(%rsp)
leaq 0x20(%rsp), %r10
pushq %r10
callq 0x2190
movl 0x30(%rsp), %r11d
addq $0x20, %rsp
movq 0x28(%rsp), %rdi
movq 0x20(%rsp), %r8
movabsq $0x300000000, %r9       # imm = 0x300000000
movl 0xc(%rsp), %r10d
jmp 0x3d070
incl %r10d
jmp 0x3d03f
leaq 0x19c87(%rip), %rdi     # 0x56e7e
pushq $0x4
popq %rsi
pushq $0x1
popq %rdx
movq 0x18(%rsp), %r14
movq %r14, %rcx
callq 0x2280
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