// File: /workspace/github_cpp_makefile_star2_O3/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 $0xa8, %rsp
movl %r9d, %eax
shrl $0x3, %eax
cmpl $0x2000, %r9d           # imm = 0x2000
movq %rdx, 0x10(%rsp)
movl $0x40, %edx
movq %r8, %r14
movq %rcx, %r8
leal -0x8001(%r9), %ecx
movq %rsi, 0x38(%rsp)
cmovael %r9d, %eax
movl %eax, 0x44(%rsp)
movl $0x102, %eax            # imm = 0x102
cmovael %eax, %edx
movl $0x3c, %eax
cmpl $0xffff8000, %ecx       # imm = 0xFFFF8000
jb 0xe5b0
leal -0x1(%r9), %ecx
movl $0x5a, %eax
testl %ecx, %r9d
jne 0xe5b0
movl 0xe8(%rsp), %eax
movl $0x102, %esi            # imm = 0x102
cmpl %esi, %eax
cmovbl %eax, %esi
movl %esi, 0x64(%rsp)
cmpq %r14, %r8
jae 0xe5ae
movl %ecx, %eax
movq 0x10(%rsp), %rcx
movq %rax, 0x78(%rsp)
movl %edx, 0x60(%rsp)
movq %r9, 0x80(%rsp)
movq $0x0, 0x68(%rsp)
movq $0x0, 0x28(%rsp)
xorl %r12d, %r12d
xorl %ebp, %ebp
movq %r14, 0x30(%rsp)
movq %rdi, 0x48(%rsp)
leaq (%rcx,%r14), %rax
movq %rax, 0x70(%rsp)
movl %r9d, %eax
movq %rax, 0xa0(%rsp)
movq 0x10(%rsp), %rdi
movl %r12d, 0x1c(%rsp)
movq %r8, 0x20(%rsp)
movq %r14, %rsi
movq %r8, %rdx
callq 0xe740
movq 0x20(%rsp), %r8
movq 0x38(%rsp), %r9
movl $0x0, %r13d
movl %eax, %r11d
testl %eax, %eax
jne 0xdff1
testl %ebp, %ebp
je 0xdfb4
movl %ebp, %ecx
addq %r8, %rcx
cmpq %r14, %rcx
ja 0xdfad
movq 0x10(%rsp), %rax
cmpb $0x0, -0x1(%rax,%rcx)
je 0xdfaf
decl %ebp
movl %ebp, %r13d
jmp 0xdff1
movq 0x10(%rsp), %rax
movq 0x70(%rsp), %rsi
leaq 0x102(%rax,%r8), %rdx
leaq (%rax,%r8), %rcx
movq %rcx, %r13
cmpq %rsi, %rdx
cmovaq %rsi, %rdx
cmpq %rdx, %rcx
je 0xdfee
movq %rcx, %r13
cmpb $0x0, (%r13)
jne 0xdfee
incq %r13
cmpq %rdx, %r13
jne 0xdfdc
movq %rdx, %r13
subl %ecx, %r13d
movl %r8d, %edi
movq 0x10(%r9), %rax
andl 0x78(%rsp), %edi
movl %r11d, %esi
movl %r11d, (%rax,%rdi,4)
movq %rax, 0x58(%rsp)
movq (%r9), %rax
movl (%rax,%rsi,4), %ecx
cmpl $-0x1, %ecx
je 0xe01b
movq 0x8(%r9), %rdx
movw %cx, (%rdx,%rdi,2)
movl %edi, (%rax,%rsi,4)
movq %rax, 0x98(%rsp)
movzwl %r13w, %ecx
movq %rsi, 0x90(%rsp)
movq 0x28(%r9), %rax
movw %r13w, (%rax,%rdi,2)
movq 0x18(%r9), %r15
movl (%r15,%rcx,4), %edx
cmpl $-0x1, %edx
je 0xe050
movq 0x20(%r9), %rsi
movw %dx, (%rsi,%rdi,2)
leaq 0x102(%r8), %r10
movq %r15, 0x88(%rsp)
movl %edi, (%r15,%rcx,4)
cmpq %r14, %r10
cmovaeq %r14, %r10
cmpl $0x0, 0x44(%rsp)
je 0xe19b
movq 0x8(%r9), %rcx
movq 0x10(%rsp), %rdx
movl $0x1, %esi
movl %r11d, 0x50(%rsp)
xorl %r12d, %r12d
xorl %ebx, %ebx
movl $0x0, 0xc(%rsp)
movzwl (%rcx,%rdi,2), %r15d
leaq (%rdx,%r10), %rbp
addq %rdx, %r8
movzwl %r15w, %r14d
movq %rdi, %r11
movl $0x0, %r15d
movl %r12d, %edx
subq %r14, %r11
cmovbq 0xa0(%rsp), %r15
leaq (%r15,%r11), %r12
cmpl %r12d, %edx
ja 0xe1a5
testq %r12, %r12
je 0xe14b
movq 0x20(%rsp), %r15
movl %r12d, %edx
subq %rdx, %r15
addq 0x10(%rsp), %r15
cmpl $0x3, %r13d
jb 0xe0f9
movzwl (%rax,%r14,2), %edx
cmpl %edx, %r13d
cmovbl %r13d, %edx
addq %rdx, %r15
addq %r8, %rdx
jmp 0xe0fc
movq %r8, %rdx
movq %rbp, %r11
cmpq %rbp, %rdx
je 0xe130
movq 0x10(%rsp), %r9
leaq (%rdx,%r9), %r11
addq %r10, %r11
subq %rdx, %r11
movb (%r15), %r9b
cmpb (%rdx), %r9b
jne 0xe128
incq %r15
incq %rdx
cmpq %rbp, %rdx
jne 0xe113
jmp 0xe12b
movq %rdx, %r11
movq 0x38(%rsp), %r9
subl %r8d, %r11d
cmpl %r11d, %ebx
jae 0xe14b
movl %r12d, 0xc(%rsp)
movl %r11d, %ebx
cmpl %r11d, 0x64(%rsp)
jbe 0xe5a1
movzwl (%rcx,%r14,2), %r15d
cmpw %r15w, %r14w
je 0xe1a5
cmpl $0x3, %r13d
jb 0xe176
cmpl %r13d, %ebx
jbe 0xe176
movq 0x20(%r9), %rdx
movzwl (%rdx,%r14,2), %r15d
movzwl (%rax,%r15,2), %edx
cmpl %edx, %r13d
je 0xe18a
jmp 0xe1a5
movl 0x50(%rsp), %r11d
movq 0x58(%rsp), %r14
movzwl %r15w, %edx
cmpl %r11d, (%r14,%rdx,4)
jne 0xe1a5
leal 0x1(%rsi), %edx
cmpl 0x44(%rsp), %esi
movl %edx, %esi
jne 0xe0a1
jmp 0xe1a5
movl $0x0, 0xc(%rsp)
xorl %ebx, %ebx
cmpl $0x0, 0xf0(%rsp)
je 0xe216
movl 0x1c(%rsp), %eax
movq 0x30(%rsp), %r14
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r8
leaq 0x3c92(%rip), %rsi      # 0x11e60
movl %ebx, %r10d
testl %eax, %eax
jne 0xe23b
cmpl $0x3, %r10d
jb 0xe23b
cmpl 0x60(%rsp), %r10d
movl 0xc(%rsp), %ebx
setbe %al
cmpl $0x102, %r10d           # imm = 0x102
setb %cl
xorl %r9d, %r9d
testb %cl, %al
je 0xe2ab
movl %r10d, %eax
movq %rax, 0x28(%rsp)
movl %ebx, %eax
movl $0x1, %r12d
movq %rax, 0x68(%rsp)
jmp 0xe31f
movl 0x1c(%rsp), %r9d
movq 0x30(%rsp), %r14
movq 0x48(%rsp), %rdi
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %r8
leaq 0x3c2a(%rip), %rsi      # 0x11e60
movl %ebx, %r10d
jmp 0xe2a7
xorl %r9d, %r9d
testl %eax, %eax
je 0xe2a7
testq %r8, %r8
je 0xe5d0
leal 0x1(%r12), %eax
cmpl %eax, %r10d
jbe 0xe568
movq 0x10(%rsp), %rax
movq 0x8(%rdi), %rsi
movq %r14, %r15
movq %rdi, %r14
movzbl -0x1(%rax,%r8), %ebp
incq %rsi
callq 0xde33
testl %eax, %eax
je 0xe5c9
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl %ebx, %r10d
movq 0x20(%rsp), %r8
movl 0xc(%rsp), %ebx
movq %r14, %rdi
leaq 0x3bc5(%rip), %rsi      # 0x11e60
xorl %r9d, %r9d
movq %r15, %r14
movl %ebp, -0x4(%rax,%rcx,4)
jmp 0xe2ab
movl 0xc(%rsp), %ebx
cmpl $0x3, %r10d
jb 0xe2be
cmpl 0x80(%rsp), %ebx
ja 0xe5c2
cmpl $0x2, %r10d
jbe 0xe2dc
cmpl 0xe0(%rsp), %r10d
jb 0xe2dc
cmpl $0x3, %r10d
jne 0xe333
cmpl $0x1000, %ebx           # imm = 0x1000
jbe 0xe333
movq 0x10(%rsp), %rax
movq 0x8(%rdi), %rsi
movq %r14, %r15
movq %r12, 0x28(%rsp)
movl %r9d, %r12d
movq %r8, %rbx
movq %rdi, %r14
movb (%rax,%r8), %bpl
incq %rsi
callq 0xde33
testl %eax, %eax
je 0xe5c9
movq (%r14), %rcx
movq 0x8(%r14), %rdx
movzbl %bpl, %eax
movq %rbx, %r8
movq %r15, %r14
movl %eax, -0x4(%rcx,%rdx,4)
incq %r8
movl %r13d, %ebp
cmpq %r14, %r8
jb 0xdf62
jmp 0xe5ae
movl $0x1c, %eax
movl $0x1, %r14d
leaq 0x3a1b(%rip), %rdx      # 0x11d60
leaq (%rax,%r14), %rcx
shrq %rcx
cmpl %r10d, (%rdx,%rcx,4)
jb 0xe35a
decq %rcx
movq %rcx, %rax
jmp 0xe360
incq %rcx
movq %rcx, %r14
cmpq %rax, %r14
jbe 0xe345
cmpq $0x1c, %r14
ja 0xe371
cmpl %r10d, (%rdx,%r14,4)
jbe 0xe374
decq %r14
movl %r14d, %eax
movl (%rdx,%rax,4), %r12d
movl $0x1d, %eax
movl $0x1, %r15d
leaq (%rax,%r15), %rcx
shrq %rcx
cmpl %ebx, (%rsi,%rcx,4)
jb 0xe39a
decq %rcx
movq %rcx, %rax
jmp 0xe3a0
incq %rcx
movq %rcx, %r15
cmpq %rax, %r15
jbe 0xe386
movq %r8, 0x20(%rsp)
movl %r10d, 0xc(%rsp)
movl %r9d, 0x1c(%rsp)
cmpq $0x1d, %r15
ja 0xe3c0
cmpl %ebx, (%rsi,%r15,4)
jbe 0xe3c3
decq %r15
movq 0x48(%rsp), %rbp
movl %r15d, %eax
subl (%rsi,%rax,4), %ebx
movq 0x8(%rbp), %rsi
movq %rbp, %rdi
incq %rsi
callq 0xde33
movq 0x8(%rbp), %rsi
testl %eax, %eax
je 0xe3f5
movq (%rbp), %rax
addl $0x101, %r14d           # imm = 0x101
movl %r14d, -0x4(%rax,%rsi,4)
incq %rsi
movq %rbp, %rdi
callq 0xde33
movq 0x8(%rbp), %rsi
testl %eax, %eax
je 0xe417
movl 0xc(%rsp), %eax
movq (%rbp), %rcx
subl %r12d, %eax
movl %eax, -0x4(%rcx,%rsi,4)
incq %rsi
movq %rbp, %rdi
callq 0xde33
movq 0x8(%rbp), %rsi
testl %eax, %eax
je 0xe433
movq (%rbp), %rax
movl %r15d, -0x4(%rax,%rsi,4)
incq %rsi
movq %rbp, %rdi
callq 0xde33
testl %eax, %eax
je 0xe44e
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
movq 0x38(%rsp), %rax
movq 0x20(%rsp), %rbx
movl $0x1, %r14d
movq 0x10(%rax), %rcx
movq 0x28(%rax), %rdx
movq (%rax), %r12
movq 0x18(%rax), %r15
movq %rcx, 0x58(%rsp)
movq %rdx, 0x50(%rsp)
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %rsi
leaq 0x1(%rbx), %rbp
movq %rbp, %rdx
callq 0xe740
xorl %ecx, %ecx
testl %eax, %eax
jne 0xe4f4
testl %r13d, %r13d
je 0xe4ba
movl %r13d, %ecx
leaq (%rbp,%rcx), %rdx
cmpq 0x30(%rsp), %rdx
ja 0xe4b2
addq 0x10(%rsp), %rbx
cmpb $0x0, (%rcx,%rbx)
je 0xe4b5
decl %r13d
movl %r13d, %ecx
jmp 0xe4f4
movq 0x10(%rsp), %rcx
movq 0x70(%rsp), %rdi
leaq 0x102(%rcx,%rbp), %rsi
leaq (%rcx,%rbp), %rdx
movq %rdx, %rcx
cmpq %rdi, %rsi
cmovaq %rdi, %rsi
cmpq %rsi, %rdx
je 0xe4f2
movq %rdx, %rcx
cmpb $0x0, (%rcx)
jne 0xe4f2
incq %rcx
cmpq %rsi, %rcx
jne 0xe4e2
movq %rsi, %rcx
subl %edx, %ecx
movl %ebp, %edx
andl 0x78(%rsp), %edx
movq 0x58(%rsp), %rsi
movq 0x38(%rsp), %r8
movl %eax, (%rsi,%rdx,4)
movl %eax, %eax
movl (%r12,%rax,4), %esi
cmpl $-0x1, %esi
je 0xe51a
movq 0x8(%r8), %rdi
movw %si, (%rdi,%rdx,2)
movq 0x50(%rsp), %rdi
movl %edx, (%r12,%rax,4)
movzwl %cx, %eax
movw %cx, (%rdi,%rdx,2)
movl (%r15,%rax,4), %esi
cmpl $-0x1, %esi
je 0xe53b
movq 0x20(%r8), %rdi
movw %si, (%rdi,%rdx,2)
incl %r14d
movl %edx, (%r15,%rax,4)
movq %rbp, %rbx
movl %ecx, %r13d
cmpl 0xc(%rsp), %r14d
jne 0xe477
movq 0x30(%rsp), %r14
movl 0x1c(%rsp), %r12d
movl %ecx, %r13d
movq %rbp, %r8
jmp 0xe31f
movq 0x98(%rsp), %rax
movq 0x90(%rsp), %rdx
movq 0x88(%rsp), %r11
movl $0xffffffff, %ecx       # imm = 0xFFFFFFFF
decq %r8
movl %r12d, %r10d
movl %ecx, (%rax,%rdx,4)
movl %r13d, %eax
movl %ecx, (%r11,%rax,4)
movq 0x68(%rsp), %rax
movl %eax, %ebx
jmp 0xe2ab
movl %r12d, 0xc(%rsp)
movl %r11d, %ebx
jmp 0xe1a5
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x56, %eax
jmp 0xe5b0
movl $0x53, %eax
jmp 0xe5b0
movl $0x51, %eax
jmp 0xe5b0