// File: sisong[P]ApkDiffPatch/lzma/C/LzFindMt.c
// Function: BtGetMatches
static void BtGetMatches(CMatchFinderMt *p, UInt32 *d)
{
  UInt32 numProcessed = 0;
  UInt32 curPos = 2;
  
  /* GetMatchesSpec() functions don't create (len = 1)
     in [len, dist] match pairs, if (p->numHashBytes >= 2)
     Also we suppose here that (matchMaxLen >= 2).
     So the following code for (reserve) is not required
     UInt32 reserve = (p->matchMaxLen * 2);
     const UInt32 kNumHashBytes_Max = 5; // BT_HASH_BYTES_MAX
     if (reserve < kNumHashBytes_Max - 1)
        reserve = kNumHashBytes_Max - 1;
     const UInt32 limit = kMtBtBlockSize - (reserve);
  */

  const UInt32 limit = kMtBtBlockSize - (p->matchMaxLen * 2);

  d[1] = p->hashNumAvail;

  if (p->failure_BT)
  {
    // printf("\n == 1 BtGetMatches() p->failure_BT\n");
    d[0] = 0;
    // d[1] = 0;
    return;
  }
  
  while (curPos < limit)
  {
    if (p->hashBufPos == p->hashBufPosLimit)
    {
      // MatchFinderMt_GetNextBlock_Hash(p);
      UInt32 avail;
      {
        const UInt32 bi = MtSync_GetNextBlock(&p->hashSync);
        const UInt32 k = GET_HASH_BLOCK_OFFSET(bi);
        const UInt32 *h = p->hashBuf + k;
        avail = h[1];
        p->hashBufPosLimit = k + h[0];
        p->hashNumAvail = avail;
        p->hashBufPos = k + 2;
      }

      {
        /* we must prevent UInt32 overflow for avail total value,
           if avail was increased with new hash block */
        UInt32 availSum = numProcessed + avail;
        if (availSum < numProcessed)
          availSum = (UInt32)(Int32)-1;
        d[1] = availSum;
      }

      if (avail >= p->numHashBytes)
        continue;

      // if (p->hashBufPos != p->hashBufPosLimit) exit(1);

      /* (avail < p->numHashBytes)
         It means that stream was finished.
         And (avail) - is a number of remaining bytes,
         we fill (d) for (avail) bytes for LZ_THREAD (receiver).
         but we don't update (p->pos) and (p->cyclicBufferPos) here in BT_THREAD */

      /* here we suppose that we have space enough:
         (kMtBtBlockSize - curPos >= p->hashNumAvail) */
      p->hashNumAvail = 0;
      d[0] = curPos + avail;
      d += curPos;
      for (; avail != 0; avail--)
        *d++ = 0;
      return;
    }
    {
      UInt32 size = p->hashBufPosLimit - p->hashBufPos;
      UInt32 pos = p->pos;
      UInt32 cyclicBufferPos = p->cyclicBufferPos;
      UInt32 lenLimit = p->matchMaxLen;
      if (lenLimit >= p->hashNumAvail)
        lenLimit = p->hashNumAvail;
      {
        UInt32 size2 = p->hashNumAvail - lenLimit + 1;
        if (size2 < size)
          size = size2;
        size2 = p->cyclicBufferSize - cyclicBufferPos;
        if (size2 < size)
          size = size2;
      }
      
      if (pos > (UInt32)kMtMaxValForNormalize - size)
      {
        const UInt32 subValue = (pos - p->cyclicBufferSize); // & ~(UInt32)(kNormalizeAlign - 1);
        pos -= subValue;
        p->pos = pos;
        MatchFinder_Normalize3(subValue, p->son, (size_t)p->cyclicBufferSize * 2);
      }

      #ifndef MFMT_GM_INLINE
      while (curPos < limit && size-- != 0)
      {
        UInt32 *startDistances = d + curPos;
        UInt32 num = (UInt32)(GetMatchesSpec1(lenLimit, pos - p->hashBuf[p->hashBufPos++],
            pos, p->buffer, p->son, cyclicBufferPos, p->cyclicBufferSize, p->cutValue,
            startDistances + 1, p->numHashBytes - 1) - startDistances);
        *startDistances = num - 1;
        curPos += num;
        cyclicBufferPos++;
        pos++;
        p->buffer++;
      }
      #else
      {
        UInt32 posRes = pos;
        const UInt32 *d_end;
        {
          d_end = GetMatchesSpecN_2(
              p->buffer + lenLimit - 1,
              pos, p->buffer, p->son, p->cutValue, d + curPos,
              p->numHashBytes - 1, p->hashBuf + p->hashBufPos,
              d + limit, p->hashBuf + p->hashBufPos + size,
              cyclicBufferPos, p->cyclicBufferSize,
              &posRes);
        }
        {
          if (!d_end)
          {
            // printf("\n == 2 BtGetMatches() p->failure_BT\n");
            // internal data failure
            p->failure_BT = True;
            d[0] = 0;
            // d[1] = 0;
            return;
          }
        }
        curPos = (UInt32)(d_end - d);
        {
          const UInt32 processed = posRes - pos;
          pos = posRes;
          p->hashBufPos += processed;
          cyclicBufferPos += processed;
          p->buffer += processed;
        }
      }
      #endif

      {
        const UInt32 processed = pos - p->pos;
        numProcessed += processed;
        p->hashNumAvail -= processed;
        p->pos = pos;
      }
      if (cyclicBufferPos == p->cyclicBufferSize)
        cyclicBufferPos = 0;
      p->cyclicBufferPos = cyclicBufferPos;
    }
  }
  
  d[0] = curPos;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xa0, %rsp
movq %rdi, -0x28(%rbp)
movq %rsi, -0x30(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x2, -0x38(%rbp)
movq -0x28(%rbp), %rax
movl 0x2f0(%rax), %ecx
shll %ecx
movl $0x10000, %eax          # imm = 0x10000
subl %ecx, %eax
movl %eax, -0x3c(%rbp)
movq -0x28(%rbp), %rax
movl 0x2e0(%rax), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x28(%rbp), %rax
cmpl $0x0, 0x2e4(%rax)
je 0x7739b
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
jmp 0x7778e
jmp 0x7739d
movl -0x38(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jae 0x77785
movq -0x28(%rbp), %rax
movl 0x2d8(%rax), %eax
movq -0x28(%rbp), %rcx
cmpl 0x2dc(%rcx), %eax
jne 0x774c4
movq -0x28(%rbp), %rdi
addq $0x318, %rdi            # imm = 0x318
callq 0x777a0
movl %eax, -0x44(%rbp)
movl -0x44(%rbp), %eax
andl $0x1, %eax
shll $0x11, %eax
movl %eax, -0x48(%rbp)
movq -0x28(%rbp), %rax
movq 0x2d0(%rax), %rax
movl -0x48(%rbp), %ecx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rax
movl 0x4(%rax), %eax
movl %eax, -0x40(%rbp)
movl -0x48(%rbp), %ecx
movq -0x50(%rbp), %rax
addl (%rax), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2dc(%rax)
movl -0x40(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2e0(%rax)
movl -0x48(%rbp), %ecx
addl $0x2, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2d8(%rax)
movl -0x34(%rbp), %eax
addl -0x40(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
cmpl -0x34(%rbp), %eax
jae 0x7744d
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
movl -0x54(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x40(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x2f4(%rcx), %eax
jb 0x7746b
jmp 0x7739d
movq -0x28(%rbp), %rax
movl $0x0, 0x2e0(%rax)
movl -0x38(%rbp), %ecx
addl -0x40(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, %ecx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, -0x30(%rbp)
cmpl $0x0, -0x40(%rbp)
je 0x774bf
movq -0x30(%rbp), %rax
movq %rax, %rcx
addq $0x4, %rcx
movq %rcx, -0x30(%rbp)
movl $0x0, (%rax)
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x77499
jmp 0x7778e
movq -0x28(%rbp), %rax
movl 0x2dc(%rax), %eax
movq -0x28(%rbp), %rcx
subl 0x2d8(%rcx), %eax
movl %eax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movl 0x2f8(%rax), %eax
movl %eax, -0x5c(%rbp)
movq -0x28(%rbp), %rax
movl 0x308(%rax), %eax
movl %eax, -0x60(%rbp)
movq -0x28(%rbp), %rax
movl 0x2f0(%rax), %eax
movl %eax, -0x64(%rbp)
movl -0x64(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x2e0(%rcx), %eax
jb 0x7751e
movq -0x28(%rbp), %rax
movl 0x2e0(%rax), %eax
movl %eax, -0x64(%rbp)
movq -0x28(%rbp), %rax
movl 0x2e0(%rax), %eax
subl -0x64(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x7753f
movl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
movq -0x28(%rbp), %rax
movl 0x30c(%rax), %eax
subl -0x60(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x68(%rbp), %eax
cmpl -0x58(%rbp), %eax
jae 0x7755d
movl -0x68(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x5c(%rbp), %eax
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
subl -0x58(%rbp), %ecx
cmpl %ecx, %eax
jbe 0x775b6
movl -0x5c(%rbp), %eax
movq -0x28(%rbp), %rcx
subl 0x30c(%rcx), %eax
movl %eax, -0x6c(%rbp)
movl -0x6c(%rbp), %ecx
movl -0x5c(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x5c(%rbp)
movl -0x5c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2f8(%rax)
movl -0x6c(%rbp), %edi
movq -0x28(%rbp), %rax
movq 0x2e8(%rax), %rsi
movq -0x28(%rbp), %rax
movl 0x30c(%rax), %eax
movl %eax, %edx
shlq %rdx
callq 0x6fdf0
movl -0x5c(%rbp), %eax
movl %eax, -0x70(%rbp)
movq -0x28(%rbp), %rax
movq 0x300(%rax), %rdi
movl -0x64(%rbp), %eax
addq %rax, %rdi
addq $-0x1, %rdi
movl -0x5c(%rbp), %eax
movl %eax, %esi
movq -0x28(%rbp), %rax
movq 0x300(%rax), %rdx
movq -0x28(%rbp), %rax
movq 0x2e8(%rax), %rcx
movq -0x28(%rbp), %rax
movl 0x310(%rax), %r8d
movq -0x30(%rbp), %r9
movl -0x38(%rbp), %eax
shlq $0x2, %rax
addq %rax, %r9
movq -0x28(%rbp), %rax
movl 0x2f4(%rax), %eax
subl $0x1, %eax
movl %eax, %eax
movl %eax, %r12d
movq -0x28(%rbp), %rax
movq 0x2d0(%rax), %r15
movq -0x28(%rbp), %rax
movl 0x2d8(%rax), %eax
shlq $0x2, %rax
addq %rax, %r15
movq -0x30(%rbp), %r14
movl -0x3c(%rbp), %eax
shlq $0x2, %rax
addq %rax, %r14
movq -0x28(%rbp), %rax
movq 0x2d0(%rax), %rbx
movq -0x28(%rbp), %rax
movl 0x2d8(%rax), %eax
shlq $0x2, %rax
addq %rax, %rbx
movl -0x58(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rbx
movl -0x60(%rbp), %eax
movl %eax, %r11d
movq -0x28(%rbp), %rax
movl 0x30c(%rax), %r10d
leaq -0x70(%rbp), %rax
movq %r12, (%rsp)
movq %r15, 0x8(%rsp)
movq %r14, 0x10(%rsp)
movq %rbx, 0x18(%rsp)
movq %r11, 0x20(%rsp)
movl %r10d, 0x28(%rsp)
movq %rax, 0x30(%rsp)
callq 0x779c0
movq %rax, -0x78(%rbp)
cmpq $0x0, -0x78(%rbp)
jne 0x776cb
movq -0x28(%rbp), %rax
movl $0x1, 0x2e4(%rax)
movq -0x30(%rbp), %rax
movl $0x0, (%rax)
jmp 0x7778e
movq -0x78(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
sarq $0x2, %rax
movl %eax, -0x38(%rbp)
movl -0x70(%rbp), %eax
subl -0x5c(%rbp), %eax
movl %eax, -0x7c(%rbp)
movl -0x70(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x7c(%rbp), %ecx
movq -0x28(%rbp), %rax
addl 0x2d8(%rax), %ecx
movl %ecx, 0x2d8(%rax)
movl -0x7c(%rbp), %eax
addl -0x60(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x7c(%rbp), %edx
movq -0x28(%rbp), %rax
movq 0x300(%rax), %rcx
movl %edx, %edx
addq %rdx, %rcx
movq %rcx, 0x300(%rax)
movl -0x5c(%rbp), %eax
movq -0x28(%rbp), %rcx
subl 0x2f8(%rcx), %eax
movl %eax, -0x80(%rbp)
movl -0x80(%rbp), %eax
addl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x80(%rbp), %edx
movq -0x28(%rbp), %rax
movl 0x2e0(%rax), %ecx
subl %edx, %ecx
movl %ecx, 0x2e0(%rax)
movl -0x5c(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2f8(%rax)
movl -0x60(%rbp), %eax
movq -0x28(%rbp), %rcx
cmpl 0x30c(%rcx), %eax
jne 0x77773
movl $0x0, -0x60(%rbp)
movl -0x60(%rbp), %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x308(%rax)
jmp 0x7739d
movl -0x38(%rbp), %ecx
movq -0x30(%rbp), %rax
movl %ecx, (%rax)
addq $0xa0, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
nop