#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef size_t uint32_t;

/* Variables and functions */
size_t const UINT32_MAX;
size_t estimate_max_nverts(int, int);
size_t const graph_nverts(size_t, int);
uint32_t calc_max_nkeys(int hash_bits, int r) {
  const size_t max_nverts = estimate_max_nverts(hash_bits, r);
  const double c = (r == 2) ? 2.125 : 1.25;
  uint32_t n = (max_nverts - r) / c;
  if (max_nverts < UINT32_MAX) {
    while (graph_nverts(n, r) <= max_nverts)
      n++;
    return n - 1;
  } else {
    uint32_t u, v;
    u = graph_nverts(n++, r);
    while ((v = graph_nverts(n++, r)) > u)
      u = v;
    return n - 2;
  }
}


x86 assembly (clang, -O0):
<calc_max_nkeys>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1030 <estimate_max_nverts@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x2,%eax
sete   %al
movzbl %al,%eax
mov    %eax,%ecx
lea    0xeae(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x10(%rbp),%rcx
sub    %rcx,%rax
movq   %rax,%xmm0
movaps 0xe9d(%rip),%xmm1
punpckldq %xmm1,%xmm0
movapd 0xea1(%rip),%xmm1
subpd  %xmm1,%xmm0
movaps %xmm0,%xmm1
unpckhpd %xmm0,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x20(%rbp),%xmm1
divsd  %xmm1,%xmm0
movaps %xmm0,%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rdx
sar    $0x3f,%rdx
movsd  0xe82(%rip),%xmm1
subsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rcx
and    %rdx,%rcx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jae    1212 <calc_max_nkeys+0xf2>
jmp    11da <calc_max_nkeys+0xba>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1040 <graph_nverts@plt>
cmp    -0x18(%rbp),%rax
ja     1201 <calc_max_nkeys+0xe1>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11da <calc_max_nkeys+0xba>
mov    -0x28(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    126b <calc_max_nkeys+0x14b>
mov    -0x28(%rbp),%rdi
mov    %rdi,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%esi
call   1040 <graph_nverts@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    %rdi,%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%esi
call   1040 <graph_nverts@plt>
mov    %rax,-0x38(%rbp)
cmp    -0x30(%rbp),%rax
jbe    125f <calc_max_nkeys+0x13f>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
jmp    122d <calc_max_nkeys+0x10d>
mov    -0x28(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_max_nkeys>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
xor    %ebx,%ebx
cmp    $0x2,%esi
sete   %bl
call   1030 <estimate_max_nverts@plt>
movslq %r14d,%rsi
sub    %rsi,%rax
movq   %rax,%xmm0
punpckldq 0xeb9(%rip),%xmm0
subpd  0xec1(%rip),%xmm0
movapd %xmm0,%xmm1
unpckhpd %xmm0,%xmm1
addsd  %xmm0,%xmm1
lea    0xebe(%rip),%rax
divsd  (%rax,%rbx,8),%xmm1
cvttsd2si %xmm1,%rax
mov    %rax,%rcx
sar    $0x3f,%rcx
subsd  0xeb5(%rip),%xmm1
cvttsd2si %xmm1,%rbx
and    %rcx,%rbx
or     %rax,%rbx
mov    %rbx,%rdi
call   1040 <graph_nverts@plt>
mov    %rax,%rbp
add    $0xffffffffffffffff,%rbx
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x2(%rbx),%rdi
mov    %r14d,%esi
call   1040 <graph_nverts@plt>
add    $0x1,%rbx
cmp    %rbp,%rax
mov    %rax,%rbp
ja     11a0 <calc_max_nkeys+0x80>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@UINT32_MAX = constant i64 0
define i64 @calc_max_nkeys(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca double
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
%11 = load i32, i32* %4
%12 = load i32, i32* %5
%13 = call i64 @estimate_max_nverts(i32 %11, i32 %12)
store i64 %13, i64* %6
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 2
%16 = zext i1 %15 to i64
%17 = select i1 %15, double 2.125000e+00, double 1.250000e+00
store double %17, double* %7
%18 = load i64, i64* %6
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = sub i64 %18, %20
%22 = uitofp i64 %21 to double
%23 = load double, double* %7
%24 = fdiv double %22, %23
%25 = fptoui double %24 to i64
store i64 %25, i64* %8
%26 = load i64, i64* %6
%27 = load i64, i64* @UINT32_MAX
%28 = icmp ult i64 %26, %27
br i1 %28, label %29, label %42
29: ; preds = %2
br label %30
30: ; preds = %36, %29
%31 = load i64, i64* %8
%32 = load i32, i32* %5
%33 = call i64 @graph_nverts(i64 %31, i32 %32)
%34 = load i64, i64* %6
%35 = icmp ule i64 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = load i64, i64* %8
%38 = add i64 %37, 1
store i64 %38, i64* %8
br label %30
39: ; preds = %30
%40 = load i64, i64* %8
%41 = sub i64 %40, 1
store i64 %41, i64* %3
br label %59
42: ; preds = %2
%43 = load i64, i64* %8
%44 = add i64 %43, 1
store i64 %44, i64* %8
%45 = load i32, i32* %5
%46 = call i64 @graph_nverts(i64 %43, i32 %45)
store i64 %46, i64* %9
br label %47
47: ; preds = %54, %42
%48 = load i64, i64* %8
%49 = add i64 %48, 1
store i64 %49, i64* %8
%50 = load i32, i32* %5
%51 = call i64 @graph_nverts(i64 %48, i32 %50)
store i64 %51, i64* %10
%52 = load i64, i64* %9
%53 = icmp ugt i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %47
%55 = load i64, i64* %10
store i64 %55, i64* %9
br label %47
56: ; preds = %47
%57 = load i64, i64* %8
%58 = sub i64 %57, 2
store i64 %58, i64* %3
br label %59
59: ; preds = %56, %39
%60 = load i64, i64* %3
ret i64 %60
}

LLVM IR (-O2):
@UINT32_MAX = constant i64 0
define i64 @calc_max_nkeys(i32 %0, i32 %1) {
%3 = tail call i64 @estimate_max_nverts(i32 %0, i32 %1)
%4 = icmp eq i32 %1, 2
%5 = select i1 %4, double 2.125000e+00, double 1.250000e+00
%6 = sext i32 %1 to i64
%7 = sub i64 %3, %6
%8 = uitofp i64 %7 to double
%9 = fdiv double %8, %5
%10 = fptoui double %9 to i64
%11 = tail call i64 @graph_nverts(i64 %10, i32 %1)
br label %12
12: ; preds = %12, %2
%13 = phi i64 [ %10, %2 ], [ %15, %12 ]
%14 = phi i64 [ %11, %2 ], [ %16, %12 ]
%15 = add i64 %13, 1
%16 = tail call i64 @graph_nverts(i64 %15, i32 %1)
%17 = icmp ugt i64 %16, %14
br i1 %17, label %12, label %18
18: ; preds = %12
ret i64 %13
}