// File: /workspace/github_c_makefile_star2_O0/FastFilter[P]BlockBloomSimulator/src/windowedblockmodel.h
// Function: findBestBitsPerKeyPerBlockWindow
size_t findBestBitsPerKeyPerBlockWindow(size_t HowManyKeys, size_t bitbudget,
                                  size_t BlockSizeInBits, size_t WindowSizeInBits,
                                  sim_stats_t *beststats, bool verbose) {
  windowed_model_t m;
  m.TotalBits = bitbudget;
  m.BlockSizeInBits = BlockSizeInBits;
  m.WindowSizeInBits = WindowSizeInBits;
  size_t howmanytests = 50000;
  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 = simulateWindow(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 $0xc8, %rsp
movb %r9b, %al
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xb0(%rsp)
movq %rcx, 0xa8(%rsp)
movq %r8, 0xa0(%rsp)
andb $0x1, %al
movb %al, 0x9f(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x90(%rsp)
movq $0xc350, 0x70(%rsp)     # imm = 0xC350
movq 0xb8(%rsp), %rdi
callq 0x2430
movq %rax, 0x68(%rsp)
movq 0xa0(%rsp), %rax
movsd 0x394a(%rip), %xmm0     # 0x5030
movsd %xmm0, (%rax)
movq $0x0, 0x60(%rsp)
movq $0x1, 0x58(%rsp)
movb $0x1, %al
cmpq $0x10, 0x58(%rsp)
movb %al, 0x27(%rsp)
jbe 0x171b
movq 0x60(%rsp), %rax
cmpq 0x58(%rsp), %rax
sete %al
movb %al, 0x27(%rsp)
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0x1728
jmp 0x18be
movq 0x58(%rsp), %rax
movq %rax, 0x78(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0xc0(%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 0x17f9
movq 0xc0(%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), %rcx
movq %rcx, 0x10(%rsp)
movq 0x18(%rax), %rax
movq %rax, 0x18(%rsp)
callq 0x12c0
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 0x176f
movq 0x40(%rsp), %xmm3
movaps 0x380a(%rip), %xmm2     # 0x5010
punpckldq %xmm2, %xmm3    # xmm3 = xmm3[0],xmm2[0],xmm3[1],xmm2[1]
movapd 0x380e(%rip), %xmm0     # 0x5020
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 0x37d6(%rip), %xmm1     # 0x5038
mulsd 0x48(%rsp), %xmm1
movq 0xa0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x18a9
movsd 0x48(%rsp), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x50(%rsp), %xmm0
movq 0xa0(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movq 0x58(%rsp), %rax
movq %rax, 0x60(%rsp)
jmp 0x18ab
movq 0x58(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x58(%rsp)
jmp 0x16fc
movq 0x68(%rsp), %rdi
callq 0x2a90
movq 0x60(%rsp), %rax
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)