#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 powerfact(double, int);
double cosine(double x) {
  int i = 0, s = 1;
  double pf, sum = 0;
  while (1) {
    pf = powerfact(x, 2 * i);
    if (-1e-16 < pf && pf < 1e-16)
      break;
    else
      sum += pf * s;
    i++;
    s *= -1;
  }
  return sum;
}


x86 assembly (clang, -O0):
<cosine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movsd  %xmm0,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x1,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    -0xc(%rbp),%edi
shl    $0x1,%edi
call   1030 <powerfact@plt>
movsd  %xmm0,-0x18(%rbp)
movsd  -0x18(%rbp),%xmm0
movsd  0xeab(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1177 <cosine+0x67>
movsd  0xea1(%rip),%xmm0
ucomisd -0x18(%rbp),%xmm0
jbe    1177 <cosine+0x67>
jmp    11a8 <cosine+0x98>
movsd  -0x18(%rbp),%xmm0
cvtsi2sdl -0x10(%rbp),%xmm2
movsd  -0x20(%rbp),%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
imul   $0xffffffff,-0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1133 <cosine+0x23>
movsd  -0x20(%rbp),%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosine>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x8(%rsp)
xor    %edi,%edi
call   1030 <powerfact@plt>
ucomisd 0xed5(%rip),%xmm0
jbe    113f <cosine+0x2f>
xorpd  %xmm2,%xmm2
movsd  0xecf(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1195 <cosine+0x85>
mov    $0x1,%ebp
xorpd  %xmm2,%xmm2
mov    $0x2,%ebx
nopl   (%rax)
xorps  %xmm1,%xmm1
cvtsi2sd %ebp,%xmm1
mulsd  %xmm0,%xmm1
addsd  %xmm1,%xmm2
movsd  %xmm2,0x10(%rsp)
neg    %ebp
movsd  0x8(%rsp),%xmm0
mov    %ebx,%edi
call   1030 <powerfact@plt>
movsd  0x10(%rsp),%xmm2
add    $0x2,%ebx
ucomisd 0xe7b(%rip),%xmm0
jbe    1150 <cosine+0x40>
movsd  0xe79(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1150 <cosine+0x40>
movapd %xmm2,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define double @cosine(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca double
%6 = alloca double
store double %0, double* %2
store i32 0, i32* %3
store i32 1, i32* %4
store double 0.000000e+00, double* %6
br label %7
7: ; preds = %1, %24
%8 = load double, double* %2
%9 = load i32, i32* %3
%10 = mul nsw i32 2, %9
%11 = call double @powerfact(double %8, i32 %10)
store double %11, double* %5
%12 = load double, double* %5
%13 = fcmp olt double 0xBC9CD2B297D889BC, %12
br i1 %13, label %14, label %18
14: ; preds = %7
%15 = load double, double* %5
%16 = fcmp olt double %15, 0x3C9CD2B297D889BC
br i1 %16, label %17, label %18
17: ; preds = %14
br label %29
18: ; preds = %14, %7
%19 = load double, double* %5
%20 = load i32, i32* %4
%21 = sitofp i32 %20 to double
%22 = load double, double* %6
%23 = call double @llvm.fmuladd.f64(double %19, double %21, double %22)
store double %23, double* %6
br label %24
24: ; preds = %18
%25 = load i32, i32* %3
%26 = add nsw i32 %25, 1
store i32 %26, i32* %3
%27 = load i32, i32* %4
%28 = mul nsw i32 %27, -1
store i32 %28, i32* %4
br label %7
29: ; preds = %17
%30 = load double, double* %6
ret double %30
}

LLVM IR (-O2):
define double @cosine(double %0) {
%2 = tail call double @powerfact(double %0, i32 0)
%3 = fcmp ogt double %2, 0xBC9CD2B297D889BC
%4 = fcmp olt double %2, 0x3C9CD2B297D889BC
%5 = and i1 %3, %4
br i1 %5, label %20, label %6
6: ; preds = %1, %6
%7 = phi double [ %16, %6 ], [ %2, %1 ]
%8 = phi double [ %12, %6 ], [ 0.000000e+00, %1 ]
%9 = phi i32 [ %13, %6 ], [ 0, %1 ]
%10 = phi i32 [ %14, %6 ], [ 1, %1 ]
%11 = sitofp i32 %10 to double
%12 = tail call double @llvm.fmuladd.f64(double %7, double %11, double %8)
%13 = add nuw nsw i32 %9, 1
%14 = sub nsw i32 0, %10
%15 = shl nuw nsw i32 %13, 1
%16 = tail call double @powerfact(double %0, i32 %15)
%17 = fcmp ogt double %16, 0xBC9CD2B297D889BC
%18 = fcmp olt double %16, 0x3C9CD2B297D889BC
%19 = and i1 %17, %18
br i1 %19, label %20, label %6
20: ; preds = %6, %1
%21 = phi double [ 0.000000e+00, %1 ], [ %12, %6 ]
ret double %21
}