// File: /workspace/github_c_makefile_star2_O3/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 $0x3d38, %rsp           # imm = 0x3D38
movq %r9, 0x70(%rsp)
movl %r8d, 0x8(%rsp)
movq %rsi, 0x50(%rsp)
movq %rdi, 0x40(%rsp)
movq %rcx, 0x60(%rsp)
movq %rcx, %rbx
movq %rdx, 0x58(%rsp)
subq %rdx, %rbx
leaq 0x2(,%rbx,2), %rdi
callq 0x33e0
movq %rax, %r12
xorl %eax, %eax
movq %rax, 0x68(%rsp)
movq %rax, 0x98(%rsp)
leaq 0x4(,%rbx,4), %rdi
callq 0x33e0
movq %rax, 0x48(%rsp)
testq %rax, %rax
je 0x13d20
testq %r12, %r12
je 0x13d20
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0xa0(%rsp)
leaq 0x138(%rsp), %r15
xorl %ebx, %ebx
movl $0x1400, %edx           # imm = 0x1400
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
leaq 0xa8(%rsp), %r14
movq 0x50(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
callq 0x117d8
leaq 0xf0(%rsp), %rbp
movl $0x8000, %edi           # imm = 0x8000
movq %rbp, %rsi
callq 0x133d0
movq 0x40(%rsp), %rdi
movq %r13, %rsi
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
movq %r14, %r8
movq %rbp, %r9
callq 0x12911
movq %r14, %rdi
movq %r15, %rsi
callq 0x13d2a
cmpl $0x0, 0x8(%rsp)
jle 0x13cda
movq %r12, 0x38(%rsp)
movl $0x900, %eax            # imm = 0x900
leaq 0x2938(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movq %rcx, 0x30(%rsp)
leaq (%r15,%rax), %rbx
movl $0xa00, %ecx            # imm = 0xA00
leaq (%rdx,%rcx), %rsi
movq %rsi, 0x28(%rsp)
leaq (%r15,%rcx), %r14
movl $0x1300, %esi           # imm = 0x1300
addq %rsi, %rdx
movq %rdx, 0x20(%rsp)
leaq (%r15,%rsi), %rbp
leaq 0x1538(%rsp), %rdx
addq %rdx, %rax
movq %rax, 0x80(%rsp)
leaq (%rdx,%rcx), %rax
movq %rax, 0x78(%rsp)
addq %rdx, %rsi
movq %rsi, 0x88(%rsp)
movsd 0x3868(%rip), %xmm0     # 0x17088
movsd %xmm0, 0x18(%rsp)
xorpd %xmm0, %xmm0
movl $0xffffffff, 0xc(%rsp)  # imm = 0xFFFFFFFF
xorl %eax, %eax
leaq 0xa8(%rsp), %r12
movl %eax, 0x4(%rsp)
movq %rbx, %r13
movq %rbp, %rbx
movq %r14, %rbp
movsd %xmm0, 0x90(%rsp)
movq %r12, %rdi
callq 0x117f5
movq %r15, %r12
movq 0x50(%rsp), %r15
movq %r15, %rdi
leaq 0xa8(%rsp), %rsi
callq 0x117d8
movq 0x40(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %r15
leaq 0xa8(%rsp), %r12
movq 0x58(%rsp), %rdx
movq 0x60(%rsp), %rcx
leaq 0x68(%rsp), %r8
leaq 0x98(%rsp), %r9
pushq 0x48(%rsp)
leaq 0xf8(%rsp), %rax
pushq %rax
pushq %r12
pushq %r15
leaq 0xf42(%rip), %rax       # 0x147f9
pushq %rax
pushq 0x60(%rsp)
callq 0x13dd3
addq $0x30, %rsp
movq 0xb8(%rsp), %rdx
movq %r12, %rdi
xorl %esi, %esi
movl $0x2, %ecx
callq 0xed1e
movapd %xmm0, %xmm1
movq (%r14), %rax
cmpl $0x0, 0x4(%rax)
movsd %xmm0, 0x10(%rsp)
jne 0x13900
cmpl $0x0, (%rax)
je 0x13926
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x13926
movq 0x96e9(%rip), %rax      # 0x1cff0
movq (%rax), %rdi
cvttsd2si %xmm1, %ecx
leaq 0x40db(%rip), %rsi      # 0x179f0
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x3350
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
ucomisd %xmm1, %xmm0
movq %rbp, %r14
movq %rbx, %rbp
movq %r13, %rbx
jbe 0x1399f
movq %r12, %rdi
movq 0x70(%rsp), %rsi
callq 0x11838
movl $0x900, %edx            # imm = 0x900
leaq 0x2938(%rsp), %rdi
movq %r15, %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
callq 0x3370
movl $0x900, %edx            # imm = 0x900
movq 0x28(%rsp), %rdi
movq %r14, %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq 0x20(%rsp), %rdi
movq %rbp, %rsi
callq 0x3370
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl $0x900, %edx            # imm = 0x900
leaq 0x1538(%rsp), %rdi
movq %r15, %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq 0x80(%rsp), %rdi
movq %rbx, %rsi
callq 0x3370
movl $0x900, %edx            # imm = 0x900
movq 0x78(%rsp), %rdi
movq %r14, %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq 0x88(%rsp), %rdi
movq %rbp, %rsi
callq 0x3370
movl $0xa00, %edx            # imm = 0xA00
movq %r15, %rdi
xorl %esi, %esi
callq 0x3260
movq %r12, %rdi
movq %r15, %rsi
callq 0x13d2a
cmpl $-0x1, 0xc(%rsp)
je 0x13bef
xorl %eax, %eax
movdqa 0x3f71(%rip), %xmm3     # 0x17990
movdqa 0x3f79(%rip), %xmm4     # 0x179a0
movdqa 0x3f81(%rip), %xmm5     # 0x179b0
movapd 0x3f89(%rip), %xmm6     # 0x179c0
movapd 0x3f91(%rip), %xmm7     # 0x179d0
movsd 0x3f38(%rip), %xmm8     # 0x17980
movdqu 0x138(%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 0x1538(%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, 0x138(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x120, %rax            # imm = 0x120
jne 0x13a48
movl $0x120, %eax            # imm = 0x120
movdqu 0x138(%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 0x1538(%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, 0x138(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x140, %rax            # imm = 0x140
jne 0x13b08
movq $0x1, 0x938(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %r15, %rdi
movq %r14, %rdx
callq 0x11690
movl $0x20, %esi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x11690
movl 0x4(%rsp), %eax
cmpl $0x6, %eax
movsd 0x10(%rsp), %xmm0
jb 0x13cc4
ucomisd 0x90(%rsp), %xmm0
jne 0x13cc4
jp 0x13cc4
movl $0x900, %edx            # imm = 0x900
movq %r15, %rdi
leaq 0x2938(%rsp), %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq %rbx, %rdi
movq 0x30(%rsp), %rsi
callq 0x3370
movl $0x900, %edx            # imm = 0x900
movq %r14, %rdi
movq 0x28(%rsp), %rsi
callq 0x3370
movl $0x100, %edx            # imm = 0x100
movq %rbp, %rdi
movq 0x20(%rsp), %rsi
callq 0x3370
leaq 0xa0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x149b4
movq %r13, %rdi
movq %rbx, %rsi
movl $0x20, %edx
callq 0x149b4
movq $0x1, 0x938(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %r15, %rdi
movq %r14, %rdx
callq 0x11690
movl $0x20, %esi
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x11690
movsd 0x10(%rsp), %xmm0
movl 0x4(%rsp), %eax
movl %eax, 0xc(%rsp)
incl %eax
cmpl 0x8(%rsp), %eax
jne 0x1383c
movq 0x68(%rsp), %rbx
movq 0x38(%rsp), %r12
movq %r12, %rdi
callq 0x3050
movq %rbx, %rdi
callq 0x3050
movq 0x48(%rsp), %rdi
callq 0x3050
leaq 0xa8(%rsp), %rdi
callq 0x117f5
leaq 0xf0(%rsp), %rdi
callq 0x134f6
addq $0x3d38, %rsp           # imm = 0x3D38
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x3550