// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rdx, %r12
movq %rsi, 0x28(%rsp)
movq %rdi, %rbp
movq %rcx, %rax
subq %rdx, %rax
leaq 0x2(,%rax,2), %rdi
callq 0x1130
xorl %ecx, %ecx
movq %rcx, 0x48(%rsp)
movq %rcx, 0xa8(%rsp)
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x7b95
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %r13
xorl %ebx, %ebx
movl $0x1400, %edx           # imm = 0x1400
movq %r13, %rdi
xorl %esi, %esi
callq 0x10c0
leaq 0x90(%rsp), %r14
movq %r14, %rdi
callq 0x6824
movq %rbp, %rdi
movq 0x28(%rsp), %rsi
movq %r12, %rdx
movq %r15, %rcx
movq %r14, %r8
callq 0x7215
movq %r14, %rdi
movq %r13, %rsi
callq 0x7b9f
movq (%rbp), %rax
cmpl $0x0, 0x4(%rax)
jle 0x7b64
movq %rbp, %r14
movq %r12, 0x70(%rsp)
movq %r15, 0x78(%rsp)
movl $0x900, %eax            # imm = 0x900
leaq 0x28b8(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movq %rcx, 0x20(%rsp)
leaq (%r13,%rax), %r12
movl $0xa00, %ecx            # imm = 0xA00
leaq (%rdx,%rcx), %rsi
movq %rsi, 0x18(%rsp)
leaq (%r13,%rcx), %r15
movl $0x1300, %esi           # imm = 0x1300
addq %rsi, %rdx
movq %rdx, 0x10(%rsp)
leaq (%r13,%rsi), %rbp
leaq 0x14b8(%rsp), %rdx
addq %rdx, %rax
movq %rax, 0x60(%rsp)
leaq (%rdx,%rcx), %rax
movq %rax, 0x58(%rsp)
addq %rdx, %rsi
movq %rsi, 0x68(%rsp)
movsd 0x2918(%rip), %xmm0     # 0xa008
movsd %xmm0, 0x38(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movq %r14, 0x40(%rsp)
movq %rbx, 0x88(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq %r12, %rbx
movq %rbp, %r12
movq %r13, %rbp
movq %r15, %r13
leaq 0x90(%rsp), %r15
movq %r15, %rdi
callq 0x6833
movq %r15, %rdi
callq 0x6824
movq %r14, %rdi
movq %r15, %r14
movq %r13, %r15
movq %rbp, %r13
movq %r12, %rbp
movq %rbx, %r12
movq 0x28(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rcx
leaq 0x48(%rsp), %r8
leaq 0xa8(%rsp), %r9
pushq %r14
pushq %r13
leaq 0xf43(%rip), %rax       # 0x86c4
pushq %rax
pushq 0x48(%rsp)
callq 0x7c48
addq $0x20, %rsp
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rcx
xorl %edx, %edx
movl $0x2, %r8d
callq 0x2468
movapd %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
movsd %xmm1, 0x8(%rsp)
ucomisd %xmm1, %xmm0
jbe 0x782e
movq %r14, %rdi
movq 0x50(%rsp), %rsi
callq 0x6849
movl $0x900, %edx            # imm = 0x900
leaq 0x28b8(%rsp), %rdi
movq %r13, %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq 0x20(%rsp), %rdi
movq %r12, %rsi
callq 0x1120
movl $0x900, %edx            # imm = 0x900
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq 0x10(%rsp), %rdi
movq %rbp, %rsi
callq 0x1120
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl $0x900, %edx            # imm = 0x900
leaq 0x14b8(%rsp), %rdi
movq %r13, %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq 0x60(%rsp), %rdi
movq %r12, %rsi
callq 0x1120
movl $0x900, %edx            # imm = 0x900
movq 0x58(%rsp), %rdi
movq %r15, %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x1120
movl $0xa00, %edx            # imm = 0xA00
movq %r13, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r14, %rdi
movq %r13, %rsi
callq 0x7b9f
cmpl $-0x1, 0x4(%rsp)
movq 0x88(%rsp), %rbx
je 0x7a80
xorl %eax, %eax
movdqa 0x3540(%rip), %xmm3     # 0xadf0
movdqa 0x3548(%rip), %xmm4     # 0xae00
movdqa 0x3550(%rip), %xmm5     # 0xae10
movapd 0x3558(%rip), %xmm6     # 0xae20
movapd 0x3560(%rip), %xmm7     # 0xae30
movsd 0x3507(%rip), %xmm8     # 0xade0
movdqu 0xb8(%rsp,%rax,8), %xmm1
movdqa %xmm1, %xmm0
pand %xmm3, %xmm0
por %xmm4, %xmm0
psrlq $0x20, %xmm1
por %xmm5, %xmm1
subpd %xmm6, %xmm1
addpd %xmm0, %xmm1
movdqu 0x14b8(%rsp,%rax,8), %xmm0
movdqa %xmm0, %xmm2
pand %xmm3, %xmm2
por %xmm4, %xmm2
psrlq $0x20, %xmm0
por %xmm5, %xmm0
subpd %xmm6, %xmm0
addpd %xmm2, %xmm0
mulpd %xmm7, %xmm0
addpd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
movapd %xmm0, %xmm1
subsd %xmm8, %xmm1
cvttsd2si %xmm1, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd %xmm8, %xmm0
cvttsd2si %xmm0, %rsi
sarq $0x3f, %rdx
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, %xmm0
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqu %xmm1, 0xb8(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x120, %rax            # imm = 0x120
jne 0x78d9
movl $0x120, %eax            # imm = 0x120
movdqu 0xb8(%rsp,%rax,8), %xmm1
movdqa %xmm1, %xmm0
pand %xmm3, %xmm0
por %xmm4, %xmm0
psrlq $0x20, %xmm1
por %xmm5, %xmm1
subpd %xmm6, %xmm1
addpd %xmm0, %xmm1
movdqu 0x14b8(%rsp,%rax,8), %xmm0
movdqa %xmm0, %xmm2
pand %xmm3, %xmm2
por %xmm4, %xmm2
psrlq $0x20, %xmm0
por %xmm5, %xmm0
subpd %xmm6, %xmm0
addpd %xmm2, %xmm0
mulpd %xmm7, %xmm0
addpd %xmm1, %xmm0
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
sarq $0x3f, %rdx
movapd %xmm0, %xmm1
subsd %xmm8, %xmm1
cvttsd2si %xmm1, %rsi
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
cvttsd2si %xmm0, %rcx
movq %rcx, %rdx
subsd %xmm8, %xmm0
cvttsd2si %xmm0, %rsi
sarq $0x3f, %rdx
andq %rdx, %rsi
orq %rcx, %rsi
movq %rsi, %xmm0
punpcklqdq %xmm0, %xmm1    # xmm1 = xmm1[0],xmm0[0]
movdqu %xmm1, 0xb8(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x140, %rax            # imm = 0x140
jne 0x7999
movq $0x1, 0x8b8(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %r13, %rdi
movq %r15, %rdx
callq 0x8a09
movl $0x20, %esi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x8a09
cmpl $0x6, %ebx
movq 0x40(%rsp), %r14
jb 0x7b51
movsd 0x8(%rsp), %xmm0
ucomisd 0x80(%rsp), %xmm0
jne 0x7b51
jp 0x7b51
movl $0x900, %edx            # imm = 0x900
movq %r13, %rdi
leaq 0x28b8(%rsp), %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq %r12, %rdi
movq 0x20(%rsp), %rsi
callq 0x1120
movl $0x900, %edx            # imm = 0x900
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x1120
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
movq 0x10(%rsp), %rsi
callq 0x1120
leaq 0xb0(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x8843
movq %r14, %rdi
movq %r12, %rsi
movl $0x20, %edx
callq 0x8843
movq $0x1, 0x8b8(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %r13, %rdi
movq %r15, %rdx
callq 0x8a09
movl $0x20, %esi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x8a09
movq 0x40(%rsp), %r14
movl %ebx, 0x4(%rsp)
incl %ebx
movq (%r14), %rax
cmpl 0x4(%rax), %ebx
jl 0x770d
movq 0x48(%rsp), %rbx
movq 0x30(%rsp), %rdi
callq 0x1030
movq %rbx, %rdi
callq 0x1030
leaq 0x90(%rsp), %rdi
callq 0x6833
addq $0x3cb8, %rsp           # imm = 0x3CB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1180