#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 __frem_for_triangle(double, int *);
double __kernel_cos(double);
double __kernel_sin(double);
double cos(double x) {
  int i;
  double y;
  x = x > 0 ? x : -x;
  y = __frem_for_triangle(x, &i);
  switch (i % 8) {
  case 0:
    return __kernel_cos(y);
  case 1:
    return __kernel_sin(0.7853981 - y);
  case 2:
    return -__kernel_sin(y);
  case 3:
    return -__kernel_cos(0.7853981 - y);
  case 4:
    return -__kernel_cos(y);
  case 5:
    return -__kernel_sin(0.7853981 - y);
  case 6:
    return __kernel_sin(y);
  default:
    return __kernel_cos(0.7853981 - y);
  }
}


x86 assembly (clang, -O0):
<cos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    115e <cos+0x2e>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    117f <cos+0x4f>
movsd  -0x10(%rbp),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
lea    -0x14(%rbp),%rdi
call   1030 <__frem_for_triangle@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x7,%edx
test   %eax,%eax
mov    %eax,%ecx
cmovs  %edx,%ecx
and    $0xfffffff8,%ecx
sub    %ecx,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x6,%eax
ja     12d3 <cos+0x1a3>
mov    -0x30(%rbp),%rax
lea    0xe3e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  0xe11(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  0xdca(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  0xd6c(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
movsd  %xmm0,-0x8(%rbp)
jmp    12ea <cos+0x1ba>
movsd  0xd25(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cos>:
push   %rax
movapd 0xec7(%rip),%xmm1
xorpd  %xmm0,%xmm1
xorpd  %xmm2,%xmm2
cmpltsd %xmm0,%xmm2
andpd  %xmm2,%xmm0
andnpd %xmm1,%xmm2
orpd   %xmm2,%xmm0
lea    0x4(%rsp),%rdi
call   1030 <__frem_for_triangle@plt>
mov    0x4(%rsp),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffff8,%ecx
sub    %ecx,%eax
cmp    $0x6,%eax
ja     1199 <cos+0x69>
lea    0xe9f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movsd  0xe86(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <__kernel_sin@plt>
pop    %rax
ret
movsd  0xe6f(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <__kernel_cos@plt>
pop    %rax
ret
movsd  0xe58(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <__kernel_cos@plt>
jmp    11dc <cos+0xac>
movsd  0xe41(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <__kernel_sin@plt>
xorpd  0xe1c(%rip),%xmm0
pop    %rax
ret

LLVM IR (-O0):
define double @cos(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
%5 = alloca double
store double %0, double* %3
%6 = load double, double* %3
%7 = fcmp ogt double %6, 0.000000e+00
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = load double, double* %3
br label %13
10: ; preds = %1
%11 = load double, double* %3
%12 = fneg double %11
br label %13
13: ; preds = %10, %8
%14 = phi double [ %9, %8 ], [ %12, %10 ]
store double %14, double* %3
%15 = load double, double* %3
%16 = call double @__frem_for_triangle(double %15, i32* %4)
store double %16, double* %5
%17 = load i32, i32* %4
%18 = srem i32 %17, 8
switch i32 %18, label %47 [
i32 0, label %19
i32 1, label %22
i32 2, label %26
i32 3, label %30
i32 4, label %35
i32 5, label %39
i32 6, label %44
]
19: ; preds = %13
%20 = load double, double* %5
%21 = call double @__kernel_cos(double %20)
store double %21, double* %2
br label %51
22: ; preds = %13
%23 = load double, double* %5
%24 = fsub double 0x3FE921FB323AE5AF, %23
%25 = call double @__kernel_sin(double %24)
store double %25, double* %2
br label %51
26: ; preds = %13
%27 = load double, double* %5
%28 = call double @__kernel_sin(double %27)
%29 = fneg double %28
store double %29, double* %2
br label %51
30: ; preds = %13
%31 = load double, double* %5
%32 = fsub double 0x3FE921FB323AE5AF, %31
%33 = call double @__kernel_cos(double %32)
%34 = fneg double %33
store double %34, double* %2
br label %51
35: ; preds = %13
%36 = load double, double* %5
%37 = call double @__kernel_cos(double %36)
%38 = fneg double %37
store double %38, double* %2
br label %51
39: ; preds = %13
%40 = load double, double* %5
%41 = fsub double 0x3FE921FB323AE5AF, %40
%42 = call double @__kernel_sin(double %41)
%43 = fneg double %42
store double %43, double* %2
br label %51
44: ; preds = %13
%45 = load double, double* %5
%46 = call double @__kernel_sin(double %45)
store double %46, double* %2
br label %51
47: ; preds = %13
%48 = load double, double* %5
%49 = fsub double 0x3FE921FB323AE5AF, %48
%50 = call double @__kernel_cos(double %49)
store double %50, double* %2
br label %51
51: ; preds = %47, %44, %39, %35, %30, %26, %22, %19
%52 = load double, double* %2
ret double %52
}

LLVM IR (-O2):
define double @cos(double %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = fcmp ogt double %0, 0.000000e+00
%5 = fneg double %0
%6 = select i1 %4, double %0, double %5
%7 = call double @__frem_for_triangle(double %6, i32* %2)
%8 = load i32, i32* %2
%9 = srem i32 %8, 8
switch i32 %9, label %31 [
i32 0, label %10
i32 1, label %12
i32 2, label %15
i32 3, label %18
i32 4, label %22
i32 5, label %25
i32 6, label %29
]
10: ; preds = %1
%11 = call double @__kernel_cos(double %7)
br label %34
12: ; preds = %1
%13 = fsub double 0x3FE921FB323AE5AF, %7
%14 = call double @__kernel_sin(double %13)
br label %34
15: ; preds = %1
%16 = call double @__kernel_sin(double %7)
%17 = fneg double %16
br label %34
18: ; preds = %1
%19 = fsub double 0x3FE921FB323AE5AF, %7
%20 = call double @__kernel_cos(double %19)
%21 = fneg double %20
br label %34
22: ; preds = %1
%23 = call double @__kernel_cos(double %7)
%24 = fneg double %23
br label %34
25: ; preds = %1
%26 = fsub double 0x3FE921FB323AE5AF, %7
%27 = call double @__kernel_sin(double %26)
%28 = fneg double %27
br label %34
29: ; preds = %1
%30 = call double @__kernel_sin(double %7)
br label %34
31: ; preds = %1
%32 = fsub double 0x3FE921FB323AE5AF, %7
%33 = call double @__kernel_cos(double %32)
br label %34
34: ; preds = %31, %29, %25, %22, %18, %15, %12, %10
%35 = phi double [ %33, %31 ], [ %30, %29 ], [ %28, %25 ], [ %24, %22 ], [ %21, %18 ], [ %17, %15 ], [ %14, %12 ], [ %11, %10 ]
ret double %35
}