double calculate_an(double *c, double *t, int k, int n) {
  double res;
  int i;
  if (n <= k) {
    return t[n - 1];
  }
  res = 0.0;
  for (i = 0; i < k; i++)
    res += c[i] * calculate_an(c, t, k, n - i - 1);
  res += c[k];
  return res;
}


x86 assembly (clang, -O0):
<calculate_an>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jg     114e <calculate_an+0x3e>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dd <calculate_an+0xcd>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11bc <calculate_an+0xac>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
sub    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
call   1030 <calculate_an@plt>
movaps %xmm0,%xmm2
movsd  -0x38(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    115d <calculate_an+0x4d>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
addsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculate_an>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebp
mov    %rsi,%r15
cmp    %edx,%ecx
jle    118c <calculate_an+0x7c>
mov    %edx,%r14d
mov    %rdi,%r12
test   %edx,%edx
jle    1198 <calculate_an+0x88>
mov    %r14d,%r13d
add    $0xffffffff,%ebp
xorpd  %xmm0,%xmm0
xor    %ebx,%ebx
nopl   (%rax)
movsd  %xmm0,0x8(%rsp)
movsd  (%r12,%rbx,8),%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13d,%edx
mov    %ebp,%ecx
call   1030 <calculate_an@plt>
mulsd  0x10(%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
addsd  %xmm0,%xmm1
movsd  %xmm1,0x8(%rsp)
movsd  0x8(%rsp),%xmm0
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    %rbx,%r13
jne    1140 <calculate_an+0x30>
jmp    119c <calculate_an+0x8c>
movslq %ebp,%rax
movsd  -0x8(%r15,%rax,8),%xmm0
jmp    11a5 <calculate_an+0x95>
xorpd  %xmm0,%xmm0
movslq %r14d,%rax
addsd  (%r12,%rax,8),%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @calculate_an(double* %0, double* %1, i32 %2, i32 %3) {
%5 = alloca double
%6 = alloca double*
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca i32
store double* %0, double** %6
store double* %1, double** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = load i32, i32* %8
%14 = icmp sle i32 %12, %13
br i1 %14, label %15, label %22
15: ; preds = %4
%16 = load double*, double** %7
%17 = load i32, i32* %9
%18 = sub nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds double, double* %16, i64 %19
%21 = load double, double* %20
store double %21, double* %5
br label %55
22: ; preds = %4
store double 0.000000e+00, double* %10
store i32 0, i32* %11
br label %23
23: ; preds = %43, %22
%24 = load i32, i32* %11
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %46
27: ; preds = %23
%28 = load double*, double** %6
%29 = load i32, i32* %11
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double, double* %31
%33 = load double*, double** %6
%34 = load double*, double** %7
%35 = load i32, i32* %8
%36 = load i32, i32* %9
%37 = load i32, i32* %11
%38 = sub nsw i32 %36, %37
%39 = sub nsw i32 %38, 1
%40 = call double @calculate_an(double* %33, double* %34, i32 %35, i32 %39)
%41 = load double, double* %10
%42 = call double @llvm.fmuladd.f64(double %32, double %40, double %41)
store double %42, double* %10
br label %43
43: ; preds = %27
%44 = load i32, i32* %11
%45 = add nsw i32 %44, 1
store i32 %45, i32* %11
br label %23
46: ; preds = %23
%47 = load double*, double** %6
%48 = load i32, i32* %8
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds double, double* %47, i64 %49
%51 = load double, double* %50
%52 = load double, double* %10
%53 = fadd double %52, %51
store double %53, double* %10
%54 = load double, double* %10
store double %54, double* %5
br label %55
55: ; preds = %46, %15
%56 = load double, double* %5
ret double %56
}

LLVM IR (-O2):
define double @calculate_an(double* nocapture %0, double* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %3, %2
br i1 %5, label %6, label %10
6: ; preds = %4
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %27
8: ; preds = %6
%9 = zext i32 %2 to i64
br label %15
10: ; preds = %4
%11 = add nsw i32 %3, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds double, double* %1, i64 %12
%14 = load double, double* %13
br label %33
15: ; preds = %8, %15
%16 = phi i64 [ 0, %8 ], [ %25, %15 ]
%17 = phi double [ 0.000000e+00, %8 ], [ %24, %15 ]
%18 = trunc i64 %16 to i32
%19 = getelementptr inbounds double, double* %0, i64 %16
%20 = load double, double* %19
%21 = xor i32 %18, -1
%22 = add i32 %21, %3
%23 = tail call double @calculate_an(double* %0, double* %1, i32 %2, i32 %22)
%24 = tail call double @llvm.fmuladd.f64(double %20, double %23, double %17)
%25 = add nuw nsw i64 %16, 1
%26 = icmp eq i64 %25, %9
br i1 %26, label %27, label %15
27: ; preds = %15, %6
%28 = phi double [ 0.000000e+00, %6 ], [ %24, %15 ]
%29 = sext i32 %2 to i64
%30 = getelementptr inbounds double, double* %0, i64 %29
%31 = load double, double* %30
%32 = fadd double %28, %31
br label %33
33: ; preds = %27, %10
%34 = phi double [ %14, %10 ], [ %32, %27 ]
ret double %34
}