// File: /workspace/github_c_makefile_star2_O3/genometools[P]genometools/src/match/diagbandseed.c
// Function: gt_diagbandseed_segment2maxmatches
static void gt_diagbandseed_segment2maxmatches(
             bool forward,
             GtArrayGtDiagbandseedMaximalmatch *memstore,
             GtUword aseqnum,
             GtUword bseqnum,
             unsigned int seedlength,
             GtUword userdefinedleastlength,
             GtUword amaxlen,
             const GtSeedpairPositions *segment_positions,
             GtUword segment_length,
             GtDiagbandseedState *dbs_state,
             GtSegmentRejectFunc segment_reject_func,
             GtSegmentRejectInfo *segment_reject_info,
             const GtChain2Dimmode *chainmode,
             FILE *fpout)
{
  GtUword previous_matchlength = seedlength, localmatchcount = 0;
  const GtSeedpairPositions *current;
  GtSeedpairPositions previous;
  GtDiagbandseedSequencePair sequencepair = {aseqnum,bseqnum};
  const bool anchor_pairs = false;
  bool rejected = false;
#ifndef NDEBUG
  GtUword idx;
#endif

  gt_assert(segment_length > 0 && seedlength <= userdefinedleastlength);
#ifndef NDEBUG
  for (idx = 1; idx < segment_length; idx++)
  {
    gt_assert(segment_positions[idx-1].bpos < segment_positions[idx].bpos ||
              (segment_positions[idx-1].bpos == segment_positions[idx].bpos &&
               segment_positions[idx-1].apos <= segment_positions[idx].apos));
  }
#endif
  previous = segment_positions[0];
  if (memstore != NULL)
  {
    memstore->nextfreeGtDiagbandseedMaximalmatch = 0;
  }
  for (current = segment_positions + 1;
       current < segment_positions + segment_length && !rejected; current++)
  {
    if (previous.bpos == current->bpos && previous.apos + 1 == current->apos)
    {
      previous_matchlength++;
      previous.apos++;
    } else
    {
      if (previous.bpos == current->bpos &&
          previous.apos + seedlength - 1 >= current->apos)
      {
        /* This case can only happen if maxfreq exclude some intermediate
           matches */
        gt_assert(previous.apos <= current->apos);
        previous_matchlength += (current->apos - previous.apos + 1);
        previous.apos = current->apos;
      } else
      {
        /*
        printf("previous.diag = %u, current.diag=%u\n",previous.bpos,
                current->bpos);
        printf("previous.apos=%u,current.apos=%u,seedlength=%u\n",
                previous.apos,current->apos,seedlength);
        gt_assert (previous.bpos != current->bpos ||
                   previous.apos + seedlength - 1 < current->apos);*/
        if (anchor_pairs)
        {
          if (previous.bpos == current->bpos &&
              previous.apos + seedlength - 1 < current->apos)
          {
            printf("A %u\n",previous.apos+1);
          }
        } else
        {
          if (gt_diagbandseed_process_mem(forward,
                                          aseqnum,
                                          bseqnum,
                                          memstore,
                                          amaxlen,
                                          &previous,
                                          previous_matchlength,
                                          userdefinedleastlength,
                                          fpout))
          {
            localmatchcount++;
            if (segment_reject_func != NULL)
            {
              gt_segment_reject_register_match(segment_reject_info,bseqnum);
              rejected = true;
            }
          }
        }
        previous_matchlength = seedlength;
        previous = *current;
      }
    }
  }
  if (!anchor_pairs)
  {
    if (!rejected && gt_diagbandseed_process_mem(forward,
                                                 aseqnum,
                                                 bseqnum,
                                                 memstore,
                                                 amaxlen,
                                                 &previous,
                                                 previous_matchlength,
                                                 userdefinedleastlength,
                                                 fpout))
    {
      localmatchcount++;
      if (segment_reject_func != NULL)
      {
        gt_segment_reject_register_match(segment_reject_info,bseqnum);
      }
    }
  }
  if (dbs_state != NULL)
  {
    dbs_state->countmatches += localmatchcount;
    if (dbs_state->maxmatchespersegment < localmatchcount)
    {
      dbs_state->maxmatchespersegment = localmatchcount;
    }
  }
  if (memstore != NULL)
  {
    if (memstore->nextfreeGtDiagbandseedMaximalmatch >= 2)
    {
      qsort(memstore->spaceGtDiagbandseedMaximalmatch,
            memstore->nextfreeGtDiagbandseedMaximalmatch,
            sizeof *memstore->spaceGtDiagbandseedMaximalmatch,
            gt_diagbandseed_bstart_ldesc_compare_mems);
    }
    if (chainmode != NULL)
    {
      const GtUword presortdim = 1;
#ifndef NDEBUG
      GtUword previous_start_b = GT_UWORD_MAX;
#endif
      GtChain2Dimmatchvalues inmatch;
      GtChain2Dim *localchain = gt_chain_chain_new();
      GtDiagbandseedMaximalmatch *mmptr;
      GtChain2Dimmatchtable *chainmatchtable
        = gt_chain_matchtable_new(memstore->nextfreeGtDiagbandseedMaximalmatch);

      for (mmptr = memstore->spaceGtDiagbandseedMaximalmatch;
           mmptr < memstore->spaceGtDiagbandseedMaximalmatch +
                   memstore->nextfreeGtDiagbandseedMaximalmatch;
           mmptr++)
      {
        inmatch.startpos[0] = mmptr->apos + 1 - mmptr->len;
        inmatch.endpos[0] = mmptr->apos;
        inmatch.startpos[1] = mmptr->bpos + 1 - mmptr->len;
        inmatch.endpos[1] = mmptr->bpos;
        gt_assert(previous_start_b == GT_UWORD_MAX ||
                  previous_start_b <= inmatch.startpos[1]);
        inmatch.weight = mmptr->len;
        gt_chain_matchtable_add(chainmatchtable,&inmatch);
#ifndef NDEBUG
        previous_start_b = inmatch.startpos[1];
#endif
      }
      gt_chain_fastchaining(chainmode,
                            localchain,
                            chainmatchtable,
                            false,
                            presortdim,
                            true,
                            gt_diagbandseed_chain_out,
                            &sequencepair,
                            NULL);
      gt_chain_matchtable_delete(chainmatchtable);
      gt_chain_chain_delete(localchain);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r14
movq %r8, 0x8(%rsp)
movl %r8d, %eax
movq %rdx, 0x18(%rsp)
movl %edx, 0x28(%rsp)
movl %r14d, 0x2c(%rsp)
movq %r9, 0x20(%rsp)
movq %rax, 0x10(%rsp)
cmpq %r9, %rax
ja 0x1986ba
movq 0xa0(%rsp), %rax
testq %rax, %rax
je 0x1986ba
movq %rsi, %rbx
movq 0x98(%rsp), %r12
cmpq $0x2, %rax
jb 0x1983f7
movl $0x1, %ecx
movl 0x4(%r12,%rcx,8), %edx
cmpl %edx, -0x4(%r12,%rcx,8)
jb 0x1983ef
jne 0x1986ab
movl -0x8(%r12,%rcx,8), %edx
cmpl (%r12,%rcx,8), %edx
ja 0x1986ab
incq %rcx
cmpq %rcx, %rax
jne 0x1983ce
movq (%r12), %rcx
movq %rcx, (%rsp)
testq %rbx, %rbx
je 0x19840c
movq $0x0, 0x10(%rbx)
movzbl %dil, %r13d
cmpq $0x2, %rax
jl 0x198510
movq %rcx, %rdx
shrq $0x20, %rdx
leaq (%r12,%rax,8), %rbp
movq 0x8(%rsp), %rax
decl %eax
movq %rax, 0x8(%rsp)
addq $0x10, %r12
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
cmpl -0x4(%r12), %edx
jne 0x198472
leal 0x1(%rcx), %r8d
movl -0x8(%r12), %edi
cmpl %edi, %r8d
jne 0x198463
incq %rax
movl %r8d, (%rsp)
xorl %esi, %esi
movl %r8d, %ecx
jmp 0x1984e4
movq 0x8(%rsp), %rsi
addl %ecx, %esi
cmpl %edi, %esi
jae 0x1984f8
subq $0x8, %rsp
movl %r13d, %edi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x98(%rsp), %r8
leaq 0x8(%rsp), %r9
pushq 0xd0(%rsp)
pushq 0x30(%rsp)
pushq %rax
callq 0x1987ad
addq $0x20, %rsp
testb %al, %al
je 0x1984cd
incq %r15
cmpq $0x0, 0xb0(%rsp)
je 0x1984cd
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x1986bf
movb $0x1, %sil
jmp 0x1984cf
xorl %esi, %esi
movq -0x8(%r12), %rcx
movq %rcx, (%rsp)
movq %rcx, %rdx
shrq $0x20, %rdx
movq 0x10(%rsp), %rax
cmpq %rbp, %r12
jae 0x19851a
addq $0x8, %r12
testb %sil, %sil
je 0x19843d
jmp 0x19851a
movl %edi, %esi
subl %ecx, %esi
jb 0x1986b5
incl %esi
addq %rsi, %rax
movl %edi, (%rsp)
xorl %esi, %esi
movl %edi, %ecx
jmp 0x1984e4
xorl %r15d, %r15d
movq 0x10(%rsp), %rax
jmp 0x19851f
testb %sil, %sil
jne 0x198575
subq $0x8, %rsp
leaq 0x8(%rsp), %r9
movl %r13d, %edi
movq 0x20(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq 0x98(%rsp), %r8
pushq 0xd0(%rsp)
pushq 0x30(%rsp)
pushq %rax
callq 0x1987ad
addq $0x20, %rsp
testb %al, %al
je 0x198575
incq %r15
cmpq $0x0, 0xb0(%rsp)
je 0x198575
movq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x1986bf
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0x198590
addq %r15, 0x20(%rax)
cmpq %r15, 0x38(%rax)
jae 0x198590
movq %r15, 0x38(%rax)
testq %rbx, %rbx
je 0x19869c
movq 0xc0(%rsp), %r14
movq 0x10(%rbx), %rsi
cmpq $0x2, %rsi
jb 0x1985bf
movq (%rbx), %rdi
leaq 0x347(%rip), %rcx       # 0x1988fc
movl $0xc, %edx
callq 0x17f60
testq %r14, %r14
je 0x19869c
callq 0x18e29c
movq %rax, %r15
movq 0x10(%rbx), %rdi
callq 0x18c840
movq %rax, %r12
cmpq $0x0, 0x10(%rbx)
jle 0x198657
movq (%rbx), %rbp
movq $-0x1, %rax
leaq 0x30(%rsp), %r13
movl (%rbp), %edx
movl 0x8(%rbp), %ecx
movl %edx, %esi
subl %ecx, %esi
incl %esi
movq %rsi, 0x30(%rsp)
movq %rdx, 0x40(%rsp)
movl 0x4(%rbp), %esi
movl %esi, %edx
subl %ecx, %edx
incl %edx
movq %rdx, 0x38(%rsp)
movq %rsi, 0x48(%rsp)
cmpq $-0x1, %rax
je 0x19862a
cmpq %rdx, %rax
ja 0x1986b0
movq %rcx, 0x50(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x18c92f
movq 0x38(%rsp), %rax
addq $0xc, %rbp
movq 0x10(%rbx), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x2, %rcx
addq (%rbx), %rcx
cmpq %rcx, %rbp
jb 0x1985f2
subq $0x8, %rsp
leaq 0x30(%rsp), %rax
leaq 0x2f2(%rip), %r10       # 0x198959
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
movl $0x1, %r8d
movl $0x1, %r9d
pushq $0x0
pushq %rax
pushq %r10
callq 0x18ca5f
addq $0x20, %rsp
movq %r12, %rdi
callq 0x18c8a6
movq %r15, %rdi
callq 0x18e2c3
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x5dff4
callq 0x5e066
callq 0x5e02d
callq 0x5e09f