// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xe0, %rsp
movl 0x20(%rbp), %eax
movl 0x18(%rbp), %eax
movl 0x10(%rbp), %eax
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq %rcx, -0x28(%rbp)
movq %r8, -0x30(%rbp)
movl %r9d, -0x34(%rbp)
movl $0x0, -0x48(%rbp)
cmpl $0x2000, -0x34(%rbp)    # imm = 0x2000
jb 0xcf97
movl -0x34(%rbp), %eax
movl %eax, -0xb4(%rbp)
jmp 0xcfa3
movl -0x34(%rbp), %eax
shrl $0x3, %eax
movl %eax, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x34(%rbp), %edx
movl $0x40, %eax
movl $0x102, %ecx            # imm = 0x102
cmpl $0x2000, %edx           # imm = 0x2000
cmovaeq %rcx, %rax
movl %eax, -0x50(%rbp)
movl $0x1, -0x54(%rbp)
movl $0x0, -0x58(%rbp)
movl $0x0, -0x64(%rbp)
movl $0x0, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
cmpl $0x0, -0x34(%rbp)
je 0xcff8
cmpl $0x8000, -0x34(%rbp)    # imm = 0x8000
jbe 0xd004
movl $0x3c, -0x4(%rbp)
jmp 0xd63d
movl -0x34(%rbp), %eax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
andl %ecx, %eax
cmpl $0x0, %eax
je 0xd020
movl $0x5a, -0x4(%rbp)
jmp 0xd63d
movl 0x18(%rbp), %eax
cmpq $0x102, %rax            # imm = 0x102
jbe 0xd032
movl $0x102, 0x18(%rbp)      # imm = 0x102
movq -0x28(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
cmpq -0x30(%rbp), %rax
jae 0xd637
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movq %rax, -0xa8(%rbp)
movl $0x0, -0xac(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xd880
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xd0dc
cmpl $0x0, -0x70(%rbp)
jne 0xd0dc
cmpl $0x0, -0x58(%rbp)
jne 0xd0a4
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xd960
movl %eax, -0x58(%rbp)
jmp 0xd0da
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
addq %rcx, %rax
cmpq -0x30(%rbp), %rax
ja 0xd0cf
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x58(%rbp), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xd0d8
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xd0da
jmp 0xd0e3
movl $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl -0x70(%rbp), %edx
movl -0x58(%rbp), %eax
movzwl %ax, %ecx
callq 0xda00
movl $0x0, -0x60(%rbp)
movl $0x0, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movq -0xa8(%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x9c(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0xc0(%rbp)
movq -0x30(%rbp), %rax
movq -0x40(%rbp), %rcx
addq $0x102, %rcx            # imm = 0x102
cmpq %rcx, %rax
jae 0xd14f
movq -0x30(%rbp), %rax
movq %rax, -0xc8(%rbp)
jmp 0xd160
movq -0x40(%rbp), %rax
addq $0x102, %rax            # imm = 0x102
movq %rax, -0xc8(%rbp)
movq -0xc0(%rbp), %rax
movq -0xc8(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x7c(%rbp)
movl -0xac(%rbp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, -0xac(%rbp)
cmpl -0x4c(%rbp), %eax
jb 0xd19a
jmp 0xd3cc
movl -0x9c(%rbp), %eax
cmpq -0xa8(%rbp), %rax
ja 0xd1c2
movq -0xa8(%rbp), %rax
movl -0x9c(%rbp), %ecx
subq %rcx, %rax
movq %rax, -0xd0(%rbp)
jmp 0xd1df
movq -0xa8(%rbp), %rax
movl -0x9c(%rbp), %ecx
subq %rcx, %rax
movl -0x34(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jae 0xd1f6
jmp 0xd3cc
movl -0x74(%rbp), %eax
movl %eax, -0x7c(%rbp)
cmpl $0x0, -0x74(%rbp)
jbe 0xd336
movq -0x20(%rbp), %rax
addq -0x40(%rbp), %rax
movq %rax, -0x90(%rbp)
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x74(%rbp), %edx
subq %rdx, %rcx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
cmpl $0x3, -0x58(%rbp)
jb 0xd291
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x9c(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %eax
cmpl -0x58(%rbp), %eax
jbe 0xd25f
movl -0x58(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %ecx
movq -0x98(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x98(%rbp)
movl -0xb0(%rbp), %ecx
movq -0x90(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x90(%rbp)
jmp 0xd293
movq -0x90(%rbp), %rcx
xorl %eax, %eax
cmpq -0x88(%rbp), %rcx
movb %al, -0xd1(%rbp)
je 0xd2ca
movq -0x98(%rbp), %rax
movzbl (%rax), %eax
movq -0x90(%rbp), %rcx
movzbl (%rcx), %ecx
cmpl %ecx, %eax
sete %al
movb %al, -0xd1(%rbp)
movb -0xd1(%rbp), %al
testb $0x1, %al
jne 0xd2d6
jmp 0xd2fc
movq -0x98(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x98(%rbp)
movq -0x90(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x90(%rbp)
jmp 0xd293
movq -0x90(%rbp), %rax
movq -0x20(%rbp), %rcx
addq -0x40(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x78(%rbp)
movl -0x78(%rbp), %eax
cmpl -0x60(%rbp), %eax
jbe 0xd334
movl -0x78(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x78(%rbp), %eax
cmpl 0x18(%rbp), %eax
jb 0xd332
jmp 0xd3cc
jmp 0xd334
jmp 0xd336
movl -0x9c(%rbp), %eax
movq -0x18(%rbp), %rcx
movq 0x8(%rcx), %rcx
movl -0x9c(%rbp), %edx
movzwl (%rcx,%rdx,2), %ecx
cmpl %ecx, %eax
jne 0xd354
jmp 0xd3cc
cmpl $0x3, -0x58(%rbp)
jb 0xd395
movl -0x60(%rbp), %eax
cmpl -0x58(%rbp), %eax
jbe 0xd395
movq -0x18(%rbp), %rax
movq 0x20(%rax), %rax
movl -0x9c(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rax
movq 0x28(%rax), %rax
movl -0x9c(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
cmpl -0x58(%rbp), %eax
je 0xd393
jmp 0xd3cc
jmp 0xd3c7
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rax
movl -0x9c(%rbp), %ecx
movzwl (%rax,%rcx,2), %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rax
movl -0x9c(%rbp), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x70(%rbp), %eax
je 0xd3c5
jmp 0xd3cc
jmp 0xd3c7
jmp 0xd17f
cmpl $0x0, 0x20(%rbp)
je 0xd4a9
cmpl $0x0, -0x64(%rbp)
jne 0xd40d
cmpl $0x3, -0x60(%rbp)
jb 0xd40d
movl -0x60(%rbp), %eax
cmpl -0x50(%rbp), %eax
ja 0xd40d
movl -0x60(%rbp), %eax
cmpq $0x102, %rax            # imm = 0x102
jae 0xd40d
movl $0x1, -0x64(%rbp)
movl -0x60(%rbp), %eax
movl %eax, -0x68(%rbp)
movl -0x5c(%rbp), %eax
movl %eax, -0x6c(%rbp)
jmp 0xd626
cmpl $0x0, -0x64(%rbp)
je 0xd4a7
movl $0x0, -0x64(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0xd431
movl $0x51, -0x48(%rbp)
jmp 0xd637
movl -0x60(%rbp), %eax
movl -0x68(%rbp), %ecx
addl $0x1, %ecx
cmpl %ecx, %eax
jbe 0xd46a
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %esi
callq 0xdad0
cmpl $0x0, %eax
jne 0xd468
movl $0x53, -0x48(%rbp)
jmp 0xd637
jmp 0xd4a5
movl -0x68(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x6c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x18(%rbp), %rax
movq (%rax), %rax
movl -0x70(%rbp), %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x58(%rbp), %ecx
movl $0xffffffff, (%rax,%rcx,4) # imm = 0xFFFFFFFF
movq -0x40(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd4a7
jmp 0xd4a9
cmpl $0x3, -0x60(%rbp)
jb 0xd4c3
movl -0x5c(%rbp), %eax
cmpl -0x34(%rbp), %eax
jbe 0xd4c3
movl $0x56, -0x48(%rbp)
jmp 0xd637
cmpl $0x3, -0x60(%rbp)
jae 0xd4f4
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %esi
callq 0xdad0
cmpl $0x0, %eax
jne 0xd4ef
movl $0x53, -0x48(%rbp)
jmp 0xd637
jmp 0xd624
movl -0x60(%rbp), %eax
cmpl 0x10(%rbp), %eax
jb 0xd50b
cmpl $0x3, -0x60(%rbp)
jne 0xd536
cmpl $0x1000, -0x5c(%rbp)    # imm = 0x1000
jbe 0xd536
movq -0x10(%rbp), %rdi
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movzbl (%rax,%rcx), %esi
callq 0xdad0
cmpl $0x0, %eax
jne 0xd531
movl $0x53, -0x48(%rbp)
jmp 0xd637
jmp 0xd622
movq -0x10(%rbp), %rdi
movl -0x60(%rbp), %eax
movl %eax, %esi
movl -0x5c(%rbp), %eax
movl %eax, %edx
callq 0xdb30
movl $0x1, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x60(%rbp), %eax
jae 0xd620
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movl -0x34(%rbp), %ecx
subl $0x1, %ecx
movl %ecx, %ecx
andq %rcx, %rax
movq %rax, -0xa8(%rbp)
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xd880
movl %eax, -0x70(%rbp)
cmpl $0x0, -0x54(%rbp)
je 0xd5f2
cmpl $0x0, -0x70(%rbp)
jne 0xd5f2
cmpl $0x0, -0x58(%rbp)
jne 0xd5ba
movq -0x20(%rbp), %rdi
movq -0x30(%rbp), %rsi
movq -0x40(%rbp), %rdx
callq 0xd960
movl %eax, -0x58(%rbp)
jmp 0xd5f0
movq -0x40(%rbp), %rax
movl -0x58(%rbp), %ecx
addq %rcx, %rax
cmpq -0x30(%rbp), %rax
ja 0xd5e5
movq -0x20(%rbp), %rax
movq -0x40(%rbp), %rcx
movl -0x58(%rbp), %edx
addq %rdx, %rcx
subq $0x1, %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0xd5ee
movl -0x58(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x58(%rbp)
jmp 0xd5f0
jmp 0xd5f9
movl $0x0, -0x58(%rbp)
movq -0x18(%rbp), %rdi
movq -0xa8(%rbp), %rsi
movl -0x70(%rbp), %edx
movl -0x58(%rbp), %eax
movzwl %ax, %ecx
callq 0xda00
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0xd550
jmp 0xd622
jmp 0xd624
jmp 0xd626
movq -0x40(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x40(%rbp)
jmp 0xd03a
movl -0x48(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
nopl (%rax)