// File: /workspace/github_c_makefile_star2_O3/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 $0x2f18, %rsp           # imm = 0x2F18
movq %rdx, %r15
movl %esi, %ebp
movq %rdi, %r14
addq $0x1405, %r15           # imm = 0x1405
leaq 0x11961(%rip), %rsi     # 0x19db8
movq %r15, %rdi
callq 0x21b0
testq %rax, %rax
je 0x87a2
movq %rax, %r12
leaq 0x11a42(%rip), %rdi     # 0x19eb4
movl $0x5, %esi
movl $0x1, %edx
movq %rax, %rcx
callq 0x2240
movq (%r14), %rax
movq 0x48(%rax), %rdx
leaq 0x1194a(%rip), %rsi     # 0x19ddc
movq %r12, %rdi
xorl %eax, %eax
callq 0x2160
leaq 0x11949(%rip), %rdi     # 0x19dec
movl $0x33, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2240
leaq 0x11964(%rip), %rdi     # 0x19e20
movl $0x14, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2240
leaq 0x11960(%rip), %rdi     # 0x19e35
movl $0x16, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2240
leaq 0x1195e(%rip), %rdi     # 0x19e4c
movl $0x15, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2240
testl %ebp, %ebp
jle 0x876a
movl %ebp, %eax
movq %rax, 0x18(%rsp)
xorl %r13d, %r13d
movq (%r14,%r13,8), %rax
cmpl $0x0, 0x58(%rax)
je 0x875c
movslq (%rax), %rdi
callq 0x5880
movq %rax, %rbp
movq %r14, %rdi
movl %r13d, %esi
movq %rax, %rdx
callq 0x5118
movq (%r14,%r13,8), %rax
movl (%rax), %esi
testl %esi, %esi
jle 0x856a
xorl %eax, %eax
movzbl (%rbp,%rax), %ecx
cmpl $0x45, %ecx
je 0x855f
cmpl $0x48, %ecx
je 0x855f
movb $0x43, (%rbp,%rax)
movq (%r14,%r13,8), %rcx
movl (%rcx), %esi
incq %rax
movslq %esi, %rcx
cmpq %rcx, %rax
jl 0x8545
movq %rbp, %rdi
movl $0x48, %edx
leaq 0x1f70(%rsp), %rcx
callq 0x4f0a
movl %eax, 0xc(%rsp)
movq (%r14,%r13,8), %rax
movl (%rax), %esi
movq %rbp, %rdi
movl $0x45, %edx
leaq 0xfd0(%rsp), %rcx
callq 0x4f0a
movl %eax, %r15d
movq (%r14,%r13,8), %rax
movl (%rax), %esi
movq %rbp, %rdi
movl $0x43, %edx
leaq 0x30(%rsp), %rcx
callq 0x4f0a
movl %eax, %ebx
movq %rbp, %rdi
callq 0x2040
movl %r15d, %edi
movl %r15d, 0x10(%rsp)
testl %r15d, %r15d
jle 0x85f6
movq (%r14,%r13,8), %rax
xorl %ecx, %ecx
movl 0xfd4(%rsp,%rcx,8), %edx
movl (%rax), %esi
decl %esi
cmpl %esi, %edx
je 0x85ee
incl %edx
movl %edx, 0xfd4(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %rdi
jne 0x85d6
movq %rdi, 0x28(%rsp)
movl 0xc(%rsp), %r9d
movl %r9d, %ebp
testl %r9d, %r9d
jle 0x862e
movq (%r14,%r13,8), %rax
xorl %ecx, %ecx
movl 0x1f74(%rsp,%rcx,8), %edx
movl (%rax), %esi
decl %esi
cmpl %esi, %edx
je 0x8626
incl %edx
movl %edx, 0x1f74(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %rbp
jne 0x860e
movl %ebx, %r8d
testl %ebx, %ebx
jle 0x8655
movq (%r14,%r13,8), %rax
xorl %ecx, %ecx
movl 0x34(%rsp,%rcx,8), %edx
movl (%rax), %esi
decl %esi
cmpl %esi, %edx
je 0x864d
incl %edx
movl %edx, 0x34(%rsp,%rcx,8)
incq %rcx
cmpq %rcx, %r8
jne 0x863b
movq %r8, 0x20(%rsp)
movl %ebx, 0x14(%rsp)
testl %r9d, %r9d
leaq 0x117fa(%rip), %r15     # 0x19e62
jle 0x86b0
xorl %ebx, %ebx
movq (%r14,%r13,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0x1f70(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0x1f74(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq %r12, %rdi
movq %r15, %rsi
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
cmpq %rbx, %rbp
jne 0x866c
cmpl $0x0, 0x10(%rsp)
leaq 0x117c0(%rip), %r15     # 0x19e7c
movq 0x28(%rsp), %rbp
jle 0x8709
xorl %ebx, %ebx
movq (%r14,%r13,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0xfd0(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0xfd4(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq %r12, %rdi
movq %r15, %rsi
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
cmpq %rbx, %rbp
jne 0x86c5
cmpl $0x0, 0x14(%rsp)
leaq 0x11782(%rip), %r15     # 0x19e97
movq 0x20(%rsp), %rbp
jle 0x875c
xorl %ebx, %ebx
movq (%r14,%r13,8), %rax
movsbl 0x40(%rax), %edx
movq 0x68(%rax), %rax
movslq 0x30(%rsp,%rbx,8), %rcx
movq (%rax,%rcx,8), %rcx
addq $0x4, %rcx
movslq 0x34(%rsp,%rbx,8), %rsi
movq (%rax,%rsi,8), %r9
addq $0x4, %r9
movq %r12, %rdi
movq %r15, %rsi
movl %edx, %r8d
xorl %eax, %eax
callq 0x2160
incq %rbx
cmpq %rbx, %rbp
jne 0x871e
incq %r13
cmpq 0x18(%rsp), %r13
jne 0x8512
leaq 0x1173f(%rip), %rdi     # 0x19eb0
movl $0x9, %esi
movl $0x1, %edx
movq %r12, %rcx
callq 0x2240
movq %r12, %rdi
callq 0x20a0
movl $0x1, %eax
addq $0x2f18, %rsp           # imm = 0x2F18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11611(%rip), %rsi     # 0x19dba
xorl %edi, %edi
movq %r15, %rdx
xorl %eax, %eax
addq $0x2f18, %rsp           # imm = 0x2F18
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2424
nop