// File: phillip-h[P]fractal-gen/lodepng.cpp
// Function: encodeLZ77(uivector*, Hash*, unsigned char const*, unsigned long, unsigned long, unsigned int, unsigned int, unsigned int, unsigned int)
static unsigned encodeLZ77(uivector* out, Hash* hash,
                           const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize,
                           unsigned minmatch, unsigned nicematch, unsigned lazymatching)
{
  size_t pos;
  unsigned i, error = 0;
  /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
  unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8;
  unsigned maxlazymatch = windowsize >= 8192 ? MAX_SUPPORTED_DEFLATE_LENGTH : 64;

  unsigned usezeros = 1; /*not sure if setting it to false for windowsize < 8192 is better or worse*/
  unsigned numzeros = 0;

  unsigned offset; /*the offset represents the distance in LZ77 terminology*/
  unsigned length;
  unsigned lazy = 0;
  unsigned lazylength = 0, lazyoffset = 0;
  unsigned hashval;
  unsigned current_offset, current_length;
  unsigned prev_offset;
  const unsigned char *lastptr, *foreptr, *backptr;
  unsigned hashpos;

  if(windowsize == 0 || windowsize > 32768) return 60; /*error: windowsize smaller/larger than allowed*/
  if((windowsize & (windowsize - 1)) != 0) return 90; /*error: must be power of two*/

  if(nicematch > MAX_SUPPORTED_DEFLATE_LENGTH) nicematch = MAX_SUPPORTED_DEFLATE_LENGTH;

  for(pos = inpos; pos < insize; ++pos)
  {
    size_t wpos = pos & (windowsize - 1); /*position for in 'circular' hash buffers*/
    unsigned chainlength = 0;

    hashval = getHash(in, insize, pos);

    if(usezeros && hashval == 0)
    {
      if(numzeros == 0) numzeros = countZeros(in, insize, pos);
      else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
    }
    else
    {
      numzeros = 0;
    }

    updateHashChain(hash, wpos, hashval, numzeros);

    /*the length and offset found for the current position*/
    length = 0;
    offset = 0;

    hashpos = hash->chain[wpos];

    lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];

    /*search for the longest string*/
    prev_offset = 0;
    for(;;)
    {
      if(chainlength++ >= maxchainlength) break;
      current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize;

      if(current_offset < prev_offset) break; /*stop when went completely around the circular buffer*/
      prev_offset = current_offset;
      if(current_offset > 0)
      {
        /*test the next characters*/
        foreptr = &in[pos];
        backptr = &in[pos - current_offset];

        /*common case in PNGs is lots of zeros. Quickly skip over them as a speedup*/
        if(numzeros >= 3)
        {
          unsigned skip = hash->zeros[hashpos];
          if(skip > numzeros) skip = numzeros;
          backptr += skip;
          foreptr += skip;
        }

        while(foreptr != lastptr && *backptr == *foreptr) /*maximum supported length by deflate is max length*/
        {
          ++backptr;
          ++foreptr;
        }
        current_length = (unsigned)(foreptr - &in[pos]);

        if(current_length > length)
        {
          length = current_length; /*the longest length*/
          offset = current_offset; /*the offset that is related to this longest length*/
          /*jump out once a length of max length is found (speed gain). This also jumps
          out if length is MAX_SUPPORTED_DEFLATE_LENGTH*/
          if(current_length >= nicematch) break;
        }
      }

      if(hashpos == hash->chain[hashpos]) break;

      if(numzeros >= 3 && length > numzeros)
      {
        hashpos = hash->chainz[hashpos];
        if(hash->zeros[hashpos] != numzeros) break;
      }
      else
      {
        hashpos = hash->chain[hashpos];
        /*outdated hash value, happens if particular value was not encountered in whole last window*/
        if(hash->val[hashpos] != (int)hashval) break;
      }
    }

    if(lazymatching)
    {
      if(!lazy && length >= 3 && length <= maxlazymatch && length < MAX_SUPPORTED_DEFLATE_LENGTH)
      {
        lazy = 1;
        lazylength = length;
        lazyoffset = offset;
        continue; /*try the next byte*/
      }
      if(lazy)
      {
        lazy = 0;
        if(pos == 0) ERROR_BREAK(81);
        if(length > lazylength + 1)
        {
          /*push the previous character as literal*/
          if(!uivector_push_back(out, in[pos - 1])) ERROR_BREAK(83 /*alloc fail*/);
        }
        else
        {
          length = lazylength;
          offset = lazyoffset;
          hash->head[hashval] = -1; /*the same hashchain update will be done, this ensures no wrong alteration*/
          hash->headz[numzeros] = -1; /*idem*/
          --pos;
        }
      }
    }
    if(length >= 3 && offset > windowsize) ERROR_BREAK(86 /*too big (or overflown negative) offset*/);

    /*encode it as length/distance pair or literal value*/
    if(length < 3) /*only lengths of 3 or higher are supported as length/distance pair*/
    {
      if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
    }
    else if(length < minmatch || (length == 3 && offset > 4096))
    {
      /*compensate for the fact that longer offsets have more extra bits, a
      length of only 3 may be not worth it then*/
      if(!uivector_push_back(out, in[pos])) ERROR_BREAK(83 /*alloc fail*/);
    }
    else
    {
      addLengthDistance(out, length, offset);
      for(i = 1; i < length; ++i)
      {
        ++pos;
        wpos = pos & (windowsize - 1);
        hashval = getHash(in, insize, pos);
        if(usezeros && hashval == 0)
        {
          if(numzeros == 0) numzeros = countZeros(in, insize, pos);
          else if(pos + numzeros > insize || in[pos + numzeros - 1] != 0) --numzeros;
        }
        else
        {
          numzeros = 0;
        }
        updateHashChain(hash, wpos, hashval, numzeros);
      }
    }
  } /*end of the loop through each character of input*/

  return error;
}

