// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3d48, %rsp           # imm = 0x3D48
movq %r9, 0x80(%rsp)
movl %r8d, %r12d
movq %rsi, 0x60(%rsp)
movq %rdi, 0x58(%rsp)
movq %rcx, 0x70(%rsp)
movq %rcx, %rbx
movq %rdx, 0x68(%rsp)
subq %rdx, %rbx
leaq 0x2(,%rbx,2), %rdi
callq 0x33d0
movq %rax, %r14
andq $0x0, 0x78(%rsp)
andq $0x0, 0xa8(%rsp)
leaq 0x4(,%rbx,4), %rdi
callq 0x33d0
movq %rax, 0x50(%rsp)
testq %rax, %rax
je 0x127f3
movq %r14, 0x48(%rsp)
testq %r14, %r14
je 0x127f3
movabsq $0x200000001, %rax      # imm = 0x200000001
leaq 0xb0(%rsp), %rcx
movq %rax, (%rcx)
leaq 0x148(%rsp), %r15
movl $0x1400, %edx           # imm = 0x1400
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
leaq 0x100(%rsp), %rbx
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x109e8
leaq 0xb8(%rsp), %r13
movl $0x8000, %edi           # imm = 0x8000
movq %r13, %rsi
callq 0x11fa8
movq 0x58(%rsp), %rdi
movq %r14, %rsi
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
movq %rbx, %r8
movq %r13, %r9
callq 0x11a93
movq %rbx, %rdi
movq %r15, %rsi
callq 0x127fb
movl $0x900, %eax            # imm = 0x900
leaq 0x2948(%rsp), %rcx
leaq (%rcx,%rax), %rdx
movq %rdx, 0x40(%rsp)
leaq (%r15,%rax), %rbp
movl $0xa00, %edx            # imm = 0xA00
leaq (%rcx,%rdx), %rsi
movq %rsi, 0x38(%rsp)
leaq (%r15,%rdx), %rsi
movq %rsi, 0x20(%rsp)
movl $0x1300, %esi           # imm = 0x1300
addq %rsi, %rcx
movq %rcx, 0x30(%rsp)
leaq (%r15,%rsi), %rcx
movq %rcx, 0x18(%rsp)
leaq 0x1548(%rsp), %rcx
addq %rcx, %rdx
movq %rdx, 0x98(%rsp)
addq %rcx, %rsi
movq %rsi, 0x90(%rsp)
testl %r12d, %r12d
movl $0x0, %edx
cmovlel %edx, %r12d
movl %r12d, 0x2c(%rsp)
leaq (%rcx,%rax), %rax
movq %rax, 0x88(%rsp)
pushq $-0x1
popq %rax
movl %eax, 0x4(%rsp)
xorpd %xmm1, %xmm1
movsd 0x3cb4(%rip), %xmm0     # 0x16088
movsd %xmm0, 0x10(%rsp)
xorl %r12d, %r12d
movq 0x48(%rsp), %r14
cmpl 0x2c(%rsp), %r12d
je 0x127ab
movsd %xmm1, 0xa0(%rsp)
movq %rbx, %rdi
callq 0x10a05
movq 0x60(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x109e8
movq 0x58(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movq 0x48(%rsp), %r14
movq 0x68(%rsp), %rdx
movq 0x70(%rsp), %rcx
leaq 0x78(%rsp), %r8
leaq 0xa8(%rsp), %r9
pushq 0x50(%rsp)
leaq 0xc0(%rsp), %rax
pushq %rax
pushq %rbx
pushq %r15
leaq 0xc9f(%rip), %rax       # 0x130eb
pushq %rax
pushq %r14
callq 0x12878
addq $0x30, %rsp
movq 0x110(%rsp), %rdx
movq %rbx, %rdi
xorl %esi, %esi
pushq $0x2
popq %rcx
callq 0xe0e6
movapd %xmm0, %xmm1
movq (%r13), %rax
cmpl $0x0, 0x4(%rax)
movsd %xmm0, 0x8(%rsp)
jne 0x12492
cmpl $0x0, (%rax)
je 0x124b7
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x124b7
movq 0x9b57(%rip), %rax      # 0x1bff0
movq (%rax), %rdi
cvttsd2si %xmm1, %ecx
leaq 0x4499(%rip), %rsi      # 0x16940
movl %r12d, %edx
xorl %eax, %eax
callq 0x3340
movsd 0x8(%rsp), %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1252e
movq %rbx, %rdi
movq 0x80(%rsp), %rsi
callq 0x10a48
movl $0x900, %edx            # imm = 0x900
leaq 0x2948(%rsp), %rdi
movq %r15, %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq 0x40(%rsp), %rdi
movq %rbp, %rsi
callq 0x3360
movl $0x900, %edx            # imm = 0x900
movq 0x38(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq 0x30(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x3360
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movl $0x900, %edx            # imm = 0x900
leaq 0x1548(%rsp), %rdi
movq %r15, %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x3360
movl $0x900, %edx            # imm = 0x900
movq 0x98(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq 0x90(%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x3360
xorl %eax, %eax
cmpq $0x120, %rax            # imm = 0x120
je 0x1259e
andq $0x0, 0x148(%rsp,%rax,8)
incq %rax
jmp 0x12588
movl $0x120, %eax            # imm = 0x120
cmpq $0x140, %rax            # imm = 0x140
je 0x125b9
andq $0x0, 0x148(%rsp,%rax,8)
incq %rax
jmp 0x125a3
movq %rbx, %rdi
movq %r15, %rsi
callq 0x127fb
cmpl $-0x1, 0x4(%rsp)
je 0x126ea
xorl %eax, %eax
movsd 0x1a47(%rip), %xmm3     # 0x14020
movapd 0x1a4f(%rip), %xmm4     # 0x14030
movsd 0x4327(%rip), %xmm5     # 0x16910
movsd 0x4327(%rip), %xmm6     # 0x16918
cmpq $0x120, %rax            # imm = 0x120
je 0x12661
movsd 0x148(%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 0x1548(%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, 0x148(%rsp,%rax,8)
incq %rax
jmp 0x125f1
movl $0x120, %eax            # imm = 0x120
cmpq $0x140, %rax            # imm = 0x140
je 0x126d6
movsd 0x148(%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 0x1548(%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, 0x148(%rsp,%rax,8)
incq %rax
jmp 0x12666
movq $0x1, 0x948(%rsp)
movq %r15, %rdi
callq 0x1315c
cmpl $0x6, %r12d
movsd 0x8(%rsp), %xmm1
jb 0x127a3
ucomisd 0xa0(%rsp), %xmm1
jne 0x127a3
jp 0x127a3
movl $0x900, %edx            # imm = 0x900
movq %r15, %rdi
leaq 0x2948(%rsp), %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
movq 0x40(%rsp), %rsi
callq 0x3360
movl $0x900, %edx            # imm = 0x900
movq 0x20(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x3360
movl $0x100, %edx            # imm = 0x100
movq 0x18(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x3360
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x132d3
movq %r13, %rdi
movq %rbp, %rsi
pushq $0x20
popq %rdx
callq 0x132d3
movq $0x1, 0x948(%rsp)
movq %r15, %rdi
callq 0x1315c
movsd 0x8(%rsp), %xmm1
movl %r12d, 0x4(%rsp)
incl %r12d
jmp 0x123e2
movq %r14, %rdi
callq 0x3050
movq 0x78(%rsp), %rdi
callq 0x3050
movq 0x50(%rsp), %rdi
callq 0x3050
leaq 0x100(%rsp), %rdi
callq 0x10a05
leaq 0xb8(%rsp), %rdi
callq 0x1209d
addq $0x3d48, %rsp           # imm = 0x3D48
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $-0x1
popq %rdi
callq 0x3540