#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 */
int __ieee754_rem_pio2(double, double *);
int /*<<< orphan*/ __kernel_cos(double, double, double *);
int /*<<< orphan*/ __kernel_sin(double, double, int, double *);
void cos(double x, double *result) {
  double y[2], z = 0.0;
  int n, ix;
  ix = *(1 + (int *)&x);
  ix &= 0x7fffffff;
  if (ix <= 0x3fe921fb) {
    __kernel_cos(x, z, result);
    return;
  } else if (ix >= 0x7ff00000) {
    *result = x - x;
    return;
  } else {
    n = __ieee754_rem_pio2(x, y);
    switch (n & 3) {
    case 0:
      __kernel_cos(y[0], y[1], result);
      return;
    case 1:
      __kernel_sin(y[0], y[1], 1, result);
      *result = -*result;
      return;
    case 2:
      __kernel_cos(y[0], y[1], result);
      *result = -*result;
      return;
    default:
      __kernel_sin(y[0], y[1], 1, result);
      return;
    }
  }
}


x86 assembly (clang, -O0):
<cos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
and    $0x7fffffff,%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x3fe921fb,-0x30(%rbp)
jg     117f <cos+0x4f>
movsd  -0x8(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm1
mov    -0x10(%rbp),%rdi
call   1050 <__kernel_cos@plt>
jmp    129d <cos+0x16d>
cmpl   $0x7ff00000,-0x30(%rbp)
jl     11a3 <cos+0x73>
movsd  -0x8(%rbp),%xmm0
subsd  -0x8(%rbp),%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    129d <cos+0x16d>
movsd  -0x8(%rbp),%xmm0
lea    -0x20(%rbp),%rdi
call   1040 <__ieee754_rem_pio2@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x34(%rbp)
je     11ea <cos+0xba>
jmp    11c8 <cos+0x98>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
je     1202 <cos+0xd2>
jmp    11d9 <cos+0xa9>
mov    -0x34(%rbp),%eax
sub    $0x2,%eax
je     1246 <cos+0x116>
jmp    1285 <cos+0x155>
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x10(%rbp),%rdi
call   1050 <__kernel_cos@plt>
jmp    129d <cos+0x16d>
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x10(%rbp),%rsi
mov    $0x1,%edi
call   1030 <__kernel_sin@plt>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    129d <cos+0x16d>
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x10(%rbp),%rdi
call   1050 <__kernel_cos@plt>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mov    -0x10(%rbp),%rax
movsd  %xmm0,(%rax)
jmp    129d <cos+0x16d>
movsd  -0x20(%rbp),%xmm0
movsd  -0x18(%rbp),%xmm1
mov    -0x10(%rbp),%rsi
mov    $0x1,%edi
call   1030 <__kernel_sin@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cos>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
movq   %xmm0,%rax
shr    $0x20,%rax
and    $0x7fffffff,%eax
cmp    $0x3fe921fb,%eax
ja     115d <cos+0x2d>
xorps  %xmm1,%xmm1
mov    %rbx,%rdi
add    $0x10,%rsp
pop    %rbx
jmp    1050 <__kernel_cos@plt>
cmp    $0x7ff00000,%eax
jb     1171 <cos+0x41>
subsd  %xmm0,%xmm0
movsd  %xmm0,(%rbx)
jmp    11f6 <cos+0xc6>
mov    %rsp,%rdi
call   1040 <__ieee754_rem_pio2@plt>
and    $0x3,%eax
cmp    $0x2,%eax
je     119f <cos+0x6f>
cmp    $0x1,%eax
je     11b4 <cos+0x84>
test   %eax,%eax
jne    11de <cos+0xae>
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %rbx,%rdi
call   1050 <__kernel_cos@plt>
jmp    11f6 <cos+0xc6>
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    %rbx,%rdi
call   1050 <__kernel_cos@plt>
jmp    11cc <cos+0x9c>
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    $0x1,%edi
mov    %rbx,%rsi
call   1030 <__kernel_sin@plt>
movsd  (%rbx),%xmm0
xorpd  0xe28(%rip),%xmm0
movlpd %xmm0,(%rbx)
jmp    11f6 <cos+0xc6>
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
mov    $0x1,%edi
mov    %rbx,%rsi
call   1030 <__kernel_sin@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define void @cos(double %0, double* %1) {
%3 = alloca double
%4 = alloca double*
%5 = alloca [2 x double]
%6 = alloca double
%7 = alloca i32
%8 = alloca i32
store double %0, double* %3
store double* %1, double** %4
store double 0.000000e+00, double* %6
%9 = bitcast double* %3 to i32*
%10 = getelementptr inbounds i32, i32* %9, i64 1
%11 = load i32, i32* %10
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = and i32 %12, 2147483647
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp sle i32 %14, 1072243195
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load double, double* %3
%18 = load double, double* %6
%19 = load double*, double** %4
%20 = call i32 @__kernel_cos(double %17, double %18, double* %19)
br label %71
21: ; preds = %2
%22 = load i32, i32* %8
%23 = icmp sge i32 %22, 2146435072
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load double, double* %3
%26 = load double, double* %3
%27 = fsub double %25, %26
%28 = load double*, double** %4
store double %27, double* %28
br label %71
29: ; preds = %21
%30 = load double, double* %3
%31 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%32 = call i32 @__ieee754_rem_pio2(double %30, double* %31)
store i32 %32, i32* %7
%33 = load i32, i32* %7
%34 = and i32 %33, 3
switch i32 %34, label %64 [
i32 0, label %35
i32 1, label %42
i32 2, label %53
]
35: ; preds = %29
%36 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%37 = load double, double* %36
%38 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 1
%39 = load double, double* %38
%40 = load double*, double** %4
%41 = call i32 @__kernel_cos(double %37, double %39, double* %40)
br label %71
42: ; preds = %29
%43 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%44 = load double, double* %43
%45 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 1
%46 = load double, double* %45
%47 = load double*, double** %4
%48 = call i32 @__kernel_sin(double %44, double %46, i32 1, double* %47)
%49 = load double*, double** %4
%50 = load double, double* %49
%51 = fneg double %50
%52 = load double*, double** %4
store double %51, double* %52
br label %71
53: ; preds = %29
%54 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%55 = load double, double* %54
%56 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 1
%57 = load double, double* %56
%58 = load double*, double** %4
%59 = call i32 @__kernel_cos(double %55, double %57, double* %58)
%60 = load double*, double** %4
%61 = load double, double* %60
%62 = fneg double %61
%63 = load double*, double** %4
store double %62, double* %63
br label %71
64: ; preds = %29
%65 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 0
%66 = load double, double* %65
%67 = getelementptr inbounds [2 x double], [2 x double]* %5, i64 0, i64 1
%68 = load double, double* %67
%69 = load double*, double** %4
%70 = call i32 @__kernel_sin(double %66, double %68, i32 1, double* %69)
br label %71
71: ; preds = %64, %53, %42, %35, %24, %16
ret void
}

LLVM IR (-O2):
define void @cos(double %0, double* %1) {
%3 = alloca [2 x double]
%4 = bitcast [2 x double]* %3 to i8*
%5 = bitcast double %0 to i64
%6 = lshr i64 %5, 32
%7 = trunc i64 %6 to i32
%8 = and i32 %7, 2147483647
%9 = icmp ult i32 %8, 1072243196
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @__kernel_cos(double %0, double 0.000000e+00, double* %1)
br label %44
12: ; preds = %2
%13 = icmp ugt i32 %8, 2146435071
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = fsub double %0, %0
store double %15, double* %1
br label %44
16: ; preds = %12
%17 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 0
%18 = call i32 @__ieee754_rem_pio2(double %0, double* %17)
%19 = and i32 %18, 3
switch i32 %19, label %39 [
i32 0, label %20
i32 1, label %25
i32 2, label %32
]
20: ; preds = %16
%21 = load double, double* %17
%22 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
%23 = load double, double* %22
%24 = call i32 @__kernel_cos(double %21, double %23, double* %1)
br label %44
25: ; preds = %16
%26 = load double, double* %17
%27 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
%28 = load double, double* %27
%29 = call i32 @__kernel_sin(double %26, double %28, i32 1, double* %1)
%30 = load double, double* %1
%31 = fneg double %30
store double %31, double* %1
br label %44
32: ; preds = %16
%33 = load double, double* %17
%34 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
%35 = load double, double* %34
%36 = call i32 @__kernel_cos(double %33, double %35, double* %1)
%37 = load double, double* %1
%38 = fneg double %37
store double %38, double* %1
br label %44
39: ; preds = %16
%40 = load double, double* %17
%41 = getelementptr inbounds [2 x double], [2 x double]* %3, i64 0, i64 1
%42 = load double, double* %41
%43 = call i32 @__kernel_sin(double %40, double %42, i32 1, double* %1)
br label %44
44: ; preds = %39, %32, %25, %20, %14, %10
ret void
}