// File: /workspace/github_c_makefile_star2_O3/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 $0x88, %rsp
movq %rcx, %rbx
movq %rdx, 0x8(%rsp)
movl %r9d, %eax
shrl $0x3, %eax
cmpl $0x2000, %r9d           # imm = 0x2000
cmovael %r9d, %eax
movl %eax, 0x1c(%rsp)
movl $0x102, %eax            # imm = 0x102
movl $0x40, %edx
cmovael %eax, %edx
movq %rdi, 0x10(%rsp)
leal -0x8001(%r9), %ecx
movl $0x3c, %eax
cmpl $0xffff8000, %ecx       # imm = 0xFFFF8000
jb 0xb38e
leal -0x1(%r9), %ecx
movl $0x5a, %eax
testl %ecx, %r9d
jne 0xb38e
movq %r8, %r14
movq %rsi, %r12
movl 0xc8(%rsp), %eax
movl $0x102, %esi            # imm = 0x102
cmpl %esi, %eax
cmovbl %eax, %esi
movl %esi, 0x3c(%rsp)
cmpq %r8, %rbx
jae 0xb38c
movl %edx, 0x38(%rsp)
movl %ecx, %eax
movq %rax, 0x58(%rsp)
movq 0x8(%rsp), %rax
addq %r14, %rax
movq %rax, 0x48(%rsp)
movq %r9, 0x60(%rsp)
movl %r9d, %eax
movq %rax, 0x80(%rsp)
movq $0x0, 0x40(%rsp)
movq $0x0, 0x50(%rsp)
xorl %r15d, %r15d
xorl %ebp, %ebp
movq %r14, 0x30(%rsp)
movl %r15d, 0x18(%rsp)
movq 0x8(%rsp), %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0xb51c
movl $0x0, %r13d
testl %eax, %eax
jne 0xae60
testl %ebp, %ebp
je 0xae21
movl %ebp, %edx
addq %rbx, %rdx
cmpq %r14, %rdx
ja 0xae1a
movq 0x8(%rsp), %rcx
cmpb $0x0, -0x1(%rcx,%rdx)
je 0xae1c
decl %ebp
movl %ebp, %r13d
jmp 0xae60
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbx), %rsi
addq $0x102, %rsi            # imm = 0x102
movq 0x48(%rsp), %rdx
cmpq %rdx, %rsi
cmovaq %rdx, %rsi
addq %rbx, %rcx
movq %rcx, %r13
cmpq %rsi, %rcx
je 0xae5d
movq %rcx, %r13
cmpb $0x0, (%r13)
jne 0xae5d
incq %r13
cmpq %rsi, %r13
jne 0xae4b
movq %rsi, %r13
subl %ecx, %r13d
movl %ebx, %edi
andl 0x58(%rsp), %edi
movq 0x10(%r12), %rcx
movq %rcx, 0x28(%rsp)
movl %eax, (%rcx,%rdi,4)
movq (%r12), %rsi
movl %eax, %r8d
movl (%rsi,%r8,4), %edx
cmpl $-0x1, %edx
je 0xae8c
movq 0x8(%r12), %rcx
movw %dx, (%rcx,%rdi,2)
movq %rsi, 0x78(%rsp)
movl %edi, (%rsi,%r8,4)
movq 0x28(%r12), %rcx
movq %rcx, 0x20(%rsp)
movw %r13w, (%rcx,%rdi,2)
movq 0x18(%r12), %r10
movzwl %r13w, %ecx
movl (%r10,%rcx,4), %edx
cmpl $-0x1, %edx
je 0xaebf
movq 0x20(%r12), %rsi
movw %dx, (%rsi,%rdi,2)
movq %r8, 0x70(%rsp)
leaq 0x102(%rbx), %r9
cmpq %r14, %r9
cmovaeq %r14, %r9
movq %r10, 0x68(%rsp)
movl %edi, (%r10,%rcx,4)
cmpl $0x0, 0x1c(%rsp)
je 0xaff5
movq 0x8(%rsp), %rcx
addq %rcx, %r9
movq 0x8(%r12), %r11
movzwl (%r11,%rdi,2), %ebp
leaq (%rcx,%rbx), %r8
xorl %esi, %esi
movl $0x1, %edx
xorl %r14d, %r14d
movl $0x0, 0x4(%rsp)
movl %esi, %ecx
movzwl %bp, %r15d
movq %rdi, %rsi
subq %r15, %rsi
movl $0x0, %r10d
cmovbq 0x80(%rsp), %r10
addq %r10, %rsi
cmpl %esi, %ecx
ja 0xb000
testq %rsi, %rsi
je 0xafa6
movl %esi, %ecx
movq %rbx, %rbp
subq %rcx, %rbp
addq 0x8(%rsp), %rbp
cmpl $0x3, %r13d
jb 0xaf65
movq 0x20(%rsp), %rcx
movzwl (%rcx,%r15,2), %ecx
cmpl %ecx, %r13d
cmovbl %r13d, %ecx
addq %rcx, %rbp
addq %r8, %rcx
jmp 0xaf68
movq %r8, %rcx
movq %r9, %r10
cmpq %r9, %rcx
je 0xaf8c
movb (%rbp), %r10b
cmpb (%rcx), %r10b
jne 0xaf89
incq %rbp
incq %rcx
cmpq %r9, %rcx
jne 0xaf70
movq %r9, %r10
jmp 0xaf8c
movq %rcx, %r10
subl %r8d, %r10d
cmpl %r10d, %r14d
jae 0xafa6
movl %esi, 0x4(%rsp)
movl %r10d, %r14d
cmpl %r10d, 0x3c(%rsp)
jbe 0xb380
movzwl (%r11,%r15,2), %ebp
cmpw %bp, %r15w
je 0xb000
cmpl $0x3, %r13d
jb 0xafd6
cmpl %r13d, %r14d
jbe 0xafd6
movq 0x20(%r12), %rcx
movzwl (%rcx,%r15,2), %ebp
movq 0x20(%rsp), %rcx
movzwl (%rcx,%rbp,2), %ecx
cmpl %ecx, %r13d
je 0xafe4
jmp 0xb000
movzwl %bp, %ecx
movq 0x28(%rsp), %r10
cmpl %eax, (%r10,%rcx,4)
jne 0xb000
leal 0x1(%rdx), %ecx
cmpl 0x1c(%rsp), %edx
movl %ecx, %edx
jne 0xaf0e
jmp 0xb000
movl $0x0, 0x4(%rsp)
xorl %r14d, %r14d
cmpl $0x0, 0xd0(%rsp)
je 0xb05d
movl 0x18(%rsp), %eax
testl %eax, %eax
leaq 0x3689(%rip), %rdx      # 0xe6a0
movq 0x50(%rsp), %rsi
jne 0xb06e
cmpl $0x3, %r14d
jb 0xb06e
cmpl 0x38(%rsp), %r14d
setbe %al
cmpl $0x102, %r14d           # imm = 0x102
setb %cl
xorl %r15d, %r15d
testb %cl, %al
je 0xb0ef
movl $0x1, %r15d
movl %r14d, %eax
movq %rax, 0x50(%rsp)
movl 0x4(%rsp), %eax
movq %rax, 0x40(%rsp)
jmp 0xb155
movl 0x18(%rsp), %r15d
leaq 0x3637(%rip), %rdx      # 0xe6a0
jmp 0xb0ef
xorl %r15d, %r15d
testl %eax, %eax
je 0xb0ef
testq %rbx, %rbx
je 0xb3ae
leal 0x1(%rsi), %eax
cmpl %eax, %r14d
jbe 0xb0c3
movq 0x8(%rsp), %rax
movzbl -0x1(%rax,%rbx), %ebp
movq 0x10(%rsp), %r15
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0xacb8
testl %eax, %eax
je 0xb3a7
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
leaq 0x35e2(%rip), %rdx      # 0xe6a0
xorl %r15d, %r15d
jmp 0xb0ef
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x78(%rsp), %rax
movq 0x70(%rsp), %rdi
movl %ecx, (%rax,%rdi,4)
movl %r13d, %eax
movq 0x68(%rsp), %rdi
movl %ecx, (%rdi,%rax,4)
decq %rbx
movq 0x40(%rsp), %rax
movl %eax, 0x4(%rsp)
movl %esi, %r14d
cmpl $0x3, %r14d
movl 0x4(%rsp), %esi
jb 0xb103
cmpl 0x60(%rsp), %esi
ja 0xb3a0
cmpl $0x2, %r14d
jbe 0xb121
cmpl 0xc0(%rsp), %r14d
jb 0xb121
cmpl $0x3, %r14d
jne 0xb16e
cmpl $0x1000, %esi           # imm = 0x1000
jbe 0xb16e
movq 0x8(%rsp), %rax
movb (%rax,%rbx), %bpl
movq 0x10(%rsp), %r14
movq 0x8(%r14), %rsi
incq %rsi
movq %r14, %rdi
callq 0xacb8
testl %eax, %eax
je 0xb3a7
movzbl %bpl, %eax
movq (%r14), %rcx
movq 0x8(%r14), %rdx
movl %eax, -0x4(%rcx,%rdx,4)
incq %rbx
movl %r13d, %ebp
movq 0x30(%rsp), %r14
cmpq %r14, %rbx
jb 0xade1
jmp 0xb38c
movl %r15d, 0x20(%rsp)
movl $0x1c, %eax
movl $0x1, %r15d
leaq (%rax,%r15), %rcx
shrq %rcx
cmpl %r14d, (%rdx,%rcx,4)
jb 0xb193
decq %rcx
movq %rcx, %rax
jmp 0xb199
incq %rcx
movq %rcx, %r15
cmpq %rax, %r15
jbe 0xb17e
cmpq $0x1c, %r15
ja 0xb1aa
cmpl %r14d, (%rdx,%r15,4)
jbe 0xb1ad
decq %r15
movl %r15d, %eax
movl (%rdx,%rax,4), %eax
movl %eax, 0x28(%rsp)
movl $0x1d, %eax
movl $0x1, %ebp
leaq 0x35d8(%rip), %rdx      # 0xe7a0
leaq (%rax,%rbp), %rcx
shrq %rcx
cmpl %esi, (%rdx,%rcx,4)
jb 0xb1dc
decq %rcx
movq %rcx, %rax
jmp 0xb1e2
incq %rcx
movq %rcx, %rbp
cmpq %rax, %rbp
jbe 0xb1c8
cmpq $0x1d, %rbp
ja 0xb1f2
cmpl %esi, (%rdx,%rbp,4)
jbe 0xb1f5
decq %rbp
movl %ebp, %eax
subl (%rdx,%rax,4), %esi
movl %esi, 0x4(%rsp)
movq 0x10(%rsp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0xacb8
movq 0x10(%rsp), %rdx
movq %rdx, %rcx
movq 0x8(%rdx), %rsi
testl %eax, %eax
je 0xb22e
addl $0x101, %r15d           # imm = 0x101
movq (%rcx), %rax
movl %r15d, -0x4(%rax,%rsi,4)
incq %rsi
movq %rcx, %r15
movq %rcx, %rdi
callq 0xacb8
movq 0x8(%r15), %rsi
testl %eax, %eax
je 0xb252
movl %r14d, %eax
subl 0x28(%rsp), %eax
movq (%r15), %rcx
movl %eax, -0x4(%rcx,%rsi,4)
incq %rsi
movq %r15, %rdi
callq 0xacb8
movq 0x8(%r15), %rsi
testl %eax, %eax
je 0xb26c
movq (%r15), %rax
movl %ebp, -0x4(%rax,%rsi,4)
incq %rsi
movq %r15, %rdi
callq 0xacb8
testl %eax, %eax
je 0xb28a
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl 0x4(%rsp), %edx
movl %edx, -0x4(%rax,%rcx,4)
movl $0x1, %r15d
leaq 0x1(%rbx), %rbp
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
movq %rbp, %rdx
callq 0xb51c
xorl %ecx, %ecx
testl %eax, %eax
jne 0xb30f
testl %r13d, %r13d
je 0xb2d2
movl %r13d, %ecx
leaq (%rcx,%rbp), %rdx
cmpq 0x30(%rsp), %rdx
ja 0xb2ca
addq 0x8(%rsp), %rbx
cmpb $0x0, (%rcx,%rbx)
je 0xb2cd
decl %r13d
movl %r13d, %ecx
jmp 0xb30f
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp), %rsi
addq $0x102, %rsi            # imm = 0x102
movq 0x48(%rsp), %rdx
cmpq %rdx, %rsi
cmovaq %rdx, %rsi
leaq (%rcx,%rbp), %rdx
movq %rdx, %rcx
cmpq %rsi, %rdx
je 0xb30d
movq %rdx, %rcx
cmpb $0x0, (%rcx)
jne 0xb30d
incq %rcx
cmpq %rsi, %rcx
jne 0xb2fd
movq %rsi, %rcx
subl %edx, %ecx
movl %ebp, %edx
andl 0x58(%rsp), %edx
movq 0x10(%r12), %rsi
movl %eax, (%rsi,%rdx,4)
movq (%r12), %rsi
movl %eax, %eax
movl (%rsi,%rax,4), %edi
cmpl $-0x1, %edi
je 0xb335
movq 0x8(%r12), %r8
movw %di, (%r8,%rdx,2)
movl %edx, (%rsi,%rax,4)
movq 0x28(%r12), %rax
movw %cx, (%rax,%rdx,2)
movq 0x18(%r12), %rax
movzwl %cx, %esi
movl (%rax,%rsi,4), %edi
cmpl $-0x1, %edi
je 0xb35b
movq 0x20(%r12), %r8
movw %di, (%r8,%rdx,2)
movl %edx, (%rax,%rsi,4)
incl %r15d
movq %rbp, %rbx
movl %ecx, %r13d
cmpl %r14d, %r15d
jne 0xb290
movl %ecx, %r13d
movq %rbp, %rbx
movl 0x20(%rsp), %r15d
jmp 0xb155
movl %esi, 0x4(%rsp)
movl %r10d, %r14d
jmp 0xb000
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x56, %eax
jmp 0xb38e
movl $0x53, %eax
jmp 0xb38e
movl $0x51, %eax
jmp 0xb38e