// File: /workspace/github_cpp_makefile_star2_O2/mhe[P]raytracerframework_cpp/lodepng.cpp
// Function: encodeLZ77(uivector*, unsigned char const*, unsigned long, unsigned int)
static unsigned encodeLZ77(uivector* out, const unsigned char* in, size_t size, unsigned windowSize)
{
  /**generate hash table**/
  vector table; /*HASH_NUM_VALUES uivectors; this represents what would be an std::vector<std::vector<unsigned> > in C++*/
  uivector tablepos1, tablepos2;
  unsigned pos, i, error = 0;
  
  vector_init(&table, sizeof(uivector));
  if(!vector_resize(&table, HASH_NUM_VALUES)) return 9917;
  for(i = 0; i < HASH_NUM_VALUES; i++)
  {
    uivector* v = (uivector*)vector_get(&table, i);
    uivector_init(v);
  }

  /*remember start and end positions in the tables to searching in*/
  uivector_init(&tablepos1);
  uivector_init(&tablepos2);
  if(!uivector_resizev(&tablepos1, HASH_NUM_VALUES, 0)) error = 9918;
  if(!uivector_resizev(&tablepos2, HASH_NUM_VALUES, 0)) error = 9919;
  
  if(!error)
  {
    for(pos = 0; pos < size; pos++)
    {
      unsigned length = 0, offset = 0; /*the length and offset found for the current position*/
      unsigned max_offset = pos < windowSize ? pos : windowSize; /*how far back to test*/
      unsigned tablepos;
    
      /*/search for the longest string*/
      /*first find out where in the table to start (the first value that is in the range from "pos - max_offset" to "pos")*/
      unsigned hash = getHash(in, size, pos);
      if(!uivector_push_back((uivector*)vector_get(&table, hash), pos))  { error = 9920; break; }
      
      while(((uivector*)vector_get(&table, hash))->data[tablepos1.data[hash]] < pos - max_offset) tablepos1.data[hash]++; /*it now points to the first value in the table for which the index is larger than or equal to pos - max_offset*/
      while(((uivector*)vector_get(&table, hash))->data[tablepos2.data[hash]] < pos) tablepos2.data[hash]++; /*it now points to the first value in the table for which the index is larger than or equal to pos*/

      for(tablepos = tablepos2.data[hash] - 1; tablepos >= tablepos1.data[hash] && tablepos < tablepos2.data[hash]; tablepos--)
      {
        unsigned backpos = ((uivector*)vector_get(&table, hash))->data[tablepos];
        unsigned current_offset = pos - backpos;

        /*test the next characters*/
        unsigned current_length = 0;
        unsigned backtest = backpos;
        unsigned foretest = pos;
        while(foretest < size && in[backtest] == in[foretest] && current_length < MAX_SUPPORTED_DEFLATE_LENGTH) /*maximum supporte length by deflate is max length*/
        {
          if(backpos >= pos) backpos -= current_offset; /*continue as if we work on the decoded bytes after pos by jumping back before pos*/
          current_length++;
          backtest++;
          foretest++;
        }
        if(current_length > length)
        {
          length = current_length; /*the longest length*/
          offset = current_offset; /*the offset that is related to this longest length*/
          if(current_length == MAX_SUPPORTED_DEFLATE_LENGTH) break; /*you can jump out of this for loop once a length of max length is found (gives significant speed gain)*/
        }
      }
      
      /**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 = 9921; break; }
      }
      else
      {
        unsigned j;
        addLengthDistance(out, length, offset);
        for(j = 0; j < length - 1; j++)
        {
          pos++;
          if(!uivector_push_back((uivector*)vector_get(&table, getHash(in, size, pos)), pos)) { error = 9922; break; }
        }
      }
    } /*end of the loop through each character of input*/
  } /*end of "if(!error)"*/
  
  /*cleanup*/
  for(i = 0; i < table.size; i++)
  {
    uivector* v = (uivector*)vector_get(&table, i);
    uivector_cleanup(v);
  }
  vector_cleanup(&table);
  uivector_cleanup(&tablepos1);
  uivector_cleanup(&tablepos2);
  return error;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %ecx, -0x5c(%rbp)
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, -0x30(%rbp)
xorps %xmm0, %xmm0
leaq -0xa0(%rbp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
movl $0x18, 0x18(%rdi)
movl $0x10000, %ebx          # imm = 0x10000
movl $0x10000, %esi          # imm = 0x10000
callq 0x16d47
testl %eax, %eax
je 0x16ba3
movq -0xa0(%rbp), %rax
movl -0x88(%rbp), %r12d
movq %rax, -0x58(%rbp)
xorps %xmm0, %xmm0
subq $0x1, %rbx
jb 0x16943
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
addq %r12, %rax
jmp 0x16930
xorps %xmm0, %xmm0
leaq -0xe0(%rbp), %rdi
movaps %xmm0, (%rdi)
andq $0x0, 0x10(%rdi)
leaq -0xc0(%rbp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
movl $0x10000, %esi          # imm = 0x10000
callq 0x16846
xorl %ecx, %ecx
testl %eax, %eax
movq %r12, -0x38(%rbp)
movl $0x26be, %r12d          # imm = 0x26BE
cmovnel %ecx, %r12d
movl $0x10000, %esi          # imm = 0x10000
movq %rbx, %rdi
callq 0x16846
testl %eax, %eax
movl $0x26bf, %eax           # imm = 0x26BF
cmovnel %r12d, %eax
movq -0x38(%rbp), %r12
testl %eax, %eax
jne 0x16bbf
movq -0xe0(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x70(%rbp)
movl $0x0, -0x3c(%rbp)
xorl %ebx, %ebx
movl %ebx, %edx
cmpq %r14, %rdx
jae 0x16baa
movq %r15, %rdi
movq %r14, %rsi
movq %rdx, -0x50(%rbp)
callq 0x16d95
movl %eax, %r13d
imulq %r13, %r12
addq -0x58(%rbp), %r12
movq %r12, %rdi
movl %ebx, %esi
callq 0x16dcc
testl %eax, %eax
je 0x16baf
movq (%r12), %rax
movl %ebx, %ecx
subl -0x5c(%rbp), %ecx
movl $0x0, %edx
cmovbl %edx, %ecx
movq -0x78(%rbp), %rdi
movl (%rdi,%r13,4), %edx
movq -0x70(%rbp), %r8
movl %edx, %esi
cmpl %ecx, (%rax,%rsi,4)
jae 0x16a26
incl %edx
movl %edx, (%rdi,%r13,4)
jmp 0x16a17
movl (%r8,%r13,4), %ecx
movl %ecx, %edx
cmpl %ebx, (%rax,%rdx,4)
jae 0x16a39
incl %ecx
movl %ecx, (%r8,%r13,4)
jmp 0x16a2a
movl (%rdi,%r13,4), %edx
xorl %r13d, %r13d
xorl %r12d, %r12d
movl %ecx, %esi
decl %esi
cmpl %edx, %esi
jb 0x16aa4
cmpl %ecx, %esi
jae 0x16aa4
movl %esi, %edi
movl (%rax,%rdi,4), %r9d
movl %ebx, %edi
subl %r9d, %edi
xorl %r8d, %r8d
leal (%rbx,%r8), %r10d
cmpq %r14, %r10
jae 0x16a85
leal (%r9,%r8), %r11d
movl %r11d, %r11d
movb (%r15,%r11), %r11b
cmpb (%r15,%r10), %r11b
jne 0x16a85
cmpl $0x101, %r8d            # imm = 0x101
ja 0x16a85
incl %r8d
jmp 0x16a5d
cmpl $0x102, %r8d            # imm = 0x102
setne %r9b
cmpl %r13d, %r8d
setbe %r10b
cmoval %r8d, %r13d
cmoval %edi, %r12d
orb %r9b, %r10b
jne 0x16a45
cmpl $0x2, %r13d
ja 0x16ac9
movq -0x50(%rbp), %rax
movzbl (%r15,%rax), %esi
movq -0x30(%rbp), %rdi
callq 0x16dcc
testl %eax, %eax
jne 0x16b98
jmp 0x16bb6
movl %r13d, %edx
movl %r12d, %eax
movq %rax, -0x48(%rbp)
leaq 0xbdd6(%rip), %rdi      # 0x228b0
pushq $0x1d
popq %rsi
callq 0x16e22
movq %rax, -0x68(%rbp)
movl %eax, %edx
movl %r13d, %eax
leaq 0xbdbe(%rip), %rcx      # 0x228b0
subl (%rcx,%rdx,4), %eax
movl %eax, -0x50(%rbp)
leaq 0xbeb1(%rip), %rdi      # 0x229b0
pushq $0x1e
popq %rsi
movq -0x48(%rbp), %rdx
callq 0x16e22
movq %rax, -0x48(%rbp)
movl %eax, %ecx
leaq 0xbe98(%rip), %rax      # 0x229b0
subl (%rax,%rcx,4), %r12d
movq -0x68(%rbp), %rsi
addl $0x101, %esi            # imm = 0x101
movq -0x30(%rbp), %rdi
callq 0x16dcc
movq -0x30(%rbp), %rdi
movl -0x50(%rbp), %esi
callq 0x16dcc
movq -0x30(%rbp), %rdi
movq -0x48(%rbp), %rsi
callq 0x16dcc
movq -0x30(%rbp), %rdi
movl %r12d, %esi
callq 0x16dcc
movl %r13d, %r12d
decl %r12d
addl %ebx, %r13d
decl %r13d
subl $0x1, %r12d
jb 0x16b95
incl %ebx
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x16d95
movl %eax, %edi
imulq -0x38(%rbp), %rdi
addq -0x58(%rbp), %rdi
movl %ebx, %esi
callq 0x16dcc
testl %eax, %eax
jne 0x16b60
movl $0x26c2, -0x3c(%rbp)    # imm = 0x26C2
jmp 0x16b98
movl %r13d, %ebx
incl %ebx
movq -0x38(%rbp), %r12
jmp 0x169c3
movl $0x26bd, %eax           # imm = 0x26BD
jmp 0x16c10
movl -0x3c(%rbp), %eax
jmp 0x16bbf
movl $0x26c0, %eax           # imm = 0x26C0
jmp 0x16bbb
movl $0x26c1, %eax           # imm = 0x26C1
movq -0x38(%rbp), %r12
movl %eax, %r15d
movq -0x98(%rbp), %rbx
xorl %eax, %eax
movl %eax, %r14d
cmpq %r14, %rbx
jbe 0x16be9
movq %r14, %rdi
imulq %r12, %rdi
addq -0x58(%rbp), %rdi
callq 0x16628
leal 0x1(%r14), %eax
jmp 0x16bcb
leaq -0xa0(%rbp), %rdi
callq 0x16dff
leaq -0xe0(%rbp), %rdi
callq 0x16628
leaq -0xc0(%rbp), %rdi
callq 0x16628
movl %r15d, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq