// File: /workspace/github_c_makefile_star2_O2/josch[P]stride/molscr.c
// Function: MolScript
int MolScript(CHAIN **Chain, int NChain, COMMAND *Cmd)
{

  int HelixBound[MAX_ASSIGN][2], SheetBound[MAX_ASSIGN][2], CoilBound[MAX_ASSIGN][2];
  int NHelix, NSheet, NCoil, i, Cn;
  char *Asn;
  FILE *fi;

  if( !(fi = fopen(Cmd->MolScriptFile,"w")) )
    return(escape(FAILURE,"\nCan not open molscript file %s\n\n",Cmd->MolScriptFile));

  fprintf(fi,"plot\n");
  fprintf(fi,"read mol \"%s\";\n",Chain[0]->File);
  fprintf(fi,"transform atom * by centre position in amino-acids\n");
  fprintf(fi,"by rotation z  0.0	\n");
  fprintf(fi,"by rotation y -260.0	\n");
  fprintf(fi,"by rotation x -40.0;\n");
      
  for( Cn=0; Cn<NChain; Cn++ ) {

    if( !Chain[Cn]->Valid )
       continue;

    Asn = (char *)ckalloc(Chain[Cn]->NRes*sizeof(char));

    ExtractAsn(Chain,Cn,Asn);
    for( i=0; i<Chain[Cn]->NRes; i++ )
      if( Asn[i] != 'H' && Asn[i] != 'E' )
	Asn[i] = 'C';

    NHelix = Boundaries(Asn,Chain[Cn]->NRes,'H',HelixBound);
    NSheet = Boundaries(Asn,Chain[Cn]->NRes,'E',SheetBound);
    NCoil  = Boundaries(Asn,Chain[Cn]->NRes,'C',CoilBound);

    free(Asn);
    
    for( i=0; i<NSheet; i++ )  
      if( SheetBound[i][1] != Chain[Cn]->NRes-1 ) 
	SheetBound[i][1]++;
    for( i=0; i<NHelix;  i++ ) 
      if( HelixBound[i][1] != Chain[Cn]->NRes-1 ) 
	HelixBound[i][1]++;
    for( i=0; i<NCoil;   i++ ) 
      if( CoilBound[i][1]  != Chain[Cn]->NRes-1 )
	CoilBound[i][1]++;
    
    for( i=0; i<NHelix; i++ )
      fprintf(fi,"helix from %c%s to %c%s;\n",
	      Chain[Cn]->Id,Chain[Cn]->Rsd[HelixBound[i][0]]->PDB_ResNumb,
	      Chain[Cn]->Id,Chain[Cn]->Rsd[HelixBound[i][1]]->PDB_ResNumb);
  
    for( i=0; i<NSheet; i++ )
      fprintf(fi,"strand from %c%s to %c%s;\n",
	      Chain[Cn]->Id,Chain[Cn]->Rsd[SheetBound[i][0]]->PDB_ResNumb,
	      Chain[Cn]->Id,Chain[Cn]->Rsd[SheetBound[i][1]]->PDB_ResNumb);
    
    for( i=0; i<NCoil; i++ )
      fprintf(fi,"coil from %c%s to %c%s;\n",
	      Chain[Cn]->Id,Chain[Cn]->Rsd[CoilBound[i][0]]->PDB_ResNumb,
	      Chain[Cn]->Id,Chain[Cn]->Rsd[CoilBound[i][1]]->PDB_ResNumb);
  }

  fprintf(fi,"end_plot\n");
  fclose(fi);
  
  return(SUCCESS);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2ef8, %rsp           # imm = 0x2EF8
movq %rdx, %rbx
movl %esi, %r12d
movq %rdi, %r15
addq $0x1405, %rbx           # imm = 0x1405
leaq 0x11464(%rip), %rsi     # 0x18d80
movq %rbx, %rdi
callq 0x21c0
testq %rax, %rax
je 0x7c27
movq %rax, %r13
leaq 0x11545(%rip), %rdi     # 0x18e7c
pushq $0x5
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
movq %rax, %rcx
callq 0x2250
movq (%r15), %rax
movq 0x48(%rax), %rdx
leaq 0x1144e(%rip), %rsi     # 0x18da4
xorl %r14d, %r14d
movq %r13, %rdi
xorl %eax, %eax
callq 0x2160
leaq 0x1144a(%rip), %rdi     # 0x18db4
pushq $0x33
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x2250
leaq 0x11469(%rip), %rdi     # 0x18de8
pushq $0x14
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x2250
leaq 0x11469(%rip), %rdi     # 0x18dfd
pushq $0x16
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x2250
leaq 0x1146b(%rip), %rdi     # 0x18e14
pushq $0x15
popq %rsi
movq %rbx, %rdx
movq %r13, %rcx
callq 0x2250
testl %r12d, %r12d
cmovlel %r14d, %r12d
xorl %r14d, %r14d
movq %r13, (%rsp)
movq %r12, 0x8(%rsp)
cmpq %r12, %r14
je 0x7bf5
movq (%r15,%r14,8), %rax
cmpl $0x0, 0x58(%rax)
je 0x7bed
movslq (%rax), %rdi
callq 0x50f4
movq %rax, %rbx
movq %r15, %rdi
movl %r14d, %esi
movq %rax, %rdx
callq 0x49db
xorl %eax, %eax
movq (%r15,%r14,8), %rcx
movslq (%rcx), %rsi
cmpq %rsi, %rax
jge 0x7a1f
movzbl (%rbx,%rax), %ecx
cmpl $0x45, %ecx
je 0x7a1a
cmpl $0x48, %ecx
je 0x7a1a
movb $0x43, (%rbx,%rax)
incq %rax
jmp 0x79fc
movq %rbx, %rdi
pushq $0x48
popq %rdx
leaq 0x1f50(%rsp), %rcx
callq 0x47d8
movl %eax, %r13d
movq (%r15,%r14,8), %rax
movl (%rax), %esi
movq %rbx, %rdi
pushq $0x45
popq %rdx
leaq 0xfb0(%rsp), %rcx
callq 0x47d8
movl %eax, %r12d
movq (%r15,%r14,8), %rax
movl (%rax), %esi
movq %rbx, %rdi
pushq $0x43
popq %rdx
leaq 0x10(%rsp), %rcx
callq 0x47d8
movl %eax, %ebp
movq %rbx, %rdi
callq 0x2040
testl %r12d, %r12d
movl $0x0, %eax
cmovlel %eax, %r12d
xorl %eax, %eax
cmpq %rax, %r12
je 0x7aa5
movl 0xfb4(%rsp,%rax,8), %ecx
movq (%r15,%r14,8), %rdx
movl (%rdx), %edx
decl %edx
cmpl %edx, %ecx
je 0x7aa0
incl %ecx
movl %ecx, 0xfb4(%rsp,%rax,8)
incq %rax
jmp 0x7a7f
testl %r13d, %r13d
movl $0x0, %eax
cmovlel %eax, %r13d
xorl %eax, %eax
cmpq %rax, %r13
je 0x7ad9
movl 0x1f54(%rsp,%rax,8), %ecx
movq (%r15,%r14,8), %rdx
movl (%rdx), %edx
decl %edx
cmpl %edx, %ecx
je 0x7ad4
incl %ecx
movl %ecx, 0x1f54(%rsp,%rax,8)
incq %rax
jmp 0x7ab3
testl %ebp, %ebp
movl $0x0, %eax
cmovlel %eax, %ebp
xorl %eax, %eax
cmpq %rax, %rbp
je 0x7b05
movl 0x14(%rsp,%rax,8), %ecx
movq (%r15,%r14,8), %rdx
movl (%rdx), %edx
decl %edx
cmpl %edx, %ecx
je 0x7b00
incl %ecx
movl %ecx, 0x14(%rsp,%rax,8)
incq %rax
jmp 0x7ae5
xorl %ebx, %ebx
cmpq %rbx, %r13
je 0x7b52
movq (%r15,%r14,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0x1f50(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0x1f54(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq (%rsp), %rdi
leaq 0x112e7(%rip), %rsi     # 0x18e2a
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
jmp 0x7b07
xorl %ebx, %ebx
movq (%rsp), %r13
cmpq %rbx, %r12
je 0x7ba2
movq (%r15,%r14,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0xfb0(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0xfb4(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq %r13, %rdi
leaq 0x112b1(%rip), %rsi     # 0x18e44
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
jmp 0x7b58
xorl %ebx, %ebx
movq 0x8(%rsp), %r12
cmpq %rbx, %rbp
je 0x7bed
movq (%r15,%r14,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0x10(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0x14(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq %r13, %rdi
leaq 0x11281(%rip), %rsi     # 0x18e5f
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
jmp 0x7ba9
incq %r14
jmp 0x79ca
leaq 0x1127c(%rip), %rdi     # 0x18e78
pushq $0x9
popq %rsi
pushq $0x1
popq %rdx
movq %r13, %rcx
callq 0x2250
movq %r13, %rdi
callq 0x20a0
pushq $0x1
popq %rax
addq $0x2ef8, %rsp           # imm = 0x2EF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11154(%rip), %rsi     # 0x18d82
xorl %edi, %edi
movq %rbx, %rdx
xorl %eax, %eax
addq $0x2ef8, %rsp           # imm = 0x2EF8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xece4
nop