double cos(double x) {
  double cos;
  if (x < -3.14159265)
    x += 6.28318531;
  else if (x > 3.14159265)
    x -= 6.28318531;
  x += 1.57079632;
  if (x > 3.14159265)
    x -= 6.28318531;
  if (x < 0) {
    cos = 1.27323954 * x + 0.405284735 * x * x;
    if (cos < 0)
      cos = .225 * (cos * -cos - cos) + cos;
    else
      cos = .225 * (cos * cos - cos) + cos;
  } else {
    cos = 1.27323954 * x - 0.405284735 * x * x;
    if (cos < 0)
      cos = .225 * (cos * -cos - cos) + cos;
    else
      cos = .225 * (cos * cos - cos) + cos;
  }
  return cos;
}


x86 assembly (clang, -O0):
<cos>:
push   %rbp
mov    %rsp,%rbp
movsd  %xmm0,-0x8(%rbp)
movsd  0xeef(%rip),%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    1133 <cos+0x33>
movsd  0xeec(%rip),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1165 <cos+0x65>
movsd  -0x8(%rbp),%xmm0
movsd  0xec8(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    1160 <cos+0x60>
movsd  -0x8(%rbp),%xmm0
movsd  0xeb9(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    1165 <cos+0x65>
movsd  0xeab(%rip),%xmm0
addsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
movsd  0xe84(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    11a4 <cos+0xa4>
movsd  -0x8(%rbp),%xmm0
movsd  0xe75(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    129d <cos+0x19d>
movsd  -0x8(%rbp),%xmm0
movsd  0xe69(%rip),%xmm1
mulsd  -0x8(%rbp),%xmm1
mulsd  -0x8(%rbp),%xmm1
movsd  0xe4f(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1250 <cos+0x150>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm2
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  0xdf2(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    1298 <cos+0x198>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  0xda5(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    139f <cos+0x29f>
movsd  -0x8(%rbp),%xmm0
movsd  0xd7e(%rip),%xmm1
mulsd  -0x8(%rbp),%xmm1
mulsd  -0x8(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
movsd  0xd4d(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x10(%rbp),%xmm0
jbe    1352 <cos+0x252>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm2
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  0xcf0(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    139a <cos+0x29a>
movsd  -0x10(%rbp),%xmm0
movsd  -0x10(%rbp),%xmm2
movsd  -0x10(%rbp),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  -0x10(%rbp),%xmm1
movsd  0xca3(%rip),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
jmp    139f <cos+0x29f>
movsd  -0x10(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<cos>:
movsd  0xef8(%rip),%xmm1
ucomisd %xmm0,%xmm1
jbe    1118 <cos+0x18>
addsd  0xf02(%rip),%xmm0
jmp    112a <cos+0x2a>
ucomisd 0xee8(%rip),%xmm0
jbe    112a <cos+0x2a>
addsd  0xee6(%rip),%xmm0
addsd  0xeee(%rip),%xmm0
movsd  0xed6(%rip),%xmm2
addsd  %xmm0,%xmm2
movsd  0xec2(%rip),%xmm1
cmpltsd %xmm0,%xmm1
andpd  %xmm1,%xmm2
andnpd %xmm0,%xmm1
orpd   %xmm2,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm1,%xmm0
jbe    116b <cos+0x6b>
movsd  0xed7(%rip),%xmm3
jmp    1173 <cos+0x73>
movsd  0xeb5(%rip),%xmm3
mulsd  %xmm1,%xmm3
mulsd  %xmm1,%xmm3
movsd  0xead(%rip),%xmm2
mulsd  %xmm1,%xmm2
addsd  %xmm3,%xmm2
ucomisd %xmm2,%xmm0
movapd 0xeb9(%rip),%xmm1
xorpd  %xmm2,%xmm1
movapd %xmm2,%xmm0
jbe    11a7 <cos+0xa7>
mulsd  %xmm1,%xmm0
jmp    11ab <cos+0xab>
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
mulsd  0xe81(%rip),%xmm0
addsd  %xmm2,%xmm0
ret

LLVM IR (-O0):
define double @cos(double %0) {
%2 = alloca double
%3 = alloca double
store double %0, double* %2
%4 = load double, double* %2
%5 = fcmp olt double %4, 0xC00921FB53C8D4F1
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load double, double* %2
%8 = fadd double %7, 0x401921FB5474A168
store double %8, double* %2
br label %16
9: ; preds = %1
%10 = load double, double* %2
%11 = fcmp ogt double %10, 0x400921FB53C8D4F1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load double, double* %2
%14 = fsub double %13, 0x401921FB5474A168
store double %14, double* %2
br label %15
15: ; preds = %12, %9
br label %16
16: ; preds = %15, %6
%17 = load double, double* %2
%18 = fadd double %17, 0x3FF921FB52713C02
store double %18, double* %2
%19 = load double, double* %2
%20 = fcmp ogt double %19, 0x400921FB53C8D4F1
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load double, double* %2
%23 = fsub double %22, 0x401921FB5474A168
store double %23, double* %2
br label %24
24: ; preds = %21, %16
%25 = load double, double* %2
%26 = fcmp olt double %25, 0.000000e+00
br i1 %26, label %27, label %54
27: ; preds = %24
%28 = load double, double* %2
%29 = load double, double* %2
%30 = fmul double 0x3FD9F02F62992759, %29
%31 = load double, double* %2
%32 = fmul double %30, %31
%33 = call double @llvm.fmuladd.f64(double 0x3FF45F306C8462A6, double %28, double %32)
store double %33, double* %3
%34 = load double, double* %3
%35 = fcmp olt double %34, 0.000000e+00
br i1 %35, label %36, label %45
36: ; preds = %27
%37 = load double, double* %3
%38 = load double, double* %3
%39 = fneg double %38
%40 = load double, double* %3
%41 = fneg double %40
%42 = call double @llvm.fmuladd.f64(double %37, double %39, double %41)
%43 = load double, double* %3
%44 = call double @llvm.fmuladd.f64(double 2.250000e-01, double %42, double %43)
store double %44, double* %3
br label %53
45: ; preds = %27
%46 = load double, double* %3
%47 = load double, double* %3
%48 = load double, double* %3
%49 = fneg double %48
%50 = call double @llvm.fmuladd.f64(double %46, double %47, double %49)
%51 = load double, double* %3
%52 = call double @llvm.fmuladd.f64(double 2.250000e-01, double %50, double %51)
store double %52, double* %3
br label %53
53: ; preds = %45, %36
br label %82
54: ; preds = %24
%55 = load double, double* %2
%56 = load double, double* %2
%57 = fmul double 0x3FD9F02F62992759, %56
%58 = load double, double* %2
%59 = fmul double %57, %58
%60 = fneg double %59
%61 = call double @llvm.fmuladd.f64(double 0x3FF45F306C8462A6, double %55, double %60)
store double %61, double* %3
%62 = load double, double* %3
%63 = fcmp olt double %62, 0.000000e+00
br i1 %63, label %64, label %73
64: ; preds = %54
%65 = load double, double* %3
%66 = load double, double* %3
%67 = fneg double %66
%68 = load double, double* %3
%69 = fneg double %68
%70 = call double @llvm.fmuladd.f64(double %65, double %67, double %69)
%71 = load double, double* %3
%72 = call double @llvm.fmuladd.f64(double 2.250000e-01, double %70, double %71)
store double %72, double* %3
br label %81
73: ; preds = %54
%74 = load double, double* %3
%75 = load double, double* %3
%76 = load double, double* %3
%77 = fneg double %76
%78 = call double @llvm.fmuladd.f64(double %74, double %75, double %77)
%79 = load double, double* %3
%80 = call double @llvm.fmuladd.f64(double 2.250000e-01, double %78, double %79)
store double %80, double* %3
br label %81
81: ; preds = %73, %64
br label %82
82: ; preds = %81, %53
%83 = load double, double* %3
ret double %83
}

LLVM IR (-O2):
define double @cos(double %0) {
%2 = fcmp olt double %0, 0xC00921FB53C8D4F1
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = fadd double %0, 0x401921FB5474A168
br label %9
5: ; preds = %1
%6 = fcmp ogt double %0, 0x400921FB53C8D4F1
br i1 %6, label %7, label %9
7: ; preds = %5
%8 = fadd double %0, 0xC01921FB5474A168
br label %9
9: ; preds = %5, %7, %3
%10 = phi double [ %4, %3 ], [ %8, %7 ], [ %0, %5 ]
%11 = fadd double %10, 0x3FF921FB52713C02
%12 = fcmp ogt double %11, 0x400921FB53C8D4F1
%13 = fadd double %11, 0xC01921FB5474A168
%14 = select i1 %12, double %13, double %11
%15 = fcmp olt double %14, 0.000000e+00
br i1 %15, label %16, label %28
16: ; preds = %9
%17 = fmul double %14, 0x3FD9F02F62992759
%18 = fmul double %14, %17
%19 = tail call double @llvm.fmuladd.f64(double %14, double 0x3FF45F306C8462A6, double %18)
%20 = fcmp olt double %19, 0.000000e+00
%21 = fneg double %19
br i1 %20, label %22, label %25
22: ; preds = %16
%23 = tail call double @llvm.fmuladd.f64(double %19, double %21, double %21)
%24 = tail call double @llvm.fmuladd.f64(double %23, double 2.250000e-01, double %19)
br label %40
25: ; preds = %16
%26 = tail call double @llvm.fmuladd.f64(double %19, double %19, double %21)
%27 = tail call double @llvm.fmuladd.f64(double %26, double 2.250000e-01, double %19)
br label %40
28: ; preds = %9
%29 = fmul double %14, 0xBFD9F02F62992759
%30 = fmul double %14, %29
%31 = tail call double @llvm.fmuladd.f64(double %14, double 0x3FF45F306C8462A6, double %30)
%32 = fcmp olt double %31, 0.000000e+00
%33 = fneg double %31
br i1 %32, label %34, label %37
34: ; preds = %28
%35 = tail call double @llvm.fmuladd.f64(double %31, double %33, double %33)
%36 = tail call double @llvm.fmuladd.f64(double %35, double 2.250000e-01, double %31)
br label %40
37: ; preds = %28
%38 = tail call double @llvm.fmuladd.f64(double %31, double %31, double %33)
%39 = tail call double @llvm.fmuladd.f64(double %38, double 2.250000e-01, double %31)
br label %40
40: ; preds = %34, %37, %22, %25
%41 = phi double [ %24, %22 ], [ %27, %25 ], [ %36, %34 ], [ %39, %37 ]
ret double %41
}