#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 */

/* Variables and functions */
int M_PI;
double cos(int);
double cosine_sum_unsafe(double phase, int num_coefs, double *coefs) {
  double c = cos(2 * M_PI * phase);
  double c2 = c + c;
  double bk = coefs[num_coefs - 1];
  double bk1 = coefs[num_coefs - 2] + c2 * bk;
  double temp;
  for (int i = num_coefs - 3; i > 0; i--) {
    temp = bk1;
    bk1 = coefs[i] + c2 * bk1 - bk;
    bk = temp;
  }
  return coefs[0] + c * bk1 - bk;
}


x86 assembly (clang, -O0):
<cosine_sum_unsafe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <cos@plt>
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x38(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x3,%eax
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    11f4 <cosine_sum_unsafe+0xe4>
movsd  -0x38(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x44(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm1
movsd  -0x28(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
subsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x44(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x44(%rbp)
jmp    119f <cosine_sum_unsafe+0x8f>
mov    -0x18(%rbp),%rax
movsd  (%rax),%xmm1
movsd  -0x20(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
subsd  -0x30(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosine_sum_unsafe>:
push   %r14
push   %rbx
push   %rax
mov    0x2ecd(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
cvtsi2sd %eax,%xmm1
mov    %rsi,%r14
mov    %edi,%ebx
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edi
call   1030 <cos@plt>
movapd %xmm0,%xmm2
addsd  %xmm0,%xmm2
movslq %ebx,%rax
movsd  -0x8(%r14,%rax,8),%xmm1
movapd %xmm2,%xmm3
mulsd  %xmm1,%xmm3
addsd  -0x10(%r14,%rax,8),%xmm3
cmp    $0x4,%eax
jl     1197 <cosine_sum_unsafe+0x87>
add    $0xfffffffd,%ebx
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movapd %xmm2,%xmm4
mulsd  %xmm3,%xmm4
addsd  -0x8(%r14,%rbx,8),%xmm4
subsd  %xmm1,%xmm4
add    $0xffffffffffffffff,%rbx
movapd %xmm3,%xmm1
movapd %xmm4,%xmm3
cmp    $0x1,%rbx
ja     1170 <cosine_sum_unsafe+0x60>
jmp    119b <cosine_sum_unsafe+0x8b>
movapd %xmm3,%xmm4
mulsd  %xmm4,%xmm0
addsd  (%r14),%xmm0
subsd  %xmm1,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
@M_PI = global i32 0
define double @cosine_sum_unsafe(double %0, i32 %1, double* nocapture %2) {
%4 = load i32, i32* @M_PI
%5 = shl nsw i32 %4, 1
%6 = sitofp i32 %5 to double
%7 = fmul double %6, %0
%8 = fptosi double %7 to i32
%9 = tail call double @cos(i32 %8)
%10 = fadd double %9, %9
%11 = add nsw i32 %1, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds double, double* %2, i64 %12
%14 = load double, double* %13
%15 = add nsw i32 %1, -2
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds double, double* %2, i64 %16
%18 = load double, double* %17
%19 = tail call double @llvm.fmuladd.f64(double %10, double %14, double %18)
%20 = icmp sgt i32 %1, 3
br i1 %20, label %21, label %24
21: ; preds = %3
%22 = add nsw i32 %1, -3
%23 = zext i32 %22 to i64
br label %30
24: ; preds = %30, %3
%25 = phi double [ %19, %3 ], [ %37, %30 ]
%26 = phi double [ %14, %3 ], [ %33, %30 ]
%27 = load double, double* %2
%28 = tail call double @llvm.fmuladd.f64(double %9, double %25, double %27)
%29 = fsub double %28, %26
ret double %29
30: ; preds = %21, %30
%31 = phi i64 [ %23, %21 ], [ %38, %30 ]
%32 = phi double [ %14, %21 ], [ %33, %30 ]
%33 = phi double [ %19, %21 ], [ %37, %30 ]
%34 = getelementptr inbounds double, double* %2, i64 %31
%35 = load double, double* %34
%36 = tail call double @llvm.fmuladd.f64(double %10, double %33, double %35)
%37 = fsub double %36, %32
%38 = add nsw i64 %31, -1
%39 = icmp ugt i64 %31, 1
br i1 %39, label %30, label %24
}