// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %r9d, %r14d
shrl $0x3, %r14d
cmpl $0x2000, %r9d           # imm = 0x2000
movq %rcx, %r12
movq %rsi, 0x20(%rsp)
movq %rdi, 0x30(%rsp)
movl $0x102, %eax            # imm = 0x102
cmovael %r9d, %r14d
pushq $0x40
popq %rcx
cmovael %eax, %ecx
leal -0x8001(%r9), %eax
movl %ecx, 0x3c(%rsp)
cmpl $0xffff8000, %eax       # imm = 0xFFFF8000
jae 0xb5da
pushq $0x3c
jmp 0xba2e
blsrl %r9d, %eax
jne 0xba2c
movl 0xa8(%rsp), %eax
leal -0x1(%r9), %ecx
movq %rdx, %r13
movq %r8, 0x10(%rsp)
movq %r9, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movl $0x0, 0x2c(%rsp)
movq %rdx, 0x8(%rsp)
movl %r14d, 0x40(%rsp)
movq %rcx, 0x58(%rsp)
movl $0x102, %ecx            # imm = 0x102
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %r9d, %eax
movq %rax, 0x60(%rsp)
xorl %r15d, %r15d
xorl %eax, %eax
movl %ecx, 0x44(%rsp)
movq 0x10(%rsp), %rbx
cmpq %rbx, %r12
jae 0xba3e
movl %eax, 0x18(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0xbb91
movl $0x0, %ebp
movl %eax, %edx
movl $0x0, (%rsp)
testl %eax, %eax
jne 0xb6aa
testl %r15d, %r15d
je 0xb691
movl %r15d, %eax
addq %r12, %rax
cmpq 0x10(%rsp), %rax
ja 0xb684
cmpb $0x0, -0x1(%r13,%rax)
je 0xb687
decl %r15d
movq 0x10(%rsp), %rbx
movl %r15d, %ebp
jmp 0xb6aa
movq 0x10(%rsp), %rbx
movl %edx, %ebp
movq %r13, %rdi
movq %r12, %rdx
movq %rbx, %rsi
callq 0xbbdd
movl %ebp, %edx
movl %eax, %ebp
movl %r12d, %r15d
andl 0x58(%rsp), %r15d
movq 0x20(%rsp), %r14
movzwl %bp, %ecx
movl %edx, 0x4(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xbc0e
movq 0x8(%r14), %rax
movl 0x40(%rsp), %r14d
leaq 0x102(%r12), %rcx
leaq (%r13,%r12), %rdx
cmpq %rbx, %rcx
cmovaeq %rbx, %rcx
xorl %r10d, %r10d
xorl %esi, %esi
addq %r13, %rcx
xorl %r13d, %r13d
movzwl (%rax,%r15,2), %edi
cmpl %r14d, %esi
je 0xb7ee
movzwl %di, %r9d
movq %r15, %r8
movl $0x0, %r11d
subq %r9, %r8
cmovbq 0x60(%rsp), %r11
addq %r11, %r8
cmpl %r8d, %r10d
ja 0xb7ee
testq %r8, %r8
je 0xb787
movl %r8d, %r10d
movq %r12, %r11
subq %r10, %r11
addq 0x8(%rsp), %r11
cmpl $0x3, %ebp
jb 0xb756
movq 0x20(%rsp), %r10
movq 0x28(%r10), %r10
movzwl (%r10,%r9,2), %r10d
cmpl %r10d, %ebp
cmovbl %ebp, %r10d
addq %r10, %r11
addq %rdx, %r10
jmp 0xb759
movq %rdx, %r10
cmpq %rcx, %r10
je 0xb76e
movb (%r11), %bl
cmpb (%r10), %bl
jne 0xb771
incq %r11
incq %r10
jmp 0xb759
movq %rcx, %r10
subl %edx, %r10d
cmpl %r10d, %r13d
jae 0xb787
movl %r8d, (%rsp)
movl %r10d, %r13d
cmpl %r10d, 0x44(%rsp)
jbe 0xb7e7
movzwl (%rax,%r9,2), %r11d
cmpw %r11w, %di
je 0xb7ee
incl %esi
cmpl $0x3, %ebp
jb 0xb7c3
cmpl %ebp, %r13d
jbe 0xb7c3
movq 0x20(%rsp), %r10
movq 0x20(%r10), %rdi
movq 0x28(%r10), %r10
movzwl (%rdi,%r9,2), %edi
movzwl (%r10,%rdi,2), %r9d
movl %r8d, %r10d
cmpl %r9d, %ebp
je 0xb6f6
jmp 0xb7ee
movq 0x20(%rsp), %rdi
movl %r8d, %r10d
movl 0x4(%rsp), %r8d
movzwl %r11w, %r9d
movq 0x10(%rdi), %rbx
movl %r11d, %edi
cmpl %r8d, (%rbx,%r9,4)
je 0xb6f6
jmp 0xb7ee
movl %r8d, (%rsp)
movl %r10d, %r13d
cmpl $0x0, 0xb0(%rsp)
je 0xb848
movl 0x18(%rsp), %eax
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %rsi
testl %eax, %eax
jne 0xb853
cmpl $0x3, %r13d
jb 0xb853
cmpl 0x3c(%rsp), %r13d
setbe %al
cmpl $0x102, %r13d           # imm = 0x102
setb %cl
xorl %edi, %edi
testb %cl, %al
je 0xb8b1
pushq $0x1
popq %rcx
movl (%rsp), %edx
movl %r13d, %eax
movq 0x8(%rsp), %r13
movq %rax, 0x48(%rsp)
movl %edx, 0x2c(%rsp)
jmp 0xb904
movl 0x18(%rsp), %edi
movq 0x50(%rsp), %rbx
jmp 0xb8b1
xorl %edi, %edi
testl %eax, %eax
je 0xb8b1
testq %r12, %r12
je 0xba4b
leal 0x1(%rsi), %eax
cmpl %eax, %r13d
jbe 0xb88a
movq 0x8(%rsp), %rax
movq 0x30(%rsp), %rdi
movzbl -0x1(%rax,%r12), %esi
callq 0xbc53
xorl %edi, %edi
testl %eax, %eax
jne 0xb8b1
jmp 0xba46
movq 0x20(%rsp), %rdx
movl 0x4(%rsp), %ecx
decq %r12
movl %esi, %r13d
movq (%rdx), %rax
orl $-0x1, (%rax,%rcx,4)
movl %ebp, %ecx
movq 0x18(%rdx), %rax
orl $-0x1, (%rax,%rcx,4)
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
cmpl $0x3, %r13d
jb 0xb8c0
cmpl %ebx, (%rsp)
ja 0xba42
movl %edi, 0x4(%rsp)
cmpl $0x2, %r13d
jbe 0xb8e3
cmpl 0xa0(%rsp), %r13d
jb 0xb8e3
cmpl $0x3, %r13d
jne 0xb911
cmpl $0x1001, (%rsp)         # imm = 0x1001
jb 0xb911
movq 0x8(%rsp), %r13
movq 0x30(%rsp), %rdi
movzbl (%r13,%r12), %esi
callq 0xbc53
movl 0x4(%rsp), %ecx
testl %eax, %eax
je 0xba46
incq %r12
movl %ebp, %r15d
movl %ecx, %eax
jmp 0xb638
movl (%rsp), %eax
leaq 0x2fc5(%rip), %rbx      # 0xe8e0
movl %r13d, %edx
movq %rbx, %rdi
movq %rax, 0x18(%rsp)
pushq $0x1d
popq %rsi
callq 0xbc86
movq %rax, %r15
movl %r15d, %eax
movl %r13d, %r14d
subl (%rbx,%rax,4), %r14d
leaq 0x309e(%rip), %rbx      # 0xe9e0
movq %rbx, %rdi
pushq $0x1e
popq %rsi
movq 0x18(%rsp), %rdx
callq 0xbc86
movl (%rsp), %ecx
addl $0x101, %r15d           # imm = 0x101
movl %eax, %edx
movq %rax, 0x18(%rsp)
subl (%rbx,%rdx,4), %ecx
movq 0x30(%rsp), %rbx
movl %r15d, %esi
movq 0x10(%rsp), %r15
movq %rbx, %rdi
movl %ecx, (%rsp)
callq 0xbc53
movq %rbx, %rdi
movl %r14d, %esi
callq 0xbc53
movq 0x18(%rsp), %rsi
movq %rbx, %rdi
callq 0xbc53
movl (%rsp), %esi
movq %rbx, %rdi
callq 0xbc53
decl %r13d
incq %r12
subl $0x1, %r13d
jb 0xba1b
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xbb91
movl %eax, %ebx
xorl %r14d, %r14d
testl %eax, %eax
jne 0xb9fc
testl %ebp, %ebp
je 0xb9e9
movl %ebp, %eax
leaq (%r12,%rax), %rcx
cmpq %r15, %rcx
ja 0xb9e2
addq 0x8(%rsp), %rax
cmpb $0x0, -0x1(%r12,%rax)
je 0xb9e4
decl %ebp
movl %ebp, %r14d
jmp 0xb9fc
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xbbdd
movl %eax, %r14d
movl %r12d, %esi
andl 0x58(%rsp), %esi
movq 0x20(%rsp), %rdi
movzwl %r14w, %ecx
movl %ebx, %edx
callq 0xbc0e
incq %r12
movl %r14d, %ebp
jmp 0xb9a7
movq 0x8(%rsp), %r13
movl 0x4(%rsp), %ecx
decq %r12
jmp 0xb904
pushq $0x5a
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0xba2f
pushq $0x56
jmp 0xba48
pushq $0x53
popq %rax
jmp 0xba2f
pushq $0x51
jmp 0xba48