// File: /workspace/github_c_makefile_star2_O0/madler[P]pigz/zopfli/src/zopfli/squeeze.c
// Function: ZopfliLZ77Optimal
void ZopfliLZ77Optimal(ZopfliBlockState *s,
                       const unsigned char* in, size_t instart, size_t inend,
                       int numiterations,
                       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;
  ZopfliHash hash;
  ZopfliHash* h = &hash;
  SymbolStats stats, beststats, laststats;
  int i;
  float* costs = (float*)malloc(sizeof(float) * (blocksize + 1));
  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 (!costs) exit(-1); /* Allocation failed. */
  if (!length_array) exit(-1); /* Allocation failed. */

  InitRanState(&ran_state);
  InitStats(&stats);
  ZopfliInitLZ77Store(in, &currentstore);
  ZopfliAllocHash(ZOPFLI_WINDOW_SIZE, h);

  /* 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, h);
  GetStatistics(&currentstore, &stats);

  /* Repeat statistics with each time the cost model from the previous stat
  run. */
  for (i = 0; i < numiterations; i++) {
    ZopfliCleanLZ77Store(&currentstore);
    ZopfliInitLZ77Store(in, &currentstore);
    LZ77OptimalRun(s, in, instart, inend, &path, &pathsize,
                   length_array, GetCostStat, (void*)&stats,
                   &currentstore, h, costs);
    cost = ZopfliCalculateBlockSize(&currentstore, 0, currentstore.size, 2);
    if (s->options->verbose_more || (s->options->verbose && cost < bestcost)) {
      fprintf(stderr, "Iteration %d: %d bit\n", i, (int) cost);
    }
    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);
  free(costs);
  ZopfliCleanLZ77Store(&currentstore);
  ZopfliCleanHash(h);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x3d58, %rsp           # imm = 0x3D58
movq %rdi, -0x20(%rbp)
movq %rsi, -0x28(%rbp)
movq %rdx, -0x30(%rbp)
movq %rcx, -0x38(%rbp)
movl %r8d, -0x3c(%rbp)
movq %r9, -0x48(%rbp)
movq -0x38(%rbp), %rax
subq -0x30(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
addq $0x1, %rdi
shlq %rdi
callq 0x33d0
movq %rax, -0x58(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x68(%rbp)
leaq -0xf8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq -0x50(%rbp), %rdi
addq $0x1, %rdi
shlq $0x2, %rdi
callq 0x33d0
movq %rax, -0x3d10(%rbp)
movsd 0x482a(%rip), %xmm0     # 0x1e150
movsd %xmm0, -0x3d20(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0x3d28(%rbp)
movl $0xffffffff, -0x3d34(%rbp) # imm = 0xFFFFFFFF
cmpq $0x0, -0x3d10(%rbp)
jne 0x19957
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3540
cmpq $0x0, -0x58(%rbp)
jne 0x19968
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3540
leaq -0x3d30(%rbp), %rdi
callq 0x19c60
leaq -0x1500(%rbp), %rdi
callq 0x19c80
movq -0x28(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x16b40
movq -0x100(%rbp), %rsi
movl $0x8000, %edi           # imm = 0x8000
callq 0x19290
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x100(%rbp), %r9
leaq -0xb0(%rbp), %r8
callq 0x189b0
leaq -0xb0(%rbp), %rdi
leaq -0x1500(%rbp), %rsi
callq 0x19cf0
movl $0x0, -0x3d04(%rbp)
movl -0x3d04(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jge 0x19c1b
leaq -0xb0(%rbp), %rdi
callq 0x16bc0
movq -0x28(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x16b40
movq -0x20(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x30(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x58(%rbp), %r15
movq -0x100(%rbp), %r10
movq -0x3d10(%rbp), %rax
leaq -0x60(%rbp), %r8
leaq -0x68(%rbp), %r9
leaq 0x4d3(%rip), %r14       # 0x19f10
leaq -0x1500(%rbp), %rbx
leaq -0xb0(%rbp), %r11
movq %r15, (%rsp)
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movq %r11, 0x18(%rsp)
movq %r10, 0x20(%rsp)
movq %rax, 0x28(%rsp)
callq 0x19df0
movq -0xa0(%rbp), %rdx
leaq -0xb0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %ecx
callq 0x12860
movsd %xmm0, -0x3d18(%rbp)
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpl $0x0, 0x4(%rax)
jne 0x19ac0
movq -0x20(%rbp), %rax
movq (%rax), %rax
cmpl $0x0, (%rax)
je 0x19ae6
movsd -0x3d18(%rbp), %xmm1
movsd -0x3d20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19ae6
movq 0x9531(%rip), %rax      # 0x22ff8
movq (%rax), %rdi
movl -0x3d04(%rbp), %edx
cvttsd2si -0x3d18(%rbp), %ecx
leaq 0x4f4d(%rip), %rsi      # 0x1ea2c
movb $0x0, %al
callq 0x3340
movsd -0x3d18(%rbp), %xmm1
movsd -0x3d20(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x19b2f
movq -0x48(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
callq 0x16c30
leaq -0x1500(%rbp), %rdi
leaq -0x2900(%rbp), %rsi
callq 0x19fb0
movsd -0x3d18(%rbp), %xmm0
movsd %xmm0, -0x3d20(%rbp)
leaq -0x1500(%rbp), %rdi
leaq -0x3d00(%rbp), %rsi
callq 0x19fb0
leaq -0x1500(%rbp), %rdi
callq 0x1a040
leaq -0xb0(%rbp), %rdi
leaq -0x1500(%rbp), %rsi
callq 0x19cf0
cmpl $-0x1, -0x3d34(%rbp)
je 0x19b9c
leaq -0x1500(%rbp), %rdx
movsd 0x4e87(%rip), %xmm0     # 0x1ea00
leaq -0x3d00(%rbp), %rsi
movsd 0x4e80(%rip), %xmm1     # 0x1ea08
movq %rdx, %rdi
callq 0x1a0b0
leaq -0x1500(%rbp), %rdi
callq 0x1a260
cmpl $0x5, -0x3d04(%rbp)
jle 0x19bf7
movsd -0x3d18(%rbp), %xmm0
ucomisd -0x3d28(%rbp), %xmm0
jne 0x19bf7
jp 0x19bf7
leaq -0x2900(%rbp), %rdi
leaq -0x1500(%rbp), %rsi
callq 0x19fb0
leaq -0x3d30(%rbp), %rdi
leaq -0x1500(%rbp), %rsi
callq 0x1a2b0
leaq -0x1500(%rbp), %rdi
callq 0x1a260
movl -0x3d04(%rbp), %eax
movl %eax, -0x3d34(%rbp)
movsd -0x3d18(%rbp), %xmm0
movsd %xmm0, -0x3d28(%rbp)
movl -0x3d04(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3d04(%rbp)
jmp 0x199e1
movq -0x58(%rbp), %rdi
callq 0x3050
movq -0x60(%rbp), %rdi
callq 0x3050
movq -0x3d10(%rbp), %rdi
callq 0x3050
leaq -0xb0(%rbp), %rdi
callq 0x16bc0
movq -0x100(%rbp), %rdi
callq 0x194a0
addq $0x3d58, %rsp           # imm = 0x3D58
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop