// File: /workspace/github_c_makefile_star2_O1/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 $0x98, %rsp
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movl %r9d, %eax
shrl $0x3, %eax
cmpl $0x2000, %r9d           # imm = 0x2000
cmovael %r9d, %eax
movl %eax, 0x2c(%rsp)
movl $0x102, %eax            # imm = 0x102
movl $0x40, %ecx
cmovael %eax, %ecx
movl %ecx, 0x50(%rsp)
movq %rdi, 0x38(%rsp)
movq %r9, 0x30(%rsp)
leal -0x8001(%r9), %eax
movl $0x3c, 0x4(%rsp)
cmpl $0xffff8000, %eax       # imm = 0xFFFF8000
jb 0xad5e
movq 0x30(%rsp), %rcx
leal -0x1(%rcx), %eax
movl $0x5a, 0x4(%rsp)
testl %eax, %ecx
jne 0xad5e
movq %r8, %r15
movl 0xd8(%rsp), %ecx
movl $0x102, %edx            # imm = 0x102
cmpl %edx, %ecx
cmovbl %ecx, %edx
movl %edx, 0x54(%rsp)
movl $0x0, 0x4(%rsp)
cmpq %r8, %r12
jae 0xad5e
movl %eax, %eax
movq %rax, 0x70(%rsp)
movq 0x8(%rsp), %rax
addq %r15, %rax
movq %rax, 0x68(%rsp)
movl 0x30(%rsp), %eax
movq %rax, 0x90(%rsp)
movq $0x0, 0x58(%rsp)
movq $0x0, 0x60(%rsp)
movq $0x0, 0x20(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movq %r15, 0x18(%rsp)
movq %rsi, 0x48(%rsp)
movq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xaed4
movq 0x48(%rsp), %r8
movl $0x0, %r14d
testl %eax, %eax
jne 0xa7d0
testl %ebx, %ebx
je 0xa795
movl %ebx, %ecx
addq %r12, %rcx
cmpq %r15, %rcx
ja 0xa78e
movq 0x8(%rsp), %rdx
cmpb $0x0, -0x1(%rdx,%rcx)
je 0xa790
decl %ebx
movl %ebx, %r14d
jmp 0xa7d0
movq 0x8(%rsp), %rcx
leaq (%rcx,%r12), %rdx
addq $0x102, %rdx            # imm = 0x102
movq 0x68(%rsp), %rsi
cmpq %rsi, %rdx
cmovaq %rsi, %rdx
addq %r12, %rcx
movq %rcx, %r14
cmpq %rdx, %rcx
je 0xa7cd
movq %rcx, %r14
cmpb $0x0, (%r14)
jne 0xa7cd
incq %r14
cmpq %rdx, %r14
jne 0xa7bf
subl %ecx, %r14d
movl %r12d, %edi
andl 0x70(%rsp), %edi
movq 0x10(%r8), %rcx
movq %rcx, 0x40(%rsp)
movl %eax, (%rcx,%rdi,4)
movq (%r8), %rsi
movl %eax, %r9d
movl (%rsi,%r9,4), %ecx
cmpl $-0x1, %ecx
je 0xa7fa
movq 0x8(%r8), %rdx
movw %cx, (%rdx,%rdi,2)
movq %rsi, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movl %edi, (%rsi,%r9,4)
movq 0x28(%r8), %r9
movw %r14w, (%r9,%rdi,2)
movq 0x18(%r8), %r11
movzwl %r14w, %ecx
movl (%r11,%rcx,4), %edx
cmpl $-0x1, %edx
je 0xa830
movq 0x20(%r8), %rsi
movw %dx, (%rsi,%rdi,2)
leaq 0x102(%r12), %r10
cmpq %r15, %r10
cmovaeq %r15, %r10
movq %r11, 0x78(%rsp)
movl %edi, (%r11,%rcx,4)
cmpl $0x0, 0x2c(%rsp)
movq %r12, 0x10(%rsp)
je 0xa94b
movq 0x8(%rsp), %rcx
addq %rcx, %r10
movq 0x8(%r8), %r11
movzwl (%r11,%rdi,2), %ebp
leaq (%rcx,%r12), %r15
xorl %edx, %edx
movl $0x1, %ecx
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %edx, %ebx
movzwl %bp, %esi
movq %rdi, %rdx
subq %rsi, %rdx
movl $0x0, %ebp
cmovbq 0x90(%rsp), %rbp
addq %rbp, %rdx
cmpl %edx, %ebx
ja 0xa951
testq %rdx, %rdx
je 0xa8fd
movl %edx, %ebp
movq 0x10(%rsp), %rbx
subq %rbp, %rbx
addq 0x8(%rsp), %rbx
cmpl $0x3, %r14d
jb 0xa8cc
movzwl (%r9,%rsi,2), %ebp
cmpl %ebp, %r14d
cmovbl %r14d, %ebp
addq %rbp, %rbx
addq %r15, %rbp
jmp 0xa8cf
movq %r15, %rbp
cmpq %r10, %rbp
je 0xa8e5
movb (%rbx), %r8b
cmpb (%rbp), %r8b
jne 0xa8e5
incq %rbx
incq %rbp
jmp 0xa8cf
subl %r15d, %ebp
cmpl %ebp, %r13d
jae 0xa8fd
movl %edx, %r12d
movl %ebp, %r13d
cmpl %ebp, 0x54(%rsp)
jbe 0xad3b
movzwl (%r11,%rsi,2), %ebp
cmpw %bp, %si
je 0xa951
cmpl $0x3, %r14d
jb 0xa92c
cmpl %r14d, %r13d
jbe 0xa92c
movq 0x48(%rsp), %r8
movq 0x20(%r8), %r8
movzwl (%r8,%rsi,2), %ebp
movzwl (%r9,%rbp,2), %esi
cmpl %esi, %r14d
je 0xa93a
jmp 0xa951
movzwl %bp, %esi
movq 0x40(%rsp), %r8
cmpl %eax, (%r8,%rsi,4)
jne 0xa951
leal 0x1(%rcx), %esi
cmpl 0x2c(%rsp), %ecx
movl %esi, %ecx
jne 0xa87a
jmp 0xa951
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpl $0x0, 0xe0(%rsp)
je 0xa9c6
cmpl $0x0, 0x20(%rsp)
movq 0x38(%rsp), %r15
movl %r12d, %edi
jne 0xa9b0
cmpl $0x3, %r13d
jb 0xa9b0
cmpl 0x50(%rsp), %r13d
setbe %al
cmpl $0x102, %r13d           # imm = 0x102
setb %cl
testb %cl, %al
je 0xa9b0
movb $0x1, %al
movl $0x1, %ecx
movq %rcx, 0x20(%rsp)
movl %r13d, %ecx
movq %rcx, 0x60(%rsp)
movl %edi, %ecx
movq %rcx, 0x58(%rsp)
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
jmp 0xad26
cmpl $0x0, 0x20(%rsp)
jne 0xaa45
movq $0x0, 0x20(%rsp)
jmp 0xa9ce
movq 0x38(%rsp), %r15
movl %r12d, %edi
movq 0x10(%rsp), %r12
cmpl $0x3, %r13d
jb 0xa9ee
cmpl 0x30(%rsp), %edi
jbe 0xa9ee
xorl %eax, %eax
movl $0x56, 0x4(%rsp)
jmp 0xad21
cmpl $0x2, %r13d
jbe 0xaa14
cmpl 0xd0(%rsp), %r13d
jb 0xaa14
cmpl $0x3, %r13d
jne 0xab01
cmpl $0x1000, %edi           # imm = 0x1000
jbe 0xab01
movq 0x8(%rsp), %rax
movb (%rax,%r12), %bl
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0xa60b
testl %eax, %eax
je 0xaa99
movzbl %bl, %eax
movq (%r15), %rcx
movq 0x8(%r15), %rdx
movl %eax, -0x4(%rcx,%rdx,4)
movb $0x1, %al
jmp 0xad21
cmpq $0x0, 0x10(%rsp)
movq 0x60(%rsp), %rdx
je 0xaaa3
leal 0x1(%rdx), %eax
cmpl %eax, %r13d
jbe 0xaabe
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movzbl -0x1(%rax,%rcx), %ebx
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0xa60b
testl %eax, %eax
je 0xad46
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
movq $0x0, 0x20(%rsp)
jmp 0xa9cb
movl $0x53, 0x4(%rsp)
jmp 0xaab7
movq $0x0, 0x20(%rsp)
movl $0x51, 0x4(%rsp)
xorl %r12d, %r12d
xorl %eax, %eax
jmp 0xad21
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
movq 0x88(%rsp), %rax
movq 0x80(%rsp), %rdi
movl %ecx, (%rax,%rdi,4)
movl %r14d, %eax
movq 0x78(%rsp), %rdi
movl %ecx, (%rdi,%rax,4)
movq 0x10(%rsp), %r12
decq %r12
movq $0x0, 0x20(%rsp)
movq 0x58(%rsp), %rax
movl %eax, %edi
movl %edx, %r13d
jmp 0xa9d3
movl $0x1c, %eax
movl $0x1, %r15d
leaq 0x3aad(%rip), %rdx      # 0xe5c0
leaq 0x3ba6(%rip), %rsi      # 0xe6c0
leaq (%rax,%r15), %rcx
shrq %rcx
cmpl %r13d, (%rdx,%rcx,4)
jb 0xab2f
decq %rcx
movq %rcx, %rax
jmp 0xab35
incq %rcx
movq %rcx, %r15
cmpq %rax, %r15
jbe 0xab1a
cmpq $0x1c, %r15
ja 0xab46
cmpl %r13d, (%rdx,%r15,4)
jbe 0xab49
decq %r15
movl %r15d, %eax
movl (%rdx,%rax,4), %edx
movl $0x1d, %eax
movl $0x1, %ebp
leaq (%rax,%rbp), %rcx
shrq %rcx
cmpl %edi, (%rsi,%rcx,4)
jb 0xab6d
decq %rcx
movq %rcx, %rax
jmp 0xab73
incq %rcx
movq %rcx, %rbp
cmpq %rax, %rbp
jbe 0xab59
cmpq $0x1d, %rbp
movl %edx, 0x40(%rsp)
ja 0xab87
cmpl %edi, (%rsi,%rbp,4)
jbe 0xab8a
decq %rbp
movl %ebp, %eax
subl (%rsi,%rax,4), %edi
movl %edi, 0x10(%rsp)
movq 0x38(%rsp), %rbx
movq 0x8(%rbx), %rsi
incq %rsi
movq %rbx, %rdi
callq 0xa60b
testl %eax, %eax
je 0xabbe
addl $0x101, %r15d           # imm = 0x101
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl %r15d, -0x4(%rax,%rcx,4)
movq 0x8(%rbx), %rsi
incq %rsi
movq %rbx, %rdi
callq 0xa60b
testl %eax, %eax
je 0xabe3
movl %r13d, %eax
subl 0x40(%rsp), %eax
movq (%rbx), %rcx
movq 0x8(%rbx), %rdx
movl %eax, -0x4(%rcx,%rdx,4)
movq 0x8(%rbx), %rsi
incq %rsi
movq %rbx, %rdi
callq 0xa60b
testl %eax, %eax
je 0xac01
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
movq 0x8(%rbx), %rsi
incq %rsi
movq %rbx, %rdi
callq 0xa60b
testl %eax, %eax
je 0xac23
movq (%rbx), %rax
movq 0x8(%rbx), %rcx
movl 0x10(%rsp), %edx
movl %edx, -0x4(%rax,%rcx,4)
cmpl $0x3, %r13d
movl $0x2, %eax
cmovbl %eax, %r13d
movl $0x1, %r15d
leaq 0x1(%r12), %rbp
movq 0x8(%rsp), %rdi
movq 0x18(%rsp), %rsi
movq %rbp, %rdx
callq 0xaed4
movq 0x48(%rsp), %rsi
xorl %ecx, %ecx
testl %eax, %eax
jne 0xacb9
testl %r14d, %r14d
je 0xac7f
movl %r14d, %ecx
leaq (%rcx,%rbp), %rdx
cmpq 0x18(%rsp), %rdx
ja 0xac77
addq 0x8(%rsp), %r12
cmpb $0x0, (%rcx,%r12)
je 0xac7a
decl %r14d
movl %r14d, %ecx
jmp 0xacb9
movq 0x8(%rsp), %rcx
leaq (%rcx,%rbp), %rdi
addq $0x102, %rdi            # imm = 0x102
movq 0x68(%rsp), %rdx
cmpq %rdx, %rdi
cmovaq %rdx, %rdi
leaq (%rcx,%rbp), %rdx
movq %rdx, %rcx
cmpq %rdi, %rdx
je 0xacb7
movq %rdx, %rcx
cmpb $0x0, (%rcx)
jne 0xacb7
incq %rcx
cmpq %rdi, %rcx
jne 0xacaa
subl %edx, %ecx
movl %ebp, %edx
andl 0x70(%rsp), %edx
movq 0x10(%rsi), %rdi
movl %eax, (%rdi,%rdx,4)
movq (%rsi), %r9
movl %eax, %eax
movl (%r9,%rax,4), %edi
cmpl $-0x1, %edi
je 0xacdd
movq 0x8(%rsi), %r8
movw %di, (%r8,%rdx,2)
movl %edx, (%r9,%rax,4)
movq 0x28(%rsi), %rax
movw %cx, (%rax,%rdx,2)
movq 0x18(%rsi), %rax
movzwl %cx, %r9d
movl (%rax,%r9,4), %edi
cmpl $-0x1, %edi
je 0xad03
movq 0x20(%rsi), %r8
movw %di, (%r8,%rdx,2)
movl %edx, (%rax,%r9,4)
incl %r15d
movq %rbp, %r12
movl %ecx, %r14d
cmpl %r13d, %r15d
jne 0xac36
movb $0x1, %al
movl %ecx, %r14d
movq %rbp, %r12
movq 0x18(%rsp), %r15
testb %al, %al
je 0xad5e
incq %r12
movl %r14d, %ebx
cmpq %r15, %r12
jb 0xa755
jmp 0xad5e
movl %edx, %r12d
movl %ebp, %r13d
jmp 0xa951
movq $0x0, 0x20(%rsp)
movl $0x53, 0x4(%rsp)
xorl %eax, %eax
jmp 0xa9a1
movl 0x4(%rsp), %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq