// File: 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
subq $0xd0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl $0x0, -0x84(%rbp)
leaq -0x48(%rbp), %rdi
movl $0x18, %esi
callq 0x2e6b0
leaq -0x48(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
callq 0x2e6f0
cmpl $0x0, %eax
jne 0x2dfd1
movl $0x26bd, -0x4(%rbp)     # imm = 0x26BD
jmp 0x2e470
movl $0x0, -0x80(%rbp)
cmpl $0x10000, -0x80(%rbp)   # imm = 0x10000
jae 0x2e00d
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq %rax, -0x90(%rbp)
movq -0x90(%rbp), %rdi
callq 0x2d6a0
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2dfd8
leaq -0x60(%rbp), %rdi
callq 0x2d6a0
leaq -0x78(%rbp), %rdi
callq 0x2d6a0
leaq -0x60(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %edx, %edx
callq 0x2dbb0
cmpl $0x0, %eax
jne 0x2e03e
movl $0x26be, -0x84(%rbp)    # imm = 0x26BE
leaq -0x78(%rbp), %rdi
movl $0x10000, %esi          # imm = 0x10000
xorl %edx, %edx
callq 0x2dbb0
cmpl $0x0, %eax
jne 0x2e05d
movl $0x26bf, -0x84(%rbp)    # imm = 0x26BF
cmpl $0x0, -0x84(%rbp)
jne 0x2e410
movl $0x0, -0x7c(%rbp)
movl -0x7c(%rbp), %eax
cmpq -0x20(%rbp), %rax
jae 0x2e40e
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
movl -0x7c(%rbp), %eax
cmpl -0x24(%rbp), %eax
jae 0x2e0a5
movl -0x7c(%rbp), %eax
movl %eax, -0xcc(%rbp)
jmp 0x2e0ae
movl -0x24(%rbp), %eax
movl %eax, -0xcc(%rbp)
movl -0xcc(%rbp), %eax
movl %eax, -0x9c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x7c(%rbp), %eax
movl %eax, %edx
callq 0x2e7d0
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq %rax, %rdi
movl -0x7c(%rbp), %esi
callq 0x2e870
cmpl $0x0, %eax
jne 0x2e102
movl $0x26c0, -0x84(%rbp)    # imm = 0x26C0
jmp 0x2e40e
jmp 0x2e104
movl -0xa4(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq (%rax), %rax
movq -0x60(%rbp), %rcx
movl -0xa4(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %eax
movl -0x7c(%rbp), %ecx
subl -0x9c(%rbp), %ecx
cmpl %ecx, %eax
jae 0x2e14a
movq -0x60(%rbp), %rax
movl -0xa4(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x2e104
jmp 0x2e14c
movl -0xa4(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq (%rax), %rax
movq -0x78(%rbp), %rcx
movl -0xa4(%rbp), %edx
movl (%rcx,%rdx,4), %ecx
movl (%rax,%rcx,4), %eax
cmpl -0x7c(%rbp), %eax
jae 0x2e18a
movq -0x78(%rbp), %rax
movl -0xa4(%rbp), %ecx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
jmp 0x2e14c
movq -0x78(%rbp), %rax
movl -0xa4(%rbp), %ecx
movl (%rax,%rcx,4), %eax
subl $0x1, %eax
movl %eax, -0xa0(%rbp)
movl -0xa0(%rbp), %ecx
movq -0x60(%rbp), %rdx
movl -0xa4(%rbp), %eax
movl %eax, %esi
xorl %eax, %eax
cmpl (%rdx,%rsi,4), %ecx
movb %al, -0xcd(%rbp)
jb 0x2e1db
movl -0xa0(%rbp), %eax
movq -0x78(%rbp), %rcx
movl -0xa4(%rbp), %edx
cmpl (%rcx,%rdx,4), %eax
setb %al
movb %al, -0xcd(%rbp)
movb -0xcd(%rbp), %al
testb $0x1, %al
jne 0x2e1ea
jmp 0x2e339
movl -0xa4(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq (%rax), %rax
movl -0xa0(%rbp), %ecx
movl (%rax,%rcx,4), %eax
movl %eax, -0xa8(%rbp)
movl -0x7c(%rbp), %eax
subl -0xa8(%rbp), %eax
movl %eax, -0xac(%rbp)
movl $0x0, -0xb0(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0xb4(%rbp)
movl -0x7c(%rbp), %eax
movl %eax, -0xb8(%rbp)
movl -0xb8(%rbp), %eax
movl %eax, %ecx
xorl %eax, %eax
cmpq -0x20(%rbp), %rcx
movb %al, -0xce(%rbp)
jae 0x2e28e
movq -0x18(%rbp), %rax
movl -0xb4(%rbp), %ecx
movzbl (%rax,%rcx), %ecx
movq -0x18(%rbp), %rax
movl -0xb8(%rbp), %edx
movzbl (%rax,%rdx), %edx
xorl %eax, %eax
cmpl %edx, %ecx
movb %al, -0xce(%rbp)
jne 0x2e28e
movl -0xb0(%rbp), %eax
cmpq $0x102, %rax            # imm = 0x102
setb %al
movb %al, -0xce(%rbp)
movb -0xce(%rbp), %al
testb $0x1, %al
jne 0x2e29a
jmp 0x2e2eb
movl -0xa8(%rbp), %eax
cmpl -0x7c(%rbp), %eax
jb 0x2e2b9
movl -0xac(%rbp), %ecx
movl -0xa8(%rbp), %eax
subl %ecx, %eax
movl %eax, -0xa8(%rbp)
movl -0xb0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb0(%rbp)
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
movl -0xb8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb8(%rbp)
jmp 0x2e23b
movl -0xb0(%rbp), %eax
cmpl -0x94(%rbp), %eax
jbe 0x2e323
movl -0xb0(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0xac(%rbp), %eax
movl %eax, -0x98(%rbp)
movl -0xb0(%rbp), %eax
cmpq $0x102, %rax            # imm = 0x102
jne 0x2e321
jmp 0x2e339
jmp 0x2e323
jmp 0x2e325
movl -0xa0(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0xa0(%rbp)
jmp 0x2e1a0
cmpl $0x3, -0x94(%rbp)
jae 0x2e36f
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rax
movl -0x7c(%rbp), %ecx
movzbl (%rax,%rcx), %esi
callq 0x2e870
cmpl $0x0, %eax
jne 0x2e36a
movl $0x26c1, -0x84(%rbp)    # imm = 0x26C1
jmp 0x2e40e
jmp 0x2e3fe
movq -0x10(%rbp), %rdi
movl -0x94(%rbp), %eax
movl %eax, %esi
movl -0x98(%rbp), %eax
movl %eax, %edx
callq 0x2e8d0
movl $0x0, -0xbc(%rbp)
movl -0xbc(%rbp), %eax
movl -0x94(%rbp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jae 0x2e3fc
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
movl -0x7c(%rbp), %eax
movl %eax, %edx
callq 0x2e7d0
movl %eax, %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq %rax, %rdi
movl -0x7c(%rbp), %esi
callq 0x2e870
cmpl $0x0, %eax
jne 0x2e3e9
movl $0x26c2, -0x84(%rbp)    # imm = 0x26C2
jmp 0x2e3fc
jmp 0x2e3eb
movl -0xbc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xbc(%rbp)
jmp 0x2e392
jmp 0x2e3fe
jmp 0x2e400
movl -0x7c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x7c(%rbp)
jmp 0x2e071
jmp 0x2e410
movl $0x0, -0x80(%rbp)
movl -0x80(%rbp), %eax
cmpq -0x40(%rbp), %rax
jae 0x2e44c
movl -0x80(%rbp), %eax
movl %eax, %esi
leaq -0x48(%rbp), %rdi
callq 0x2e7a0
movq %rax, -0xc8(%rbp)
movq -0xc8(%rbp), %rdi
callq 0x2d9b0
movl -0x80(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x80(%rbp)
jmp 0x2e417
leaq -0x48(%rbp), %rdi
callq 0x2e990
leaq -0x60(%rbp), %rdi
callq 0x2d9b0
leaq -0x78(%rbp), %rdi
callq 0x2d9b0
movl -0x84(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
nopl (%rax)