// File: /workspace/github_cpp_makefile_star2_O1/drummyfish[P]flowchart/src/lodepng.cpp
// Function: encodeLZ77(uivector*, Hash*, unsigned char const*, unsigned long, unsigned long, unsigned int)
static unsigned encodeLZ77(uivector* out, Hash* hash,
                           const unsigned char* in, size_t inpos, size_t insize, unsigned windowsize)
{
  unsigned short numzeros = 0;
  int usezeros = windowsize >= 8192; /*for small window size, the 'max chain length' optimization does a better job*/
  unsigned pos, i, error = 0;

  if(!error)
  {
    unsigned offset; /*the offset represents the distance in LZ77 terminology*/
    unsigned length;
#ifdef LAZY_MATCHING
    unsigned lazy = 0;
    unsigned lazylength = 0, lazyoffset = 0;
#endif /*LAZY_MATCHING*/
    unsigned hashval;
    unsigned current_offset, current_length;
    const unsigned char *lastptr, *foreptr, *backptr;
    unsigned short hashpos, prevpos;

    for(pos = inpos; pos < insize; pos++)
    {
      size_t wpos = pos % windowsize; /*position for in 'circular' hash buffers*/

      hashval = getHash(in, insize, pos);
      updateHashChain(hash, pos, hashval, windowsize);

      if(usezeros && hashval == 0)
      {
        numzeros = countZeros(in, insize, pos);
        hash->zeros[wpos] = numzeros;
      }

      /*the length and offset found for the current position*/
      length = 0;
      offset = 0;

      prevpos = hash->head[hashval];
      hashpos = hash->chain[prevpos];

      lastptr = &in[insize < pos + MAX_SUPPORTED_DEFLATE_LENGTH ? insize : pos + MAX_SUPPORTED_DEFLATE_LENGTH];

      /*search for the longest string*/
      if(hash->val[wpos] == (int)hashval)
      {
        /*for large window lengths, assume the user wants no compression loss. Otherwise, max hash chain length speedup.*/
        unsigned maxchainlength = windowsize >= 8192 ? windowsize : windowsize / 8;
        for(;;)
        {
          /*stop when went completely around the circular buffer*/
          if(prevpos < wpos && hashpos > prevpos && hashpos <= wpos) break;
          if(prevpos > wpos && (hashpos <= wpos || hashpos > prevpos)) break;
          if(maxchainlength-- == 0) break;

          current_offset = hashpos <= wpos ? wpos - hashpos : wpos - hashpos + windowsize;
          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(usezeros && hashval == 0 && hash->val[hashpos] == 0 /*hashval[hashpos] may be out of date*/)
            {
              unsigned short skip = hash->zeros[hashpos];
              if(skip > numzeros) skip = numzeros;
              backptr += skip;
              foreptr += skip;
            }

            /* multiple checks at once per array bounds check */
            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)*/
              if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH) break;
            }
          }

          if(hashpos == hash->chain[hashpos]) break;

          prevpos = hashpos;
          hashpos = hash->chain[hashpos];
        }
      }

#ifdef LAZY_MATCHING
      if(!lazy && length >= 3 && length < MAX_SUPPORTED_DEFLATE_LENGTH)
      {
        lazy = 1;
        lazylength = length;
        lazyoffset = offset;
        continue;
      }
      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*/
          pos--;
        }
      }
#endif /*LAZY_MATCHING*/

      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 == 3 && offset > 2048)
        {
          /*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 + 0])) ERROR_BREAK(83 /*alloc fail*/);
          if(!uivector_push_back(out, in[pos + 1])) ERROR_BREAK(83 /*alloc fail*/);
          if(!uivector_push_back(out, in[pos + 2])) ERROR_BREAK(83 /*alloc fail*/);
        }
        else
        {
          addLengthDistance(out, length, offset);
        }
        for(i = 1; i < length; i++)
        {
          pos++;
          hashval = getHash(in, insize, pos);
          updateHashChain(hash, pos, hashval, windowsize);
          if(usezeros && hashval == 0)
          {
            hash->zeros[pos % windowsize] = countZeros(in, insize, pos);
          }
        }
      }

    } /*end of the loop through each character of input*/
  } /*end of "if(!error)"*/

  return error;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rcx, %r11
