// File: /workspace/github_c_makefile_star2_O1/ugobas[P]tnm/NeedlemanWunsch.c
// Function: affinealign
int affinealign(char *seq1,
                int  length1,
                char *seq2,
                int  length2,
                int verbose,
                int identity,
                int  penalty,
                int  penext,
                char *align1,
                char *align2,
                int  *align_len)
{
   XY    **dirn   = NULL;
   int   **matrix = NULL,
         maxdim,
         i,    j,    k,    l,
         i1,   j1,
         dia,  right, down,
         rcell, dcell, maxoff,
         match = 1,
         thisscore,
         gapext,
         score;

   maxdim = MAX(length1, length2);

   /* Initialise the score matrix                                       */
   if((matrix = (int **)Array2D(sizeof(int), maxdim, maxdim))==NULL)
      return(0);
   if((dirn   = (XY **)Array2D(sizeof(XY), maxdim, maxdim))==NULL)
      return(0);

   for(i=0;i<maxdim;i++)
   {
      for(j=0;j<maxdim;j++)
      {
         matrix[i][j] = 0;
         dirn[i][j].x = -1;
         dirn[i][j].y = -1;
      }
   }

   /* Fill in scores up the right hand side of the matrix               */
   for(j=0; j<length2; j++)
   {
      if(identity)
      {
         if(seq1[length1-1] == seq2[j]) matrix[length1-1][j] = match;
      }
      else
      {
         matrix[length1-1][j] = CalcMDMScore(seq1[length1-1], seq2[j]);
      }
   }

   /* Fill in scores along the bottom row of the matrix                 */
   for(i=0; i<length1; i++)
   {
      if(identity)
      {
         if(seq1[i] == seq2[length2-1]) matrix[i][length2-1] = match;
      }
      else
      {
         matrix[i][length2-1] = CalcMDMScore(seq1[i], seq2[length2-1]);
      }
   }

   i = length1 - 1;
   j = length2 - 1;

   /* Move back along the diagonal                                      */
   while(i > 0 && j > 0)
   {
      i--;
      j--;

      /* Fill in the scores along this row                              */
      for(i1 = i; i1 > -1; i1--)
      {
         dia   = matrix[i1+1][j+1];

         /* Find highest score to right of diagonal                     */
         rcell = i1+2;
         if(i1+2 >= length1)  right = 0;
         else                 right = matrix[i1+2][j+1] - penalty;

         gapext = 1;
         for(k = i1+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j+1] - (penalty + gapext*penext);

            if(thisscore > right)
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j+2;
         if(j+2 >= length2)  down = 0;
         else                down   = matrix[i1+1][j+2] - penalty;

         gapext = 1;
         for(l = j+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i1+1][l] - (penalty + gapext*penext);

            if(thisscore > down)
            {
               down = thisscore;
               dcell = l;
            }
         }

         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i1][j] = dia;
            dirn[i1][j].x = i1+1;
            dirn[i1][j].y = j+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i1][j] = right;
               dirn[i1][j].x = rcell;
               dirn[i1][j].y = j+1;
            }
            else
            {
               matrix[i1][j] = down;
               dirn[i1][j].x = i1+1;
               dirn[i1][j].y = dcell;
            }
         }

         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i1] == seq2[j]) matrix[i1][j] += match;
         }
         else
         {
            matrix[i1][j] += CalcMDMScore(seq1[i1],seq2[j]);
         }
      }

      /* Fill in the scores in this column                              */
      for(j1 = j; j1 > -1; j1--)
      {
         dia   = matrix[i+1][j1+1];

         /* Find highest score to right of diagonal                     */
         rcell = i+2;
         if(i+2 >= length1)   right = 0;
         else                 right = matrix[i+2][j1+1] - penalty;

         gapext = 1;
         for(k = i+3; k<length1; k++, gapext++)
         {
            thisscore = matrix[k][j1+1] - (penalty + gapext*penext);

            if(thisscore > right)
            {
               right = thisscore;
               rcell = k;
            }
         }

         /* Find highest score below diagonal                           */
         dcell = j1+2;
         if(j1+2 >= length2)  down = 0;
         else                 down = matrix[i+1][j1+2] - penalty;

         gapext = 1;
         for(l = j1+3; l<length2; l++, gapext++)
         {
            thisscore = matrix[i+1][l] - (penalty + gapext*penext);

            if(thisscore > down)
            {
               down = thisscore;
               dcell = l;
            }
         }

         /* Set score to best of these                                  */
         maxoff = MAX(right, down);
         if(dia >= maxoff)
         {
            matrix[i][j1] = dia;
            dirn[i][j1].x = i+1;
            dirn[i][j1].y = j1+1;
         }
         else
         {
            if(right > down)
            {
               matrix[i][j1] = right;
               dirn[i][j1].x = rcell;
               dirn[i][j1].y = j1+1;
            }
            else
            {
               matrix[i][j1] = down;
               dirn[i][j1].x = i+1;
               dirn[i][j1].y = dcell;
            }
         }

         /* Add the score for a match                                   */
         if(identity)
         {
            if(seq1[i] == seq2[j1]) matrix[i][j1] += match;
         }
         else
         {
            matrix[i][j1] += CalcMDMScore(seq1[i],seq2[j1]);
         }
      }
   }

   score = TraceBack(matrix, dirn, length1, length2,
                     seq1, seq2, align1, align2, align_len);

   if(verbose)
   {
      printf("Matrix:\n-------\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("%3d ",matrix[i][j]);
         }
         printf("\n");
      }

      printf("Path:\n-----\n");
      for(j=0; j<length2;j++)
      {
         for(i=0; i<length1; i++)
         {
            printf("(%3d,%3d) ",dirn[i][j].x,dirn[i][j].y);
         }
         printf("\n");
      }
   }

   FreeArray2D((char **)matrix, maxdim, maxdim);
   FreeArray2D((char **)dirn,   maxdim, maxdim);

   return(score);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movl %r9d, 0x24(%rsp)
