int count_trees(int num_keys) {
  if (num_keys <= 1) {
    return 1;
  } else {
    int root, nb_left_subtrees, nb_right_subtrees, count = 0;
    for (root = 1; root <= num_keys; root++) {
      nb_left_subtrees = count_trees(root - 1);
      nb_right_subtrees = count_trees(num_keys - root);
      count += nb_left_subtrees * nb_right_subtrees;
    }
    return count;
  }
}


x86 assembly (clang, -O0):
<count_trees>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     1131 <count_trees+0x21>
movl   $0x1,-0x4(%rbp)
jmp    1188 <count_trees+0x78>
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     1182 <count_trees+0x72>
mov    -0xc(%rbp),%edi
sub    $0x1,%edi
call   1030 <count_trees@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
sub    -0xc(%rbp),%edi
call   1030 <count_trees@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
imul   -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    113f <count_trees+0x2f>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_trees>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1,%r14d
cmp    $0x2,%edi
jl     1150 <count_trees+0x40>
mov    %edi,%ebx
add    $0xffffffff,%ebx
xor    %ebp,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %ebp,%edi
call   1030 <count_trees@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1030 <count_trees@plt>
imul   %r15d,%eax
add    %eax,%r14d
add    $0x1,%ebp
add    $0xffffffff,%ebx
jb     1130 <count_trees+0x20>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @count_trees(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = load i32, i32* %3
%9 = icmp sle i32 %8, 1
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 1, i32* %2
br label %34
11: ; preds = %1
store i32 0, i32* %7
store i32 1, i32* %4
br label %12
12: ; preds = %29, %11
%13 = load i32, i32* %4
%14 = load i32, i32* %3
%15 = icmp sle i32 %13, %14
br i1 %15, label %16, label %32
16: ; preds = %12
%17 = load i32, i32* %4
%18 = sub nsw i32 %17, 1
%19 = call i32 @count_trees(i32 %18)
store i32 %19, i32* %5
%20 = load i32, i32* %3
%21 = load i32, i32* %4
%22 = sub nsw i32 %20, %21
%23 = call i32 @count_trees(i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %5
%25 = load i32, i32* %6
%26 = mul nsw i32 %24, %25
%27 = load i32, i32* %7
%28 = add nsw i32 %27, %26
store i32 %28, i32* %7
br label %29
29: ; preds = %16
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %12
32: ; preds = %12
%33 = load i32, i32* %7
store i32 %33, i32* %2
br label %34
34: ; preds = %32, %10
%35 = load i32, i32* %2
ret i32 %35
}

LLVM IR (-O2):
define i32 @count_trees(i32 %0) {
%2 = icmp slt i32 %0, 2
br i1 %2, label %14, label %3
3: ; preds = %1, %3
%4 = phi i32 [ %11, %3 ], [ 0, %1 ]
%5 = phi i32 [ %12, %3 ], [ 1, %1 ]
%6 = add nsw i32 %5, -1
%7 = tail call i32 @count_trees(i32 %6)
%8 = sub nsw i32 %0, %5
%9 = tail call i32 @count_trees(i32 %8)
%10 = mul nsw i32 %9, %7
%11 = add nsw i32 %10, %4
%12 = add nuw i32 %5, 1
%13 = icmp eq i32 %5, %0
br i1 %13, label %14, label %3
14: ; preds = %3, %1
%15 = phi i32 [ 1, %1 ], [ %11, %3 ]
ret i32 %15
}