// File: /workspace/github_cpp_makefile_star2_O1/sisong[P]ApkDiffPatch/lzma/C/LzFindOpt.c
// Function: GetMatchesSpecN_2
Z7_NO_INLINE
UInt32 * Z7_FASTCALL GetMatchesSpecN_2(const Byte *lenLimit, size_t pos, const Byte *cur, CLzRef *son,
    UInt32 _cutValue, UInt32 *d, size_t _maxLen, const UInt32 *hash, const UInt32 *limit, const UInt32 *size,
    size_t _cyclicBufferPos, UInt32 _cyclicBufferSize,
    UInt32 *posRes)
{
  do // while (hash != size)
  {
    UInt32 delta;
    
  #ifndef cbs
    UInt32 cbs;
  #endif

    if (hash == size)
      break;

    delta = *hash++;

    if (delta == 0)
      return NULL;

    lenLimit++;

  #ifndef cbs
    cbs = _cyclicBufferSize;
    if ((UInt32)pos < cbs)
    {
      if (delta > (UInt32)pos)
        return NULL;
      cbs = (UInt32)pos;
    }
  #endif

    if (delta >= cbs)
    {
      CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);
      *d++ = 0;
      ptr1[0] = kEmptyHashValue;
      ptr1[1] = kEmptyHashValue;
    }
else
{
  UInt32 *_distances = ++d;

  CLzRef *ptr0 = son + ((size_t)_cyclicBufferPos << 1) + 1;
  CLzRef *ptr1 = son + ((size_t)_cyclicBufferPos << 1);

  UInt32 cutValue = _cutValue;
  const Byte *len0 = cur, *len1 = cur;
  const Byte *maxLen = cur + _maxLen;

  // if (cutValue == 0) { *ptr0 = *ptr1 = kEmptyHashValue; } else
  for (LOG_ITER(g_NumIters_Tree++);;)
  {
    LOG_ITER(g_NumIters_Loop++);
    {
      // SPEC code
      CLzRef *pair = son + ((size_t)((ptrdiff_t)_cyclicBufferPos - (ptrdiff_t)delta
          + (ptrdiff_t)(UInt32)(_cyclicBufferPos < delta ? cbs : 0)
          ) << 1);

      const ptrdiff_t diff = (ptrdiff_t)0 - (ptrdiff_t)delta;
      const Byte *len = (len0 < len1 ? len0 : len1);

    #ifdef USE_SON_PREFETCH
      const UInt32 pair0 = *pair;
    #endif

      if (len[diff] == len[0])
      {
        if (++len != lenLimit && len[diff] == len[0])
          while (++len != lenLimit)
          {
            LOG_ITER(g_NumIters_Bytes++);
            if (len[diff] != len[0])
              break;
          }
        if (maxLen < len)
        {
          maxLen = len;
          *d++ = (UInt32)(len - cur);
          *d++ = delta - 1;
          
          if (len == lenLimit)
          {
            const UInt32 pair1 = pair[1];
            *ptr1 =
              #ifdef USE_SON_PREFETCH
                pair0;
              #else
                pair[0];
              #endif
            *ptr0 = pair1;

            _distances[-1] = (UInt32)(d - _distances);

            #ifdef USE_LONG_MATCH_OPT

                if (hash == size || *hash != delta || lenLimit[diff] != lenLimit[0] || d >= limit)
                  break;

            {
              for (;;)
              {
                *d++ = 2;
                *d++ = (UInt32)(lenLimit - cur);
                *d++ = delta - 1;
                cur++;
                lenLimit++;
                // SPEC
                _cyclicBufferPos++;
                {
                  // SPEC code
                  CLzRef *dest = son + ((size_t)(_cyclicBufferPos) << 1);
                  const CLzRef *src = dest + ((diff
                      + (ptrdiff_t)(UInt32)((_cyclicBufferPos < delta) ? cbs : 0)) << 1);
                  // CLzRef *ptr = son + ((size_t)(pos) << 1) - CYC_TO_POS_OFFSET * 2;
                  #if 0
                  *(UInt64 *)(void *)dest = *((const UInt64 *)(const void *)src);
                  #else
                  const UInt32 p0 = src[0];
                  const UInt32 p1 = src[1];
                  dest[0] = p0;
                  dest[1] = p1;
                  #endif
                }
                pos++;
                hash++;
                if (hash == size || *hash != delta || lenLimit[diff] != lenLimit[0] || d >= limit)
                  break;
              } // for() end for long matches
            }
            #endif

            break; // break from TREE iterations
          }
        }
      }
      {
        const UInt32 curMatch = (UInt32)pos - delta; // (UInt32)(pos + diff);
        if (len[diff] < len[0])
        {
          delta = pair[1];
          *ptr1 = curMatch;
          ptr1 = pair + 1;
          len1 = len;
          if (delta >= curMatch)
            return NULL;
        }
        else
        {
          delta = *pair;
          *ptr0 = curMatch;
          ptr0 = pair;
          len0 = len;
          if (delta >= curMatch)
            return NULL;
        }
        delta = (UInt32)pos - delta;
 
        if (--cutValue == 0 || delta >= cbs)
        {
          *ptr0 = *ptr1 = kEmptyHashValue;
          _distances[-1] = (UInt32)(d - _distances);
          break;
        }
      }
    }
  } // for (tree iterations)
}
    pos++;
    _cyclicBufferPos++;
    cur++;
  }
  while (d < limit);
  *posRes = (UInt32)pos;
  return d;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movl %r8d, -0x34(%rsp)
