// File: /workspace/github_c_makefile_star2_O0/thegenemyers[P]DASCRUBBER/DASpatch.c
// Function: span_patch
static Patch *span_patch(Overlap *ovls, int novl, Interval *lblock, Interval *rblock)
{ static Patch patch;

  int j, k;
  int lft, rgt; 
  int lcv, rcv; 
  int bread, bcomp, blen;
  int ab, ae;
  int lidx, ridx, sidx, cidx;
  Patch *can;

  lft = lblock->end;
  rgt = rblock->beg;
  lcv = lft - COVER_LEN;
  rcv = rgt + COVER_LEN;
  if (lcv < lblock->beg)
    lcv = lblock->beg;
  if (rcv > rblock->end)
    rcv = rblock->end;

  //  Find LA pairs or LAs spanning the gap flank [lcv,rcv]

  patch.bread = -1;
  patch.bad   = DB->maxlen;
  patch.avg   = 100;
  for (j = 0; j < novl; j = k)
    { bread = ovls[j].bread;
      blen  = DB->reads[bread].rlen;
      bcomp = COMP(ovls[j].flags);
      if (bcomp)
        cidx = j;

      lidx = ridx = sidx = -1;    //  For all LA's with same b-read
      for (k = j; k < novl; k++)
        { if (ovls[k].bread != bread)
            break;
          if (COMP(ovls[k].flags) != (uint32) bcomp)   //  Note when b switches orientation
            { cidx  = k;
              bcomp = COMP(ovls[k].flags);
            }
          ab = ovls[k].path.abpos;
          ae = ovls[k].path.aepos;

#ifdef SHOW_PAIRS
          printf("\n %5d [%5d,%5d] %c",bread,ab,ae,COMP(ovls[k].flags)?'c':'n');
          if (ab <= lcv && ae >= rcv)
            printf("s");
          else
            printf(" ");
#endif

          //  Is LA a spanner, left-partner, or right partner

          if (ab <= lcv && ae >= rcv)
            { sidx = k;
              lidx = ridx = -1;
              continue;
            }

#ifdef SHOW_PAIRS
         if (ae >= rcv && ab <= rcv && ab - ovls[k].path.bbpos <= lft - COVER_LEN)
           printf("r");
         else
           printf(" ");
         if (ab <= lcv && ae >= lcv && ae + (blen-ovls[j].path.bepos) >= rgt + COVER_LEN)
           printf("l");
         else
           printf(" ");
#endif

          if (ae >= rcv && ab <= rcv && ab - ovls[k].path.bbpos <= lft - COVER_LEN)
            ridx = k;
          if (ab <= lcv && ae >= lcv && ae + (blen-ovls[j].path.bepos) >= rgt + COVER_LEN)
            lidx = k;
        }
      if (! bcomp)
        cidx = k;

#ifdef SHOW_PAIRS
      printf(" =");
      if (sidx >= 0)
        printf(" S");
      if (lidx >= 0)
        printf(" L");
      if (ridx >= 0)
        printf(" R");
      if (0 <= lidx && lidx < ridx && (ridx < cidx || lidx >= cidx))
        printf(" G");
      if ((0<=ridx && ridx<cidx && cidx<=lidx) || (0<=lidx && lidx<cidx && cidx<=ridx))
        printf(" A");
#endif

      //  Check for spanning pair

      if (sidx >= 0)
        lidx = ridx = sidx;
      else if (0 > lidx || lidx >= ridx || (ridx >= cidx && cidx > lidx))
        continue;

      //  Otherwise consider the gap linkable and try to patch it, declaring a split
      //    iff all patch attemtps fail

#ifdef DEBUG_GAP_FILL
      if (lidx != ridx)
        printf("       %5d [%5d,%5d] [%5d,%5d]",
               ovls[lidx].bread,ovls[lidx].path.abpos,ovls[lidx].path.aepos,
               ovls[ridx].path.abpos,ovls[ridx].path.aepos);
      else
        printf("       %5d [%5d,%5d] SSS",
               ovls[lidx].bread,ovls[lidx].path.abpos,ovls[lidx].path.aepos);
#endif

      can = compute_patch(lft,rgt,ovls+lidx,ovls+ridx);

      if (can != NULL)
        {
#ifdef DEBUG_GAP_FILL
          printf(" %d",can->end - can->beg);
#endif
          if ( ! unsuitable(can->bread,can->beg,can->end) && can->anc <= ANCHOR_THRESH)
            { if (can->bad < patch.bad)
                patch = *can;
              else if (can->bad == patch.bad)
                { if (can->avg < patch.avg)
                    patch = *can;
                }
#ifdef DEBUG_GAP_FILL
              printf("  AA %d %d(%d)",can->anc,can->bad,can->avg);
#endif
            }
        }
#ifdef DEBUG_GAP_FILL
      printf("\n");
#endif
    } 

#ifdef DEBUG_GAP_FILL
  if (patch.bread >= 0)
    printf("    SPAN %5d:  PATCH = %d%c[%d..%d]  %d %d(%d)\n",rgt-lft,
           patch.bread,patch.comp?'c':'n',patch.beg,
           patch.end,patch.anc,patch.bad,patch.avg);
  else
    printf("    SPAN PATCH FAIL\n");
#endif

  return (&patch);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x18(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x20(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x30(%rbp)
movl -0x2c(%rbp), %eax
subl $0x190, %eax            # imm = 0x190
movl %eax, -0x34(%rbp)
movl -0x30(%rbp), %eax
addl $0x190, %eax            # imm = 0x190
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl (%rcx), %eax
jge 0x3c34
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
movq -0x20(%rbp), %rcx
cmpl 0x4(%rcx), %eax
jle 0x3c4a
movq -0x20(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x38(%rbp)
movl $0xffffffff, 0x278e0(%rip) # imm = 0xFFFFFFFF
movq 0x2752d(%rip), %rax     # 0x2b188
movl 0x20(%rax), %eax
movl %eax, 0x278e4(%rip)     # 0x2b548
movl $0x64, 0x278de(%rip)    # 0x2b54c
movl $0x0, -0x24(%rbp)
movl -0x24(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x3f74
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
movl %eax, -0x3c(%rbp)
movq 0x274eb(%rip), %rax     # 0x2b188
movq 0x60(%rax), %rax
movslq -0x3c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
movl %eax, -0x44(%rbp)
movq -0x8(%rbp), %rax
movslq -0x24(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
movl %eax, -0x40(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x3cd6
movl -0x24(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl $0xffffffff, -0x58(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
movl -0x24(%rbp), %eax
movl %eax, -0x28(%rbp)
movl -0x28(%rbp), %eax
cmpl -0xc(%rbp), %eax
jge 0x3e26
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x28(%rax), %eax
cmpl -0x3c(%rbp), %eax
je 0x3d19
jmp 0x3e26
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
cmpl -0x40(%rbp), %eax
je 0x3d51
movl -0x28(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %eax
andl $0x1, %eax
movl %eax, -0x40(%rbp)
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movl %eax, -0x48(%rbp)
movq -0x8(%rbp), %rax
movslq -0x28(%rbp), %rcx
imulq $0x30, %rcx, %rcx
addq %rcx, %rax
movl 0x18(%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x34(%rbp), %eax
jg 0x3da1
movl -0x4c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jl 0x3da1
movl -0x28(%rbp), %eax
movl %eax, -0x58(%rbp)
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl $0xffffffff, -0x50(%rbp) # imm = 0xFFFFFFFF
jmp 0x3e18
movl -0x4c(%rbp), %eax
cmpl -0x38(%rbp), %eax
jl 0x3dd9
movl -0x48(%rbp), %eax
cmpl -0x38(%rbp), %eax
jg 0x3dd9
movl -0x48(%rbp), %eax
movq -0x8(%rbp), %rcx
movslq -0x28(%rbp), %rdx
imulq $0x30, %rdx, %rdx
addq %rdx, %rcx
subl 0x14(%rcx), %eax
movl -0x2c(%rbp), %ecx
subl $0x190, %ecx            # imm = 0x190
cmpl %ecx, %eax
jg 0x3dd9
movl -0x28(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x48(%rbp), %eax
cmpl -0x34(%rbp), %eax
jg 0x3e16
movl -0x4c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jl 0x3e16
movl -0x4c(%rbp), %eax
movl -0x44(%rbp), %ecx
movq -0x8(%rbp), %rdx
movslq -0x24(%rbp), %rsi
imulq $0x30, %rsi, %rsi
addq %rsi, %rdx
subl 0x1c(%rdx), %ecx
addl %ecx, %eax
movl -0x30(%rbp), %ecx
addl $0x190, %ecx            # imm = 0x190
cmpl %ecx, %eax
jl 0x3e16
movl -0x28(%rbp), %eax
movl %eax, -0x50(%rbp)
jmp 0x3e18
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
jmp 0x3cf1
cmpl $0x0, -0x40(%rbp)
jne 0x3e32
movl -0x28(%rbp), %eax
movl %eax, -0x5c(%rbp)
cmpl $0x0, -0x58(%rbp)
jl 0x3e43
movl -0x58(%rbp), %eax
movl %eax, -0x54(%rbp)
movl %eax, -0x50(%rbp)
jmp 0x3e69
xorl %eax, %eax
cmpl -0x50(%rbp), %eax
jg 0x3e62
movl -0x50(%rbp), %eax
cmpl -0x54(%rbp), %eax
jge 0x3e62
movl -0x54(%rbp), %eax
cmpl -0x5c(%rbp), %eax
jl 0x3e67
movl -0x5c(%rbp), %eax
cmpl -0x50(%rbp), %eax
jle 0x3e67
jmp 0x3f69
jmp 0x3e69
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movq -0x8(%rbp), %rdx
movslq -0x50(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rdx
movq -0x8(%rbp), %rcx
movslq -0x54(%rbp), %rax
imulq $0x30, %rax, %rax
addq %rax, %rcx
callq 0x3f90
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
je 0x3f67
movq -0x68(%rbp), %rax
movl (%rax), %edi
movq -0x68(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x68(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x42d0
cmpl $0x0, %eax
jne 0x3f65
movq -0x68(%rbp), %rax
movl 0x10(%rax), %eax
cmpl 0x27584(%rip), %eax     # 0x2b454
jg 0x3f65
movq -0x68(%rbp), %rax
movl 0x14(%rax), %eax
cmpl 0x27665(%rip), %eax     # 0x2b548
jge 0x3f14
movq -0x68(%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0x27641(%rip)     # 0x2b534
movq 0x8(%rax), %rcx
movq %rcx, 0x2763e(%rip)     # 0x2b53c
movq 0x10(%rax), %rcx
movq %rcx, 0x2763b(%rip)     # 0x2b544
movl 0x18(%rax), %eax
movl %eax, 0x2763a(%rip)     # 0x2b54c
jmp 0x3f63
movq -0x68(%rbp), %rax
movl 0x14(%rax), %eax
cmpl 0x27627(%rip), %eax     # 0x2b548
jne 0x3f61
movq -0x68(%rbp), %rax
movl 0x18(%rax), %eax
cmpl 0x2761c(%rip), %eax     # 0x2b54c
jge 0x3f5f
movq -0x68(%rbp), %rax
movq (%rax), %rcx
movq %rcx, 0x275f4(%rip)     # 0x2b534
movq 0x8(%rax), %rcx
movq %rcx, 0x275f1(%rip)     # 0x2b53c
movq 0x10(%rax), %rcx
movq %rcx, 0x275ee(%rip)     # 0x2b544
movl 0x18(%rax), %eax
movl %eax, 0x275ed(%rip)     # 0x2b54c
jmp 0x3f61
jmp 0x3f63
jmp 0x3f65
jmp 0x3f67
jmp 0x3f69
movl -0x28(%rbp), %eax
movl %eax, -0x24(%rbp)
jmp 0x3c75
leaq 0x275b9(%rip), %rax     # 0x2b534
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)