#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, int, double *);
double cosine_sum(double phase, int num_coefs, double *coefs) {
  if (num_coefs == 0) {
    return 0;
  } else if (num_coefs == 1) {
    return coefs[0];
  } else if (num_coefs == 2) {
    return coefs[0] + cos(2 * M_PI * phase) * coefs[1];
  }
  return cosine_sum_unsafe(phase, num_coefs, coefs);
}


x86 assembly (clang, -O0):
<cosine_sum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %rsi,-0x20(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    114b <cosine_sum+0x2b>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dc <cosine_sum+0xbc>
cmpl   $0x1,-0x14(%rbp)
jne    1167 <cosine_sum+0x47>
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dc <cosine_sum+0xbc>
cmpl   $0x2,-0x14(%rbp)
jne    11bc <cosine_sum+0x9c>
mov    -0x20(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
shl    $0x1,%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x10(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <cos@plt>
movsd  -0x28(%rbp),%xmm1
mov    -0x20(%rbp),%rax
movsd  0x8(%rax),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    11dc <cosine_sum+0xbc>
jmp    11c1 <cosine_sum+0xa1>
jmp    11c6 <cosine_sum+0xa6>
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1030 <cosine_sum_unsafe@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosine_sum>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     113c <cosine_sum+0x1c>
mov    %rsi,%rbx
cmp    $0x2,%edi
je     1142 <cosine_sum+0x22>
cmp    $0x1,%edi
jne    1179 <cosine_sum+0x59>
movsd  (%rbx),%xmm0
jmp    1173 <cosine_sum+0x53>
xorpd  %xmm0,%xmm0
jmp    1173 <cosine_sum+0x53>
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
add    %eax,%eax
cvtsi2sd %eax,%xmm1
movsd  (%rbx),%xmm2
movsd  %xmm2,0x8(%rsp)
mulsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edi
call   1040 <cos@plt>
mulsd  0x8(%rbx),%xmm0
addsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
ret
mov    %rbx,%rsi
add    $0x10,%rsp
pop    %rbx
jmp    1030 <cosine_sum_unsafe@plt>

LLVM IR (-O0):
@M_PI = global i32 0
define double @cosine_sum(double %0, i32 %1, double* %2) {
%4 = alloca double
%5 = alloca double
%6 = alloca i32
%7 = alloca double*
store double %0, double* %5
store i32 %1, i32* %6
store double* %2, double** %7
%8 = load i32, i32* %6
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %3
store double 0.000000e+00, double* %4
br label %43
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load double*, double** %7
%16 = getelementptr inbounds double, double* %15, i64 0
%17 = load double, double* %16
store double %17, double* %4
br label %43
18: ; preds = %11
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %36
21: ; preds = %18
%22 = load double*, double** %7
%23 = getelementptr inbounds double, double* %22, i64 0
%24 = load double, double* %23
%25 = load i32, i32* @M_PI
%26 = mul nsw i32 2, %25
%27 = sitofp i32 %26 to double
%28 = load double, double* %5
%29 = fmul double %27, %28
%30 = fptosi double %29 to i32
%31 = call double @cos(i32 %30)
%32 = load double*, double** %7
%33 = getelementptr inbounds double, double* %32, i64 1
%34 = load double, double* %33
%35 = call double @llvm.fmuladd.f64(double %31, double %34, double %24)
store double %35, double* %4
br label %43
36: ; preds = %18
br label %37
37: ; preds = %36
br label %38
38: ; preds = %37
%39 = load double, double* %5
%40 = load i32, i32* %6
%41 = load double*, double** %7
%42 = call double @cosine_sum_unsafe(double %39, i32 %40, double* %41)
store double %42, double* %4
br label %43
43: ; preds = %38, %21, %14, %10
%44 = load double, double* %4
ret double %44
}

LLVM IR (-O2):
@M_PI = global i32 0
define double @cosine_sum(double %0, i32 %1, double* %2) {
switch i32 %1, label %17 [
i32 0, label %19
i32 1, label %4
i32 2, label %6
]
4: ; preds = %3
%5 = load double, double* %2
br label %19
6: ; preds = %3
%7 = load double, double* %2
%8 = load i32, i32* @M_PI
%9 = shl nsw i32 %8, 1
%10 = sitofp i32 %9 to double
%11 = fmul double %10, %0
%12 = fptosi double %11 to i32
%13 = tail call double @cos(i32 %12)
%14 = getelementptr inbounds double, double* %2, i64 1
%15 = load double, double* %14
%16 = tail call double @llvm.fmuladd.f64(double %13, double %15, double %7)
br label %19
17: ; preds = %3
%18 = tail call double @cosine_sum_unsafe(double %0, i32 %1, double* %2)
br label %19
19: ; preds = %3, %17, %6, %4
%20 = phi double [ %5, %4 ], [ %16, %6 ], [ %18, %17 ], [ 0.000000e+00, %3 ]
ret double %20
}