double fourier_eval(double ca, double sa, int n, double *coefs) {
  double cna = ca;
  double sna = sa;
  double result = cna * coefs[0] + sna * coefs[1];
  for (int i = 2; i < (n << 1); i += 2) {
    double im = cna * ca - sna * sa;
    double re = cna * sa + sna * ca;
    result += cna * coefs[i] + sna * coefs[i + 1];
    cna = im;
    sna = re;
  }
  return result;
}


x86 assembly (clang, -O0):
<fourier_eval>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm2
movsd  -0x30(%rbp),%xmm1
mov    -0x20(%rbp),%rax
mulsd  0x8(%rax),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
movl   $0x2,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    121f <fourier_eval+0x11f>
movsd  -0x28(%rbp),%xmm0
movsd  -0x8(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x30(%rbp),%xmm1
mulsd  -0x8(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x28(%rbp),%xmm0
mov    -0x20(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm2
movsd  -0x30(%rbp),%xmm1
mov    -0x20(%rbp),%rax
mov    -0x3c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mulsd  (%rax,%rcx,8),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
addsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x3c(%rbp)
jmp    1158 <fourier_eval+0x58>
movsd  -0x38(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<fourier_eval>:
movapd %xmm0,%xmm2
movsd  0x8(%rsi),%xmm3
mulsd  %xmm1,%xmm3
movsd  (%rsi),%xmm0
mulsd  %xmm2,%xmm0
addsd  %xmm3,%xmm0
add    %edi,%edi
cmp    $0x3,%edi
jl     1199 <fourier_eval+0x99>
mov    %edi,%eax
movapd %xmm2,%xmm3
unpcklpd %xmm1,%xmm3
unpcklpd %xmm2,%xmm1
mov    $0x2,%ecx
movapd 0xec5(%rip),%xmm2
movapd %xmm3,%xmm5
movapd %xmm3,%xmm4
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movapd %xmm5,%xmm6
unpckhpd %xmm5,%xmm6
movsd  0x8(%rsi,%rcx,8),%xmm7
mulsd  %xmm6,%xmm7
xorpd  %xmm2,%xmm6
shufpd $0x2,%xmm5,%xmm6
mulpd  %xmm1,%xmm6
unpcklpd %xmm4,%xmm4
mulpd  %xmm3,%xmm4
addpd  %xmm6,%xmm4
movsd  (%rsi,%rcx,8),%xmm6
mulsd  %xmm5,%xmm6
addsd  %xmm7,%xmm6
addsd  %xmm6,%xmm0
add    $0x2,%rcx
movapd %xmm4,%xmm5
cmp    %rax,%rcx
jb     1150 <fourier_eval+0x50>
ret

LLVM IR (-O0):
define double @fourier_eval(double %0, double %1, i32 %2, double* %3) {
%5 = alloca double
%6 = alloca double
%7 = alloca i32
%8 = alloca double*
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca i32
%13 = alloca double
%14 = alloca double
store double %0, double* %5
store double %1, double* %6
store i32 %2, i32* %7
store double* %3, double** %8
%15 = load double, double* %5
store double %15, double* %9
%16 = load double, double* %6
store double %16, double* %10
%17 = load double, double* %9
%18 = load double*, double** %8
%19 = getelementptr inbounds double, double* %18, i64 0
%20 = load double, double* %19
%21 = load double, double* %10
%22 = load double*, double** %8
%23 = getelementptr inbounds double, double* %22, i64 1
%24 = load double, double* %23
%25 = fmul double %21, %24
%26 = call double @llvm.fmuladd.f64(double %17, double %20, double %25)
store double %26, double* %11
store i32 2, i32* %12
br label %27
27: ; preds = %65, %4
%28 = load i32, i32* %12
%29 = load i32, i32* %7
%30 = shl i32 %29, 1
%31 = icmp slt i32 %28, %30
br i1 %31, label %32, label %68
32: ; preds = %27
%33 = load double, double* %9
%34 = load double, double* %5
%35 = load double, double* %10
%36 = load double, double* %6
%37 = fmul double %35, %36
%38 = fneg double %37
%39 = call double @llvm.fmuladd.f64(double %33, double %34, double %38)
store double %39, double* %13
%40 = load double, double* %9
%41 = load double, double* %6
%42 = load double, double* %10
%43 = load double, double* %5
%44 = fmul double %42, %43
%45 = call double @llvm.fmuladd.f64(double %40, double %41, double %44)
store double %45, double* %14
%46 = load double, double* %9
%47 = load double*, double** %8
%48 = load i32, i32* %12
%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 = load double*, double** %8
%54 = load i32, i32* %12
%55 = add nsw i32 %54, 1
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds double, double* %53, i64 %56
%58 = load double, double* %57
%59 = fmul double %52, %58
%60 = call double @llvm.fmuladd.f64(double %46, double %51, double %59)
%61 = load double, double* %11
%62 = fadd double %61, %60
store double %62, double* %11
%63 = load double, double* %13
store double %63, double* %9
%64 = load double, double* %14
store double %64, double* %10
br label %65
65: ; preds = %32
%66 = load i32, i32* %12
%67 = add nsw i32 %66, 2
store i32 %67, i32* %12
br label %27
68: ; preds = %27
%69 = load double, double* %11
ret double %69
}

LLVM IR (-O2):
define double @fourier_eval(double %0, double %1, i32 %2, double* nocapture %3) {
%5 = load double, double* %3
%6 = getelementptr inbounds double, double* %3, i64 1
%7 = load double, double* %6
%8 = fmul double %7, %1
%9 = tail call double @llvm.fmuladd.f64(double %0, double %5, double %8)
%10 = shl i32 %2, 1
%11 = icmp sgt i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %4
%13 = zext i32 %10 to i64
%14 = insertelement <2 x double> poison, double %0, i64 0
%15 = insertelement <2 x double> %14, double %1, i64 1
%16 = insertelement <2 x double> poison, double %1, i64 0
%17 = insertelement <2 x double> %16, double %0, i64 1
br label %20
18: ; preds = %20, %4
%19 = phi double [ %9, %4 ], [ %38, %20 ]
ret double %19
20: ; preds = %12, %20
%21 = phi i64 [ 2, %12 ], [ %39, %20 ]
%22 = phi double [ %9, %12 ], [ %38, %20 ]
%23 = phi <2 x double> [ %15, %12 ], [ %30, %20 ]
%24 = extractelement <2 x double> %23, i64 1
%25 = fneg double %24
%26 = insertelement <2 x double> %23, double %25, i64 0
%27 = fmul <2 x double> %26, %17
%28 = extractelement <2 x double> %23, i64 0
%29 = shufflevector <2 x double> %23, <2 x double> undef, <2 x i32> zeroinitializer
%30 = call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %29, <2 x double> %15, <2 x double> %27)
%31 = getelementptr inbounds double, double* %3, i64 %21
%32 = load double, double* %31
%33 = or i64 %21, 1
%34 = getelementptr inbounds double, double* %3, i64 %33
%35 = load double, double* %34
%36 = fmul double %24, %35
%37 = tail call double @llvm.fmuladd.f64(double %28, double %32, double %36)
%38 = fadd double %22, %37
%39 = add nuw nsw i64 %21, 2
%40 = icmp ult i64 %39, %13
br i1 %40, label %20, label %18
}