// Assembly:
subq $0xd8, %rsp
movl 0xf0(%rsp), %eax
movl 0xe8(%rsp), %eax
movl 0xe0(%rsp), %eax
movq %rdi, 0xc8(%rsp)
movq %rsi, 0xc0(%rsp)
movq %rdx, 0xb8(%rsp)
movq %rcx, 0xb0(%rsp)
movq %r8, 0xa8(%rsp)
movl %r9d, 0xa4(%rsp)
movl $0x0, 0x90(%rsp)
cmpl $0x2000, 0xa4(%rsp) # imm = 0x2000
jb 0x14c11
movl 0xa4(%rsp), %eax
movl %eax, 0x24(%rsp)
jmp 0x14c1f
movl 0xa4(%rsp), %eax
shrl $0x3, %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0xa4(%rsp), %edx
movl $0x40, %eax
movl $0x102, %ecx            # imm = 0x102
cmpl $0x2000, %edx           # imm = 0x2000
cmovaeq %rcx, %rax
movl %eax, 0x88(%rsp)
movl $0x1, 0x84(%rsp)
movl $0x0, 0x80(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
cmpl $0x0, 0xa4(%rsp)
je 0x14c91
cmpl $0x8000, 0xa4(%rsp) # imm = 0x8000
jbe 0x14ca1
movl $0x3c, 0xd4(%rsp)
jmp 0x1547e
movl 0xa4(%rsp), %eax
movl 0xa4(%rsp), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0x14cc9
movl $0x5a, 0xd4(%rsp)
jmp 0x1547e
movl 0xe8(%rsp), %eax
cmpq $0x102, %rax            # imm = 0x102
jbe 0x14ce3
movl $0x102, 0xe8(%rsp) # imm = 0x102
movq 0xb0(%rsp), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
cmpq 0xa8(%rsp), %rax
jae 0x15470
movq 0x98(%rsp), %rax
movl 0xa4(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x15710
movl %eax, 0x68(%rsp)
cmpl $0x0, 0x84(%rsp)
je 0x14def
cmpl $0x0, 0x68(%rsp)
jne 0x14def
cmpl $0x0, 0x80(%rsp)
jne 0x14d97
movq 0xb8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x15810
movl %eax, 0x80(%rsp)
jmp 0x14ded
movq 0x98(%rsp), %rax
movl 0x80(%rsp), %ecx
addq %rcx, %rax
cmpq 0xa8(%rsp), %rax
ja 0x14dda
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
movl 0x80(%rsp), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x14deb
movl 0x80(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x14ded
jmp 0x14dfa
movl $0x0, 0x80(%rsp)
movq 0xc0(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x68(%rsp), %edx
movl 0x80(%rsp), %eax
movzwl %ax, %ecx
callq 0x158c0
movl $0x0, 0x78(%rsp)
movl $0x0, 0x7c(%rsp)
movq 0xc0(%rsp), %rax
movq 0x8(%rax), %rax
movq 0x30(%rsp), %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x3c(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xa8(%rsp), %rax
movq 0x98(%rsp), %rcx
addq $0x102, %rcx            # imm = 0x102
cmpq %rcx, %rax
jae 0x14e7b
movq 0xa8(%rsp), %rax
movq %rax, 0x10(%rsp)
jmp 0x14e8e
movq 0x98(%rsp), %rax
addq $0x102, %rax            # imm = 0x102
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rax
movq 0x10(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x50(%rsp)
movl $0x0, 0x5c(%rsp)
movl 0x2c(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0x2c(%rsp)
cmpl 0x8c(%rsp), %eax
jb 0x14ec3
jmp 0x15107
movl 0x3c(%rsp), %eax
cmpq 0x30(%rsp), %rax
ja 0x14ee1
movq 0x30(%rsp), %rax
movl 0x3c(%rsp), %ecx
subq %rcx, %rax
movq %rax, 0x8(%rsp)
jmp 0x14efc
movq 0x30(%rsp), %rax
movl 0x3c(%rsp), %ecx
subq %rcx, %rax
movl 0xa4(%rsp), %ecx
addq %rcx, %rax
movq %rax, 0x8(%rsp)
movq 0x8(%rsp), %rax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jae 0x14f14
jmp 0x15107
movl 0x64(%rsp), %eax
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x64(%rsp)
jbe 0x1505c
movq 0xb8(%rsp), %rax
addq 0x98(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
movl 0x64(%rsp), %edx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
cmpl $0x3, 0x80(%rsp)
jb 0x14fbb
movq 0xc0(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x3c(%rsp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x80(%rsp), %eax
jbe 0x14f95
movl 0x80(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x28(%rsp), %ecx
movq 0x40(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x40(%rsp)
movl 0x28(%rsp), %ecx
movq 0x48(%rsp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x48(%rsp)
jmp 0x14fbd
movq 0x48(%rsp), %rcx
xorl %eax, %eax
cmpq 0x50(%rsp), %rcx
movb %al, 0x7(%rsp)
je 0x14fe8
movq 0x40(%rsp), %rax
movzbl (%rax), %eax
movq 0x48(%rsp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, 0x7(%rsp)
movb 0x7(%rsp), %al
testb $0x1, %al
jne 0x14ff2
jmp 0x15010
movq 0x40(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x40(%rsp)
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x14fbd
movq 0x48(%rsp), %rax
movq 0xb8(%rsp), %rcx
addq 0x98(%rsp), %rcx
subq %rcx, %rax
movl %eax, 0x60(%rsp)
movl 0x60(%rsp), %eax
cmpl 0x78(%rsp), %eax
jbe 0x1505a
movl 0x60(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x60(%rsp), %eax
cmpl 0xe8(%rsp), %eax
jb 0x15058
jmp 0x15107
jmp 0x1505a
jmp 0x1505c
movl 0x3c(%rsp), %eax
movq 0xc0(%rsp), %rcx
movq 0x8(%rcx), %rcx
movl 0x3c(%rsp), %edx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jne 0x1507d
jmp 0x15107
cmpl $0x3, 0x80(%rsp)
jb 0x150cd
movl 0x78(%rsp), %eax
cmpl 0x80(%rsp), %eax
jbe 0x150cd
movq 0xc0(%rsp), %rax
movq 0x20(%rax), %rax
movl 0x3c(%rsp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x3c(%rsp)
movq 0xc0(%rsp), %rax
movq 0x28(%rax), %rax
movl 0x3c(%rsp), %ecx
movzwl (%rax,%rcx,2), %eax
cmpl 0x80(%rsp), %eax
je 0x150cb
jmp 0x15107
jmp 0x15102
movq 0xc0(%rsp), %rax
movq 0x8(%rax), %rax
movl 0x3c(%rsp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, 0x3c(%rsp)
movq 0xc0(%rsp), %rax
movq 0x10(%rax), %rax
movl 0x3c(%rsp), %ecx
movl (%rax,%rcx,4), %eax
cmpl 0x68(%rsp), %eax
je 0x15100
jmp 0x15107
jmp 0x15102
jmp 0x14ea8
cmpl $0x0, 0xf0(%rsp)
je 0x1522a
cmpl $0x0, 0x74(%rsp)
jne 0x15159
cmpl $0x3, 0x78(%rsp)
jb 0x15159
movl 0x78(%rsp), %eax
cmpl 0x88(%rsp), %eax
ja 0x15159
movl 0x78(%rsp), %eax
cmpq $0x102, %rax            # imm = 0x102
jae 0x15159
movl $0x1, 0x74(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x6c(%rsp)
jmp 0x15457
cmpl $0x0, 0x74(%rsp)
je 0x15228
movl $0x0, 0x74(%rsp)
cmpq $0x0, 0x98(%rsp)
jne 0x15187
movl $0x51, 0x90(%rsp)
jmp 0x15470
movl 0x78(%rsp), %eax
movl 0x70(%rsp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jbe 0x151d2
movq 0xc8(%rsp), %rdi
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %esi
callq 0x159b0
cmpl $0x0, %eax
jne 0x151d0
movl $0x53, 0x90(%rsp)
jmp 0x15470
jmp 0x15226
movl 0x70(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x7c(%rsp)
movq 0xc0(%rsp), %rax
movq (%rax), %rax
movl 0x68(%rsp), %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq 0xc0(%rsp), %rax
movq 0x18(%rax), %rax
movl 0x80(%rsp), %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq 0x98(%rsp), %rax
addq $-0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x15228
jmp 0x1522a
cmpl $0x3, 0x78(%rsp)
jb 0x1524e
movl 0x7c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jbe 0x1524e
movl $0x56, 0x90(%rsp)
jmp 0x15470
cmpl $0x3, 0x78(%rsp)
jae 0x15290
movq 0xc8(%rsp), %rdi
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
movzbl (%rax,%rcx), %esi
callq 0x159b0
cmpl $0x0, %eax
jne 0x1528b
movl $0x53, 0x90(%rsp)
jmp 0x15470
jmp 0x15455
movl 0x78(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jb 0x152ae
cmpl $0x3, 0x78(%rsp)
jne 0x152e9
cmpl $0x1000, 0x7c(%rsp)     # imm = 0x1000
jbe 0x152e9
movq 0xc8(%rsp), %rdi
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
movzbl (%rax,%rcx), %esi
callq 0x159b0
cmpl $0x0, %eax
jne 0x152e4
movl $0x53, 0x90(%rsp)
jmp 0x15470
jmp 0x15453
movq 0xc8(%rsp), %rdi
movl 0x78(%rsp), %eax
movl %eax, %esi
movl 0x7c(%rsp), %eax
movl %eax, %edx
callq 0x15a10
movl $0x1, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0x78(%rsp), %eax
jae 0x15451
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movl 0xa4(%rsp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movq %rax, 0x30(%rsp)
movq 0xb8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x15710
movl %eax, 0x68(%rsp)
cmpl $0x0, 0x84(%rsp)
je 0x15410
cmpl $0x0, 0x68(%rsp)
jne 0x15410
cmpl $0x0, 0x80(%rsp)
jne 0x153b8
movq 0xb8(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x15810
movl %eax, 0x80(%rsp)
jmp 0x1540e
movq 0x98(%rsp), %rax
movl 0x80(%rsp), %ecx
addq %rcx, %rax
cmpq 0xa8(%rsp), %rax
ja 0x153fb
movq 0xb8(%rsp), %rax
movq 0x98(%rsp), %rcx
movl 0x80(%rsp), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x1540c
movl 0x80(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x1540e
jmp 0x1541b
movl $0x0, 0x80(%rsp)
movq 0xc0(%rsp), %rdi
movq 0x30(%rsp), %rsi
movl 0x68(%rsp), %edx
movl 0x80(%rsp), %eax
movzwl %ax, %ecx
callq 0x158c0
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x1530d
jmp 0x15453
jmp 0x15455
jmp 0x15457
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
jmp 0x14cf3
movl 0x90(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
addq $0xd8, %rsp
retq
nopl (%rax)