// File: /workspace/github_cpp_makefile_star2_O3/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 %rdi, 0x30(%rsp)
movl %r11d, %r15d
cmpq %r8, %r15
jae 0xc849
movl %r9d, %r14d
movq %r8, %r12
movq %rdx, %rbx
leaq (%rdx,%r8), %rax
movq %rax, 0x58(%rsp)
movl %r9d, %eax
shrl $0x3, %eax
cmpl $0x2000, %r9d           # imm = 0x2000
cmovael %r9d, %eax
movl %eax, 0x44(%rsp)
movl %r9d, %eax
movq %rax, 0x38(%rsp)
xorl %r10d, %r10d
movl $0x0, 0x20(%rsp)
movq $0x0, 0x48(%rsp)
xorl %eax, %eax
movl %r9d, 0xc(%rsp)
movq %r8, 0x28(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdx, (%rsp)
movl %eax, 0x24(%rsp)
movl %r11d, %eax
xorl %edx, %edx
divl %r14d
movl %edx, %edi
leaq 0x3(%r15), %rcx
movq %r12, %rax
subq %r15, %rax
cmpq %r12, %rcx
movl $0x3, %ecx
cmovbq %rcx, %rax
leaq (%rbx,%r15), %rbp
movl $0x0, %ecx
testq %rax, %rax
je 0xc271
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %edx, %edx
movzbl (%rbp,%r8), %r9d
shll %cl, %r9d
xorl %r9d, %edx
incq %r8
addl $0x2, %ecx
cmpq %rax, %r8
jb 0xc257
movzwl %dx, %ecx
movq %r11, 0x10(%rsp)
movq %r15, %rax
xorl %edx, %edx
divq 0x38(%rsp)
movq 0x8(%rsi), %rbx
movl %ecx, (%rbx,%rdx,4)
movq (%rsi), %r8
movl %ecx, %r9d
movl (%r8,%r9,4), %eax
cmpl $-0x1, %eax
je 0xc29e
movq 0x10(%rsi), %rcx
movw %ax, (%rcx,%rdx,2)
cmpl $0x2000, %r14d          # imm = 0x2000
setae %al
movl %edx, (%r8,%r9,4)
testq %r9, %r9
sete %r11b
andb %al, %r11b
cmpb $0x1, %r11b
jne 0xc2f7
leaq 0x102(%rbp), %rax
movq 0x58(%rsp), %rcx
cmpq %rcx, %rax
cmovaq %rcx, %rax
movq %rbp, %r10
cmpq %rax, %rbp
je 0xc2eb
movq %rbp, %r10
cmpb $0x0, (%r10)
jne 0xc2eb
incq %r10
cmpq %rax, %r10
jne 0xc2da
movq %rax, %r10
subl %ebp, %r10d
movq 0x18(%rsi), %rax
movw %r10w, (%rax,%rdi,2)
movq %r10, 0x50(%rsp)
addq $0x102, %r15            # imm = 0x102
cmpq %r12, %r15
cmovaeq %r12, %r15
cmpl %r9d, (%rbx,%rdi,4)
jne 0xc436
movq %rbx, 0x70(%rsp)
movq %r9, 0x60(%rsp)
movq %r8, 0x68(%rsp)
addq (%rsp), %r15
movq 0x10(%rsi), %rax
movzwl %dx, %ecx
movzwl (%rax,%rcx,2), %r9d
xorl %r8d, %r8d
xorl %r10d, %r10d
movl 0x44(%rsp), %ecx
movl %edx, %r14d
movl %r9d, %edx
movzwl %r14w, %ebx
movzwl %r9w, %r9d
cmpq %rdi, %rbx
jae 0xc35f
cmpw %r14w, %dx
jbe 0xc35f
cmpl %r9d, %edi
jae 0xc45b
cmpl %r9d, %edi
setae %r14b
cmpw %bx, %dx
seta %r12b
cmpq %rdi, %rbx
jbe 0xc37b
orb %r12b, %r14b
jne 0xc45b
subl $0x1, %ecx
jb 0xc45b
movl %r9d, %r12d
movq %rdi, %rbx
subq %r12, %rbx
movl $0x0, %r9d
cmovbl 0x38(%rsp), %r9d
addl %r9d, %ebx
je 0xc3f8
movq 0x10(%rsp), %r14
subl %ebx, %r14d
addq (%rsp), %r14
testb %r11b, %r11b
je 0xc3bb
movq 0x70(%rsp), %rsi
cmpl $0x0, (%rsi,%r12,4)
je 0xc40f
movq %rbp, %r9
movq %r15, %r13
cmpq %r15, %r9
je 0xc3e1
movb (%r14), %r13b
cmpb (%r9), %r13b
jne 0xc3de
incq %r14
incq %r9
cmpq %r15, %r9
jne 0xc3c6
movq %r15, %r13
jmp 0xc3e1
movq %r9, %r13
subq %rbp, %r13
cmpl %r13d, %r10d
jae 0xc3f8
movl %ebx, %r8d
movl %r13d, %r10d
cmpl $0x102, %r13d           # imm = 0x102
je 0xc461
movzwl (%rax,%r12,2), %r9d
movl %r8d, %ebx
movl %r10d, %r13d
cmpw %r9w, %dx
jne 0xc33d
jmp 0xc461
movq 0x18(%rsp), %rsi
movq 0x18(%rsi), %r9
movzwl (%r9,%r12,2), %r9d
movq 0x50(%rsp), %rsi
cmpw %si, %r9w
cmovael %esi, %r9d
movzwl %r9w, %r9d
addq %r9, %r14
addq %rbp, %r9
jmp 0xc3be
xorl %ebx, %ebx
movl $0x0, %r13d
cmpl $0x0, 0x24(%rsp)
movq 0x10(%rsp), %r11
movq 0x30(%rsp), %r15
leaq 0x5efc(%rip), %rsi      # 0x12350
jne 0xc4c4
jmp 0xc53f
movl %r8d, %ebx
movl %r10d, %r13d
movl 0x24(%rsp), %edx
testl %edx, %edx
movq 0x10(%rsp), %r11
jne 0xc4a0
leal -0x3(%r13), %ecx
movl $0x1, %eax
cmpl $0xff, %ecx
jae 0xc4a0
movl %ebx, 0x20(%rsp)
movq %r13, 0x48(%rsp)
movl 0xc(%rsp), %r14d
movq 0x28(%rsp), %r12
movq (%rsp), %rbx
movq 0x18(%rsp), %rsi
jmp 0xc835
testl %edx, %edx
movl 0xc(%rsp), %r14d
movq 0x28(%rsp), %r12
movq 0x30(%rsp), %r15
leaq 0x5e98(%rip), %rsi      # 0x12350
movq 0x68(%rsp), %r8
movq 0x60(%rsp), %r9
je 0xc52a
testl %r11d, %r11d
je 0xc86a
movq 0x48(%rsp), %rcx
leal 0x1(%rcx), %eax
cmpl %eax, %r13d
jbe 0xc516
leal -0x1(%r11), %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebp
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0xbc0b
testl %eax, %eax
je 0xc85c
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %ebp, -0x4(%rax,%rcx,4)
movq 0x10(%rsp), %r11
leaq 0x5e3c(%rip), %rsi      # 0x12350
jmp 0xc52a
movl $0xffffffff, (%r8,%r9,4) # imm = 0xFFFFFFFF
decl %r11d
movl 0x20(%rsp), %eax
movl %eax, %ebx
movl %ecx, %r13d
cmpl $0x3, %r13d
jb 0xc539
cmpl %r14d, %ebx
ja 0xc863
cmpl $0x2, %r13d
ja 0xc588
movq %r11, %r15
movl %r11d, %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebx
movq 0x30(%rsp), %r13
movq 0x8(%r13), %rsi
incq %rsi
movq %r13, %rdi
callq 0xbc0b
testl %eax, %eax
je 0xc85c
movq (%r13), %rax
movq 0x8(%r13), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
xorl %eax, %eax
movq 0x18(%rsp), %rsi
movq %r15, %r11
movq (%rsp), %rbx
jmp 0xc835
cmpl $0x3, %r13d
movq %r11, 0x10(%rsp)
jne 0xc633
cmpl $0x800, %ebx            # imm = 0x800
jbe 0xc633
movl %r11d, %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebx
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
movq %r15, %rbp
movq %r11, %r15
callq 0xbc0b
movl $0x53, %ecx
testl %eax, %eax
je 0xc84b
movq (%rbp), %rax
movq 0x8(%rbp), %rsi
movl %ebx, -0x4(%rax,%rsi,4)
leal 0x1(%r15), %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebx
incq %rsi
movq %rbp, %rdi
callq 0xbc0b
testl %eax, %eax
je 0xc85c
movq (%rbp), %rax
movq 0x8(%rbp), %rsi
movl %ebx, -0x4(%rax,%rsi,4)
leal 0x2(%r15), %eax
movq (%rsp), %rcx
movzbl (%rcx,%rax), %ebx
incq %rsi
movq %rbp, %rdi
callq 0xbc0b
testl %eax, %eax
movq %rbp, %r15
movl $0x53, %ecx
jne 0xc727
jmp 0xc84b
movl $0x1, %eax
movl $0x1c, %r12d
leaq 0x5c0b(%rip), %rdx      # 0x12250
leaq (%rax,%r12), %rcx
shrq %rcx
cmpl %r13d, (%rdx,%rcx,4)
jbe 0xc65f
leaq -0x1(%rcx), %r12
cmpl %r13d, -0x4(%rdx,%rcx,4)
ja 0xc665
jmp 0xc670
incq %rcx
movq %rcx, %rax
cmpq %r12, %rax
jbe 0xc645
movl $0x1c, %r12d
movl %r12d, %eax
movl %r13d, %ebp
subl (%rdx,%rax,4), %ebp
movl $0x1, %eax
movl $0x1d, %r14d
leaq (%rax,%r14), %rcx
shrq %rcx
cmpl %ebx, (%rsi,%rcx,4)
jbe 0xc69c
leaq -0x1(%rcx), %r14
cmpl %ebx, -0x4(%rsi,%rcx,4)
ja 0xc6a2
jmp 0xc6ad
incq %rcx
movq %rcx, %rax
cmpq %r14, %rax
jbe 0xc684
movl $0x1d, %r14d
movl %r14d, %eax
subl (%rsi,%rax,4), %ebx
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0xbc0b
movq 0x8(%r15), %rsi
testl %eax, %eax
je 0xc6d9
addl $0x101, %r12d           # imm = 0x101
movq (%r15), %rax
movl %r12d, -0x4(%rax,%rsi,4)
incq %rsi
movq %r15, %rdi
callq 0xbc0b
movq 0x8(%r15), %rsi
testl %eax, %eax
je 0xc6f3
movq (%r15), %rax
movl %ebp, -0x4(%rax,%rsi,4)
incq %rsi
movq %r15, %rdi
callq 0xbc0b
movq 0x8(%r15), %rsi
testl %eax, %eax
movq 0x28(%rsp), %r12
je 0xc713
movq (%r15), %rax
movl %r14d, -0x4(%rax,%rsi,4)
incq %rsi
movq %r15, %rdi
callq 0xbc0b
testl %eax, %eax
movl 0xc(%rsp), %r14d
je 0xc732
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
movq 0x18(%rsp), %rsi
movq (%rsi), %r15
movq 0x8(%rsi), %rdi
movq 0x10(%rsp), %r11
leal 0x1(%r11), %r8d
movl $0x1, %r9d
movq (%rsp), %rbx
incl %r11d
xorl %ecx, %ecx
movq %r12, %rax
subq %r11, %rax
jbe 0xc7a9
leaq 0x3(%r11), %rdx
cmpq %r12, %rdx
movl $0x3, %edx
cmovbq %rdx, %rax
testq %rax, %rax
je 0xc7a9
movq %r11, %rbp
movl %r8d, %edx
addq %rbx, %rdx
xorl %ecx, %ecx
xorl %r11d, %r11d
xorl %r10d, %r10d
movzbl (%rdx,%r11), %ebx
shll %cl, %ebx
xorl %ebx, %r10d
incq %r11
addl $0x2, %ecx
cmpq %rax, %r11
jb 0xc784
movzwl %r10w, %ecx
movq 0x18(%rsp), %rsi
movq %rbp, %r11
movq (%rsp), %rbx
movq %r11, %rax
xorl %edx, %edx
divq 0x38(%rsp)
movl %ecx, (%rdi,%rdx,4)
movl %ecx, %eax
movl (%r15,%rax,4), %ecx
cmpl $-0x1, %ecx
je 0xc7ca
movq 0x10(%rsi), %r10
movw %cx, (%r10,%rdx,2)
movl %edx, (%r15,%rax,4)
cmpl $0x2000, %r14d          # imm = 0x2000
jb 0xc824
testl %eax, %eax
jne 0xc824
leaq (%rbx,%r11), %rdx
addq $0x102, %rdx            # imm = 0x102
movq 0x58(%rsp), %rax
cmpq %rax, %rdx
cmovaq %rax, %rdx
leaq (%rbx,%r11), %rax
movq %rax, %rcx
cmpq %rdx, %rax
je 0xc811
movq %rax, %rcx
cmpb $0x0, (%rcx)
jne 0xc811
incq %rcx
cmpq %rdx, %rcx
jne 0xc801
movq %rdx, %rcx
subl %eax, %ecx
movq 0x18(%rsi), %r10
movl %r11d, %eax
xorl %edx, %edx
divl %r14d
movw %cx, (%r10,%rdx,2)
incl %r9d
incl %r8d
cmpl %r13d, %r9d
jne 0xc751
xorl %eax, %eax
incl %r11d
movq %r11, %r15
cmpq %r12, %r11
movq 0x50(%rsp), %r10
jb 0xc21e
xorl %ecx, %ecx
movl %ecx, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x53, %ecx
jmp 0xc84b
movl $0x56, %ecx
jmp 0xc84b
movl $0x51, %ecx
jmp 0xc84b