#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 atan(int);
int /*<<< orphan*/ exit(int);
double my_fac(int);
double pow(double, int);
int /*<<< orphan*/ printf(char *);
double my_cos(double x) {
  double val1 = 1, val2 = 0, fac, term, sign = -1;
  int expnt = 4;
  if (x < 0) {
    printf("Invalid x value: 0 <= x <= pi/2\n\n");
    printf("usage: echo x value | ./p3");
    exit(1);
  } else if (x > (4 * atan(1) / 2)) {
    printf("Invalid x value: 0 <= x <= pi/2\n\n");
    printf("usage: echo x value | ./p3");
    exit(1);
  } else {
    fac = 2;
    term = x * x;
    val1 = val1 + sign * (term / fac);
    sign = -1 * sign;
    while (val1 != val2) {
      val2 = val1;
      val1 = val1 + sign * (term * pow(x, 2)) / (fac * my_fac(expnt));
      expnt = expnt + 2;
      sign = -1 * sign;
      fac = fac * my_fac(expnt);
      term = term * pow(x, 2);
    }
  }
  return val1;
}


x86 assembly (clang, -O0):
<my_cos>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  0xea0(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  0xe83(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
movl   $0x4,-0x34(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x8(%rbp),%xmm0
jbe    11b9 <my_cos+0x69>
lea    0xe7a(%rip),%rdi
call   1030 <printf@plt>
lea    0xe90(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    $0x1,%edi
call   1050 <atan@plt>
movsd  -0x40(%rbp),%xmm0
shl    $0x2,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cvtsi2sd %eax,%xmm1
ucomisd %xmm1,%xmm0
jbe    120d <my_cos+0xbd>
lea    0xe26(%rip),%rdi
call   1030 <printf@plt>
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movsd  0xdfb(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x8(%rbp),%xmm0
mulsd  -0x8(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm1
movsd  -0x30(%rbp),%xmm0
movsd  -0x28(%rbp),%xmm2
divsd  -0x20(%rbp),%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
movsd  0xdae(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x10(%rbp),%xmm0
ucomisd -0x18(%rbp),%xmm0
jne    1277 <my_cos+0x127>
jp     1277 <my_cos+0x127>
jmp    136a <my_cos+0x21a>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x18(%rbp)
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x70(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x78(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
movsd  -0x78(%rbp),%xmm1
movaps %xmm0,%xmm2
movsd  -0x70(%rbp),%xmm0
mulsd  %xmm2,%xmm1
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x60(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <my_fac@plt>
movsd  -0x68(%rbp),%xmm2
movsd  -0x60(%rbp),%xmm1
movaps %xmm0,%xmm3
movsd  -0x58(%rbp),%xmm0
mulsd  %xmm3,%xmm2
divsd  %xmm2,%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x10(%rbp)
mov    -0x34(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x34(%rbp)
movsd  0xcf2(%rip),%xmm0
mulsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <my_fac@plt>
movaps %xmm0,%xmm1
movsd  -0x50(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x8(%rbp),%xmm0
mov    $0x2,%edi
call   1060 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x48(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x28(%rbp)
jmp    125c <my_cos+0x10c>
jmp    136f <my_cos+0x21f>
jmp    1374 <my_cos+0x224>
movsd  -0x10(%rbp),%xmm0
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_cos>:
push   %rbx
sub    $0x40,%rsp
xorpd  %xmm1,%xmm1
movsd  %xmm0,0x8(%rsp)
ucomisd %xmm0,%xmm1
ja     12b5 <my_cos+0x155>
mov    $0x1,%edi
call   1060 <atan@plt>
add    %eax,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  0x8(%rsp),%xmm1
ucomisd %xmm0,%xmm1
ja     12b5 <my_cos+0x155>
movapd %xmm1,%xmm2
mulsd  %xmm1,%xmm2
movsd  0xe54(%rip),%xmm3
mulsd  %xmm2,%xmm3
addsd  0xe50(%rip),%xmm3
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm3
jne    11c8 <my_cos+0x68>
jnp    12ab <my_cos+0x14b>
movsd  0xe40(%rip),%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0xe2a(%rip),%xmm0
movapd %xmm0,0x30(%rsp)
mov    $0x4,%ebx
nopl   0x0(%rax)
movsd  %xmm3,0x20(%rsp)
movsd  %xmm2,0x28(%rsp)
movapd %xmm1,%xmm0
mov    $0x2,%edi
call   1070 <pow@plt>
mulsd  0x28(%rsp),%xmm0
mulsd  0x30(%rsp),%xmm0
movsd  %xmm0,0x18(%rsp)
mov    %ebx,%edi
call   1050 <my_fac@plt>
mulsd  0x10(%rsp),%xmm0
movsd  0x18(%rsp),%xmm1
divsd  %xmm0,%xmm1
addsd  0x20(%rsp),%xmm1
movsd  %xmm1,0x18(%rsp)
add    $0x2,%ebx
movapd 0x30(%rsp),%xmm0
xorpd  0xdd0(%rip),%xmm0
movapd %xmm0,0x30(%rsp)
mov    %ebx,%edi
call   1050 <my_fac@plt>
movsd  0x10(%rsp),%xmm1
mulsd  %xmm0,%xmm1
movsd  %xmm1,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
mov    $0x2,%edi
call   1070 <pow@plt>
movsd  0x28(%rsp),%xmm2
movsd  0x8(%rsp),%xmm1
mulsd  %xmm0,%xmm2
movsd  0x18(%rsp),%xmm0
ucomisd 0x20(%rsp),%xmm0
movapd %xmm0,%xmm3
jne    11f0 <my_cos+0x90>
jp     11f0 <my_cos+0x90>
jmp    12af <my_cos+0x14f>
movapd %xmm3,%xmm0
add    $0x40,%rsp
pop    %rbx
ret
lea    0xd8f(%rip),%rdi
call   1030 <puts@plt>
lea    0xd68(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"Invalid x value: 0 <= x <= pi/2\0A\0A\00"
@.str.1 = [27 x i8] c"usage: echo x value | ./p3\00"
define double @my_cos(double %0) {
%2 = alloca double
%3 = alloca double
%4 = alloca double
%5 = alloca double
%6 = alloca double
%7 = alloca double
%8 = alloca i32
store double %0, double* %2
store double 1.000000e+00, double* %3
store double 0.000000e+00, double* %4
store double -1.000000e+00, double* %7
store i32 4, i32* %8
%9 = load double, double* %2
%10 = fcmp olt double %9, 0.000000e+00
br i1 %10, label %11, label %15
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %1
%16 = load double, double* %2
%17 = call i32 @atan(i32 1)
%18 = mul nsw i32 4, %17
%19 = sdiv i32 %18, 2
%20 = sitofp i32 %19 to double
%21 = fcmp ogt double %16, %20
br i1 %21, label %22, label %26
22: ; preds = %15
%23 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
store double 2.000000e+00, double* %5
%27 = load double, double* %2
%28 = load double, double* %2
%29 = fmul double %27, %28
store double %29, double* %6
%30 = load double, double* %3
%31 = load double, double* %7
%32 = load double, double* %6
%33 = load double, double* %5
%34 = fdiv double %32, %33
%35 = call double @llvm.fmuladd.f64(double %31, double %34, double %30)
store double %35, double* %3
%36 = load double, double* %7
%37 = fmul double -1.000000e+00, %36
store double %37, double* %7
br label %38
38: ; preds = %42, %26
%39 = load double, double* %3
%40 = load double, double* %4
%41 = fcmp une double %39, %40
br i1 %41, label %42, label %69
42: ; preds = %38
%43 = load double, double* %3
store double %43, double* %4
%44 = load double, double* %3
%45 = load double, double* %7
%46 = load double, double* %6
%47 = load double, double* %2
%48 = call double @pow(double %47, i32 2)
%49 = fmul double %46, %48
%50 = fmul double %45, %49
%51 = load double, double* %5
%52 = load i32, i32* %8
%53 = call double @my_fac(i32 %52)
%54 = fmul double %51, %53
%55 = fdiv double %50, %54
%56 = fadd double %44, %55
store double %56, double* %3
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 2
store i32 %58, i32* %8
%59 = load double, double* %7
%60 = fmul double -1.000000e+00, %59
store double %60, double* %7
%61 = load double, double* %5
%62 = load i32, i32* %8
%63 = call double @my_fac(i32 %62)
%64 = fmul double %61, %63
store double %64, double* %5
%65 = load double, double* %6
%66 = load double, double* %2
%67 = call double @pow(double %66, i32 2)
%68 = fmul double %65, %67
store double %68, double* %6
br label %38
69: ; preds = %38
br label %70
70: ; preds = %69
br label %71
71: ; preds = %70
%72 = load double, double* %3
ret double %72
}

LLVM IR (-O2):
@.str.1 = [27 x i8] c"usage: echo x value | ./p3\00"
@str.2 = [33 x i8] c"Invalid x value: 0 <= x <= pi/2\0A\00"
define double @my_cos(double %0) {
%2 = fcmp olt double %0, 0.000000e+00
br i1 %2, label %3, label %7
3: ; preds = %1
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.2, i64 0, i64 0))
%5 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = tail call i32 @atan(i32 1)
%9 = shl nsw i32 %8, 1
%10 = sitofp i32 %9 to double
%11 = fcmp olt double %10, %0
br i1 %11, label %12, label %16
12: ; preds = %7
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.2, i64 0, i64 0))
%14 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = fmul double %0, %0
%18 = fmul double %17, 5.000000e-01
%19 = tail call double @llvm.fmuladd.f64(double %18, double -1.000000e+00, double 1.000000e+00)
%20 = fcmp une double %19, 0.000000e+00
br i1 %20, label %21, label %41
21: ; preds = %16, %21
%22 = phi i32 [ %34, %21 ], [ 4, %16 ]
%23 = phi double [ %35, %21 ], [ 1.000000e+00, %16 ]
%24 = phi double [ %39, %21 ], [ %17, %16 ]
%25 = phi double [ %37, %21 ], [ 2.000000e+00, %16 ]
%26 = phi double [ %33, %21 ], [ %19, %16 ]
%27 = tail call double @pow(double %0, i32 2)
%28 = fmul double %24, %27
%29 = fmul double %23, %28
%30 = tail call double @my_fac(i32 %22)
%31 = fmul double %25, %30
%32 = fdiv double %29, %31
%33 = fadd double %26, %32
%34 = add nuw nsw i32 %22, 2
%35 = fneg double %23
%36 = tail call double @my_fac(i32 %34)
%37 = fmul double %25, %36
%38 = tail call double @pow(double %0, i32 2)
%39 = fmul double %24, %38
%40 = fcmp une double %33, %26
br i1 %40, label %21, label %41
41: ; preds = %21, %16
%42 = phi double [ %19, %16 ], [ %33, %21 ]
ret double %42
}