// File: /workspace/github_c_makefile_star2_O0/FastFilter[P]BlockBloomSimulator/src/blockmodel.h
// Function: findBestBitsPerKeyPerBlock
size_t findBestBitsPerKeyPerBlock(size_t HowManyKeys, size_t bitbudget,
                                  size_t BlockSizeInBits,
                                  sim_stats_t *beststats, bool verbose) {
  model_t m;
  m.NumberOfBlocks = bitbudget / BlockSizeInBits;
  m.BlockSizeInBits = BlockSizeInBits;
  size_t howmanytests = 100000;
  bitset_t *b = bitset_create_with_capacity(bitbudget);
  beststats->collision_rate = 1.0;
  size_t BestBitsPerKeyPerBlock = 0;
  for (size_t BitsPerKeyPerBlock = 1;
       (BitsPerKeyPerBlock <= 16) ||
       (BestBitsPerKeyPerBlock == BitsPerKeyPerBlock);
       BitsPerKeyPerBlock++) {
    m.BitsPerKeyPerBlock = BitsPerKeyPerBlock;
    // we take the average of several attempts
    //double collisionrate = 0;
    sim_stats_t s;
    s.collision_rate = 0;
    s.density = 0;
    size_t attempts = HowManyKeys < 10000000 ? 5 : 1;
    for (int i = 0; i < attempts; i++) {
      sim_stats_t ts = simulate(HowManyKeys, m, howmanytests, b);
      s.density += ts.density;
      s.collision_rate += ts.collision_rate;
    }
    s.collision_rate /= attempts;
    s.density /= attempts;

    if (s.collision_rate * 1.01 <
        beststats->collision_rate) { // has to be at least one percent better
      beststats->collision_rate = s.collision_rate;
      beststats->density = s.density; 
      BestBitsPerKeyPerBlock = BitsPerKeyPerBlock;
    }
  }
  bitset_free(b);
  return BestBitsPerKeyPerBlock;
}

// Assembly:
subq $0xb8, %rsp
movb %r8b, %al
movq %rdi, 0xb0(%rsp)
movq %rsi, 0xa8(%rsp)
movq %rdx, 0xa0(%rsp)
movq %rcx, 0x98(%rsp)
andb $0x1, %al
movb %al, 0x97(%rsp)
movq 0xa8(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xa0(%rsp)
movq %rax, 0x88(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x80(%rsp)
movq $0x186a0, 0x70(%rsp)    # imm = 0x186A0
movq 0xa8(%rsp), %rdi
callq 0x34a0
movq %rax, 0x68(%rsp)
movq 0x98(%rsp), %rax
movsd 0x4986(%rip), %xmm0     # 0x6040
movsd %xmm0, (%rax)
movq $0x0, 0x60(%rsp)
movq $0x1, 0x58(%rsp)
movb $0x1, %al
cmpq $0x10, 0x58(%rsp)
movb %al, 0x27(%rsp)
jbe 0x16ef
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
sete %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x16fc
jmp 0x1886
movq 0x58(%rsp), %rax
movq %rax, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xb0(%rsp), %rdx
movl $0x1, %eax
movl $0x5, %ecx
cmpq $0x989680, %rdx         # imm = 0x989680
cmovbl %ecx, %eax
cltq
movq %rax, 0x40(%rsp)
movl $0x0, 0x3c(%rsp)
movslq 0x3c(%rsp), %rax
cmpq 0x40(%rsp), %rax
jae 0x17c1
movq 0xb0(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x78(%rsp), %rax
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x12a0
movsd %xmm0, 0x28(%rsp)
movsd %xmm1, 0x30(%rsp)
movsd 0x30(%rsp), %xmm0
addsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x28(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movl 0x3c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x3c(%rsp)
jmp 0x1743
movq 0x40(%rsp), %xmm3
movaps 0x4842(%rip), %xmm2     # 0x6010
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movapd 0x4846(%rip), %xmm0     # 0x6020
subpd %xmm0, %xmm3
movaps %xmm3, %xmm1
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
addsd %xmm1, %xmm3
movsd 0x48(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x48(%rsp)
movq 0x40(%rsp), %xmm1
punpckldq %xmm2, %xmm1    # xmm1 = xmm1[0],xmm2[0],xmm1[1],xmm2[1]
subpd %xmm0, %xmm1
movaps %xmm1, %xmm0
unpckhpd %xmm1, %xmm1            # xmm1 = xmm1[1,1]
addsd %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x481e(%rip), %xmm1     # 0x6048
mulsd 0x48(%rsp), %xmm1
movq 0x98(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1871
movsd 0x48(%rsp), %xmm0
movq 0x98(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x50(%rsp), %xmm0
movq 0x98(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x1873
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x16d0
movq 0x68(%rsp), %rdi
callq 0x3b00
movq 0x60(%rsp), %rax
addq $0xb8, %rsp
retq
nopl (%rax)