// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movl %r9d, 0x24(%rsp)
movl %r8d, %ebp
movl %ecx, %r13d
movq %rdx, 0x60(%rsp)
movl %esi, %ebx
movq %rdi, 0x58(%rsp)
cmpl %ecx, %esi
movl %ecx, %r14d
cmovgl %esi, %r14d
pushq $0x4
popq %rdi
movl %r14d, %esi
movl %r14d, %edx
callq 0x46351
xorl %r15d, %r15d
testq %rax, %rax
je 0x46add
movq %rax, 0x10(%rsp)
pushq $0x8
popq %rdi
movl %r14d, %esi
movl %r14d, %edx
callq 0x46351
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x46add
movl %ebp, 0x78(%rsp)
movl 0xe8(%rsp), %ebp
xorl %eax, %eax
testl %r14d, %r14d
movl $0x0, %ecx
cmovgl %r14d, %ecx
movl %r14d, %edx
movq 0x10(%rsp), %rdi
cmpq %rcx, %rax
je 0x464d2
xorl %esi, %esi
cmpq %rsi, %rdx
je 0x464cd
movq (%rdi,%rax,8), %r8
andl $0x0, (%r8,%rsi,4)
movq 0x48(%rsp), %r8
movq (%r8,%rax,8), %r8
orq $-0x1, (%r8,%rsi,8)
incq %rsi
jmp 0x464ac
incq %rax
jmp 0x464a5
movl %r14d, 0x7c(%rsp)
leal -0x1(%rbx), %eax
movl %eax, 0x50(%rsp)
movslq %eax, %r14
xorl %r15d, %r15d
testl %r13d, %r13d
movl $0x0, %eax
cmovgl %r13d, %eax
movq %rax, 0x88(%rsp)
pushq $0x1
popq %r12
cmpq %r15, 0x88(%rsp)
je 0x46545
movq 0x58(%rsp), %rax
movb (%rax,%r14), %cl
movq 0x60(%rsp), %rax
movb (%rax,%r15), %dl
cmpl $0x0, 0x24(%rsp)
je 0x46528
movl %r12d, %eax
cmpb %dl, %cl
je 0x46538
jmp 0x46540
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x46af2
movq 0x10(%rsp), %rdi
movq (%rdi,%r14,8), %rcx
movl %eax, (%rcx,%r15,4)
incq %r15
jmp 0x464fc
leal -0x1(%r13), %eax
movl %eax, 0xc(%rsp)
movslq %eax, %r15
xorl %r12d, %r12d
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
movq %rax, 0x80(%rsp)
pushq $0x1
popq %r14
cmpq %r12, 0x80(%rsp)
je 0x465b2
movq 0x58(%rsp), %rax
movb (%rax,%r12), %cl
movq 0x60(%rsp), %rax
movb (%rax,%r15), %dl
cmpl $0x0, 0x24(%rsp)
je 0x46595
movl %r14d, %eax
cmpb %dl, %cl
je 0x465a5
jmp 0x465ad
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x46af2
movq 0x10(%rsp), %rdi
movq (%rdi,%r12,8), %rcx
movl %eax, (%rcx,%r15,4)
incq %r12
jmp 0x46569
leal 0x1(%rbx), %r12d
leal 0x1(%r13), %r15d
movl 0x50(%rsp), %edx
movl 0xc(%rsp), %r14d
movl 0xe0(%rsp), %eax
addl %ebp, %eax
negl %eax
movl %eax, 0xc(%rsp)
leaq (,%r15,4), %rax
movq %rax, 0xa0(%rsp)
leaq (%rdi,%r12,8), %rax
movq %rax, 0x50(%rsp)
testl %edx, %edx
jle 0x469bd
testl %r14d, %r14d
jle 0x469bd
leaq -0x1(%rdx), %rcx
leaq -0x1(%r14), %rax
leal 0x1(%r14), %esi
movl %esi, 0x1c(%rsp)
movq %rax, 0x90(%rsp)
movl %eax, %r9d
movl %r12d, %esi
movq %rcx, 0x98(%rsp)
movl %ecx, %r10d
movq %rdx, 0x70(%rsp)
movq %r9, 0x68(%rsp)
testl %r10d, %r10d
js 0x467db
leal 0x2(%r10), %ecx
xorl %edx, %edx
cmpl %ebx, %ecx
jge 0x46655
movl %ecx, %eax
movq (%rdi,%rax,8), %rax
movl (%rax,%r14,4), %edx
movl 0xe0(%rsp), %eax
subl %eax, %edx
movl %esi, %eax
leaq (%rdi,%rax,8), %r8
leal 0x1(%r10), %eax
movl %eax, 0x40(%rsp)
movq %r10, 0x30(%rsp)
movl %r10d, %r11d
movq 0x8(%rdi,%r11,8), %rdi
movl (%rdi,%r14,4), %eax
movl %eax, 0x20(%rsp)
movl 0xc(%rsp), %r9d
movl %esi, %r10d
cmpl %ebx, %r10d
jge 0x466a4
movq (%r8), %rax
movl (%rax,%r14,4), %eax
addl %r9d, %eax
cmpl %edx, %eax
cmovgl %eax, %edx
cmovgl %r10d, %ecx
addq $0x8, %r8
incl %r10d
subl %ebp, %r9d
jmp 0x46680
movq %r11, 0x28(%rsp)
movl %esi, 0x3c(%rsp)
xorl %r9d, %r9d
cmpl %r13d, 0x1c(%rsp)
jge 0x466c6
movl 0x4(%rdi,%r14,4), %r9d
movl 0xe0(%rsp), %eax
subl %eax, %r9d
addq 0xa0(%rsp), %rdi
movl 0xc(%rsp), %r10d
xorl %r11d, %r11d
movl 0x1c(%rsp), %r8d
leaq (%r15,%r11), %rax
cmpl %r13d, %eax
jge 0x466fe
movl (%rdi,%r11,4), %esi
addl %r10d, %esi
cmpl %r9d, %esi
cmovgl %esi, %r9d
cmovgl %eax, %r8d
incq %r11
subl %ebp, %r10d
jmp 0x466db
cmpl %r9d, %edx
movl %r9d, %eax
cmovgl %edx, %eax
movl 0x20(%rsp), %esi
cmpl %eax, %esi
jge 0x46735
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r11
movq (%rdi,%r11,8), %rsi
cmpl %r9d, %edx
movq 0x30(%rsp), %r10
jle 0x46758
movq 0x68(%rsp), %r9
movl %edx, (%rsi,%r9,4)
movl %r14d, %r8d
jmp 0x46768
movl %esi, %eax
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %r11
movq (%rdi,%r11,8), %rsi
movq 0x68(%rsp), %r9
movl %eax, (%rsi,%r9,4)
movl %r14d, %r8d
movq 0x30(%rsp), %r10
jmp 0x46764
movq 0x68(%rsp), %rax
movl %r9d, (%rsi,%rax,4)
movq %rax, %r9
movl 0x40(%rsp), %ecx
movq 0x48(%rsp), %rax
movq (%rax,%r11,8), %rax
movl %ecx, (%rax,%r9,8)
movl %r8d, 0x4(%rax,%r9,8)
movq 0x58(%rsp), %rax
movb (%rax,%r11), %cl
movq 0x60(%rsp), %rax
movb (%rax,%r9), %dl
cmpl $0x0, 0x24(%rsp)
je 0x4679c
pushq $0x1
popq %rax
cmpb %dl, %cl
je 0x467c4
jmp 0x467c8
movsbl %cl, %edi
movsbl %dl, %esi
movq %r11, 0x28(%rsp)
callq 0x46af2
movq 0x30(%rsp), %r10
movq 0x68(%rsp), %r9
movq 0x10(%rsp), %rdi
movq 0x28(%rsp), %rcx
movq (%rdi,%rcx,8), %rsi
addl %eax, (%rsi,%r9,4)
decl %r10d
movl 0x3c(%rsp), %esi
decl %esi
movq 0x70(%rsp), %rdx
jmp 0x4662f
leal 0x1(%rdx), %eax
movl %eax, 0x1c(%rsp)
movl 0x98(%rsp), %r10d
movl %r15d, 0x30(%rsp)
movq 0x90(%rsp), %rax
movl %eax, %r9d
movq %r10, 0x40(%rsp)
testl %r9d, %r9d
js 0x46993
movl %r9d, %r14d
leaq 0x1(%r14), %r8
xorl %ecx, %ecx
cmpl %ebx, 0x1c(%rsp)
jge 0x46829
movq 0x8(%rdi,%rdx,8), %rax
movl (%rax,%r8,4), %ecx
movl 0xe0(%rsp), %eax
subl %eax, %ecx
movl 0x30(%rsp), %r10d
shlq $0x2, %r10
movq %r9, 0x28(%rsp)
leal 0x1(%r9), %eax
movl %eax, 0x20(%rsp)
movq (%rdi,%rdx,8), %rdi
movl 0x4(%rdi,%r14,4), %eax
movl %eax, 0x3c(%rsp)
movl 0xc(%rsp), %r9d
xorl %r11d, %r11d
movl 0x1c(%rsp), %edx
leaq (%r12,%r11), %rax
cmpl %ebx, %eax
jge 0x46880
movq 0x50(%rsp), %rsi
movq (%rsi,%r11,8), %rsi
movl (%rsi,%r8,4), %esi
addl %r9d, %esi
cmpl %ecx, %esi
cmovgl %esi, %ecx
cmovgl %eax, %edx
incq %r11
subl %ebp, %r9d
jmp 0x46858
movq 0x28(%rsp), %rax
leal 0x2(%rax), %r8d
xorl %r9d, %r9d
cmpl %r13d, %r8d
jge 0x468a2
movl %r8d, %eax
movl (%rdi,%rax,4), %r9d
movl 0xe0(%rsp), %eax
subl %eax, %r9d
addq %r10, %rdi
movl 0xc(%rsp), %r10d
movl 0x30(%rsp), %r11d
cmpl %r13d, %r11d
jge 0x468d0
movl (%rdi), %eax
addl %r10d, %eax
cmpl %r9d, %eax
cmovgl %eax, %r9d
cmovgl %r11d, %r8d
addq $0x4, %rdi
incl %r11d
subl %ebp, %r10d
jmp 0x468af
cmpl %r9d, %ecx
movl %r9d, %eax
cmovgl %ecx, %eax
movl 0x3c(%rsp), %esi
cmpl %eax, %esi
jge 0x468fa
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %r10
movq (%rdi,%r10,8), %rsi
cmpl %r9d, %ecx
jle 0x4691c
movl %ecx, (%rsi,%r14,4)
jmp 0x46915
movl %esi, %eax
movq 0x10(%rsp), %rdi
movq 0x40(%rsp), %r10
movq (%rdi,%r10,8), %rsi
movl %eax, (%rsi,%r14,4)
movq 0x70(%rsp), %rax
movl %eax, %edx
movl 0x20(%rsp), %r8d
jmp 0x46927
movl %r9d, (%rsi,%r14,4)
movq 0x70(%rsp), %rax
movl %eax, %edx
movq 0x48(%rsp), %rax
movq (%rax,%r10,8), %rax
movl %edx, (%rax,%r14,8)
movl %r8d, 0x4(%rax,%r14,8)
movq 0x58(%rsp), %rax
movb (%rax,%r10), %cl
movq 0x60(%rsp), %rax
movb (%rax,%r14), %dl
cmpl $0x0, 0x24(%rsp)
movq 0x28(%rsp), %r9
je 0x46960
pushq $0x1
popq %rax
cmpb %dl, %cl
je 0x4697e
jmp 0x46982
movsbl %cl, %edi
movsbl %dl, %esi
callq 0x46af2
movq 0x28(%rsp), %r9
movq 0x40(%rsp), %r10
movq 0x10(%rsp), %rdi
movq (%rdi,%r10,8), %rsi
addl %eax, (%rsi,%r14,4)
decl %r9d
decl 0x30(%rsp)
movq 0x70(%rsp), %rdx
jmp 0x467ff
decq %r12
decq %r15
addq $-0x4, 0xa0(%rsp)
addq $-0x8, 0x50(%rsp)
movq 0x90(%rsp), %r14
movq 0x98(%rsp), %rdx
jmp 0x465eb
subq $0x8, %rsp
movq 0x50(%rsp), %rsi
movl %ebx, %edx
movl %r13d, %ecx
movq 0x60(%rsp), %r8
movq 0x68(%rsp), %r9
pushq 0x108(%rsp)
pushq 0x108(%rsp)
pushq 0x108(%rsp)
callq 0x46bdf
addq $0x20, %rsp
movl %eax, 0x50(%rsp)
cmpl $0x0, 0x78(%rsp)
movq 0x88(%rsp), %r13
movq 0x80(%rsp), %rbp
je 0x46ab8
leaq 0x117f7(%rip), %rdi     # 0x58210
callq 0x2090
pushq $0xa
popq %rbx
leaq 0xbd45(%rip), %r12      # 0x5276d
xorl %r14d, %r14d
cmpq %r13, %r14
movq 0x10(%rsp), %rax
je 0x46a65
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x46a59
movq (%rax,%r15,8), %rax
movl (%rax,%r14,4), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x20f0
movq 0x10(%rsp), %rax
incq %r15
jmp 0x46a38
movl %ebx, %edi
callq 0x2060
incq %r14
jmp 0x46a2b
leaq 0x117b4(%rip), %rdi     # 0x58220
callq 0x2090
pushq $0xa
popq %rbx
leaq 0x1173e(%rip), %r12     # 0x581b9
xorl %r14d, %r14d
cmpq %r13, %r14
je 0x46ab8
xorl %r15d, %r15d
cmpq %r15, %rbp
je 0x46aac
movq 0x48(%rsp), %rax
movq (%rax,%r15,8), %rax
movl (%rax,%r14,8), %esi
movl 0x4(%rax,%r14,8), %edx
movq %r12, %rdi
xorl %eax, %eax
callq 0x20f0
incq %r15
jmp 0x46a86
movl %ebx, %edi
callq 0x2060
incq %r14
jmp 0x46a7e
movq 0x10(%rsp), %rdi
movl 0x7c(%rsp), %ebx
movl %ebx, %esi
movl %ebx, %edx
callq 0x463dc
movq 0x48(%rsp), %rdi
movl %ebx, %esi
movl %ebx, %edx
callq 0x463dc
movl 0x50(%rsp), %r15d
movl %r15d, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq