// File: /workspace/github_c_makefile_star2_O3/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, 0x14(%rsp)
movl %r8d, %ebp
movq %rdx, 0x48(%rsp)
movq %rdi, 0x40(%rsp)
cmpl %ecx, %esi
movq %rcx, 0x20(%rsp)
movl %ecx, %ebx
movq %rsi, 0x8(%rsp)
cmovgl %esi, %ebx
movl $0x4, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c62c
xorl %r14d, %r14d
testq %rax, %rax
je 0x4cecf
movq %rax, (%rsp)
movl $0x8, %edi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c62c
testq %rax, %rax
je 0x4cecf
testl %ebx, %ebx
movq (%rsp), %rdi
jle 0x4c79d
movl %ebx, %r9d
xorl %ecx, %ecx
movq (%rdi,%rcx,8), %rdx
movq (%rax,%rcx,8), %rsi
xorl %r8d, %r8d
movl $0x0, (%rdx,%r8,4)
movq $-0x1, (%rsi,%r8,8)
incq %r8
cmpq %r8, %r9
jne 0x4c77d
incq %rcx
cmpq %r9, %rcx
jne 0x4c772
movq %rax, 0x18(%rsp)
movl %ebx, 0x9c(%rsp)
movq 0x20(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x30(%rsp)
testl %eax, %eax
jle 0x4c807
movslq 0x8(%rsp), %rbx
xorl %r14d, %r14d
movq 0x40(%rsp), %rax
movb -0x1(%rax,%rbx), %cl
movq 0x48(%rsp), %rax
movb (%rax,%r14), %dl
cmpl $0x0, 0x14(%rsp)
je 0x4c7e5
movl $0x1, %eax
cmpb %dl, %cl
je 0x4c7f4
jmp 0x4c7fd
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x4cee4
movq (%rsp), %rdi
movq -0x8(%rdi,%rbx,8), %rcx
movl %eax, (%rcx,%r14,4)
incq %r14
cmpq %r14, 0x30(%rsp)
jne 0x4c7c1
movl %ebp, 0x98(%rsp)
movq 0x8(%rsp), %rax
movl %eax, %ecx
movq %rcx, 0x50(%rsp)
testl %eax, %eax
jle 0x4cd90
movl 0x108(%rsp), %ebx
movslq 0x20(%rsp), %r14
xorl %r15d, %r15d
movq 0x50(%rsp), %r12
movq 0x40(%rsp), %rax
movb (%rax,%r15), %cl
movq 0x48(%rsp), %rax
movb -0x1(%rax,%r14), %dl
cmpl $0x0, 0x14(%rsp)
je 0x4c85b
movl $0x1, %eax
cmpb %dl, %cl
je 0x4c86a
jmp 0x4c873
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x4cee4
movq (%rsp), %rdi
movq (%rdi,%r15,8), %rcx
movl %eax, -0x4(%rcx,%r14,4)
incq %r15
cmpq %r15, %r12
jne 0x4c836
cmpl $0x1, 0x8(%rsp)
sete %al
cmpl $0x2, 0x20(%rsp)
setl %cl
orb %al, %cl
jne 0x4cd90
movq 0x20(%rsp), %rcx
leal -0x1(%rcx), %esi
movq 0x8(%rsp), %rax
leal -0x1(%rax), %r11d
movq 0x50(%rsp), %rdx
leaq 0x1(%rdx), %r14
leal -0x2(%rax), %r8d
leal -0x2(%rcx), %eax
cmpl %r8d, %eax
movl %r8d, %ecx
movq %rax, 0xa8(%rsp)
cmovbl %eax, %ecx
movl %ecx, 0xa0(%rsp)
movq 0x30(%rsp), %rax
leaq 0x1(%rax), %r9
movl 0x100(%rsp), %ecx
leal (%rbx,%rcx), %r10d
movq %r8, %rcx
negl %r10d
movl %r10d, 0x10(%rsp)
leaq 0x4(,%rax,4), %rax
movq %rax, 0xb0(%rsp)
leaq (%rdi,%rdx,8), %rax
addq $0x8, %rax
movq %rax, 0x88(%rsp)
movq $0x0, 0x70(%rsp)
movq $-0x1, %r12
movq %rsi, 0xc0(%rsp)
movq %rsi, 0x60(%rsp)
movq %r9, 0x78(%rsp)
movq 0x60(%rsp), %r15
movq %r11, 0x68(%rsp)
leaq -0x1(%r15), %rax
leaq 0x1(%r15), %r11
leal 0x2(%r15), %edx
movl %edx, 0x80(%rsp)
movq %r12, %rbp
movq %r14, 0x58(%rsp)
movq %r14, %r13
movq %rcx, 0xb8(%rsp)
movq %rcx, %rsi
movq %r11, 0x38(%rsp)
movq %rax, 0x60(%rsp)
leal 0x2(%rsi), %eax
xorl %ecx, %ecx
cmpl 0x8(%rsp), %eax
jge 0x4c987
movq 0x10(%rdi,%rsi,8), %rcx
movl (%rcx,%r15,4), %ecx
movl 0x100(%rsp), %edx
subl %edx, %ecx
movq %rsi, 0x28(%rsp)
leal 0x3(%rsi), %edx
cmpl 0x8(%rsp), %edx
jge 0x4c9c7
movl %r13d, %edx
leaq (%rdi,%rdx,8), %rdx
movl 0x10(%rsp), %esi
xorl %r10d, %r10d
movq (%rdx,%r10,8), %r8
movl (%r8,%r15,4), %r8d
addl %esi, %r8d
leal (%r10,%r13), %r9d
cmpl %ecx, %r8d
cmovgl %r8d, %ecx
cmovgl %r9d, %eax
incq %r10
subl %ebx, %esi
cmpl %r10d, %ebp
jne 0x4c9a3
movq 0x28(%rsp), %rdx
movq 0x8(%rdi,%rdx,8), %rdi
xorl %edx, %edx
cmpq 0x30(%rsp), %r11
jge 0x4c9e8
movl 0x4(%rdi,%r15,4), %edx
movl 0x100(%rsp), %esi
subl %esi, %edx
movl 0x80(%rsp), %esi
cmpl 0x20(%rsp), %esi
movq %rdi, 0x90(%rsp)
jge 0x4ca43
movq 0xb0(%rsp), %rsi
leaq (%rdi,%rsi), %r8
movl 0x10(%rsp), %r9d
xorl %r10d, %r10d
movl %r11d, %esi
movq 0x78(%rsp), %rbx
movl 0x108(%rsp), %r14d
movl (%r8,%r10,4), %r11d
addl %r9d, %r11d
leal (%rbx,%r10), %edi
cmpl %edx, %r11d
cmovgl %r11d, %edx
cmovgl %edi, %esi
incq %r10
subl %r14d, %r9d
cmpl %r10d, %r12d
jne 0x4ca21
jmp 0x4ca46
movl %r11d, %esi
movq 0x28(%rsp), %r11
leaq 0x1(%r11), %r8
movq 0x90(%rsp), %rdi
movl (%rdi,%r15,4), %r9d
cmpl %edx, %ecx
movl %edx, %edi
cmovgl %ecx, %edi
cmpl %edi, %r9d
jge 0x4ca98
movq (%rsp), %rdi
movq (%rdi,%r11,8), %r10
cmpl %edx, %ecx
movl 0x108(%rsp), %ebx
movq 0x60(%rsp), %r14
movq %r11, %r9
jle 0x4cac5
movl %ecx, (%r10,%r14,4)
movq 0x18(%rsp), %rcx
movq (%rcx,%r9,8), %rcx
movl %eax, (%rcx,%r14,8)
movl %r15d, %esi
jmp 0x4cad6
movq (%rsp), %rdi
movq (%rdi,%r11,8), %r10
movq 0x60(%rsp), %r14
movl %r9d, (%r10,%r14,4)
movq 0x18(%rsp), %rax
movq (%rax,%r11,8), %rcx
movl %r8d, (%rcx,%r14,8)
movl %r15d, %esi
movl 0x108(%rsp), %ebx
movq %r11, %r9
jmp 0x4cad6
movl %edx, (%r10,%r14,4)
movq 0x18(%rsp), %rax
movq (%rax,%r9,8), %rcx
movl %r8d, (%rcx,%r14,8)
movl %esi, 0x4(%rcx,%r14,8)
movq 0x40(%rsp), %rax
movb (%rax,%r9), %cl
movq 0x48(%rsp), %rax
movb (%rax,%r14), %dl
cmpl $0x0, 0x14(%rsp)
je 0x4caff
movl $0x1, %eax
cmpb %dl, %cl
je 0x4cb17
jmp 0x4cb1b
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x4cee4
movq 0x28(%rsp), %r9
movq (%rsp), %rdi
movq (%rdi,%r9,8), %r10
addl %eax, (%r10,%r14,4)
leaq -0x1(%r9), %rsi
decq %r13
incq %rbp
testl %r9d, %r9d
movq 0x38(%rsp), %r11
jg 0x4c96a
movq 0x68(%rsp), %rcx
leaq -0x1(%rcx), %r11
leaq 0x1(%rcx), %rsi
leal 0x2(%rcx), %eax
movl %eax, 0xa4(%rsp)
movq %r12, %r13
movq 0x78(%rsp), %rbp
movq 0xa8(%rsp), %r15
movq 0x70(%rsp), %rax
movl %eax, %r8d
movq 0x58(%rsp), %r14
movq %rsi, 0x80(%rsp)
movq %r11, 0x38(%rsp)
movl %r8d, 0x28(%rsp)
movq (%rdi,%rcx,8), %rdx
movl 0x4(%rdx,%r15,4), %r8d
cmpq 0x50(%rsp), %rsi
jge 0x4cb9e
movq 0x8(%rdi,%rcx,8), %rcx
movl 0x4(%rcx,%r15,4), %ecx
movl 0x100(%rsp), %eax
subl %eax, %ecx
jmp 0x4cba0
xorl %ecx, %ecx
movq 0x88(%rsp), %rax
movl %r8d, 0x90(%rsp)
movl 0xa4(%rsp), %edi
cmpl 0x8(%rsp), %edi
jge 0x4cbeb
movl 0x10(%rsp), %edi
xorl %r8d, %r8d
movq (%rax,%r8,8), %r9
movl 0x4(%r9,%r15,4), %r9d
addl %edi, %r9d
leal (%r14,%r8), %r10d
cmpl %ecx, %r9d
cmovgl %r9d, %ecx
cmovgl %r10d, %esi
incq %r8
subl %ebx, %edi
cmpl %r8d, %r12d
jne 0x4cbc4
jmp 0x4cbeb
leal 0x2(%r15), %eax
xorl %r8d, %r8d
movq 0x20(%rsp), %rdi
cmpl %edi, %eax
jge 0x4cc0b
movl 0x8(%rdx,%r15,4), %r8d
movl 0x100(%rsp), %r9d
subl %r9d, %r8d
leal 0x3(%r15), %r9d
cmpl %edi, %r9d
jge 0x4cc47
movl %ebp, %r9d
shlq $0x2, %r9
addq %r9, %rdx
movl 0x10(%rsp), %r9d
xorl %r10d, %r10d
movl (%rdx,%r10,4), %r11d
addl %r9d, %r11d
leal (%r10,%rbp), %r14d
cmpl %r8d, %r11d
cmovgl %r11d, %r8d
cmovgl %r14d, %eax
incq %r10
subl %ebx, %r9d
cmpl %r10d, %r13d
jne 0x4cc26
leaq 0x1(%r15), %r9
cmpl %r8d, %ecx
movl %r8d, %edx
cmovgl %ecx, %edx
movl 0x90(%rsp), %r10d
cmpl %edx, %r10d
jge 0x4cc86
movq (%rsp), %rdi
movq 0x38(%rsp), %r11
movq (%rdi,%r11,8), %rdx
cmpl %r8d, %ecx
jle 0x4ccb0
movl %ecx, (%rdx,%r15,4)
movq 0x18(%rsp), %rax
movq (%rax,%r11,8), %rax
movl %esi, (%rax,%r15,8)
jmp 0x4cca9
movq (%rsp), %rdi
movq 0x38(%rsp), %r11
movq (%rdi,%r11,8), %rdx
movl %r10d, (%rdx,%r15,4)
movq 0x18(%rsp), %rax
movq (%rax,%r11,8), %rax
movq 0x68(%rsp), %rcx
movl %ecx, (%rax,%r15,8)
movl %r9d, 0x4(%rax,%r15,8)
jmp 0x4cccb
movl %r8d, (%rdx,%r15,4)
movq 0x18(%rsp), %rcx
movq (%rcx,%r11,8), %rcx
movq 0x68(%rsp), %rsi
movl %esi, (%rcx,%r15,8)
movl %eax, 0x4(%rcx,%r15,8)
movq 0x58(%rsp), %r14
movq 0x40(%rsp), %rax
movb (%rax,%r11), %cl
movq 0x48(%rsp), %rax
movb (%rax,%r15), %sil
cmpl $0x0, 0x14(%rsp)
je 0x4ccfa
movl $0x1, %eax
cmpb %sil, %cl
movl 0x28(%rsp), %r8d
je 0x4cd18
jmp 0x4cd1c
movsbl %cl, %edi
movsbl %sil, %esi
callq 0x4cee4
movq 0x38(%rsp), %r11
movq (%rsp), %rdi
movq (%rdi,%r11,8), %rdx
movl 0x28(%rsp), %r8d
addl %eax, (%rdx,%r15,4)
decq %r15
decq %rbp
incl %r8d
incq %r13
cmpl 0xc0(%rsp), %r8d
movq 0x68(%rsp), %rcx
movq 0x80(%rsp), %rsi
jne 0x4cb74
decq %r14
movq 0x70(%rsp), %rdx
leal 0x1(%rdx), %eax
movq 0x78(%rsp), %r9
decq %r9
movq 0xb8(%rsp), %rcx
decq %rcx
decq 0xa8(%rsp)
incq %r12
addq $-0x4, 0xb0(%rsp)
addq $-0x8, 0x88(%rsp)
cmpl 0xa0(%rsp), %edx
movq %rax, 0x70(%rsp)
jne 0x4c928
subq $0x8, %rsp
movq 0x20(%rsp), %r12
movq %r12, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %rbx
movl %ebx, %ecx
movq 0x48(%rsp), %r8
movq 0x50(%rsp), %r9
pushq 0x128(%rsp)
pushq 0x128(%rsp)
pushq 0x128(%rsp)
callq 0x4cfd3
addq $0x20, %rsp
movl %eax, %ebp
cmpl $0x0, 0x98(%rsp)
movq 0x50(%rsp), %r13
je 0x4ceac
leaq 0x114b4(%rip), %rdi     # 0x5e2a0
callq 0x2090
testl %ebx, %ebx
jle 0x4cea0
leaq 0xba0d(%rip), %rbx      # 0x5880d
xorl %r14d, %r14d
movq (%rsp), %r12
cmpl $0x0, 0x8(%rsp)
jle 0x4ce2b
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, %r13
jne 0x4ce11
movl $0xa, %edi
callq 0x2060
incq %r14
cmpq 0x30(%rsp), %r14
jne 0x4ce07
leaq 0x1146a(%rip), %rdi     # 0x5e2b0
callq 0x2090
cmpl $0x0, 0x20(%rsp)
movq 0x18(%rsp), %r12
jle 0x4ceac
leaq 0x113eb(%rip), %rbx     # 0x5e249
xorl %r14d, %r14d
cmpl $0x0, 0x8(%rsp)
jle 0x4ce8a
xorl %r15d, %r15d
movq (%r12,%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, %r13
jne 0x4ce6b
movl $0xa, %edi
callq 0x2060
incq %r14
cmpq 0x30(%rsp), %r14
jne 0x4ce61
jmp 0x4ceac
leaq 0x11409(%rip), %rdi     # 0x5e2b0
callq 0x2090
movq (%rsp), %rdi
movl 0x9c(%rsp), %ebx
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c6be
movq %r12, %rdi
movl %ebx, %esi
movl %ebx, %edx
callq 0x4c6be
movl %ebp, %r14d
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq