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


x86 assembly (clang, -O0):
<sin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movsd  %xmm0,-0x10(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
mov    $0xffffffff,%eax
mov    $0x1,%ecx
ucomisd %xmm1,%xmm0
cmova  %ecx,%eax
mov    %eax,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    117a <sin+0x4a>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    119b <sin+0x6b>
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    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x4,%edx
jl     11e2 <sin+0xb2>
cmpl   $0x1,-0x18(%rbp)
jne    11e2 <sin+0xb2>
movl   $0xffffffff,-0x18(%rbp)
jmp    120c <sin+0xdc>
mov    -0x14(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x4,%edx
jl     1207 <sin+0xd7>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1207 <sin+0xd7>
movl   $0x1,-0x18(%rbp)
jmp    120c <sin+0xdc>
mov    -0x14(%rbp),%eax
mov    %eax,%edx
add    $0x3,%edx
test   %eax,%eax
mov    %eax,%ecx
cmovs  %edx,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
mov    %eax,-0x2c(%rbp)
je     1250 <sin+0x120>
jmp    122e <sin+0xfe>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
je     1276 <sin+0x146>
jmp    123f <sin+0x10f>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     12a4 <sin+0x174>
jmp    12ca <sin+0x19a>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12f3 <sin+0x1c3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x34(%rbp)
movsd  0xd7c(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12f3 <sin+0x1c3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
call   1050 <__kernel_cos@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12f3 <sin+0x1c3>
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsd  0xd28(%rip),%xmm0
subsd  -0x20(%rbp),%xmm0
call   1040 <__kernel_sin@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
imul   %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sin>:
push   %rbx
sub    $0x20,%rsp
movapd %xmm0,%xmm3
movapd 0xebf(%rip),%xmm1
xorpd  %xmm0,%xmm1
xorpd  %xmm0,%xmm0
cmpltsd %xmm3,%xmm0
movapd %xmm0,%xmm2
movapd %xmm3,0x10(%rsp)
andpd  %xmm3,%xmm2
andnpd %xmm1,%xmm0
orpd   %xmm2,%xmm0
lea    0xc(%rsp),%rdi
call   1030 <__frem_for_triangle@plt>
mov    0xc(%rsp),%eax
lea    0x7(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
lea    0x3(%rax),%edx
cmovns %eax,%edx
and    $0xfffffff8,%ecx
mov    %eax,%ebx
sub    %ecx,%ebx
and    $0xfffffffc,%edx
sub    %edx,%eax
cmp    $0x2,%eax
je     11c1 <sin+0x91>
cmp    $0x1,%eax
je     11b1 <sin+0x81>
test   %eax,%eax
je     11aa <sin+0x7a>
movsd  0xe6e(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1040 <__kernel_sin@plt>
jmp    11c6 <sin+0x96>
movsd  0xe57(%rip),%xmm1
subsd  %xmm0,%xmm1
movapd %xmm1,%xmm0
call   1050 <__kernel_cos@plt>
xor    %ecx,%ecx
cmp    $0x4,%ebx
setge  %cl
xorpd  %xmm0,%xmm0
movapd 0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
lea    -0x1(%rcx,%rcx,1),%ecx
mov    $0x1,%edx
cmovbe %ecx,%edx
cmp    $0x4,%ebx
mov    $0xffffffff,%ecx
cmovl  %edx,%ecx
ucomisd %xmm0,%xmm1
cmovbe %edx,%ecx
imul   %eax,%ecx
xorps  %xmm0,%xmm0
cvtsi2sd %ecx,%xmm0
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define double @sin(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store double %0, double* %3
%7 = load double, double* %3
%8 = fcmp ogt double %7, 0.000000e+00
%9 = zext i1 %8 to i64
%10 = select i1 %8, i32 1, i32 -1
store i32 %10, i32* %5
%11 = load double, double* %3
%12 = fcmp ogt double %11, 0.000000e+00
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load double, double* %3
br label %18
15: ; preds = %1
%16 = load double, double* %3
%17 = fneg double %16
br label %18
18: ; preds = %15, %13
%19 = phi double [ %14, %13 ], [ %17, %15 ]
store double %19, double* %3
%20 = load double, double* %3
%21 = call double @__frem_for_triangle(double %20, i32* %4)
store double %21, double* %6
%22 = load i32, i32* %4
%23 = srem i32 %22, 8
%24 = icmp sge i32 %23, 4
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %29
28: ; preds = %25
store i32 -1, i32* %5
br label %38
29: ; preds = %25, %18
%30 = load i32, i32* %4
%31 = srem i32 %30, 8
%32 = icmp sge i32 %31, 4
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %37
36: ; preds = %33
store i32 1, i32* %5
br label %37
37: ; preds = %36, %33, %29
br label %38
38: ; preds = %37, %28
%39 = load i32, i32* %4
%40 = srem i32 %39, 4
switch i32 %40, label %60 [
i32 0, label %41
i32 1, label %47
i32 2, label %54
]
41: ; preds = %38
%42 = load i32, i32* %5
%43 = load double, double* %6
%44 = call i32 @__kernel_sin(double %43)
%45 = mul nsw i32 %42, %44
%46 = sitofp i32 %45 to double
store double %46, double* %2
br label %67
47: ; preds = %38
%48 = load i32, i32* %5
%49 = load double, double* %6
%50 = fsub double 0x3FE921FB323AE5AF, %49
%51 = call i32 @__kernel_cos(double %50)
%52 = mul nsw i32 %48, %51
%53 = sitofp i32 %52 to double
store double %53, double* %2
br label %67
54: ; preds = %38
%55 = load i32, i32* %5
%56 = load double, double* %6
%57 = call i32 @__kernel_cos(double %56)
%58 = mul nsw i32 %55, %57
%59 = sitofp i32 %58 to double
store double %59, double* %2
br label %67
60: ; preds = %38
%61 = load i32, i32* %5
%62 = load double, double* %6
%63 = fsub double 0x3FE921FB323AE5AF, %62
%64 = call i32 @__kernel_sin(double %63)
%65 = mul nsw i32 %61, %64
%66 = sitofp i32 %65 to double
store double %66, double* %2
br label %67
67: ; preds = %60, %54, %47, %41
%68 = load double, double* %2
ret double %68
}

LLVM IR (-O2):
define double @sin(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
%10 = icmp sgt i32 %9, 3
%11 = srem i32 %8, 4
switch i32 %11, label %19 [
i32 0, label %12
i32 1, label %14
i32 2, label %17
]
12: ; preds = %1
%13 = call i32 @__kernel_sin(double %7)
br label %22
14: ; preds = %1
%15 = fsub double 0x3FE921FB323AE5AF, %7
%16 = call i32 @__kernel_cos(double %15)
br label %22
17: ; preds = %1
%18 = call i32 @__kernel_cos(double %7)
br label %22
19: ; preds = %1
%20 = fsub double 0x3FE921FB323AE5AF, %7
%21 = call i32 @__kernel_sin(double %20)
br label %22
22: ; preds = %19, %17, %14, %12
%23 = phi i32 [ %21, %19 ], [ %18, %17 ], [ %16, %14 ], [ %13, %12 ]
%24 = and i1 %4, %10
%25 = or i1 %4, %10
%26 = select i1 %25, i32 1, i32 -1
%27 = select i1 %24, i32 -1, i32 %26
%28 = mul nsw i32 %23, %27
%29 = sitofp i32 %28 to double
ret double %29
}