#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 */
double an(int);
double bn(int);
double cos(int);
double sin(int);
double func(double x, int N) {
  double ret = 3.14159265 / 8;
  int i;
  for (i = 1; i < N; i++) {
    ret += an(i) * cos(i * x);
    ret += bn(i) * sin(i * x);
  }
  return ret;
}


x86 assembly (clang, -O0):
<func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %edi,-0xc(%rbp)
movsd  0xea8(%rip),%xmm0
movsd  %xmm0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f2 <func+0xb2>
mov    -0x1c(%rbp),%edi
call   1060 <an@plt>
movsd  %xmm0,-0x30(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1050 <cos@plt>
movaps %xmm0,%xmm2
movsd  -0x30(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <bn@plt>
movsd  %xmm0,-0x28(%rbp)
cvtsi2sdl -0x1c(%rbp),%xmm0
mulsd  -0x8(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1030 <sin@plt>
movaps %xmm0,%xmm2
movsd  -0x28(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1164 <func+0x24>
movsd  -0x18(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jl     11fa <func+0xba>
mov    %edi,%r14d
movsd  0xea4(%rip),%xmm2
mov    $0x1,%ebp
movsd  0xe9f(%rip),%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  %xmm0,0x18(%rsp)
cs nopw 0x0(%rax,%rax,1)
nop
movsd  %xmm2,0x8(%rsp)
mov    %ebp,%edi
call   1060 <an@plt>
movsd  %xmm0,(%rsp)
movsd  0x10(%rsp),%xmm0
mulsd  0x18(%rsp),%xmm0
cvttsd2si %xmm0,%ebx
mov    %ebx,%edi
call   1050 <cos@plt>
mulsd  (%rsp),%xmm0
addsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebp,%edi
call   1040 <bn@plt>
movsd  %xmm0,(%rsp)
mov    %ebx,%edi
call   1030 <sin@plt>
movapd %xmm0,%xmm2
mulsd  (%rsp),%xmm2
addsd  0x8(%rsp),%xmm2
movsd  0x10(%rsp),%xmm0
addsd  0xe1e(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
add    $0x1,%ebp
cmp    %ebp,%r14d
jne    1180 <func+0x40>
jmp    1202 <func+0xc2>
movsd  0xdfe(%rip),%xmm2
movapd %xmm2,%xmm0
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define double @func(double %0, i32 %1) {
%3 = alloca double
%4 = alloca i32
%5 = alloca double
%6 = alloca i32
store double %0, double* %3
store i32 %1, i32* %4
store double 0x3FD921FB53C8D4F1, double* %5
store i32 1, i32* %6
br label %7
7: ; preds = %32, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %4
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %35
11: ; preds = %7
%12 = load i32, i32* %6
%13 = call double @an(i32 %12)
%14 = load i32, i32* %6
%15 = sitofp i32 %14 to double
%16 = load double, double* %3
%17 = fmul double %15, %16
%18 = fptosi double %17 to i32
%19 = call double @cos(i32 %18)
%20 = load double, double* %5
%21 = call double @llvm.fmuladd.f64(double %13, double %19, double %20)
store double %21, double* %5
%22 = load i32, i32* %6
%23 = call double @bn(i32 %22)
%24 = load i32, i32* %6
%25 = sitofp i32 %24 to double
%26 = load double, double* %3
%27 = fmul double %25, %26
%28 = fptosi double %27 to i32
%29 = call double @sin(i32 %28)
%30 = load double, double* %5
%31 = call double @llvm.fmuladd.f64(double %23, double %29, double %30)
store double %31, double* %5
br label %32
32: ; preds = %11
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %7
35: ; preds = %7
%36 = load double, double* %5
ret double %36
}

LLVM IR (-O2):
define double @func(double %0, i32 %1) {
%3 = icmp sgt i32 %1, 1
br i1 %3, label %4, label %18
4: ; preds = %2, %4
%5 = phi i32 [ %16, %4 ], [ 1, %2 ]
%6 = phi double [ %15, %4 ], [ 0x3FD921FB53C8D4F1, %2 ]
%7 = tail call double @an(i32 %5)
%8 = sitofp i32 %5 to double
%9 = fmul double %8, %0
%10 = fptosi double %9 to i32
%11 = tail call double @cos(i32 %10)
%12 = tail call double @llvm.fmuladd.f64(double %7, double %11, double %6)
%13 = tail call double @bn(i32 %5)
%14 = tail call double @sin(i32 %10)
%15 = tail call double @llvm.fmuladd.f64(double %13, double %14, double %12)
%16 = add nuw nsw i32 %5, 1
%17 = icmp eq i32 %16, %1
br i1 %17, label %18, label %4
18: ; preds = %4, %2
%19 = phi double [ 0x3FD921FB53C8D4F1, %2 ], [ %15, %4 ]
ret double %19
}