// File: /workspace/github_c_makefile_star2_O2/FastFilter[P]BlockBloomSimulator/src/laneblockmodel.h
// Function: findBestLaneSize
size_t findBestLaneSize(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 BestLanes = 0;
  size_t valid_lanes[] = {8, 16, 32, 64};
  for (size_t laneindex = 0;
       laneindex < sizeof(valid_lanes) / sizeof(valid_lanes[0]); laneindex++) {
    size_t lanesize = valid_lanes[laneindex];
    if (lanesize > m.BlockSizeInBits)
      continue;
    m.BitsPerKeyPerBlock = m.BlockSizeInBits / lanesize;
    // we take the averge of several attempts
    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_lane(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;
      BestLanes = lanesize;
    }
  }
  bitset_free(b);
  return BestLanes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %rbx
movq %rdx, %rcx
movq %rdi, %r15
xorl %ebp, %ebp
movq %rsi, %rax
xorl %edx, %edx
divq %rcx
movq %rax, 0x60(%rsp)
movq %rcx, 0x40(%rsp)
movq %rcx, 0x58(%rsp)
movq %rsi, %rdi
callq 0x2483
movq %rax, %r12
movabsq $0x3ff0000000000000, %rax # imm = 0x3FF0000000000000
xorl %ecx, %ecx
cmpq $0x989680, %r15         # imm = 0x989680
setb %cl
leaq 0x1(,%rcx,4), %rcx
movq %rcx, 0x38(%rsp)
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rbx, 0x28(%rsp)
movq %rax, (%rbx)
leaq 0x2ac8(%rip), %r14      # 0x4570
movq $0x0, 0x18(%rsp)
cmpq $0x4, %rbp
je 0x1b7d
movq (%r14,%rbp,8), %r13
movq 0x40(%rsp), %rax
cmpq %rax, %r13
ja 0x1b75
xorl %edx, %edx
divq %r13
movq %rax, 0x50(%rsp)
xorpd %xmm1, %xmm1
movq 0x38(%rsp), %rbx
xorpd %xmm2, %xmm2
movsd %xmm1, 0x30(%rsp)
subq $0x1, %rbx
jb 0x1b35
movq 0x60(%rsp), %rax
movq %rax, 0x10(%rsp)
movupd 0x50(%rsp), %xmm0
movupd %xmm0, (%rsp)
movl $0x186a0, %esi          # imm = 0x186A0
movq %r15, %rdi
movq %r12, %rdx
movsd %xmm2, 0x48(%rsp)
callq 0x1854
movsd 0x30(%rsp), %xmm2
addsd %xmm1, %xmm2
movapd %xmm2, %xmm1
movsd 0x48(%rsp), %xmm2
addsd %xmm0, %xmm2
jmp 0x1ae4
divsd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm0
mulsd 0x2539(%rip), %xmm0     # 0x4080
movq 0x28(%rsp), %rax
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x1b75
movsd 0x30(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movq 0x28(%rsp), %rax
movsd %xmm2, (%rax)
movsd %xmm0, 0x8(%rax)
movq %r13, 0x18(%rsp)
incq %rbp
jmp 0x1ab1
movq %r12, %rdi
callq 0x275d
movq 0x18(%rsp), %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq