// File: /workspace/github_c_makefile_star2_O0/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:
subq $0xd8, %rsp
movb %r8b, %al
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movq %rdx, 0xc0(%rsp)
movq %rcx, 0xb8(%rsp)
andb $0x1, %al
movb %al, 0xb7(%rsp)
movq 0xc8(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0xc0(%rsp)
movq %rax, 0xa8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq $0x186a0, 0x90(%rsp) # imm = 0x186A0
movq 0xc8(%rsp), %rdi
callq 0x34a0
movq %rax, 0x88(%rsp)
movq 0xb8(%rsp), %rax
movsd 0x4010(%rip), %xmm0     # 0x6040
movsd %xmm0, (%rax)
movq $0x0, 0x80(%rsp)
movq 0x44e9(%rip), %rax      # 0x6530
movq %rax, 0x60(%rsp)
movq 0x44e5(%rip), %rax      # 0x6538
movq %rax, 0x68(%rsp)
movq 0x44e1(%rip), %rax      # 0x6540
movq %rax, 0x70(%rsp)
movq 0x44dd(%rip), %rax      # 0x6548
movq %rax, 0x78(%rsp)
movq $0x0, 0x58(%rsp)
cmpq $0x4, 0x58(%rsp)
jae 0x2250
movq 0x58(%rsp), %rax
movq 0x60(%rsp,%rax,8), %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rax
cmpq 0xa0(%rsp), %rax
jbe 0x20a8
jmp 0x223d
movq 0xa0(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x50(%rsp)
movq %rax, 0x98(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0xd0(%rsp), %rdx
movl $0x1, %eax
movl $0x5, %ecx
cmpq $0x989680, %rdx         # imm = 0x989680
cmovbl %ecx, %eax
cltq
movq %rax, 0x38(%rsp)
movl $0x0, 0x34(%rsp)
movslq 0x34(%rsp), %rax
cmpq 0x38(%rsp), %rax
jae 0x2188
movq 0xd0(%rsp), %rdi
movq 0x90(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x98(%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 0x1cb0
movsd %xmm0, 0x20(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
addsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x20(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x34(%rsp)
jmp 0x20fe
movq 0x38(%rsp), %xmm3
movaps 0x3e7b(%rip), %xmm2     # 0x6010
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movapd 0x3e7f(%rip), %xmm0     # 0x6020
subpd %xmm0, %xmm3
movaps %xmm3, %xmm1
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
addsd %xmm1, %xmm3
movsd 0x40(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x40(%rsp)
movq 0x38(%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 0x48(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x3e57(%rip), %xmm1     # 0x6048
mulsd 0x40(%rsp), %xmm1
movq 0xb8(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x223b
movsd 0x40(%rsp), %xmm0
movq 0xb8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x48(%rsp), %xmm0
movq 0xb8(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x50(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x223d
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x2079
movq 0x88(%rsp), %rdi
callq 0x3b00
movq 0x80(%rsp), %rax
addq $0xd8, %rsp
retq
nopl (%rax)