// File: /workspace/github_c_makefile_star2_O1/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, 0x48(%rsp)
movq %rcx, %r14
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbp
movq %rcx, %rax
subq %rdx, %rax
leaq 0x2(,%rax,2), %rdi
callq 0x1120
xorl %ecx, %ecx
movq %rcx, 0x40(%rsp)
movq %rcx, 0xa8(%rsp)
movq %rax, 0x28(%rsp)
testq %rax, %rax
je 0x7a4e
movabsq $0x200000001, %rax      # imm = 0x200000001
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
xorl %r13d, %r13d
movl $0x1400, %edx           # imm = 0x1400
xorl %esi, %esi
callq 0x10c0
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x6684
movq %rbp, %rdi
movq %r12, 0x68(%rsp)
movq %r12, %rsi
movq %r15, 0x70(%rsp)
movq %r15, %rdx
movq %r14, 0x78(%rsp)
movq %r14, %rcx
movq %rbx, %r8
callq 0x70e6
movq %rbx, %rdi
leaq 0xb8(%rsp), %rbx
movq %rbx, %rsi
callq 0x7a58
movq (%rbp), %rax
cmpl $0x0, 0x4(%rax)
jle 0x7a1b
movq %rbp, %r12
movl $0x900, %eax            # imm = 0x900
leaq 0x28b8(%rsp), %rdx
leaq (%rdx,%rax), %rcx
movq %rcx, 0x20(%rsp)
leaq (%rbx,%rax), %r15
movl $0xa00, %ecx            # imm = 0xA00
leaq (%rdx,%rcx), %rsi
movq %rsi, 0x18(%rsp)
leaq (%rbx,%rcx), %rbp
movl $0x1300, %esi           # imm = 0x1300
addq %rsi, %rdx
movq %rdx, 0x10(%rsp)
leaq (%rbx,%rsi), %r14
leaq 0x14b8(%rsp), %rdx
addq %rdx, %rax
movq %rax, 0x58(%rsp)
leaq (%rdx,%rcx), %rax
movq %rax, 0x50(%rsp)
addq %rdx, %rsi
movq %rsi, 0x60(%rsp)
movsd 0x2a60(%rip), %xmm0     # 0xa008
movsd %xmm0, 0x30(%rsp)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl $0xffffffff, 0x4(%rsp)  # imm = 0xFFFFFFFF
movq %r12, 0x38(%rsp)
movq %r13, 0x88(%rsp)
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq %r15, %r13
movq %r14, %r15
movq %rbp, %r14
movq %rbx, %rbp
leaq 0x90(%rsp), %rbx
movq %rbx, %rdi
callq 0x6693
movq %rbx, %rdi
callq 0x6684
movq %r12, %rdi
movq %rbx, %r12
movq %rbp, %rbx
movq %r14, %rbp
movq %r15, %r14
movq %r13, %r15
movq 0x68(%rsp), %rsi
movq 0x70(%rsp), %rdx
movq 0x78(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0xa8(%rsp), %r9
pushq %r12
pushq %rbx
leaq 0xf31(%rip), %rax       # 0x8569
pushq %rax
pushq 0x40(%rsp)
callq 0x7b01
addq $0x20, %rsp
movq 0x90(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq 0xa0(%rsp), %rcx
xorl %edx, %edx
movl $0x2, %r8d
callq 0x2458
movapd %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
movsd %xmm1, 0x8(%rsp)
ucomisd %xmm1, %xmm0
jbe 0x76e5
movq %r12, %rdi
movq 0x48(%rsp), %rsi
callq 0x66a9
movl $0x900, %edx            # imm = 0x900
leaq 0x28b8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x20(%rsp), %rdi
movq %r15, %rsi
callq 0x1110
movl $0x900, %edx            # imm = 0x900
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x1110
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x30(%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 0x50(%rsp), %rdi
movq %rbp, %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq 0x60(%rsp), %rdi
movq %r14, %rsi
callq 0x1110
movl $0xa00, %edx            # imm = 0xA00
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r12, %rdi
movq %rbx, %rsi
callq 0x7a58
cmpl $-0x1, 0x4(%rsp)
movq 0x88(%rsp), %r13
je 0x7937
xorl %eax, %eax
movdqa 0x3699(%rip), %xmm3     # 0xae00
movdqa 0x36a1(%rip), %xmm4     # 0xae10
movdqa 0x36a9(%rip), %xmm5     # 0xae20
movapd 0x36b1(%rip), %xmm6     # 0xae30
movapd 0x36b9(%rip), %xmm7     # 0xae40
movsd 0x3660(%rip), %xmm8     # 0xadf0
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 0x7790
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 0x7850
movq $0x1, 0x8b8(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x88ad
movl $0x20, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x88ad
cmpl $0x6, %r13d
movq 0x38(%rsp), %r12
jb 0x7a0a
movsd 0x8(%rsp), %xmm0
ucomisd 0x80(%rsp), %xmm0
jne 0x7a0a
jp 0x7a0a
movl $0x900, %edx            # imm = 0x900
movq %rbx, %rdi
leaq 0x28b8(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq %r15, %rdi
movq 0x20(%rsp), %rsi
callq 0x1110
movl $0x900, %edx            # imm = 0x900
movq %rbp, %rdi
movq 0x18(%rsp), %rsi
callq 0x1110
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
movq 0x10(%rsp), %rsi
callq 0x1110
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
movl $0x120, %edx            # imm = 0x120
callq 0x86e8
movq %r12, %rdi
movq %r15, %rsi
movl $0x20, %edx
callq 0x86e8
movq $0x1, 0x8b8(%rsp)
movl $0x120, %esi            # imm = 0x120
movq %rbx, %rdi
movq %rbp, %rdx
callq 0x88ad
movl $0x20, %esi
movq %r15, %rdi
movq %r14, %rdx
callq 0x88ad
movq 0x38(%rsp), %r12
movl %r13d, 0x4(%rsp)
incl %r13d
movq (%r12), %rax
cmpl 0x4(%rax), %r13d
jl 0x75c5
movq 0x28(%rsp), %rdi
callq 0x1030
movq 0x40(%rsp), %rdi
callq 0x1030
leaq 0x90(%rsp), %rdi
callq 0x6693
addq $0x3cb8, %rsp           # imm = 0x3CB8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
callq 0x1170