// File: /workspace/github_c_makefile_star2_O1/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 0x3e0a
movq %r13, %rdi
callq 0x4252
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), %r11d
leal 0x4(,%rax,2), %r14d
addl %eax, %eax
leal 0x40(,%rax,8), %esi
cmpl 0x20(%r13), %esi
jle 0x3e62
movq %r13, %rdi
movl %r11d, %ebx
movq %r8, %r12
callq 0x429d
movq %r12, %r8
movl %ebx, %r11d
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 %r11d, %esi
cmpq 0x18(%r12), %rax
movl $0x1, %ebx
movl $0x80000001, %eax       # imm = 0x80000001
cmovnel %eax, %ebx
sete %dl
testl %r15d, %r15d
cmovsl %eax, %ebx
testl %r11d, %r11d
cmovnsl %esi, %ebx
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 0x3f04
xorl %r11d, %r11d
testb $0x1, %al
je 0x3f11
movl 0x24(%r12), %eax
cltd
idivl 0x8(%r9)
movl %edx, %eax
jmp 0x3f13
movl 0x20(%r12), %eax
cltd
idivl 0x8(%r9)
movl %edx, %r11d
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 %r11
pushq %r15
pushq %rbx
pushq %r10
movq %rbx, %r12
movq %rcx, %r14
movq %r11, %rbx
movq %r15, 0x78(%rsp)
movq %rax, %r15
callq 0x42e9
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 %r14, 0x30(%rsp)
movq %r14, %rcx
movq %r12, %r14
movq %rax, %r12
movl %r8d, %r9d
movq %r15, 0x38(%rsp)
pushq %r15
movq 0x58(%rsp), %r15
movq %rbx, 0x48(%rsp)
pushq %rbx
pushq %r15
pushq %r14
pushq 0x48(%rsp)
callq 0x5bfe
movq 0x50(%rsp), %rdx
addq $0x30, %rsp
movl 0x10(%r12), %r9d
movl 0x14(%r12), %ecx
leal (%rcx,%r9), %eax
subl %eax, %edx
cmpl $0x2c, %ebp
jg 0x4043
cmpl $0x2c, %edx
jg 0x40aa
addl 0x18(%r12), %r9d
movl %r9d, %eax
shrl $0x1f, %eax
addl %r9d, %eax
sarl %eax
movl %eax, 0x10(%r12)
movl %eax, 0x18(%r12)
addl 0x1c(%r12), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, 0x14(%r12)
movl %eax, 0x1c(%r12)
movq 0x28(%rsp), %rbx
movl 0x18(%rbx), %eax
movl 0x1c(%rbx), %ecx
addl 0x10(%rbx), %eax
movl %eax, %edx
shrl $0x1f, %edx
addl %eax, %edx
sarl %edx
movl %edx, 0x10(%rbx)
movl %edx, 0x18(%rbx)
addl 0x14(%rbx), %ecx
movl %ecx, %eax
shrl $0x1f, %eax
addl %ecx, %eax
sarl %eax
movl %eax, 0x14(%rbx)
movl %eax, 0x1c(%rbx)
xorl %eax, %eax
movl %eax, 0x8(%r12)
movl %eax, 0x8(%rbx)
movq 0x10(%rsp), %r13
jmp 0x40f3
cmpl $0x2c, %edx
movq 0x10(%rsp), %r13
movq 0x28(%rsp), %rbx
jg 0x40f3
movl 0x18(%r12), %eax
movl 0x1c(%r12), %r10d
movl %eax, %r8d
subl %r10d, %r8d
movl %r8d, 0xc(%rsp)
addl %eax, %r10d
movl $0x0, 0x8(%rbx)
movq $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 0x5bfe
jmp 0x40ef
subl %ecx, %r9d
leaq 0xc(%rsp), %r8
movl %r9d, (%r8)
xorl %ecx, %ecx
movq 0x28(%rsp), %rbx
movl %ecx, 0x8(%rbx)
movl %ecx, 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 0x42e9
addq $0x30, %rsp
movl 0xc(%r12), %eax
movl %eax, 0xc(%rbx)
movl 0x8(%r13), %eax
testb $0x2, %al
jne 0x4130
testb $0x1, %al
jne 0x41cb
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)
jmp 0x4240
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
cmpq $0x3, %rcx
jl 0x4240
addq $-0x4, %rcx
xorl %edx, %edx
movzwl 0x4(%rax,%rcx,2), %esi
movzwl (%rax,%rdx,2), %edi
movw %di, 0x4(%rax,%rcx,2)
movw %si, (%rax,%rdx,2)
movzwl 0x6(%rax,%rcx,2), %esi
movzwl 0x2(%rax,%rdx,2), %edi
movw %di, 0x6(%rax,%rcx,2)
movw %si, 0x2(%rax,%rdx,2)
addq $0x2, %rdx
leaq -0x2(%rcx), %rsi
cmpq %rcx, %rdx
movq %rsi, %rcx
jl 0x4193
jmp 0x4240
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
cmpq $0x3, %rcx
jl 0x4240
addq $-0x4, %rcx
xorl %edx, %edx
movzwl 0x4(%rax,%rcx,2), %esi
movzwl (%rax,%rdx,2), %edi
movw %di, 0x4(%rax,%rcx,2)
movw %si, (%rax,%rdx,2)
movzwl 0x6(%rax,%rcx,2), %esi
movzwl 0x2(%rax,%rdx,2), %edi
movw %di, 0x6(%rax,%rcx,2)
movw %si, 0x2(%rax,%rdx,2)
addq $0x2, %rdx
leaq -0x2(%rcx), %rsi
cmpq %rcx, %rdx
movq %rsi, %rcx
jl 0x420a
movq %rbx, %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq