// File: blackle[P]LZMA-Vizualizer/LzmaSpec.cpp
// Function: output_kkp(CLzmaDecoder&, double)
static int output_kkp(CLzmaDecoder& lzmaDecoder, double maxPerplexity) {
  FILE* fo = fopen(options.kkpout.c_str(), "wb");
  if (!fo) {
    return 1;
  }

  double totalPerpl = 0;
  for (size_t j = 0; j < lzmaDecoder.OutWindow.OutStream.Data.size(); j++) {
    totalPerpl += lzmaDecoder.Perplexities[j];
  }
  totalPerpl /= 8;

  uint32_t zero = 0;
  uint32_t one = htole32((uint32_t)1);
  uint32_t mone = 0xffffffffu;
  uint32_t size32l = htole32((uint32_t)lzmaDecoder.OutWindow.OutStream.Data.size());

  // magic bytes
  fwrite("KK64", 1, 4, fo);
  // number of binary bytes
  fwrite(&size32l, 4, 1, fo);
  // number of source code files
  fwrite(&zero, 4, 1, fo);

  // normally, source code info would be here

  // number of symbols (zero)
  fwrite(&zero, 4, 1, fo);

  // normally, symbols would be here

  // all compressed bytes
  for (size_t j = 0; j < lzmaDecoder.OutWindow.OutStream.Data.size(); ++j) {
    uint8_t v = lzmaDecoder.OutWindow.OutStream.Data[j];
    fwrite(&v, 1, 1, fo); // orig. data
    fwrite(&zero, 2, 1, fo); // symbol index
    double psz = lzmaDecoder.Perplexities[j] / 8;
    fwrite(&psz, 8, 1, fo);
    fwrite(&zero, 2, 1, fo); // source code line
    fwrite(&zero, 2, 1, fo); // source code file index
  }

  fclose(fo);

  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movsd %xmm0, -0x18(%rbp)
leaq 0xcd98(%rip), %rdi      # 0x10180
addq $0x20, %rdi
callq 0x2080
movq %rax, %rdi
leaq 0x7e06(%rip), %rsi      # 0xb201
callq 0x21b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x3417
movl $0x1, -0x4(%rbp)
jmp 0x362f
xorps %xmm0, %xmm0
movsd %xmm0, -0x28(%rbp)
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rax
movq %rax, -0x60(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
addq $0x18, %rdi
callq 0x5970
movq %rax, %rcx
movq -0x60(%rbp), %rax
cmpq %rcx, %rax
jae 0x347d
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movq -0x30(%rbp), %rsi
callq 0x6310
movss (%rax), %xmm0
cvtss2sd %xmm0, %xmm0
addsd -0x28(%rbp), %xmm0
movsd %xmm0, -0x28(%rbp)
movq -0x30(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x30(%rbp)
jmp 0x3427
movsd -0x28(%rbp), %xmm0
movsd 0x7bd6(%rip), %xmm1     # 0xb060
divsd %xmm1, %xmm0
movsd %xmm0, -0x28(%rbp)
movl $0x0, -0x34(%rbp)
movl $0x1, %edi
callq 0x3ca0
movl %eax, -0x38(%rbp)
movl $0xffffffff, -0x3c(%rbp) # imm = 0xFFFFFFFF
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
addq $0x18, %rdi
callq 0x5970
movl %eax, %edi
callq 0x3ca0
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rcx
leaq 0x7d30(%rip), %rdi      # 0xb204
movl $0x1, %esi
movl $0x4, %edx
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x4, %esi
movl $0x1, %edx
callq 0x22a0
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
addq $0x18, %rdi
callq 0x5970
movq %rax, %rcx
movq -0x68(%rbp), %rax
cmpq %rcx, %rax
jae 0x361f
movq -0x10(%rbp), %rdi
addq $0x18, %rdi
addq $0x18, %rdi
movq -0x48(%rbp), %rsi
callq 0x6330
movb (%rax), %al
movb %al, -0x49(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x49(%rbp), %rdi
movl $0x1, %edx
movq %rdx, %rsi
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x10(%rbp), %rdi
addq $0x48, %rdi
movq -0x48(%rbp), %rsi
callq 0x6310
movss (%rax), %xmm0
movss 0x7a50(%rip), %xmm1     # 0xb00c
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, -0x58(%rbp)
movq -0x20(%rbp), %rcx
leaq -0x58(%rbp), %rdi
movl $0x8, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x20(%rbp), %rcx
leaq -0x34(%rbp), %rdi
movl $0x2, %esi
movl $0x1, %edx
callq 0x22a0
movq -0x48(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x48(%rbp)
jmp 0x3530
movq -0x20(%rbp), %rdi
callq 0x2160
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax,%rax)