// File: /workspace/github_c_makefile_star2_O0/FastFilter[P]BlockBloomSimulator/src/softblockmodel.h
// Function: findBestFold
size_t findBestFold(size_t HowManyKeys, size_t bitbudget,
                                  size_t BlockSizeInBits,
                                  sim_stats_t *beststats, bool verbose) {
  softmodel_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 BestFold = 0;
  for (size_t fold = 0; fold <= 16; fold++) {
    m.FoldCount = fold;
    // 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_soft(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; 
      BestFold = fold;
    }
  }
  bitset_free(b);
  return BestFold;
}

// Assembly:
subq $0xa8, %rsp
movb %r8b, %al
movq %rdi, 0xa0(%rsp)
movq %rsi, 0x98(%rsp)
movq %rdx, 0x90(%rsp)
movq %rcx, 0x88(%rsp)
andb $0x1, %al
movb %al, 0x87(%rsp)
movq 0x98(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x90(%rsp)
movq %rax, 0x78(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x70(%rsp)
movq $0x186a0, 0x60(%rsp)    # imm = 0x186A0
movq 0x98(%rsp), %rdi
callq 0x34a0
movq %rax, 0x58(%rsp)
movq 0x88(%rsp), %rax
movsd 0x343c(%rip), %xmm0     # 0x6040
movsd %xmm0, (%rax)
movq $0x0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
cmpq $0x10, 0x48(%rsp)
ja 0x2db0
movq 0x48(%rsp), %rax
movq %rax, 0x68(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0xa0(%rsp), %rdx
movl $0x1, %eax
movl $0x5, %ecx
cmpq $0x989680, %rdx         # imm = 0x989680
cmovbl %ecx, %eax
cltq
movq %rax, 0x30(%rsp)
movl $0x0, 0x2c(%rsp)
movslq 0x2c(%rsp), %rax
cmpq 0x30(%rsp), %rax
jae 0x2ceb
movq 0xa0(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq 0x58(%rsp), %rdx
leaq 0x68(%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 0x2720
movsd %xmm0, 0x18(%rsp)
movsd %xmm1, 0x20(%rsp)
movsd 0x20(%rsp), %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x18(%rsp), %xmm0
addsd 0x38(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x2c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
jmp 0x2c6d
movq 0x30(%rsp), %xmm3
movaps 0x3318(%rip), %xmm2     # 0x6010
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movapd 0x331c(%rip), %xmm0     # 0x6020
subpd %xmm0, %xmm3
movaps %xmm3, %xmm1
unpckhpd %xmm3, %xmm3            # xmm3 = xmm3[1,1]
addsd %xmm1, %xmm3
movsd 0x38(%rsp), %xmm1
divsd %xmm3, %xmm1
movsd %xmm1, 0x38(%rsp)
movq 0x30(%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 0x40(%rsp), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x32f4(%rip), %xmm1     # 0x6048
mulsd 0x38(%rsp), %xmm1
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2d9b
movsd 0x38(%rsp), %xmm0
movq 0x88(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x40(%rsp), %xmm0
movq 0x88(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x48(%rsp), %rax
movq %rax, 0x50(%rsp)
jmp 0x2d9d
movq 0x48(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x48(%rsp)
jmp 0x2c1a
movq 0x58(%rsp), %rdi
callq 0x3b00
movq 0x50(%rsp), %rax
addq $0xa8, %rsp
retq
nopw (%rax,%rax)