movq %rdx, -0x68(%rsp)
movq %rsi, %r14
movq %rdi, -0x80(%rsp)
movl 0x78(%rsp), %eax
movq 0x70(%rsp), %rdx
movq %rdx, -0x78(%rsp)
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %r8
movq 0x58(%rsp), %rbx
leaq 0x8(%rcx), %rdx
movq %rdx, -0x18(%rsp)
movq %rcx, -0x70(%rsp)
cmpq %rdi, %rbx
je 0x3b7f7
movl (%rbx), %r10d
addq $0x4, %rbx
movl $0x1, %r11d
testl %r10d, %r10d
je 0x3b7fd
incq -0x80(%rsp)
movl %eax, %edx
cmpl %eax, %r14d
jae 0x3b476
movl %r14d, %edx
cmpl %r14d, %r10d
ja 0x3b7fd
cmpl %edx, %r10d
jae 0x3b802
movq %rbx, -0x58(%rsp)
movq %r9, -0x28(%rsp)
addq $0x4, %r9
movq -0x78(%rsp), %rax
leaq (%rcx,%rax,8), %rsi
movq %rsi, -0x50(%rsp)
leaq 0x4(%rcx,%rax,8), %rax
movq %rax, -0x48(%rsp)
movq -0x68(%rsp), %r13
movq 0x50(%rsp), %rax
leaq (%r13,%rax), %r12
movl %edx, -0x30(%rsp)
movl %edx, %eax
movq %rax, -0x20(%rsp)
movl -0x34(%rsp), %eax
movl %eax, -0x2c(%rsp)
movq %r13, %rdi
movq %r9, %rsi
movq %r9, -0x40(%rsp)
movl %r10d, %edx
movq %rdx, %r8
negq %r8
movq -0x78(%rsp), %rax
subq %rdx, %rax
movl $0x0, %r11d
cmovbq -0x20(%rsp), %r11
addq %rax, %r11
leaq (%rcx,%r11,8), %rbp
cmpq %r13, %rdi
movq %r13, %r15
cmovbq %rdi, %r15
movq %r15, %rax
subq %rdx, %rax
movb (%rax), %al
cmpb (%r15), %al
jne 0x3b57f
leaq 0x1(%r15), %rbx
cmpq -0x80(%rsp), %rbx
je 0x3b53a
movb (%rbx,%r8), %al
cmpb (%rbx), %al
jne 0x3b53a
addq $0x2, %r15
movq %r15, %rbx
cmpq -0x80(%rsp), %r15
je 0x3b53a
movb (%rbx,%r8), %al
leaq 0x1(%rbx), %r15
cmpb (%rbx), %al
je 0x3b524
cmpq %rbx, %r12
jae 0x3b57c
movq %r14, -0x60(%rsp)
movl (%rcx,%r11,8), %r11d
movl %ebx, %eax
subl -0x68(%rsp), %eax
movl %eax, (%rsi)
leal -0x1(%r10), %r15d
leaq 0x8(%rsi), %r14
movl %r15d, 0x4(%rsi)
cmpq -0x80(%rsp), %rbx
je 0x3b636
movq %rbx, %r12
movq %rbx, %r15
movq %r14, %rsi
movq -0x60(%rsp), %r14
movq -0x70(%rsp), %rcx
jmp 0x3b57f
movq %rbx, %r15
movl %r14d, %edx
subl %r10d, %edx
movb (%r15,%r8), %al
cmpb (%r15), %al
jae 0x3b5ba
movl 0x4(%rbp), %r10d
addq $0x4, %rbp
movq -0x50(%rsp), %rax
movl %edx, (%rax)
movl $0x1, %r11d
movq %rbp, -0x50(%rsp)
movq %r15, %r13
cmpl %edx, %r10d
jb 0x3b5d8
movq %rbp, -0x50(%rsp)
movq %r15, %r13
jmp 0x3b622
movl (%rbp), %r10d
movq -0x48(%rsp), %rax
movl %edx, (%rax)
movl $0x1, %r11d
movq %rbp, -0x48(%rsp)
movq %r15, %rdi
cmpl %edx, %r10d
jae 0x3b61a
movl %r10d, %eax
movl %r14d, %r10d
subl %eax, %r10d
decl -0x2c(%rsp)
je 0x3b5f1
xorl %r11d, %r11d
cmpl -0x30(%rsp), %r10d
jb 0x3b622
xorl %eax, %eax
movq -0x50(%rsp), %rdx
movl %eax, (%rdx)
movq -0x48(%rsp), %rdx
movl %eax, (%rdx)
movq %rsi, %rax
subq %r9, %rax
shrq $0x2, %rax
movq -0x28(%rsp), %rdx
movl %eax, (%rdx)
movl $0x4, %r11d
jmp 0x3b622
movq %rbp, -0x48(%rsp)
movq %r15, %rdi
movq %r12, %rbx
movq %rbx, %r12
testl %r11d, %r11d
je 0x3b4d2
jmp 0x3b81f
movl 0x4(%rbp), %eax
movq -0x50(%rsp), %r12
movl %r11d, (%r12)
movq -0x48(%rsp), %r11
movl %eax, (%r11)
movq %r14, %rax
subq %r9, %rax
shrq $0x2, %rax
movq -0x28(%rsp), %r9
movl %eax, (%r9)
movl $0x4, %r11d
movq -0x58(%rsp), %r12
cmpq 0x68(%rsp), %r12
je 0x3b7e0
cmpl %r10d, (%r12)
movq -0x40(%rsp), %r9
jne 0x3b7ce
movq -0x80(%rsp), %rcx
movb (%rcx,%r8), %al
cmpb (%rcx), %al
jne 0x3b7ce
cmpq 0x60(%rsp), %r14
jae 0x3b7ce
movq -0x80(%rsp), %r14
movl %r14d, %ebp
subl -0x68(%rsp), %ebp
movq -0x78(%rsp), %rax
leaq 0x1(%rax), %rcx
movq %rcx, (%rsp)
leaq (,%rax,8), %rcx
movq -0x18(%rsp), %r9
leaq (%r9,%rax,8), %rax
movq %rax, 0x8(%rsp)
leaq (,%rdx,8), %rax
subq %rax, %rcx
addq %r9, %rcx
movq %rcx, 0x10(%rsp)
addq $0x4, %r12
addq $0x14, %rsi
leaq 0x1(%r14), %rax
movq %rax, -0x8(%rsp)
leaq (%r14,%r8), %rax
incq %rax
movq %rax, -0x10(%rsp)
movl %ebp, %ecx
movq %rsi, %rax
movq %r12, %r9
movq %r12, %r14
xorl %r12d, %r12d
movq %r12, %r8
movq %rax, %rsi
movl $0x2, -0xc(%rax)
movl %ecx, -0x8(%rax)
movl %r15d, -0x4(%rax)
movq (%rsp), %rax
addq %r12, %rax
cmpq %rdx, %rax
movl $0x0, %eax
cmovbq -0x20(%rsp), %rax
movq 0x10(%rsp), %r12
leaq (%r12,%rax,8), %rax
movq (%rax,%r8,8), %rax
movq 0x8(%rsp), %r12
movq %rax, (%r12,%r8,8)
movq %r14, -0x58(%rsp)
cmpq 0x68(%rsp), %r14
je 0x3b793
cmpl %r10d, (%r9,%r8,4)
jne 0x3b793
movq -0x10(%rsp), %rax
movb (%rax,%r8), %al
movq -0x8(%rsp), %r14
cmpb (%r14,%r8), %al
jne 0x3b793
leaq -0xc(%rsi), %rbp
addq $0xc, %rbp
leaq 0x1(%r8), %r12
movq -0x58(%rsp), %rax
leaq 0x4(%rax), %r14
leaq 0xc(%rsi), %rax
cmpq 0x60(%rsp), %rbp
jb 0x3b70a
movq -0x78(%rsp), %rax
addq %r8, %rax
incq %rax
movq %rax, -0x78(%rsp)
movq -0x68(%rsp), %rax
leaq 0x1(%rax,%r8), %rax
movq %rax, -0x68(%rsp)
movq -0x60(%rsp), %r14
leaq 0x1(%r14,%r8), %r14
movq -0x80(%rsp), %rax
addq %r8, %rax
incq %rax
movq %rax, -0x80(%rsp)
jmp 0x3b7e8
movq %r14, %rsi
movq -0x60(%rsp), %r14
movq -0x70(%rsp), %rcx
jmp 0x3b625
movq %r14, %rsi
movq -0x60(%rsp), %r14
movq -0x70(%rsp), %rcx
movq -0x40(%rsp), %r9
jmp 0x3b625
movl $0x2, %r11d
movq %r9, %rsi
jmp 0x3b850
movl $0x0, (%r9)
addq $0x4, %r9
movq -0x78(%rsp), %rdx
movq $0x0, (%rcx,%rdx,8)
movq %r9, %rsi
jmp 0x3b840
cmpl $0x4, %r11d
jne 0x3b828
xorl %r11d, %r11d
movl 0x78(%rsp), %eax
movq 0x68(%rsp), %rdi
movq 0x60(%rsp), %r8
movq -0x58(%rsp), %rbx
testl %r11d, %r11d
jne 0x3b850
incq %r14
incq -0x78(%rsp)
incq -0x68(%rsp)
xorl %r11d, %r11d
testl %r11d, %r11d
jne 0x3b863
movq %rsi, %r9
cmpq %r8, %rsi
jb 0x3b43f
jmp 0x3b86d
cmpl $0x1, %r11d
jne 0x3b86d
xorl %esi, %esi
jmp 0x3b878
movq 0x80(%rsp), %rax
movl %r14d, (%rax)
movq %rsi, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop