// File: BU-DiSC[P]BF-Shared-Hashing/./LegacyBF.h
// Function: LegacyBF::Finish()
void LegacyBF::Finish() {
  uint32_t total_bits, num_lines;
  size_t num_entries = hash_entries_.size();
  char* data =
      ReserveSpace(static_cast<int>(num_entries), &total_bits, &num_lines);
  data_ = data;
  assert(data);
  if (total_bits != 0 && num_lines != 0) {
    for (auto h : hash_entries_) {
      AddHash(h, data, num_lines, total_bits);
    }

    // Check for excessive entries for 32-bit hash function
    if (num_entries >= /* minimum of 3 million */ 3000000U) {
      // More specifically, we can detect that the 32-bit hash function
      // is causing significant increase in FP rate by comparing current
      // estimated FP rate to what we would get with a normal number of
      // keys at same memory ratio.
      double est_fp_rate = LegacyBloomImpl::EstimatedFpRate(
          num_entries, total_bits / 8, num_probes_);
      double vs_fp_rate = LegacyBloomImpl::EstimatedFpRate(
          1U << 16, (1U << 16) * bits_per_key_ / 8, num_probes_);

      if (est_fp_rate >= 1.50 * vs_fp_rate) {
        // For more details, see
        // https://github.com/facebook/rocksdb/wiki/RocksDB-Bloom-Filter
        printf(
            "Using legacy SST/BBT Bloom filter with excessive key count "
            "(%.1fM @ %dbpk), causing estimated %.1fx higher filter FP rate. "
            "Consider using new Bloom with format_version>=5, smaller SST "
            "file size, or partitioned filters.",
            num_entries / 1000000.0, bits_per_key_, est_fp_rate / vs_fp_rate);
      }
    }
  }
  // See BloomFilterPolicy::GetFilterBitsReader for metadata
  data[total_bits / 8] = static_cast<char>(num_probes_);
  EncodeFixed32(data + total_bits / 8 + 1, static_cast<uint32_t>(num_lines));
  space_ = (total_bits >> 3) + 4;


  hash_entries_.clear();

}

// Assembly:
subq $0x58, %rsp
movq %rdi, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq %rdi, (%rsp)
addq $0x10, %rdi
callq 0x10ba0
movq (%rsp), %rdi
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rax
movl %eax, %esi
leaq 0x4c(%rsp), %rdx
leaq 0x48(%rsp), %rcx
callq 0xaae0
movq %rax, %rcx
movq (%rsp), %rax
movq %rcx, 0x38(%rsp)
movq 0x38(%rsp), %rcx
movq %rcx, 0x28(%rax)
cmpq $0x0, 0x38(%rsp)
je 0xa909
jmp 0xa928
leaq 0x59834(%rip), %rdi     # 0x64144
leaq 0x59832(%rip), %rsi     # 0x64149
movl $0x5c, %edx
leaq 0x59833(%rip), %rcx     # 0x64156
callq 0x6360
cmpl $0x0, 0x4c(%rsp)
je 0xaa7b
cmpl $0x0, 0x48(%rsp)
je 0xaa7b
movq (%rsp), %rax
addq $0x10, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
callq 0x10c30
movq %rax, 0x28(%rsp)
movq 0x30(%rsp), %rdi
callq 0x10c60
movq %rax, 0x20(%rsp)
leaq 0x28(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x10c90
testb $0x1, %al
jne 0xa97e
jmp 0xa9b5
leaq 0x28(%rsp), %rdi
callq 0x10cd0
movq (%rsp), %rdi
movl (%rax), %eax
movl %eax, 0x1c(%rsp)
movl 0x1c(%rsp), %esi
movq 0x38(%rsp), %rdx
movl 0x48(%rsp), %ecx
movl 0x4c(%rsp), %r8d
callq 0x10ce0
leaq 0x28(%rsp), %rdi
callq 0x10d70
jmp 0xa969
cmpq $0x2dc6c0, 0x40(%rsp)   # imm = 0x2DC6C0
jb 0xaa79
movq (%rsp), %rax
movq 0x40(%rsp), %rdi
movl 0x4c(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movl %ecx, %esi
movl 0x4(%rax), %edx
callq 0x10d90
movq (%rsp), %rax
movsd %xmm0, 0x10(%rsp)
movl (%rax), %ecx
shll $0x10, %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movl %ecx, %esi
movl 0x4(%rax), %edx
movl $0x10000, %edi          # imm = 0x10000
callq 0x10d90
movsd %xmm0, 0x8(%rsp)
movsd 0x10(%rsp), %xmm0
movsd 0x59699(%rip), %xmm1    # 0x640b0
mulsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xaa77
movq (%rsp), %rax
movaps 0x596f2(%rip), %xmm1    # 0x64120
movq 0x40(%rsp), %xmm0
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x596f0(%rip), %xmm1    # 0x64130
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd 0x59661(%rip), %xmm1    # 0x640b8
divsd %xmm1, %xmm0
movl (%rax), %esi
movsd 0x10(%rsp), %xmm1
divsd 0x8(%rsp), %xmm1
leaq 0x596fe(%rip), %rdi     # 0x6416e
movb $0x2, %al
callq 0x60a0
jmp 0xaa79
jmp 0xaa7b
movq (%rsp), %rax
movl 0x4(%rax), %eax
movb %al, %dl
movq 0x38(%rsp), %rax
movl 0x4c(%rsp), %ecx
shrl $0x3, %ecx
movl %ecx, %ecx
movb %dl, (%rax,%rcx)
movq 0x38(%rsp), %rdi
movl 0x4c(%rsp), %eax
shrl $0x3, %eax
movl %eax, %eax
addq %rax, %rdi
addq $0x1, %rdi
movl 0x48(%rsp), %esi
callq 0x10e80
movq (%rsp), %rdi
movl 0x4c(%rsp), %eax
shrl $0x3, %eax
addl $0x4, %eax
movl %eax, 0xc(%rdi)
addq $0x10, %rdi
callq 0x10ea0
addq $0x58, %rsp
retq
nopw %cs:(%rax,%rax)