// File: /workspace/github_c_makefile_star2_O2/imaya[P]zopfli-for-emscripten/squeeze.c
// Function: ZopfliLZ77Optimal
void ZopfliLZ77Optimal(ZopfliBlockState *s,
                       const unsigned char* in, size_t instart, size_t inend,
                       ZopfliLZ77Store* store) {
  /* Dist to get to here with smallest cost. */
  size_t blocksize = inend - instart;
  unsigned short* length_array =
      (unsigned short*)malloc(sizeof(unsigned short) * (blocksize + 1));
  unsigned short* path = 0;
  size_t pathsize = 0;
  ZopfliLZ77Store currentstore;
  SymbolStats stats, beststats, laststats;
  int i;
  double cost;
  double bestcost = ZOPFLI_LARGE_FLOAT;
  double lastcost = 0;
  /* Try randomizing the costs a bit once the size stabilizes. */
  RanState ran_state;
  int lastrandomstep = -1;

  if (!length_array) exit(-1); /* Allocation failed. */

  InitRanState(&ran_state);
  InitStats(&stats);
  ZopfliInitLZ77Store(&currentstore);

  /* Do regular deflate, then loop multiple shortest path runs, each time using
  the statistics of the previous run. */

  /* Initial run. */
  ZopfliLZ77Greedy(s, in, instart, inend, &currentstore);
  GetStatistics(&currentstore, &stats);

  /* Repeat statistics with each time the cost model from the previous stat
  run. */
  for (i = 0; i < s->options->numiterations; i++) {
    ZopfliCleanLZ77Store(&currentstore);
    ZopfliInitLZ77Store(&currentstore);
    LZ77OptimalRun(s, in, instart, inend, &path, &pathsize,
                   length_array, GetCostStat, (void*)&stats,
                   &currentstore);
    cost = ZopfliCalculateBlockSize(currentstore.litlens, currentstore.dists,
                                    0, currentstore.size, 2);
    if (cost < bestcost) {
      /* Copy to the output store. */
      ZopfliCopyLZ77Store(&currentstore, store);
      CopyStats(&stats, &beststats);
      bestcost = cost;
    }
    CopyStats(&stats, &laststats);
    ClearStatFreqs(&stats);
    GetStatistics(&currentstore, &stats);
    if (lastrandomstep != -1) {
      /* This makes it converge slower but better. Do it only once the
      randomness kicks in so that if the user does few iterations, it gives a
      better result sooner. */
      AddWeighedStatFreqs(&stats, 1.0, &laststats, 0.5, &stats);
      CalculateStatistics(&stats);
    }
    if (i > 5 && cost == lastcost) {
      CopyStats(&beststats, &stats);
      RandomizeStatFreqs(&ran_state, &stats);
      CalculateStatistics(&stats);
      lastrandomstep = i;
    }
    lastcost = cost;
  }

  free(length_array);
  free(path);
  ZopfliCleanLZ77Store(&currentstore);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3cb8, %rsp           # imm = 0x3CB8
movq %r8, 0x50(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %rbp
movq %rdi, (%rsp)
movq %rcx, %rax
subq %rdx, %rax
leaq 0x2(,%rax,2), %rdi
callq 0x1120
andq $0x0, 0x48(%rsp)
andq $0x0, 0x90(%rsp)
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x55ae
movabsq $0x200000001, %rax      # imm = 0x200000001
leaq 0x98(%rsp), %rcx
movq %rax, (%rcx)
leaq 0xb8(%rsp), %rbx
xorl %r13d, %r13d
movl $0x1400, %edx           # imm = 0x1400
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10c0
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0x439c
movq (%rsp), %rdi
movq %rbp, 0x70(%rsp)
movq %rbp, %rsi
movq %r15, 0x78(%rsp)
movq %r15, %rdx
movq %r14, 0x80(%rsp)
movq %r14, %rcx
movq %r12, %r8
callq 0x4d4c
movq %r12, %rdi
movq %rbx, %rsi
callq 0x55b6
movl $0x900, %eax            # imm = 0x900
leaq 0x28b8(%rsp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x30(%rsp)
leaq (%rbx,%rax), %r15
movl $0xa00, %edx            # imm = 0xA00
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x28(%rsp)
leaq (%rbx,%rdx), %rsi
movq %rsi, 0x18(%rsp)
movl $0x1300, %esi           # imm = 0x1300
addq %rsi, %rcx
movq %rcx, 0x20(%rsp)
leaq (%rbx,%rsi), %rcx
movq %rcx, 0x10(%rsp)
leaq 0x14b8(%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x58(%rsp)
addq %rcx, %rdx
movq %rdx, 0x68(%rsp)
addq %rcx, %rsi
movq %rsi, 0x60(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0xc(%rsp)
xorpd %xmm1, %xmm1
movsd 0x1e0e(%rip), %xmm0     # 0x7008
movsd %xmm0, 0x40(%rsp)
movq (%rsp), %r14
movq (%r14), %rax
cmpl 0x4(%rax), %r13d
jge 0x557b
movsd %xmm1, 0x88(%rsp)
movq %r12, %rdi
callq 0x43a8
movq %r12, %rdi
callq 0x439c
movq %r14, %rbp
movq %r14, %rdi
movq 0x70(%rsp), %rsi
movq 0x78(%rsp), %rdx
movq 0x80(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0x90(%rsp), %r9
pushq %r12
pushq %rbx
leaq 0xca8(%rip), %rax       # 0x5f01
pushq %rax
pushq 0x50(%rsp)
callq 0x5633
addq $0x20, %rsp
movq 0xa0(%rsp), %rdi
movq 0xa8(%rsp), %rsi
movq 0xb0(%rsp), %rcx
xorl %edx, %edx
pushq $0x2
popq %r8
callq 0x1ccc
movapd %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
movsd %xmm1, (%rsp)
ucomisd %xmm1, %xmm0
jbe 0x5306
movq %r12, %rdi
movq 0x50(%rsp), %rsi
callq 0x43be
movl $0x900, %edx            # imm = 0x900
leaq 0x28b8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x1110
movl $0x900, %edx            # imm = 0x900
movq 0x28(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1110
movsd (%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl $0x900, %edx            # imm = 0x900
leaq 0x14b8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x1110
movl $0x900, %edx            # imm = 0x900
movq 0x68(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x60(%rsp), %rdi
movq 0x10(%rsp), %rsi
callq 0x1110
xorl %eax, %eax
cmpq $0x120, %rax            # imm = 0x120
je 0x536d
andq $0x0, 0xb8(%rsp,%rax,8)
incq %rax
jmp 0x5357
movl $0x120, %eax            # imm = 0x120
cmpq $0x140, %rax            # imm = 0x140
je 0x5388
andq $0x0, 0xb8(%rsp,%rax,8)
incq %rax
jmp 0x5372
movq %r12, %rdi
movq %rbx, %rsi
callq 0x55b6
cmpl $-0x1, 0xc(%rsp)
je 0x54b9
xorl %eax, %eax
movsd 0x1f08(%rip), %xmm3     # 0x72b0
movapd 0x1f10(%rip), %xmm4     # 0x72c0
movsd 0x2920(%rip), %xmm5     # 0x7cd8
movsd 0x2920(%rip), %xmm6     # 0x7ce0
cmpq $0x120, %rax            # imm = 0x120
je 0x5430
movsd 0xb8(%rsp,%rax,8), %xmm0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x14b8(%rsp,%rax,8), %xmm0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd %xmm6, %xmm2
cvttsd2si %xmm2, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, 0xb8(%rsp,%rax,8)
incq %rax
jmp 0x53c0
movl $0x120, %eax            # imm = 0x120
cmpq $0x140, %rax            # imm = 0x140
je 0x54a5
movsd 0xb8(%rsp,%rax,8), %xmm0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
movsd 0x14b8(%rsp,%rax,8), %xmm0
unpcklps %xmm3, %xmm0            # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
subpd %xmm4, %xmm0
movapd %xmm0, %xmm2
unpckhpd %xmm0, %xmm2            # xmm2 = xmm2[1],xmm0[1]
addsd %xmm0, %xmm2
mulsd %xmm5, %xmm2
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
subsd %xmm6, %xmm2
cvttsd2si %xmm2, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, 0xb8(%rsp,%rax,8)
incq %rax
jmp 0x5435
movq $0x1, 0x8b8(%rsp)
movq %rbx, %rdi
callq 0x5f78
cmpl $0x6, %r13d
jb 0x556b
movsd (%rsp), %xmm0
ucomisd 0x88(%rsp), %xmm0
jne 0x556b
jp 0x556b
movl $0x900, %edx            # imm = 0x900
movq %rbx, %rdi
leaq 0x28b8(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movq 0x30(%rsp), %rsi
callq 0x1110
movl $0x900, %edx            # imm = 0x900
movq 0x18(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x10(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x1110
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x60ad
movq %r14, %rdi
movq %r15, %rsi
pushq $0x20
popq %rdx
callq 0x60ad
movq $0x1, 0x8b8(%rsp)
movq %rbx, %rdi
callq 0x5f78
movl %r13d, 0xc(%rsp)
incl %r13d
movsd (%rsp), %xmm1
movq %rbp, %r14
jmp 0x5204
movq 0x38(%rsp), %rdi
callq 0x1030
movq 0x48(%rsp), %rdi
callq 0x1030
leaq 0xa0(%rsp), %rdi
callq 0x43a8
addq $0x3cb8, %rsp           # imm = 0x3CB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rdi
callq 0x1170