movl %r8d, %ebp
movq %rdx, 0x18(%rsp)
movq %rdi, 0x40(%rsp)
cmpl %ecx, %esi
movq %rcx, 0x28(%rsp)
movl %ecx, %ebx
movq %rsi, 0x10(%rsp)
cmovgl %esi, %ebx
movl $0x4, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c647
xorl %r12d, %r12d
testq %rax, %rax
je 0x4cef3
movq %rax, 0x8(%rsp)
movl $0x8, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c647
testq %rax, %rax
je 0x4cef3
movq %rax, %r15
testl %ebx, %ebx
movq 0x8(%rsp), %rdi
jle 0x4c7c1
movl %ebx, %eax
xorl %ecx, %ecx
movq (%rdi,%rcx,8), %rdx
movq (%r15,%rcx,8), %rsi
xorl %r8d, %r8d
movl $0x0, (%rdx,%r8,4)
movq $-0x1, (%rsi,%r8,8)
incq %r8
cmpq %r8, %rax
jne 0x4c7a1
incq %rcx
cmpq %rax, %rcx
jne 0x4c796
movl %ebx, 0x9c(%rsp)
movq 0x28(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x50(%rsp)
testl %eax, %eax
jle 0x4c82c
movslq 0x10(%rsp), %rbx
decq %rbx
xorl %r14d, %r14d
movq 0x18(%rsp), %rax
movb (%rax,%r14), %cl
cmpl $0x0, 0x24(%rsp)
je 0x4c804
movl $0x1, %eax
movq 0x40(%rsp), %rdx
cmpb %cl, (%rdx,%rbx)
je 0x4c81a
jmp 0x4c822
movq 0x40(%rsp), %rax
movsbl (%rax,%rbx), %edi
movsbl %cl, %esi
callq 0x4cf08
movq 0x8(%rsp), %rdi
movq (%rdi,%rbx,8), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
cmpq %r14, 0x50(%rsp)
jne 0x4c7e3
movq %r15, 0x30(%rsp)
movq 0x10(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x60(%rsp)
testl %eax, %eax
jle 0x4c894
movslq 0x28(%rsp), %rbx
decq %rbx
xorl %r14d, %r14d
movq 0x40(%rsp), %rax
movb (%rax,%r14), %cl
cmpl $0x0, 0x24(%rsp)
je 0x4c86d
movl $0x1, %eax
movq 0x18(%rsp), %rdx
cmpb (%rdx,%rbx), %cl
je 0x4c883
jmp 0x4c88a
movq 0x18(%rsp), %rax
movsbl (%rax,%rbx), %esi
movsbl %cl, %edi
callq 0x4cf08
movq 0x8(%rsp), %rdi
movq (%rdi,%r14,8), %rcx
movl %eax, (%rcx,%rbx,4)
incq %r14
cmpq %r14, 0x60(%rsp)
jne 0x4c84c
movl %ebp, 0x98(%rsp)
cmpl $0x2, 0x10(%rsp)
setl %al
cmpl $0x2, 0x28(%rsp)
setl %cl
orb %al, %cl
jne 0x4cdc0
movl 0x108(%rsp), %r11d
movl 0x100(%rsp), %esi
movq 0x28(%rsp), %rcx
leal -0x1(%rcx), %r8d
movq 0x10(%rsp), %rax
leal -0x1(%rax), %r9d
movq 0x60(%rsp), %rdx
leaq 0x1(%rdx), %rbx
leal -0x2(%rax), %r12d
leal -0x2(%rcx), %eax
cmpl %r12d, %eax
movl %r12d, %ecx
movq %rax, 0xa8(%rsp)
cmovbl %eax, %ecx
movl %ecx, 0xa0(%rsp)
movq 0x50(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, 0x90(%rsp)
leal (%r11,%rsi), %ecx
negl %ecx
movl %ecx, 0x20(%rsp)
leaq 0x4(,%rax,4), %rax
movq %rax, 0xb0(%rsp)
leaq (%rdi,%rdx,8), %r14
addq $0x8, %r14
movq $0x0, 0x88(%rsp)
movq $-0x1, %r13
movq %r8, 0xc0(%rsp)
movq %r8, 0x48(%rsp)
movq %r14, 0x68(%rsp)
movq 0x48(%rsp), %r15
movq %r9, 0x78(%rsp)
leaq -0x1(%r15), %rax
leaq 0x1(%r15), %rcx
movq %rcx, 0x38(%rsp)
leal 0x2(%r15), %ecx
movl %ecx, 0x58(%rsp)
movq %rbx, %rbp
movq %r13, %r14
movq %rbx, 0x70(%rsp)
movq %r12, 0xb8(%rsp)
movq %rax, 0x48(%rsp)
leaq 0x2(%r12), %rax
xorl %ecx, %ecx
cmpl 0x10(%rsp), %eax
jge 0x4c9aa
movq 0x10(%rdi,%r12,8), %rcx
movl (%rcx,%r15,4), %ecx
movl 0x100(%rsp), %edx
subl %edx, %ecx
leal 0x3(%r12), %edx
cmpl 0x10(%rsp), %edx
jge 0x4c9e7
movl %ebp, %edx
leaq (%rdi,%rdx,8), %rdx
movl 0x20(%rsp), %esi
xorl %r10d, %r10d
movq (%rdx,%r10,8), %r8
movl (%r8,%r15,4), %r8d
addl %esi, %r8d
leal (%r10,%rbp), %r9d
cmpl %ecx, %r8d
cmovgl %r8d, %ecx
cmovgl %r9d, %eax
incq %r10
subl %r11d, %esi
cmpl %r10d, %r14d
jne 0x4c9c2
movq 0x8(%rdi,%r12,8), %r8
xorl %edx, %edx
movq 0x38(%rsp), %rsi
cmpq 0x50(%rsp), %rsi
jge 0x4ca08
movl 0x4(%r8,%r15,4), %edx
movl 0x100(%rsp), %esi
subl %esi, %edx
movl 0x58(%rsp), %esi
cmpl 0x28(%rsp), %esi
movq 0x90(%rsp), %rdi
movq %r8, 0x80(%rsp)
jge 0x4ca66
movq 0xb0(%rsp), %rsi
addq %rsi, %r8
movl 0x20(%rsp), %r9d
xorl %r10d, %r10d
movq 0x38(%rsp), %rsi
movl %esi, %ebx
movl (%r8,%r10,4), %r11d
addl %r9d, %r11d
leal (%rdi,%r10), %esi
cmpl %edx, %r11d
cmovgl %r11d, %edx
movl 0x108(%rsp), %r11d
cmovgl %esi, %ebx
incq %r10
subl %r11d, %r9d
cmpl %r10d, %r13d
jne 0x4ca3c
jmp 0x4ca6d
movq 0x38(%rsp), %rsi
movl %esi, %ebx
leaq 0x1(%r12), %r8
movq 0x80(%rsp), %rsi
movl (%rsi,%r15,4), %esi
cmpl %edx, %ecx
movl %edx, %r9d
cmovgl %ecx, %r9d
cmpl %r9d, %esi
jge 0x4cab4
movq 0x8(%rsp), %rdi
movq (%rdi,%r12,8), %rsi
cmpl %edx, %ecx
jle 0x4cad9
movq 0x48(%rsp), %rbx
movl %ecx, (%rsi,%rbx,4)
movq 0x30(%rsp), %rcx
movq (%rcx,%r12,8), %rcx
movl %eax, (%rcx,%rbx,8)
movl %r15d, 0x4(%rcx,%rbx,8)
jmp 0x4caf5
movq 0x8(%rsp), %rdi
movq (%rdi,%r12,8), %rax
movq 0x48(%rsp), %rbx
movl %esi, (%rax,%rbx,4)
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rax
movl %r8d, (%rax,%rbx,8)
movl %r15d, 0x4(%rax,%rbx,8)
jmp 0x4caf5
movq 0x48(%rsp), %rcx
movl %edx, (%rsi,%rcx,4)
movq 0x30(%rsp), %rax
movq (%rax,%r12,8), %rax
movl %r8d, (%rax,%rcx,8)
movl %ebx, 0x4(%rax,%rcx,8)
movq %rcx, %rbx
movq 0x40(%rsp), %rax
movb (%rax,%r12), %cl
cmpl $0x0, 0x24(%rsp)
je 0x4cb16
movl $0x1, %eax
movq 0x18(%rsp), %rdx
cmpb (%rdx,%rbx), %cl
je 0x4cb34
jmp 0x4cb3b
movq 0x18(%rsp), %rax
movsbl (%rax,%rbx), %esi
movsbl %cl, %edi
callq 0x4cf08
movl 0x108(%rsp), %r11d
movq 0x8(%rsp), %rdi
movq (%rdi,%r12,8), %rcx
addl %eax, (%rcx,%rbx,4)
leaq -0x1(%r12), %rax
decq %rbp
incq %r14
testl %r12d, %r12d
movq %rax, %r12
jg 0x4c98b
movq 0x78(%rsp), %rcx
leaq -0x1(%rcx), %r9
leaq 0x1(%rcx), %rax
movq %rax, 0x38(%rsp)
leal 0x2(%rcx), %eax
movl %eax, 0xa4(%rsp)
movq %r13, %r15
movq 0x90(%rsp), %r12
movq 0xa8(%rsp), %rbp
movq 0x88(%rsp), %rax
movl %eax, %edx
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movq %r9, 0x58(%rsp)
movl %edx, 0x80(%rsp)
movq (%rdi,%rcx,8), %rdx
movl 0x4(%rdx,%rbp,4), %eax
movq 0x38(%rsp), %rsi
cmpq 0x60(%rsp), %rsi
jge 0x4cbc9
movq 0x8(%rdi,%rcx,8), %rcx
movl 0x4(%rcx,%rbp,4), %ecx
movl 0x100(%rsp), %esi
subl %esi, %ecx
jmp 0x4cbcb
xorl %ecx, %ecx
movl 0xa4(%rsp), %esi
cmpl 0x10(%rsp), %esi
jge 0x4cc0c
movl 0x20(%rsp), %edi
xorl %r8d, %r8d
movq 0x38(%rsp), %rsi
movq (%r14,%r8,8), %r9
movl 0x4(%r9,%rbp,4), %r9d
addl %edi, %r9d
leal (%rbx,%r8), %r10d
cmpl %ecx, %r9d
cmovgl %r9d, %ecx
cmovgl %r10d, %esi
incq %r8
subl %r11d, %edi
cmpl %r8d, %r13d
jne 0x4cbe4
jmp 0x4cc11
movq 0x38(%rsp), %rsi
leaq 0x2(%rbp), %rbx
xorl %r8d, %r8d
movq 0x28(%rsp), %rdi
cmpl %edi, %ebx
jge 0x4cc31
movl 0x8(%rdx,%rbp,4), %r8d
movl 0x100(%rsp), %r9d
subl %r9d, %r8d
leal 0x3(%rbp), %r9d
cmpl %edi, %r9d
jge 0x4cc75
movl %r12d, %r9d
shlq $0x2, %r9
addq %r9, %rdx
movl 0x20(%rsp), %r9d
xorl %r10d, %r10d
movl (%rdx,%r10,4), %r11d
addl %r9d, %r11d
leal (%r12,%r10), %r14d
cmpl %r8d, %r11d
cmovgl %r11d, %r8d
movl 0x108(%rsp), %r11d
cmovgl %r14d, %ebx
incq %r10
subl %r11d, %r9d
cmpl %r10d, %r15d
jne 0x4cc4c
leaq 0x1(%rbp), %rdx
cmpl %r8d, %ecx
movl %r8d, %r9d
cmovgl %ecx, %r9d
cmpl %r9d, %eax
jge 0x4ccac
cmpl %r8d, %ecx
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %r9
jle 0x4ccd4
movq (%rdi,%r9,8), %rax
movl %ecx, (%rax,%rbp,4)
movq 0x30(%rsp), %rax
movq (%rax,%r9,8), %rax
movl %esi, (%rax,%rbp,8)
jmp 0x4ccce
movq 0x8(%rsp), %rdi
movq 0x58(%rsp), %r9
movq (%rdi,%r9,8), %rcx
movl %eax, (%rcx,%rbp,4)
movq 0x30(%rsp), %rax
movq (%rax,%r9,8), %rax
movq 0x78(%rsp), %rcx
movl %ecx, (%rax,%rbp,8)
movl %edx, 0x4(%rax,%rbp,8)
jmp 0x4ccf1
movq (%rdi,%r9,8), %rax
movl %r8d, (%rax,%rbp,4)
movq 0x30(%rsp), %rax
movq (%rax,%r9,8), %rax
movq 0x78(%rsp), %rcx
movl %ecx, (%rax,%rbp,8)
movl %ebx, 0x4(%rax,%rbp,8)
movq 0x70(%rsp), %rbx
movq 0x68(%rsp), %r14
movq 0x18(%rsp), %rax
movb (%rax,%rbp), %cl
cmpl $0x0, 0x24(%rsp)
je 0x4cd23
movl $0x1, %eax
movq 0x40(%rsp), %rdx
cmpb %cl, (%rdx,%r9)
movl 0x80(%rsp), %edx
je 0x4cd4e
jmp 0x4cd55
movq 0x40(%rsp), %rax
movsbl (%rax,%r9), %edi
movsbl %cl, %esi
callq 0x4cf08
movq 0x58(%rsp), %r9
movl 0x108(%rsp), %r11d
movq 0x8(%rsp), %rdi
movl 0x80(%rsp), %edx
movq (%rdi,%r9,8), %rcx
addl %eax, (%rcx,%rbp,4)
decq %rbp
decq %r12
incl %edx
incq %r15
cmpl 0xc0(%rsp), %edx
movq 0x78(%rsp), %rcx
jne 0x4cb9a
decq %rbx
movq 0x88(%rsp), %rcx
leal 0x1(%rcx), %eax
decq 0x90(%rsp)
movq 0xb8(%rsp), %r12
decq %r12
decq 0xa8(%rsp)
incq %r13
addq $-0x4, 0xb0(%rsp)
addq $-0x8, %r14
cmpl 0xa0(%rsp), %ecx
movq %rax, 0x88(%rsp)
jne 0x4c94f
subq $0x8, %rsp
movq 0x38(%rsp), %r13
movq %r13, %rsi
movq 0x18(%rsp), %rdx
movq 0x30(%rsp), %rbx
movl %ebx, %ecx
movq 0x48(%rsp), %r8
movq 0x20(%rsp), %r9
pushq 0x128(%rsp)
pushq 0x128(%rsp)
pushq 0x128(%rsp)
callq 0x4d012
addq $0x20, %rsp
movl %eax, %r12d
cmpl $0x0, 0x98(%rsp)
movq 0x60(%rsp), %rbp
je 0x4ced2
movl %r12d, 0x18(%rsp)
leaq 0x114a0(%rip), %rdi     # 0x5e2c2
callq 0x2090
testl %ebx, %ebx
jle 0x4ce72
leaq 0xba05(%rip), %rbx      # 0x58837
xorl %r14d, %r14d
movq 0x8(%rsp), %r12
cmpl $0x0, 0x10(%rsp)
jle 0x4ce5e
xorl %r15d, %r15d
movq (%r12,%r15,8), %rax
movl (%rax,%r14,4), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20f0
incq %r15
cmpq %r15, %rbp
jne 0x4ce44
movl $0xa, %edi
callq 0x2060
incq %r14
cmpq 0x50(%rsp), %r14
jne 0x4ce3a
leaq 0x11459(%rip), %rdi     # 0x5e2d2
callq 0x2090
cmpl $0x0, 0x28(%rsp)
movl 0x18(%rsp), %r12d
jle 0x4ced2
leaq 0x113da(%rip), %rbx     # 0x5e26b
xorl %r14d, %r14d
cmpl $0x0, 0x10(%rsp)
jle 0x4cebe
xorl %r15d, %r15d
movq (%r13,%r15,8), %rax
movl (%rax,%r14,8), %esi
movl 0x4(%rax,%r14,8), %edx
movq %rbx, %rdi
xorl %eax, %eax
callq 0x20f0
incq %r15
cmpq %r15, %rbp
jne 0x4ce9e
movl $0xa, %edi
callq 0x2060
incq %r14
cmpq 0x50(%rsp), %r14
jne 0x4ce94
movq 0x8(%rsp), %rdi
movl 0x9c(%rsp), %ebx
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c6de
movq %r13, %rdi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c6de
movl %r12d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq