// File: /workspace/github_cpp_makefile_star2_O1/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 $0x48, %rsp
movl %ecx, -0x58(%rbp)
movq %rdx, %r13
movq %rsi, %r14
movq %rdi, -0x38(%rbp)
movl $0x300000, %edi         # imm = 0x300000
callq 0x5420
testq %rax, %rax
je 0x23b4f
movq %rax, %r12
xorl %ebx, %ebx
movl $0x180000, %edx         # imm = 0x180000
movq %rax, %rdi
xorl %esi, %esi
callq 0x5170
movl $0x80000, %edi          # imm = 0x80000
callq 0x5420
testq %rax, %rax
movq %rax, -0x68(%rbp)
je 0x23b59
movl $0x40000, %edx          # imm = 0x40000
movq %rax, %rdi
xorl %esi, %esi
callq 0x5170
jmp 0x23b5e
movl $0x26bd, %eax           # imm = 0x26BD
jmp 0x23be5
movl $0x26be, %ebx           # imm = 0x26BE
movl $0x80000, %edi          # imm = 0x80000
callq 0x5420
testq %rax, %rax
movq %rax, -0x60(%rbp)
je 0x23b82
movl $0x40000, %edx          # imm = 0x40000
movq %rax, %rdi
xorl %esi, %esi
callq 0x5170
jmp 0x23b87
movl $0x26bf, %ebx           # imm = 0x26BF
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
je 0x23bf4
testq %r12, %r12
je 0x23bc8
xorl %ebx, %ebx
movq %r12, %r14
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r14)
movq (%r14), %rdi
callq 0x5390
movq $0x0, (%r14)
testq %r12, %r12
je 0x23bc8
leaq 0x1(%rbx), %rax
addq $0x18, %r14
cmpq $0xffff, %rbx           # imm = 0xFFFF
movq %rax, %rbx
jb 0x23b98
movq %r12, %rdi
callq 0x5390
movq -0x68(%rbp), %rdi
callq 0x5390
movq -0x60(%rbp), %rdi
callq 0x5390
movl -0x2c(%rbp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r13, %r13
je 0x23faf
xorl %edx, %edx
xorl %r15d, %r15d
movl $0x0, -0x2c(%rbp)
movq %r12, -0x40(%rbp)
leaq 0x6(%rdx), %rcx
movq %r13, %rax
subq %rdx, %rax
cmpq %r13, %rcx
movl $0x6, %ecx
cmovbq %rcx, %rax
testq %rax, %rax
movq %rdx, -0x48(%rbp)
je 0x23c50
addq %r14, %rdx
xorl %ecx, %ecx
xorl %edi, %edi
xorl %esi, %esi
movzbl (%rdx,%rdi), %r8d
shll %cl, %r8d
xorl %r8d, %esi
incq %rdi
addl $0x2, %ecx
cmpq %rax, %rdi
jb 0x23c35
movzwl %si, %ebx
jmp 0x23c52
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %rax
movq %r12, %rcx
leaq (%r12,%rax,8), %r12
movq 0x8(%rcx,%rax,8), %rsi
incq %rsi
movq %r12, %rdi
callq 0x236c7
testl %eax, %eax
je 0x23d78
movq (%r12), %rax
movq 0x8(%r12), %rcx
movl %r15d, -0x4(%rax,%rcx,4)
movl %r15d, %edx
subl -0x58(%rbp), %edx
movl $0x0, %ecx
cmovbl %ecx, %edx
movq -0x68(%rbp), %rdi
movl (%rdi,%rbx,4), %ecx
movl %ecx, %esi
incl %ecx
cmpl %edx, (%rax,%rsi,4)
jb 0x23c98
decl %ecx
leal (,%rbx,4), %edx
movl %ecx, (%rdi,%rdx)
movq -0x60(%rbp), %rdi
movl (%rdi,%rdx), %edx
movl %edx, %esi
incl %edx
cmpl %r15d, (%rax,%rsi,4)
jb 0x23cb4
decl %edx
movl %edx, (%rdi,%rbx,4)
movl %r15d, %esi
movq %r15, -0x50(%rbp)
leal 0x1(%r15), %ebx
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %edx, %edi
decl %edi
cmpl %ecx, %edi
jb 0x23d3e
cmpl %edx, %edi
jae 0x23d3e
movl %edi, %r8d
movl (%rax,%r8,4), %r9d
xorl %r8d, %r8d
cmpq %r13, %rsi
jae 0x23d18
xorl %r8d, %r8d
movq %rsi, %r10
leal (%r9,%r8), %r11d
movb (%r14,%r11), %r11b
cmpb (%r14,%r10), %r11b
jne 0x23d18
cmpl $0x101, %r8d            # imm = 0x101
ja 0x23d18
leal (%rbx,%r8), %r10d
incl %r8d
cmpq %r13, %r10
jb 0x23cf5
movq -0x50(%rbp), %r10
subl %r9d, %r10d
cmpl $0x102, %r8d            # imm = 0x102
setne %r9b
cmpl %r12d, %r8d
setbe %r11b
cmoval %r8d, %r12d
cmoval %r10d, %r15d
orb %r9b, %r11b
jne 0x23cd6
cmpl $0x3, %r12d
jae 0x23d86
movq -0x48(%rbp), %rax
movzbl (%r14,%rax), %ebx
movq -0x38(%rbp), %r15
movq 0x8(%r15), %rsi
incq %rsi
movq %r15, %rdi
callq 0x236c7
testl %eax, %eax
je 0x23f84
movq (%r15), %rcx
movq 0x8(%r15), %rdx
movl %ebx, -0x4(%rcx,%rdx,4)
jmp 0x23f8b
movl $0x26c0, -0x2c(%rbp)    # imm = 0x26C0
xorl %eax, %eax
jmp 0x23f7e
movl $0x1, %eax
movl $0x1c, %ecx
leaq 0xdb19(%rip), %r8       # 0x318b0
cmpq %rcx, %rax
ja 0x23dd0
leaq (%rcx,%rax), %rsi
shrq %rsi
cmpl %r12d, (%r8,%rsi,4)
jbe 0x23dbb
leaq -0x1(%rsi), %rdi
cmpl %r12d, -0x4(%r8,%rsi,4)
jbe 0x23dc5
movb $0x1, %dl
movq %rdi, %rcx
jmp 0x23dca
incq %rsi
movb $0x1, %dl
movq %rsi, %rax
jmp 0x23dca
xorl %edx, %edx
movq %rdi, %r9
testb %dl, %dl
jne 0x23d97
jmp 0x23dd6
movl $0x1c, %r9d
movl %r9d, %eax
movl %r12d, %ecx
subl (%r8,%rax,4), %ecx
movl %ecx, -0x54(%rbp)
movl $0x1, %eax
movl $0x1d, %ecx
leaq 0xdbbc(%rip), %r8       # 0x319b0
cmpq %rcx, %rax
ja 0x23e2d
leaq (%rcx,%rax), %rsi
shrq %rsi
cmpl %r15d, (%r8,%rsi,4)
jbe 0x23e18
leaq -0x1(%rsi), %rdi
cmpl %r15d, -0x4(%r8,%rsi,4)
jbe 0x23e22
movb $0x1, %dl
movq %rdi, %rcx
jmp 0x23e27
incq %rsi
movb $0x1, %dl
movq %rsi, %rax
jmp 0x23e27
xorl %edx, %edx
movq %rdi, %r10
testb %dl, %dl
jne 0x23df4
jmp 0x23e33
movl $0x1d, %r10d
movq %r9, -0x48(%rbp)
movq %r10, -0x70(%rbp)
movl %r10d, %eax
subl (%r8,%rax,4), %r15d
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0x236c7
testl %eax, %eax
je 0x23e6f
movq -0x48(%rbp), %rdx
addl $0x101, %edx            # imm = 0x101
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl %edx, -0x4(%rax,%rcx,4)
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0x236c7
testl %eax, %eax
je 0x23e95
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl -0x54(%rbp), %edx
movl %edx, -0x4(%rax,%rcx,4)
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0x236c7
testl %eax, %eax
je 0x23ebc
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movq -0x70(%rbp), %rdx
movl %edx, -0x4(%rax,%rcx,4)
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0x236c7
testl %eax, %eax
je 0x23ee0
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl %r15d, -0x4(%rax,%rcx,4)
movq -0x50(%rbp), %r15
leal (%r12,%r15), %eax
decl %eax
movl %eax, -0x48(%rbp)
incl %r15d
movq %r13, %rax
subq %r15, %rax
jbe 0x23f39
leaq 0x6(%r15), %rcx
cmpq %r13, %rcx
movl $0x6, %ecx
cmovbq %rcx, %rax
testq %rax, %rax
je 0x23f39
movl %ebx, %edx
addq %r14, %rdx
xorl %ecx, %ecx
xorl %edi, %edi
xorl %esi, %esi
movzbl (%rdx,%rdi), %r8d
shll %cl, %r8d
xorl %r8d, %esi
incq %rdi
addl $0x2, %ecx
cmpq %rax, %rdi
jb 0x23f18
movzwl %si, %eax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
jmp 0x23f3b
xorl %eax, %eax
movq -0x40(%rbp), %rcx
leaq (%rcx,%rax), %r12
movq 0x8(%rcx,%rax), %rsi
incq %rsi
movq %r12, %rdi
callq 0x236c7
movl %eax, %ecx
movb $0x1, %al
testl %ecx, %ecx
je 0x23f77
movq (%r12), %rcx
movq 0x8(%r12), %rdx
movl %r15d, -0x4(%rcx,%rdx,4)
incl %ebx
cmpl -0x48(%rbp), %r15d
jne 0x23eed
jmp 0x23f7e
movl $0x26c2, -0x2c(%rbp)    # imm = 0x26C2
movq -0x40(%rbp), %r12
jmp 0x23f93
movl $0x26c1, -0x2c(%rbp)    # imm = 0x26C1
movq -0x40(%rbp), %r12
movq -0x50(%rbp), %r15
testb %al, %al
je 0x23b8e
incl %r15d
movq %r15, %rdx
cmpq %r13, %r15
jb 0x23c0d
jmp 0x23b8e
movl $0x0, -0x2c(%rbp)
jmp 0x23b8e