#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 */
scalar_t__ umath_double_factorial(int);
int umath_double_normalize_quadrant(double);
double umath_double_pow(double, int);
double umath_double_cos(double x) {
  int q = umath_double_normalize_quadrant(x);
  int neg = 0;
  int i;
  double result = 0.0;
  static double arr[4] = {1.0, 0.0, -1.0, 0.0};
  switch (q) {
  case 2:
    x = (3.14159265) - x;
    neg = 1;
    break;
  case 3:
    x = (3.14159265) - (((3.14159265) * 2) - x);
    neg = 1;
    break;
  case 4:
    x = ((3.14159265) * 2) - x;
    neg = 1;
    break;
  }
  i = 10;
  while (i != 0) {
    result += arr[i & 3] * umath_double_pow(x, i) /
              ((double)umath_double_factorial(i));
    i--;
  }
  result += 1.0;
  if (neg == 1)
    result = -result;
  return result;
}


x86 assembly (clang, -O0):
<umath_double_cos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
call   1050 <umath_double_normalize_quadrant@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0x2,%eax
je     118f <umath_double_cos+0x5f>
jmp    116d <umath_double_cos+0x3d>
mov    -0x24(%rbp),%eax
sub    $0x3,%eax
je     11ad <umath_double_cos+0x7d>
jmp    117e <umath_double_cos+0x4e>
mov    -0x24(%rbp),%eax
sub    $0x4,%eax
je     11d7 <umath_double_cos+0xa7>
jmp    11f0 <umath_double_cos+0xc0>
movsd  0xe71(%rip),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
jmp    11f0 <umath_double_cos+0xc0>
movsd  0xe4b(%rip),%xmm1
subsd  -0x8(%rbp),%xmm1
movsd  0xe46(%rip),%xmm0
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
jmp    11f0 <umath_double_cos+0xc0>
movsd  0xe21(%rip),%xmm0
subsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movl   $0x1,-0x10(%rbp)
movl   $0xa,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1267 <umath_double_cos+0x137>
mov    -0x14(%rbp),%eax
and    $0x3,%eax
movslq %eax,%rcx
lea    0x2e2f(%rip),%rax
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    -0x14(%rbp),%edi
call   1030 <umath_double_pow@plt>
movaps %xmm0,%xmm1
movsd  -0x38(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <umath_double_factorial@plt>
movsd  -0x30(%rbp),%xmm0
cvtsi2sd %rax,%xmm1
divsd  %xmm1,%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11f7 <umath_double_cos+0xc7>
movsd  0xda1(%rip),%xmm0
addsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    12a4 <umath_double_cos+0x174>
movsd  -0x20(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x20(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<umath_double_cos>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
movsd  %xmm0,(%rsp)
call   1050 <umath_double_normalize_quadrant@plt>
cmp    $0x4,%eax
je     115d <umath_double_cos+0x2d>
cmp    $0x3,%eax
je     1173 <umath_double_cos+0x43>
cmp    $0x2,%eax
jne    118d <umath_double_cos+0x5d>
movsd  0xeb5(%rip),%xmm0
jmp    1165 <umath_double_cos+0x35>
movsd  0xe9b(%rip),%xmm0
subsd  (%rsp),%xmm0
mov    $0x1,%r14b
movapd %xmm0,%xmm1
jmp    1195 <umath_double_cos+0x65>
movsd  (%rsp),%xmm1
addsd  0xe88(%rip),%xmm1
addsd  0xe88(%rip),%xmm1
mov    $0x1,%r14b
jmp    1195 <umath_double_cos+0x65>
xor    %r14d,%r14d
movsd  (%rsp),%xmm1
mov    $0xa,%ebx
xorpd  %xmm0,%xmm0
lea    0xe8b(%rip),%r15
movsd  %xmm1,(%rsp)
nopw   0x0(%rax,%rax,1)
movapd %xmm0,0x10(%rsp)
mov    %ebx,%ebp
and    $0x3,%ebp
movsd  (%rsp),%xmm0
mov    %ebx,%edi
call   1030 <umath_double_pow@plt>
mulsd  (%r15,%rbp,8),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %ebx,%edi
call   1040 <umath_double_factorial@plt>
movapd 0x10(%rsp),%xmm0
xorps  %xmm1,%xmm1
cvtsi2sd %rax,%xmm1
movsd  0x8(%rsp),%xmm2
divsd  %xmm1,%xmm2
addsd  %xmm2,%xmm0
add    $0xffffffff,%ebx
jne    11b0 <umath_double_cos+0x80>
addsd  0xe15(%rip),%xmm0
test   %r14b,%r14b
je     1210 <umath_double_cos+0xe0>
xorpd  0xe10(%rip),%xmm0
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@umath_double_cos.arr = internal global [4 x double] [double 1.000000e+00, double 0.000000e+00, double -1.000000e+00, double 0.000000e+00]
define double @umath_double_cos(double %0) {
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store double %0, double* %2
%7 = load double, double* %2
%8 = call i32 @umath_double_normalize_quadrant(double %7)
store i32 %8, i32* %3
store i32 0, i32* %4
store double 0.000000e+00, double* %6
%9 = load i32, i32* %3
switch i32 %9, label %20 [
i32 2, label %10
i32 3, label %13
i32 4, label %17
]
10: ; preds = %1
%11 = load double, double* %2
%12 = fsub double 0x400921FB53C8D4F1, %11
store double %12, double* %2
store i32 1, i32* %4
br label %20
13: ; preds = %1
%14 = load double, double* %2
%15 = fsub double 0x401921FB53C8D4F1, %14
%16 = fsub double 0x400921FB53C8D4F1, %15
store double %16, double* %2
store i32 1, i32* %4
br label %20
17: ; preds = %1
%18 = load double, double* %2
%19 = fsub double 0x401921FB53C8D4F1, %18
store double %19, double* %2
store i32 1, i32* %4
br label %20
20: ; preds = %1, %17, %13, %10
store i32 10, i32* %5
br label %21
21: ; preds = %24, %20
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %21
%25 = load i32, i32* %5
%26 = and i32 %25, 3
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [4 x double], [4 x double]* @umath_double_cos.arr, i64 0, i64 %27
%29 = load double, double* %28
%30 = load double, double* %2
%31 = load i32, i32* %5
%32 = call double @umath_double_pow(double %30, i32 %31)
%33 = fmul double %29, %32
%34 = load i32, i32* %5
%35 = call i64 @umath_double_factorial(i32 %34)
%36 = sitofp i64 %35 to double
%37 = fdiv double %33, %36
%38 = load double, double* %6
%39 = fadd double %38, %37
store double %39, double* %6
%40 = load i32, i32* %5
%41 = add nsw i32 %40, -1
store i32 %41, i32* %5
br label %21
42: ; preds = %21
%43 = load double, double* %6
%44 = fadd double %43, 1.000000e+00
store double %44, double* %6
%45 = load i32, i32* %4
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load double, double* %6
%49 = fneg double %48
store double %49, double* %6
br label %50
50: ; preds = %47, %42
%51 = load double, double* %6
ret double %51
}

LLVM IR (-O2):
@umath_double_cos.arr = internal unnamed_addr constant [4 x double] [double 1.000000e+00, double 0.000000e+00, double -1.000000e+00, double 0.000000e+00]
define double @umath_double_cos(double %0) {
%2 = tail call i32 @umath_double_normalize_quadrant(double %0)
switch i32 %2, label %10 [
i32 2, label %3
i32 3, label %5
i32 4, label %8
]
3: ; preds = %1
%4 = fsub double 0x400921FB53C8D4F1, %0
br label %10
5: ; preds = %1
%6 = fadd double %0, 0xC01921FB53C8D4F1
%7 = fadd double %6, 0x400921FB53C8D4F1
br label %10
8: ; preds = %1
%9 = fsub double 0x401921FB53C8D4F1, %0
br label %10
10: ; preds = %1, %8, %5, %3
%11 = phi double [ %0, %1 ], [ %9, %8 ], [ %7, %5 ], [ %4, %3 ]
%12 = phi i1 [ false, %1 ], [ true, %8 ], [ true, %5 ], [ true, %3 ]
br label %13
13: ; preds = %10, %13
%14 = phi double [ 0.000000e+00, %10 ], [ %25, %13 ]
%15 = phi i32 [ 10, %10 ], [ %26, %13 ]
%16 = and i32 %15, 3
%17 = zext i32 %16 to i64
%18 = getelementptr inbounds [4 x double], [4 x double]* @umath_double_cos.arr, i64 0, i64 %17
%19 = load double, double* %18
%20 = tail call double @umath_double_pow(double %11, i32 %15)
%21 = fmul double %19, %20
%22 = tail call i64 @umath_double_factorial(i32 %15)
%23 = sitofp i64 %22 to double
%24 = fdiv double %21, %23
%25 = fadd double %14, %24
%26 = add nsw i32 %15, -1
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %13
28: ; preds = %13
%29 = fadd double %25, 1.000000e+00
%30 = fneg double %29
%31 = select i1 %12, double %30, double %29
ret double %31
}