// File: /workspace/github_cpp_makefile_star2_O3/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, -0x60(%rbp)
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movl $0x300000, %edi         # imm = 0x300000
callq 0x4410
testq %rax, %rax
je 0x2273f
movq %rax, %r13
movq %r14, -0x38(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x180000, %edx         # imm = 0x180000
movq %rax, %rdi
xorl %esi, %esi
callq 0x4170
movl $0x80000, %edi          # imm = 0x80000
callq 0x4410
movq %rax, %r15
testq %rax, %rax
je 0x22749
movl $0x40000, %edx          # imm = 0x40000
movq %r15, %rdi
xorl %esi, %esi
callq 0x4170
jmp 0x22750
movl $0x26bd, %eax           # imm = 0x26BD
jmp 0x22b63
movl $0x26be, -0x2c(%rbp)    # imm = 0x26BE
movl $0x80000, %edi          # imm = 0x80000
callq 0x4410
testq %rax, %rax
movq %rax, -0x68(%rbp)
je 0x22aff
movl $0x40000, %edx          # imm = 0x40000
movq %rax, %rdi
xorl %esi, %esi
callq 0x4170
testq %r15, %r15
je 0x22b1c
movl $0x0, -0x2c(%rbp)
testq %rbx, %rbx
je 0x22b1c
xorl %r9d, %r9d
xorl %r14d, %r14d
movl $0x0, -0x2c(%rbp)
movq %rbx, -0x50(%rbp)
movq %r13, -0x70(%rbp)
movq %r15, -0x40(%rbp)
leaq 0x6(%r9), %rcx
movq %rbx, %rax
subq %r9, %rax
cmpq %rbx, %rcx
movl $0x6, %ecx
cmovbq %rcx, %rax
addq %r12, %r9
testq %rax, %rax
movq %r9, -0x48(%rbp)
je 0x227e9
xorl %ecx, %ecx
xorl %esi, %esi
xorl %edx, %edx
movzbl (%r9,%rsi), %edi
shll %cl, %edi
xorl %edi, %edx
incq %rsi
addl $0x2, %ecx
cmpq %rax, %rsi
jb 0x227d0
movzwl %dx, %ebx
jmp 0x227eb
xorl %ebx, %ebx
leaq (%rbx,%rbx,2), %rax
leaq (,%rax,8), %r15
addq %r13, %r15
movq 0x8(%r13,%rax,8), %rsi
incq %rsi
movq %r15, %rdi
callq 0x22340
testl %eax, %eax
je 0x22b08
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %r14d, -0x4(%rax,%rcx,4)
movl %r14d, %edx
subl -0x60(%rbp), %edx
movl $0x0, %ecx
cmovbl %ecx, %edx
movq -0x40(%rbp), %rdi
movl (%rdi,%rbx,4), %ecx
movl %ecx, %esi
incl %ecx
cmpl %edx, (%rax,%rsi,4)
jb 0x22833
decl %ecx
leal (,%rbx,4), %edx
movl %ecx, (%rdi,%rdx)
movq -0x68(%rbp), %rdi
movl (%rdi,%rdx), %edx
movl %edx, %esi
incl %edx
cmpl %r14d, (%rax,%rsi,4)
jb 0x2284f
decl %edx
movl %edx, (%rdi,%rbx,4)
movl %r14d, %esi
movq %r14, -0x58(%rbp)
leal 0x1(%r14), %ebx
xorl %r15d, %r15d
xorl %r14d, %r14d
movl %edx, %edi
decl %edi
cmpl %ecx, %edi
jb 0x228db
cmpl %edx, %edi
jae 0x228db
movl %edi, %r8d
movl (%rax,%r8,4), %r9d
xorl %r8d, %r8d
cmpq -0x50(%rbp), %rsi
jae 0x228b5
xorl %r8d, %r8d
movq %rsi, %r10
leal (%r9,%r8), %r11d
movb (%r12,%r11), %r11b
cmpb (%r12,%r10), %r11b
jne 0x228b5
cmpl $0x101, %r8d            # imm = 0x101
ja 0x228b5
leal (%rbx,%r8), %r10d
incl %r8d
cmpq -0x50(%rbp), %r10
jb 0x22891
movq -0x58(%rbp), %r10
subl %r9d, %r10d
cmpl $0x102, %r8d            # imm = 0x102
setne %r9b
cmpl %r15d, %r8d
setbe %r11b
cmoval %r8d, %r15d
cmoval %r10d, %r14d
orb %r9b, %r11b
jne 0x22871
cmpl $0x3, %r15d
jae 0x2291f
movq -0x48(%rbp), %rax
movzbl (%rax), %ebx
movq -0x38(%rbp), %r14
movq 0x8(%r14), %rsi
incq %rsi
movq %r14, %rdi
callq 0x22340
testl %eax, %eax
je 0x22b11
movq (%r14), %rax
movq 0x8(%r14), %rcx
movl %ebx, -0x4(%rax,%rcx,4)
movq -0x40(%rbp), %r15
movq -0x50(%rbp), %rbx
movq -0x58(%rbp), %r14
jmp 0x22aee
movl $0x1, %eax
movl $0x1c, %esi
leaq 0xdf90(%rip), %rdx      # 0x308c0
leaq (%rax,%rsi), %rcx
shrq %rcx
cmpl %r15d, (%rdx,%rcx,4)
jbe 0x2294a
leaq -0x1(%rcx), %rsi
cmpl %r15d, -0x4(%rdx,%rcx,4)
ja 0x22950
jmp 0x2295a
incq %rcx
movq %rcx, %rax
cmpq %rsi, %rax
jbe 0x22930
movl $0x1c, %esi
movl %esi, %eax
movl %r15d, %ecx
subl (%rdx,%rax,4), %ecx
movl %ecx, -0x5c(%rbp)
movl $0x1, %eax
movl $0x1d, %r13d
leaq 0xe049(%rip), %rdx      # 0x309c0
leaq (%rax,%r13), %rcx
shrq %rcx
cmpl %r14d, (%rdx,%rcx,4)
jbe 0x22991
leaq -0x1(%rcx), %r13
cmpl %r14d, -0x4(%rdx,%rcx,4)
ja 0x22997
jmp 0x229a2
incq %rcx
movq %rcx, %rax
cmpq %r13, %rax
jbe 0x22977
movl $0x1d, %r13d
movq %rsi, -0x48(%rbp)
movl %r13d, %eax
subl (%rdx,%rax,4), %r14d
movq -0x38(%rbp), %rdi
movq 0x8(%rdi), %rsi
incq %rsi
callq 0x22340
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rsi
testl %eax, %eax
je 0x229de
movq -0x48(%rbp), %rcx
addl $0x101, %ecx            # imm = 0x101
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %ecx, -0x4(%rax,%rsi,4)
incq %rsi
movq -0x38(%rbp), %rdi
callq 0x22340
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rsi
testl %eax, %eax
je 0x22a04
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl -0x5c(%rbp), %ecx
movl %ecx, -0x4(%rax,%rsi,4)
incq %rsi
movq -0x38(%rbp), %rdi
callq 0x22340
movq -0x38(%rbp), %rcx
movq 0x8(%rcx), %rsi
testl %eax, %eax
je 0x22a28
movq -0x38(%rbp), %rax
movq (%rax), %rax
movl %r13d, -0x4(%rax,%rsi,4)
incq %rsi
movq -0x38(%rbp), %rdi
callq 0x22340
testl %eax, %eax
je 0x22a48
movq -0x38(%rbp), %rcx
movq (%rcx), %rax
movq 0x8(%rcx), %rcx
movl %r14d, -0x4(%rax,%rcx,4)
movq -0x58(%rbp), %r14
leal (%r15,%r14), %eax
decl %eax
movl %eax, -0x48(%rbp)
movq -0x70(%rbp), %r13
incl %r14d
movq -0x50(%rbp), %rdx
movq %rdx, %rax
subq %r14, %rax
jbe 0x22aa9
leaq 0x6(%r14), %rcx
cmpq %rdx, %rcx
movl $0x6, %ecx
cmovbq %rcx, %rax
testq %rax, %rax
je 0x22aa9
movl %ebx, %edx
addq %r12, %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 0x22a88
movzwl %si, %eax
shll $0x3, %eax
leal (%rax,%rax,2), %eax
jmp 0x22aab
xorl %eax, %eax
leaq (%rax,%r13), %r15
movq 0x8(%r13,%rax), %rsi
incq %rsi
movq %r15, %rdi
callq 0x22340
testl %eax, %eax
je 0x22adf
movq (%r15), %rax
movq 0x8(%r15), %rcx
movl %r14d, -0x4(%rax,%rcx,4)
incl %ebx
cmpl -0x48(%rbp), %r14d
jne 0x22a59
movl -0x48(%rbp), %eax
movl %eax, %r14d
jmp 0x22ae6
movl $0x26c2, -0x2c(%rbp)    # imm = 0x26C2
movq -0x40(%rbp), %r15
movq -0x50(%rbp), %rbx
incl %r14d
movq %r14, %r9
cmpq %rbx, %r14
jb 0x227a8
jmp 0x22b1c
movl $0x26bf, -0x2c(%rbp)    # imm = 0x26BF
jmp 0x22b1c
movl $0x26c0, -0x2c(%rbp)    # imm = 0x26C0
jmp 0x22b18
movl $0x26c1, -0x2c(%rbp)    # imm = 0x26C1
movq -0x40(%rbp), %r15
xorl %ebx, %ebx
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r13,%rbx)
movq (%r13,%rbx), %rdi
callq 0x4380
movq $0x0, (%r13,%rbx)
addq $0x18, %rbx
cmpq $0x180000, %rbx         # imm = 0x180000
jne 0x22b1e
movq %r13, %rdi
callq 0x4380
movq %r15, %rdi
callq 0x4380
movq -0x68(%rbp), %rdi
callq 0x4380
movl -0x2c(%rbp), %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq