// File: /workspace/github_c_makefile_star2_O2/Joy-Division[P]tools-mgs/lodepng/lodepng.c
// Function: encodeLZ77
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
movq %rcx, %r12
movq %rsi, 0x20(%rsp)
movq %rdi, 0x30(%rsp)
movl %r9d, %r14d
shrl $0x3, %r14d
cmpl $0x2000, %r9d           # imm = 0x2000
cmovael %r9d, %r14d
movl $0x102, %eax            # imm = 0x102
pushq $0x40
popq %rcx
cmovael %eax, %ecx
movl %ecx, 0x3c(%rsp)
leal -0x8001(%r9), %eax
cmpl $0xffff8000, %eax       # imm = 0xFFFF8000
jae 0x85e5
pushq $0x3c
jmp 0x8a36
leal -0x1(%r9), %eax
movq %rax, 0x58(%rsp)
testl %eax, %r9d
jne 0x8a34
movq %rdx, %r13
movq %r8, 0x10(%rsp)
movl 0xa8(%rsp), %eax
movl $0x102, %ecx            # imm = 0x102
cmpl %ecx, %eax
cmovbl %eax, %ecx
movl %ecx, 0x44(%rsp)
movq %r9, 0x50(%rsp)
movl %r9d, %eax
movq %rax, 0x60(%rsp)
xorl %r15d, %r15d
xorl %eax, %eax
movq $0x0, 0x48(%rsp)
movl $0x0, 0x2c(%rsp)
movq %rdx, 0x8(%rsp)
movl %r14d, 0x40(%rsp)
movq 0x10(%rsp), %rbx
cmpq %rbx, %r12
jae 0x8a46
movl %eax, 0x18(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x8b99
movl %eax, %edx
movl $0x0, (%rsp)
movl $0x0, %ebp
testl %eax, %eax
jne 0x86b3
testl %r15d, %r15d
je 0x869a
movl %r15d, %eax
addq %r12, %rax
cmpq 0x10(%rsp), %rax
ja 0x868d
cmpb $0x0, -0x1(%r13,%rax)
je 0x8690
decl %r15d
movl %r15d, %ebp
movq 0x10(%rsp), %rbx
jmp 0x86b3
movq %r13, %rdi
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
movl %edx, %ebp
movq %r12, %rdx
callq 0x8be3
movl %ebp, %edx
movl %eax, %ebp
movl %r12d, %r15d
andl 0x58(%rsp), %r15d
movzwl %bp, %ecx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movl %edx, 0x4(%rsp)
callq 0x8c16
movq 0x8(%r14), %rax
movzwl (%rax,%r15,2), %edi
leaq 0x102(%r12), %rcx
cmpq %rbx, %rcx
cmovaeq %rbx, %rcx
addq %r13, %rcx
leaq (%r12,%r13), %rdx
xorl %r13d, %r13d
xorl %r10d, %r10d
xorl %esi, %esi
movl 0x40(%rsp), %r14d
cmpl %r14d, %esi
je 0x87f6
movzwl %di, %r9d
movq %r15, %r8
subq %r9, %r8
movl $0x0, %r11d
cmovbq 0x60(%rsp), %r11
addq %r11, %r8
cmpl %r8d, %r10d
ja 0x87f6
testq %r8, %r8
je 0x878f
movl %r8d, %r10d
movq %r12, %r11
subq %r10, %r11
addq 0x8(%rsp), %r11
cmpl $0x3, %ebp
jb 0x875e
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 0x8761
movq %rdx, %r10
cmpq %rcx, %r10
je 0x8776
movb (%r11), %bl
cmpb (%r10), %bl
jne 0x8779
incq %r11
incq %r10
jmp 0x8761
movq %rcx, %r10
subl %edx, %r10d
cmpl %r10d, %r13d
jae 0x878f
movl %r8d, (%rsp)
movl %r10d, %r13d
cmpl %r10d, 0x44(%rsp)
jbe 0x87ef
movzwl (%rax,%r9,2), %r11d
cmpw %r11w, %di
je 0x87f6
incl %esi
cmpl $0x3, %ebp
jb 0x87cb
cmpl %ebp, %r13d
jbe 0x87cb
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 0x86fe
jmp 0x87f6
movzwl %r11w, %r9d
movq 0x20(%rsp), %rdi
movq 0x10(%rdi), %rbx
movl %r8d, %r10d
movl %r11d, %edi
movl 0x4(%rsp), %r8d
cmpl %r8d, (%rbx,%r9,4)
je 0x86fe
jmp 0x87f6
movl %r8d, (%rsp)
movl %r10d, %r13d
cmpl $0x0, 0xb0(%rsp)
je 0x8850
movl 0x18(%rsp), %eax
testl %eax, %eax
movq 0x50(%rsp), %rbx
movq 0x48(%rsp), %rsi
jne 0x885b
cmpl $0x3, %r13d
jb 0x885b
cmpl 0x3c(%rsp), %r13d
setbe %al
cmpl $0x102, %r13d           # imm = 0x102
setb %cl
xorl %edi, %edi
testb %cl, %al
je 0x88b9
pushq $0x1
popq %rcx
movl %r13d, %eax
movq %rax, 0x48(%rsp)
movl (%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x8(%rsp), %r13
jmp 0x890c
movl 0x18(%rsp), %edi
movq 0x50(%rsp), %rbx
jmp 0x88b9
xorl %edi, %edi
testl %eax, %eax
je 0x88b9
testq %r12, %r12
je 0x8a53
leal 0x1(%rsi), %eax
cmpl %eax, %r13d
jbe 0x8892
movq 0x8(%rsp), %rax
movzbl -0x1(%rax,%r12), %esi
movq 0x30(%rsp), %rdi
callq 0x8c5b
xorl %edi, %edi
testl %eax, %eax
jne 0x88b9
jmp 0x8a4e
movq 0x20(%rsp), %rdx
movq (%rdx), %rax
movl 0x4(%rsp), %ecx
orl $-0x1, (%rax,%rcx,4)
movq 0x18(%rdx), %rax
movl %ebp, %ecx
orl $-0x1, (%rax,%rcx,4)
decq %r12
movl 0x2c(%rsp), %eax
movl %eax, (%rsp)
movl %esi, %r13d
cmpl $0x3, %r13d
jb 0x88c8
cmpl %ebx, (%rsp)
ja 0x8a4a
movl %edi, 0x4(%rsp)
cmpl $0x2, %r13d
jbe 0x88eb
cmpl 0xa0(%rsp), %r13d
jb 0x88eb
cmpl $0x3, %r13d
jne 0x8919
cmpl $0x1001, (%rsp)         # imm = 0x1001
jb 0x8919
movq 0x8(%rsp), %r13
movzbl (%r13,%r12), %esi
movq 0x30(%rsp), %rdi
callq 0x8c5b
testl %eax, %eax
movl 0x4(%rsp), %ecx
je 0x8a4e
incq %r12
movl %ebp, %r15d
movl %ecx, %eax
jmp 0x8641
movl %r13d, %edx
movl (%rsp), %eax
movq %rax, 0x18(%rsp)
leaq 0x2c95(%rip), %rbx      # 0xb5c0
movq %rbx, %rdi
pushq $0x1d
popq %rsi
callq 0x8c8e
movq %rax, %r15
movl %r15d, %eax
movl %r13d, %r14d
subl (%rbx,%rax,4), %r14d
leaq 0x2d76(%rip), %rbx      # 0xb6c0
movq %rbx, %rdi
pushq $0x1e
popq %rsi
movq 0x18(%rsp), %rdx
callq 0x8c8e
movq %rax, 0x18(%rsp)
movl %eax, %edx
movl (%rsp), %ecx
subl (%rbx,%rdx,4), %ecx
movl %ecx, (%rsp)
addl $0x101, %r15d           # imm = 0x101
movq 0x30(%rsp), %rbx
movq %rbx, %rdi
movl %r15d, %esi
movq 0x10(%rsp), %r15
callq 0x8c5b
movq %rbx, %rdi
movl %r14d, %esi
callq 0x8c5b
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
callq 0x8c5b
movq %rbx, %rdi
movl (%rsp), %esi
callq 0x8c5b
decl %r13d
incq %r12
subl $0x1, %r13d
jb 0x8a23
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x8b99
movl %eax, %ebx
xorl %r14d, %r14d
testl %eax, %eax
jne 0x8a04
testl %ebp, %ebp
je 0x89f1
movl %ebp, %eax
leaq (%r12,%rax), %rcx
cmpq %r15, %rcx
ja 0x89ea
addq 0x8(%rsp), %rax
cmpb $0x0, -0x1(%r12,%rax)
je 0x89ec
decl %ebp
movl %ebp, %r14d
jmp 0x8a04
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x8be3
movl %eax, %r14d
movl %r12d, %esi
andl 0x58(%rsp), %esi
movzwl %r14w, %ecx
movq 0x20(%rsp), %rdi
movl %ebx, %edx
callq 0x8c16
incq %r12
movl %r14d, %ebp
jmp 0x89af
decq %r12
movq 0x8(%rsp), %r13
movl 0x4(%rsp), %ecx
jmp 0x890c
pushq $0x5a
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
jmp 0x8a37
pushq $0x56
jmp 0x8a50
pushq $0x53
popq %rax
jmp 0x8a37
pushq $0x51
jmp 0x8a50