movq %rsi, %rbp
movq %rdi, 0x30(%rsp)
movl %r11d, %esi
movl $0x0, 0x4(%rsp)
cmpq %r8, %rsi
jae 0xca64
movl %r9d, %r14d
movq %rdx, %r12
leaq (%rdx,%r8), %rax
movq %rax, 0x60(%rsp)
movl %r9d, %eax
shrl $0x3, %eax
cmpl $0x2000, %r9d           # imm = 0x2000
movl %r9d, %ecx
movq %rcx, 0x48(%rsp)
cmovael %r9d, %eax
movl %eax, 0x54(%rsp)
xorl %ebx, %ebx
movl $0x0, 0x4(%rsp)
movl $0x0, 0x44(%rsp)
movq $0x0, 0x58(%rsp)
movl $0x0, (%rsp)
movl %r9d, 0x2c(%rsp)
movq %r8, 0x20(%rsp)
movq %rdx, 0x10(%rsp)
movq %rbp, 0x18(%rsp)
movq %r11, 0x8(%rsp)
movl %r11d, %eax
xorl %edx, %edx
divl %r14d
movl %edx, %edi
leaq 0x3(%rsi), %rcx
movq %r8, %rax
subq %rsi, %rax
cmpq %r8, %rcx
movl $0x3, %ecx
cmovbq %rcx, %rax
leaq (%r12,%rsi), %r11
movl $0x0, %ecx
testq %rax, %rax
je 0xc380
xorl %ecx, %ecx
xorl %r9d, %r9d
xorl %edx, %edx
movzbl (%r11,%r9), %r10d
shll %cl, %r10d
xorl %r10d, %edx
incq %r9
addl $0x2, %ecx
cmpq %rax, %r9
jb 0xc367
movzwl %dx, %ecx
movq %rsi, %rax
xorl %edx, %edx
divq 0x48(%rsp)
movq 0x8(%rbp), %rax
movq %rax, 0x38(%rsp)
movl %ecx, (%rax,%rdx,4)
movq (%rbp), %r10
movl %ecx, %ecx
movl (%r10,%rcx,4), %eax
cmpl $-0x1, %eax
je 0xc3ae
movq 0x10(%rbp), %r9
movw %ax, (%r9,%rdx,2)
cmpl $0x2000, %r14d          # imm = 0x2000
setae %al
movq %r10, 0x70(%rsp)
movl %edx, (%r10,%rcx,4)
testq %rcx, %rcx
sete %r10b
andb %al, %r10b
cmpb $0x1, %r10b
jne 0xc40e
leaq (%r12,%rsi), %r9
addq $0x102, %r9             # imm = 0x102
movq 0x60(%rsp), %rax
cmpq %rax, %r9
cmovaq %rax, %r9
leaq (%r12,%rsi), %rax
movq %rax, %rbx
cmpq %r9, %rax
je 0xc404
movq %rax, %rbx
cmpb $0x0, (%rbx)
jne 0xc404
incq %rbx
cmpq %r9, %rbx
jne 0xc3f7
subl %eax, %ebx
movq 0x18(%rbp), %rax
movw %bx, (%rax,%rdi,2)
addq $0x102, %rsi            # imm = 0x102
cmpq %r8, %rsi
cmovaeq %r8, %rsi
xorl %r13d, %r13d
movl $0x0, %r12d
movq %rcx, 0x68(%rsp)
movq 0x38(%rsp), %rax
cmpl %ecx, (%rax,%rdi,4)
jne 0xc543
addq 0x10(%rsp), %rsi
movq 0x18(%rsp), %rax
movq 0x10(%rax), %r8
movzwl %dx, %eax
movzwl (%r8,%rax,2), %r9d
xorl %r15d, %r15d
xorl %r14d, %r14d
movl 0x54(%rsp), %eax
movl %edx, %ebp
movl %r9d, %edx
movzwl %bp, %r12d
movzwl %r9w, %r9d
cmpq %rdi, %r12
jae 0xc478
cmpw %bp, %dx
jbe 0xc478
cmpl %r9d, %edi
jae 0xc53d
cmpw %r12w, %dx
seta %bpl
cmpl %r9d, %edi
setae %r13b
cmpq %rdi, %r12
jbe 0xc495
orb %bpl, %r13b
jne 0xc53d
subl $0x1, %eax
jb 0xc53d
movl %r9d, %r9d
movq %rdi, %r13
subq %r9, %r13
movl $0x0, %ebp
cmovbl 0x48(%rsp), %ebp
addl %ebp, %r13d
je 0xc504
movq 0x8(%rsp), %rbp
subl %r13d, %ebp
addq 0x10(%rsp), %rbp
testb %r10b, %r10b
je 0xc4d4
movq 0x38(%rsp), %rcx
cmpl $0x0, (%rcx,%r9,4)
je 0xc51b
movq %r11, %r12
cmpq %rsi, %r12
je 0xc4ed
movb (%rbp), %cl
cmpb (%r12), %cl
jne 0xc4ed
incq %rbp
incq %r12
jmp 0xc4d7
subq %r11, %r12
cmpl %r12d, %r14d
jae 0xc504
movl %r13d, %r15d
movl %r12d, %r14d
cmpl $0x102, %r12d           # imm = 0x102
je 0xc543
movzwl (%r8,%r9,2), %r9d
movl %r15d, %r13d
movl %r14d, %r12d
cmpw %r9w, %dx
jne 0xc458
jmp 0xc543
movq 0x18(%rsp), %rcx
movq 0x18(%rcx), %r12
movzwl (%r12,%r9,2), %r12d
cmpw %bx, %r12w
cmovael %ebx, %r12d
movzwl %r12w, %r12d
addq %r12, %rbp
addq %r11, %r12
jmp 0xc4d7
movl %r15d, %r13d
movl %r14d, %r12d
movl (%rsp), %ecx
testl %ecx, %ecx
jne 0xc588
leal -0x3(%r12), %eax
cmpl $0xff, %eax
jae 0xc588
xorl %r15d, %r15d
movl $0x1, (%rsp)
movq %r12, 0x58(%rsp)
movl %r13d, 0x44(%rsp)
movl 0x2c(%rsp), %r14d
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r12
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r11
jmp 0xc80d
testl %ecx, %ecx
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r11
jne 0xc61e
movl 0x2c(%rsp), %r14d
movb $0x1, %r15b
cmpl $0x3, %r12d
jb 0xc5c1
cmpl %r14d, %r13d
jbe 0xc5c1
movl $0x0, (%rsp)
movl $0x56, 0x4(%rsp)
jmp 0xc769
cmpl $0x2, %r12d
movq %r11, 0x8(%rsp)
ja 0xc693
movl $0x0, (%rsp)
movl %r11d, %eax
movq 0x10(%rsp), %rcx
movq %rcx, %r13
movzbl (%rcx,%rax), %ebp
movq 0x30(%rsp), %r12
movq 0x8(%r12), %rsi
incq %rsi
movq %r12, %rdi
callq 0xbdaf
testl %eax, %eax
sete %r15b
movq 0x20(%rsp), %r8
je 0xc7e9
movq (%r12), %rax
movq 0x8(%r12), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
jmp 0xc800
movb $0x1, %r15b
testl %r11d, %r11d
movl 0x2c(%rsp), %r14d
movq 0x58(%rsp), %rcx
je 0xc757
leal 0x1(%rcx), %eax
cmpl %eax, %r12d
jbe 0xc7c6
movl $0x0, (%rsp)
leal -0x1(%r11), %eax
movq 0x10(%rsp), %rcx
movzbl (%rcx,%rax), %eax
movl %eax, 0x38(%rsp)
movq 0x30(%rsp), %rbp
movq 0x8(%rbp), %rsi
incq %rsi
movq %rbp, %rdi
callq 0xbdaf
testl %eax, %eax
je 0xca57
movq (%rbp), %rax
movq 0x8(%rbp), %rcx
movl 0x38(%rsp), %edx
movl %edx, -0x4(%rax,%rcx,4)
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r11
jmp 0xc59f
cmpl $0x3, %r12d
jne 0xc778
cmpl $0x800, %r13d           # imm = 0x800
jbe 0xc778
movl $0x0, (%rsp)
movl %r11d, %eax
movq 0x10(%rsp), %r13
movzbl (%r13,%rax), %ebp
movq 0x30(%rsp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0xbdaf
testl %eax, %eax
je 0xc7f3
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
movq 0x8(%rsp), %rax
incl %eax
movzbl (%r13,%rax), %ebp
movq 0x8(%rdi), %rsi
incq %rsi
callq 0xbdaf
testl %eax, %eax
je 0xc7f3
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
movq 0x8(%rsp), %rax
addl $0x2, %eax
movzbl (%r13,%rax), %r13d
movq 0x8(%rdi), %rsi
incq %rsi
callq 0xbdaf
testl %eax, %eax
movq 0x18(%rsp), %rbp
jne 0xc922
movl $0x53, 0x4(%rsp)
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r12
jmp 0xc57e
movl $0x0, (%rsp)
movl $0x51, 0x4(%rsp)
xorl %r11d, %r11d
movq 0x20(%rsp), %r8
movq 0x10(%rsp), %r12
jmp 0xc80d
movl $0x1, %eax
movl $0x1c, %ecx
leaq 0x59f7(%rip), %r8       # 0x12180
cmpq %rcx, %rax
ja 0xc82a
leaq (%rcx,%rax), %rsi
shrq %rsi
cmpl %r12d, (%r8,%rsi,4)
jbe 0xc7b1
leaq -0x1(%rsi), %rdi
cmpl %r12d, -0x4(%r8,%rsi,4)
jbe 0xc7bb
movb $0x1, %dl
movq %rdi, %rcx
jmp 0xc7c0
incq %rsi
movb $0x1, %dl
movq %rsi, %rax
jmp 0xc7c0
xorl %edx, %edx
movq %rdi, %r9
testb %dl, %dl
jne 0xc789
jmp 0xc830
movq 0x70(%rsp), %rax
movq 0x68(%rsp), %rdx
movl $0xffffffff, (%rax,%rdx,4) # imm = 0xFFFFFFFF
decl %r11d
movl 0x44(%rsp), %eax
movl %eax, %r13d
movl %ecx, %r12d
jmp 0xc59f
movl $0x53, 0x4(%rsp)
jmp 0xc800
movl $0x53, 0x4(%rsp)
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %rbp
movq 0x8(%rsp), %r11
movq %r13, %r12
testb %r15b, %r15b
jne 0xca64
incl %r11d
movq %r11, %rsi
cmpq %r8, %r11
jb 0xc32d
jmp 0xca64
movl $0x1c, %r9d
movl %r9d, %eax
movl %r12d, %ecx
subl (%r8,%rax,4), %ecx
movl %ecx, (%rsp)
movl $0x1, %eax
movl $0x1d, %ecx
leaq 0x5a32(%rip), %r8       # 0x12280
cmpq %rcx, %rax
ja 0xc887
leaq (%rcx,%rax), %rsi
shrq %rsi
cmpl %r13d, (%r8,%rsi,4)
jbe 0xc872
leaq -0x1(%rsi), %rdi
cmpl %r13d, -0x4(%r8,%rsi,4)
jbe 0xc87c
movb $0x1, %dl
movq %rdi, %rcx
jmp 0xc881
incq %rsi
movb $0x1, %dl
movq %rsi, %rax
jmp 0xc881
xorl %edx, %edx
movq %rdi, %r15
testb %dl, %dl
jne 0xc84e
jmp 0xc88d
movl $0x1d, %r15d
movq %r9, 0x38(%rsp)
movl %r15d, %eax
subl (%r8,%rax,4), %r13d
movq 0x30(%rsp), %r14
movq 0x8(%r14), %rsi
incq %rsi
movq %r14, %rdi
callq 0xbdaf
testl %eax, %eax
je 0xc8c7
movq 0x38(%rsp), %rdx
addl $0x101, %edx            # imm = 0x101
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl %edx, -0x4(%rax,%rcx,4)
movq 0x8(%r14), %rsi
incq %rsi
movq %r14, %rdi
callq 0xbdaf
testl %eax, %eax
je 0xc8e8
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl (%rsp), %edx
movl %edx, -0x4(%rax,%rcx,4)
movq 0x8(%r14), %rsi
incq %rsi
movq %r14, %rdi
callq 0xbdaf
testl %eax, %eax
movq %r14, %rdx
movl 0x2c(%rsp), %r14d
je 0xc90f
movq (%rdx), %rax
movq 0x8(%rdx), %rcx
movl %r15d, -0x4(%rax,%rcx,4)
movq 0x8(%rdx), %rsi
incq %rsi
movq %rdx, %rdi
callq 0xbdaf
testl %eax, %eax
je 0xc933
movq 0x30(%rsp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl %r13d, -0x4(%rax,%rcx,4)
movq (%rbp), %rsi
movq 0x8(%rbp), %rdi
cmpl $0x3, %r12d
movl $0x2, %eax
cmovbl %eax, %r12d
movq 0x8(%rsp), %r11
leal 0x1(%r11), %r15d
movl $0x1, %r9d
movq 0x20(%rsp), %r8
incl %r11d
xorl %ecx, %ecx
movq %r8, %rax
subq %r11, %rax
jbe 0xc9be
leaq 0x3(%r11), %rdx
cmpq %r8, %rdx
movl $0x3, %edx
cmovbq %rdx, %rax
testq %rax, %rax
je 0xc9be
movl %r14d, %r13d
movq %rdi, %r14
movq %r11, %rdi
movl %r15d, %edx
addq 0x10(%rsp), %rdx
xorl %ecx, %ecx
xorl %r11d, %r11d
xorl %r10d, %r10d
movzbl (%rdx,%r11), %ebp
shll %cl, %ebp
xorl %ebp, %r10d
incq %r11
addl $0x2, %ecx
cmpq %rax, %r11
jb 0xc997
movzwl %r10w, %ecx
movq 0x18(%rsp), %rbp
movq %rdi, %r11
movq %r14, %rdi
movl %r13d, %r14d
movq %r11, %rax
xorl %edx, %edx
divq 0x48(%rsp)
movl %ecx, (%rdi,%rdx,4)
movl %ecx, %eax
movl (%rsi,%rax,4), %ecx
cmpl $-0x1, %ecx
je 0xc9de
movq 0x10(%rbp), %r10
movw %cx, (%r10,%rdx,2)
movl %edx, (%rsi,%rax,4)
cmpl $0x2000, %r14d          # imm = 0x2000
jb 0xca39
testl %eax, %eax
jne 0xca39
movq 0x10(%rsp), %rcx
leaq (%rcx,%r11), %rdx
addq $0x102, %rdx            # imm = 0x102
movq 0x60(%rsp), %rax
cmpq %rax, %rdx
cmovaq %rax, %rdx
leaq (%rcx,%r11), %rax
movq %rax, %rcx
cmpq %rdx, %rax
je 0xca26
movq %rax, %rcx
cmpb $0x0, (%rcx)
jne 0xca26
incq %rcx
cmpq %rdx, %rcx
jne 0xca19
subl %eax, %ecx
movq 0x18(%rbp), %r10
movl %r11d, %eax
xorl %edx, %edx
divl %r14d
movw %cx, (%r10,%rdx,2)
incl %r9d
incl %r15d
cmpl %r12d, %r9d
jne 0xc95c
movl $0x0, (%rsp)
xorl %r15d, %r15d
jmp 0xc76e
movl $0x53, 0x4(%rsp)
jmp 0xc56f
movl 0x4(%rsp), %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq