// File: /workspace/github_c_makefile_star2_O2/thegenemyers[P]DASCRUBBER/align.c
// Function: Local_Alignment
Path *Local_Alignment(Alignment *align, Work_Data *ework, Align_Spec *espec,
                      int low, int hgh, int anti, int lbord, int hbord)
{ _Work_Data  *work = ( _Work_Data *) ework;
  _Align_Spec *spec = (_Align_Spec *) espec;

  Path *apath, *bpath;
  int   aoff, boff;
  int   minp, maxp;
  int   selfie;
  int   fshort, rshort;

  { int alen, blen;
    int maxtp, wsize;

    alen = align->alen;
    blen = align->blen;

    if (hgh-low >= 7500)
      wsize = VectorEl*(hgh-low+1);
    else
      wsize = VectorEl*10000;
    if (wsize >= work->vecmax)
      if (enlarge_vector(work,wsize))
        EXIT(NULL);

    if (alen < blen)
      maxtp = 2*(blen/spec->trace_space+2);
    else
      maxtp = 2*(alen/spec->trace_space+2);
    wsize = 4*maxtp*sizeof(uint16) + sizeof(Path);
    if (wsize > work->pntmax)
      if (enlarge_points(work,wsize))
        EXIT(NULL);

    apath = align->path;
    bpath = (Path *) work->points;

    apath->trace = ((uint16 *) (bpath+1)) + maxtp;
    bpath->trace = ((uint16 *) apath->trace) +  2*maxtp;
  }

#ifdef DEBUG_PASSES
  printf("\n");
#endif

  selfie = (align->aseq == align->bseq);

  while (((anti-hgh) >> 1) < 0)
    hgh -= 1;
   
  if (lbord < 0)
    { if (selfie && low >= 0)
        minp = 1;
      else
        minp = -INT32_MAX;
    }
  else
    minp = low-lbord;
  if (hbord < 0)
    { if (selfie && hgh <= 0)
        maxp = -1;
      else
        maxp = INT32_MAX;
    }
  else
    maxp = hgh+hbord;

  if (ACOMP(align->flags))
    { aoff = align->alen % spec->trace_space;
      boff = 0;
    }
  else if (COMP(align->flags))
    { aoff = 0;
      boff = align->blen % spec->trace_space;
    }
  else
    { aoff = 0;
      boff = 0;
    }

  if (forward_wave(work,spec,align,bpath,&low,hgh,anti,minp,maxp,aoff,boff))
    EXIT(NULL);

  fshort = ((apath->aepos + apath->bepos) - anti < DUB_TRIM);

#ifdef DEBUG_PASSES
  printf("F1 (%d,%d) ~ %d => (%d,%d) %d\n",
         (2*anti+(low+hgh))/4,(2*anti-(low+hgh))/4,hgh-low,
         apath->aepos,apath->bepos,apath->diffs);
#endif

  if (reverse_wave(work,spec,align,bpath,low,low,anti,minp,maxp,aoff,boff))
    EXIT(NULL);

#ifdef DEBUG_PASSES
  printf("R1 (%d,%d) => (%d,%d) %d\n",
         (anti+low)/2,(anti-low)/2,apath->abpos,apath->bbpos,apath->diffs);
#endif

  rshort = (anti - (apath->abpos + apath->bbpos) < DUB_TRIM);

  if (fshort)
    { if (rshort)
        { apath->aepos = apath->abpos = (apath->abpos+apath->aepos)/2;
          apath->bepos = apath->bbpos = (apath->bbpos+apath->bepos)/2;
          bpath->aepos = bpath->abpos = (bpath->abpos+bpath->aepos)/2;
          bpath->bepos = bpath->bbpos = (bpath->bbpos+bpath->bepos)/2;
          apath->tlen  = 0;
          bpath->tlen  = 0;
        }
      else
        { low  = apath->abpos - apath->bbpos;
          anti = apath->abpos + apath->bbpos;
          apath->tlen = bpath->tlen = 0;
          if (forward_wave(work,spec,align,bpath,&low,low,anti,minp,maxp,aoff,boff))
            EXIT(NULL);
        }
    }
  else
    { if (rshort)
        { low  = apath->aepos - apath->bepos;
          anti = apath->aepos + apath->bepos;
          apath->tlen = bpath->tlen = 0;
          apath->diffs = 0;
          if (reverse_wave(work,spec,align,bpath,low,low,anti,minp,maxp,aoff,boff))
            EXIT(NULL);
        }
    }
        

  bpath->diffs = apath->diffs;
  if (ACOMP(align->flags))
    { uint16 *trace = (uint16 *) apath->trace;
      uint16  p;
      int     i, j;

      bpath->aepos = apath->bepos;
      bpath->bepos = apath->aepos;
      bpath->abpos = apath->bbpos;
      bpath->bbpos = apath->abpos;

      apath->abpos = align->alen - bpath->bepos;
      apath->bbpos = align->blen - bpath->aepos;
      apath->aepos = align->alen - bpath->bbpos;
      apath->bepos = align->blen - bpath->abpos;
      i = apath->tlen-2;
      j = 0;
      while (j < i)
        { p = trace[i];
          trace[i] = trace[j];
          trace[j] = p;
          p = trace[i+1];
          trace[i+1] = trace[j+1];
          trace[j+1] = p;
          i -= 2;
          j += 2;
        }
    }
  else if (COMP(align->flags))
    { uint16 *trace = (uint16 *) bpath->trace;
      uint16  p;
      int     i, j;

      bpath->abpos = align->blen - apath->bepos;
      bpath->bbpos = align->alen - apath->aepos;
      bpath->aepos = align->blen - apath->bbpos;
      bpath->bepos = align->alen - apath->abpos;
      i = bpath->tlen-2;
      j = 0;
      while (j < i)
        { p = trace[i];
          trace[i] = trace[j];
          trace[j] = p;
          p = trace[i+1];
          trace[i+1] = trace[j+1];
          trace[j+1] = p;
          i -= 2;
          j += 2;
        }
    }
  else
    { bpath->aepos = apath->bepos;
      bpath->bepos = apath->aepos;
      bpath->abpos = apath->bbpos;
      bpath->bbpos = apath->abpos;
    }

#ifdef DEBUG_POINTS
  { uint16 *trace = (uint16 *) apath->trace;
    int     a, h;

    printf("\nA-path (%d,%d)->(%d,%d)",apath->abpos,apath->bbpos,apath->aepos,apath->bepos);
    printf(" %c\n",((COMP(align->flags) || ACOMP(align->flags)) ? 'c' : 'n'));
    a = apath->bbpos;
    for (h = 1; h < apath->tlen; h += 2)
      { int dif = trace[h-1];
        int del = trace[h];
        a += del;
        printf("      %d / %d (%d)\n",dif,del,a);
      }
  }

  { uint16 *trace = (uint16 *) bpath->trace;
    int     a, h;

    printf("\nB-path (%d,%d)->(%d,%d)",bpath->abpos,bpath->bbpos,bpath->aepos,bpath->bepos);
    printf(" %c [%d,%d]\n",((COMP(align->flags) || ACOMP(align->flags)) ? 'c' : 'n'),
                           align->blen,align->alen);
    a = bpath->bbpos;
    for (h = 1; h < bpath->tlen; h += 2)
      { int dif = trace[h-1];
        int del = trace[h];
        a += del;
        printf("      %d / %d (%d)\n",dif,del,a);
      }
  }
#endif

  return (bpath);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movl %r9d, %r10d
movl %r8d, %ebp
movl %ecx, %r15d
movq %rdx, %r9
movq %rsi, %r13
movq %rdi, %r12
movl %ecx, 0xc(%rsp)
movl 0x20(%rdi), %r14d
movl 0x24(%rdi), %ebx
movl %r8d, %eax
subl %ecx, %eax
movl %eax, %ecx
shll $0x5, %ecx
addl $0x20, %ecx
cmpl $0x1d4c, %eax           # imm = 0x1D4C
movl $0x4e200, %esi          # imm = 0x4E200
cmovgel %ecx, %esi
cmpl (%r13), %esi
movq %rdi, 0x10(%rsp)
movq %rdx, 0x18(%rsp)
movq %r10, 0x20(%rsp)
jl 0x2bf6
movq %r13, %rdi
callq 0x3017
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r12
movl 0x98(%rsp), %r8d
cmpl %ebx, %r14d
cmovgl %r14d, %ebx
movl %ebx, %eax
cltd
idivl 0x8(%r9)
movl 0x90(%rsp), %ebx
leal 0x4(,%rax,2), %r14d
addl %eax, %eax
leal 0x40(,%rax,8), %esi
cmpl 0x20(%r13), %esi
jle 0x2c47
movq %r13, %rdi
movq %r8, %r12
callq 0x3060
movq %r12, %r8
movq 0x20(%rsp), %r10
movq 0x18(%rsp), %r9
movq 0x10(%rsp), %r12
movq (%r12), %rdi
movq 0x28(%r13), %rcx
movslq %r14d, %rax
leaq (%rcx,%rax,2), %rsi
leaq (%rcx,%rax,2), %rdx
addq $0x20, %rdx
movq %rdx, (%rdi)
addl %eax, %eax
cltq
leaq (%rsi,%rax,2), %rax
addq $0x20, %rax
movq %rax, (%rcx)
movq 0x10(%r12), %rax
cmpl %ebp, %r10d
cmovll %r10d, %ebp
movl %r15d, %esi
subl %ebx, %esi
cmpq 0x18(%r12), %rax
pushq $0x1
popq %r14
movl $0x80000001, %eax       # imm = 0x80000001
cmovnel %eax, %r14d
sete %dl
testl %r15d, %r15d
cmovsl %eax, %r14d
testl %ebx, %ebx
cmovnsl %esi, %r14d
leal (%r8,%rbp), %eax
testl %ebp, %ebp
setle %sil
andb %dl, %sil
movzbl %sil, %r15d
shll $0x1f, %r15d
orl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
testl %r8d, %r8d
cmovnsl %eax, %r15d
movl 0x8(%r12), %eax
testb $0x2, %al
movq %rdi, 0x50(%rsp)
jne 0x2ce8
xorl %ebx, %ebx
testb $0x1, %al
je 0x2cf4
movl 0x24(%r12), %eax
cltd
idivl 0x8(%r9)
movl %edx, %eax
jmp 0x2cf6
movl 0x20(%r12), %eax
cltd
idivl 0x8(%r9)
movl %edx, %ebx
xorl %eax, %eax
subq $0x8, %rsp
leaq 0x14(%rsp), %r8
movq %r13, %rdi
movq %r9, %rsi
movq %r12, %rdx
movl %ebp, %r9d
pushq %rax
pushq %rbx
pushq %r15
pushq %r14
pushq %r10
movq %rcx, %r12
movq %r15, 0x78(%rsp)
movq %r14, %r15
movq %rax, %r14
callq 0x30aa
addq $0x30, %rsp
movq 0x50(%rsp), %rax
movl 0x18(%rax), %ebp
leaq 0xc(%rsp), %rcx
movl (%rcx), %r8d
subl 0x20(%rsp), %ebp
addl 0x1c(%rax), %ebp
subq $0x8, %rsp
movq %r13, 0x48(%rsp)
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq %r12, 0x30(%rsp)
movq %r12, %rcx
movq %rax, %r12
movl %r8d, %r9d
movq %r14, 0x38(%rsp)
pushq %r14
movq %r15, %r14
movq 0x58(%rsp), %r15
movq %rbx, 0x48(%rsp)
pushq %rbx
pushq %r15
pushq %r14
pushq 0x48(%rsp)
callq 0x47b1
movq 0x50(%rsp), %rdx
addq $0x30, %rsp
movq 0x10(%r12), %r9
movq %r9, %xmm0
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ecx
leal (%rcx,%r9), %eax
subl %eax, %edx
cmpl $0x2c, %ebp
jg 0x2e2c
cmpl $0x2c, %edx
jg 0x2e88
movq 0x18(%r12), %xmm1
paddd %xmm0, %xmm1
movdqa %xmm1, %xmm0
psrld $0x1f, %xmm0
paddd %xmm1, %xmm0
pshufd $0x44, %xmm0, %xmm0     # xmm0 = xmm0[0,1,0,1]
psrad $0x1, %xmm0
movdqu %xmm0, 0x10(%r12)
movq 0x28(%rsp), %rbx
movq 0x14(%rbx), %xmm0
movd 0x10(%rbx), %xmm1
movd 0x1c(%rbx), %xmm2
punpckldq %xmm1, %xmm2    # xmm2 = xmm2[0],xmm1[0],xmm2[1],xmm1[1]
paddd %xmm0, %xmm2
movdqa %xmm2, %xmm0
psrld $0x1f, %xmm0
paddd %xmm2, %xmm0
psrad $0x1, %xmm0
movq %xmm0, 0x14(%rbx)
movd %xmm0, 0x1c(%rbx)
andl $0x0, 0x8(%r12)
andl $0x0, 0x8(%rbx)
movq 0x10(%rsp), %r13
jmp 0x2ed1
cmpl $0x2c, %edx
movq 0x10(%rsp), %r13
movq 0x28(%rsp), %rbx
jg 0x2ed1
movl 0x18(%r12), %eax
movl 0x1c(%r12), %r10d
movl %eax, %r8d
subl %r10d, %r8d
andl $0x0, 0x8(%rbx)
addl %eax, %r10d
andq $0x0, 0x8(%r12)
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
movq %rbx, %rcx
movl %r8d, %r9d
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq %r15
pushq %r14
pushq %r10
callq 0x47b1
jmp 0x2ecd
subl %ecx, %r9d
leaq 0xc(%rsp), %r8
movl %r9d, (%r8)
movq 0x28(%rsp), %rbx
andl $0x0, 0x8(%rbx)
andl $0x0, 0x8(%r12)
subq $0x8, %rsp
movq 0x48(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq 0x18(%rsp), %r13
movq %r13, %rdx
movq %rbx, %rcx
pushq 0x38(%rsp)
pushq 0x48(%rsp)
pushq %r15
pushq %r14
pushq %rax
callq 0x30aa
addq $0x30, %rsp
movl 0xc(%r12), %eax
movl %eax, 0xc(%rbx)
movl 0x8(%r13), %eax
testb $0x2, %al
jne 0x2f1b
testb $0x1, %al
jne 0x2fa7
movl 0x1c(%r12), %eax
movl %eax, 0x18(%rbx)
movl 0x18(%r12), %eax
movl %eax, 0x1c(%rbx)
movl 0x14(%r12), %eax
movl %eax, 0x10(%rbx)
movl 0x10(%r12), %eax
movl %eax, 0x14(%rbx)
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%r12), %rax
movl 0x1c(%r12), %ecx
movl %ecx, 0x18(%rbx)
movl 0x18(%r12), %edx
movl %edx, 0x1c(%rbx)
movl 0x14(%r12), %esi
movl %esi, 0x10(%rbx)
movl 0x10(%r12), %esi
movl %esi, 0x14(%rbx)
movl 0x20(%r13), %esi
movl %esi, %edi
subl %edx, %edi
movl %edi, 0x10(%r12)
movl 0x24(%r13), %edx
movl %edx, %edi
subl %ecx, %edi
movl %edi, 0x14(%r12)
subl 0x14(%rbx), %esi
movl %esi, 0x18(%r12)
subl 0x10(%rbx), %edx
movl %edx, 0x1c(%r12)
movslq 0x8(%r12), %rcx
addq $-0x2, %rcx
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x2f09
movzwl (%rax,%rcx,2), %esi
movzwl (%rax,%rdx,2), %edi
movw %di, (%rax,%rcx,2)
movw %si, (%rax,%rdx,2)
movzwl 0x2(%rax,%rcx,2), %esi
movzwl 0x2(%rax,%rdx,2), %edi
movw %di, 0x2(%rax,%rcx,2)
movw %si, 0x2(%rax,%rdx,2)
addq $0x2, %rdx
addq $-0x2, %rcx
jmp 0x2f74
movq (%rbx), %rax
movl 0x24(%r13), %ecx
movl %ecx, %edx
subl 0x1c(%r12), %edx
movl %edx, 0x10(%rbx)
movl 0x20(%r13), %edx
movl %edx, %esi
subl 0x18(%r12), %esi
movl %esi, 0x14(%rbx)
subl 0x14(%r12), %ecx
movl %ecx, 0x18(%rbx)
subl 0x10(%r12), %edx
movl %edx, 0x1c(%rbx)
movslq 0x8(%rbx), %rcx
addq $-0x2, %rcx
xorl %edx, %edx
cmpq %rcx, %rdx
jge 0x2f09
movzwl (%rax,%rcx,2), %esi
movzwl (%rax,%rdx,2), %edi
movw %di, (%rax,%rcx,2)
movw %si, (%rax,%rdx,2)
movzwl 0x2(%rax,%rcx,2), %esi
movzwl 0x2(%rax,%rdx,2), %edi
movw %di, 0x2(%rax,%rcx,2)
movw %si, 0x2(%rax,%rdx,2)
addq $0x2, %rdx
addq $-0x2, %rcx
jmp 0x2fe0