#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 NAN;
double fabs(double);
scalar_t__ isinf(double);
scalar_t__ isnan(double);
double sd2eps(unsigned int);
double cosinush(double num, unsigned int sigdig) {
  if (sigdig < 1) {
    return NAN;
  }
  if (isnan(num) || isinf(num)) {
    return num;
  }
  double x = 1;
  double x2 = num * num;
  double result = x;
  double presult = 0;
  double k = 1;
  double fakt = 1;
  double eps = sd2eps(sigdig);
  while (fabs(result - presult) > eps) {
    x *= x2;
    fakt *= k * (k + 1);
    k += 2;
    presult = result;
    result = presult + x / fakt;
  }
  return result;
}


x86 assembly (clang, -O0):
<cosinush>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jae    115f <cosinush+0x2f>
mov    0x2ea7(%rip),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    127d <cosinush+0x14d>
movsd  -0x10(%rbp),%xmm0
call   1040 <isnan@plt>
cmp    $0x0,%rax
jne    1187 <cosinush+0x57>
movsd  -0x10(%rbp),%xmm0
call   1050 <isinf@plt>
cmp    $0x0,%rax
je     1196 <cosinush+0x66>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    127d <cosinush+0x14d>
movsd  0xe62(%rip),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x10(%rbp),%xmm0
mulsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  0xe34(%rip),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  0xe27(%rip),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <sd2eps@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x30(%rbp),%xmm0
subsd  -0x38(%rbp),%xmm0
movaps 0xe14(%rip),%xmm1
pand   %xmm1,%xmm0
ucomisd -0x50(%rbp),%xmm0
jbe    1273 <cosinush+0x143>
movsd  -0x28(%rbp),%xmm0
mulsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x20(%rbp)
movsd  -0x40(%rbp),%xmm0
movsd  0xdd9(%rip),%xmm1
addsd  -0x40(%rbp),%xmm1
mulsd  %xmm1,%xmm0
mulsd  -0x48(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  0xdc6(%rip),%xmm0
addsd  -0x40(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x38(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
divsd  -0x48(%rbp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x30(%rbp)
jmp    11eb <cosinush+0xbb>
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cosinush>:
push   %rbx
sub    $0x10,%rsp
test   %edi,%edi
je     1163 <cosinush+0x33>
mov    %edi,%ebx
movsd  %xmm0,0x8(%rsp)
call   1040 <isnan@plt>
test   %rax,%rax
jne    115b <cosinush+0x2b>
movsd  0x8(%rsp),%xmm0
call   1050 <isinf@plt>
test   %rax,%rax
je     1178 <cosinush+0x48>
movsd  0x8(%rsp),%xmm7
jmp    116e <cosinush+0x3e>
mov    0x2e8e(%rip),%rax
movsd  (%rax),%xmm7
movapd %xmm7,%xmm0
add    $0x10,%rsp
pop    %rbx
ret
mov    %ebx,%edi
call   1030 <sd2eps@plt>
movsd  0xe79(%rip),%xmm7
ucomisd %xmm0,%xmm7
movsd  0x8(%rsp),%xmm4
jbe    116e <cosinush+0x3e>
mulsd  %xmm4,%xmm4
movsd  0xe60(%rip),%xmm10
movsd  0xe5f(%rip),%xmm8
movapd 0xe5e(%rip),%xmm9
movapd %xmm10,%xmm5
movapd %xmm10,%xmm6
movapd %xmm10,%xmm2
movapd %xmm10,%xmm3
cs nopw 0x0(%rax,%rax,1)
mulsd  %xmm4,%xmm3
movapd %xmm6,%xmm1
addsd  %xmm10,%xmm1
mulsd  %xmm6,%xmm1
mulsd  %xmm1,%xmm5
addsd  %xmm8,%xmm6
movapd %xmm3,%xmm7
divsd  %xmm5,%xmm7
addsd  %xmm2,%xmm7
movapd %xmm7,%xmm1
subsd  %xmm2,%xmm1
andpd  %xmm9,%xmm1
ucomisd %xmm0,%xmm1
movapd %xmm7,%xmm2
ja     11d0 <cosinush+0xa0>
jmp    116e <cosinush+0x3e>

LLVM IR (-O0):
@NAN = global double 0.000000e+00
define double @cosinush(double %0, i32 %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca double
%8 = alloca double
%9 = alloca double
%10 = alloca double
%11 = alloca double
%12 = alloca double
store double %0, double* %4
store i32 %1, i32* %5
%13 = load i32, i32* %5
%14 = icmp ult i32 %13, 1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = load double, double* @NAN
store double %16, double* %3
br label %61
17: ; preds = %2
%18 = load double, double* %4
%19 = call i64 @isnan(double %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %17
%22 = load double, double* %4
%23 = call i64 @isinf(double %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21, %17
%26 = load double, double* %4
store double %26, double* %3
br label %61
27: ; preds = %21
store double 1.000000e+00, double* %6
%28 = load double, double* %4
%29 = load double, double* %4
%30 = fmul double %28, %29
store double %30, double* %7
%31 = load double, double* %6
store double %31, double* %8
store double 0.000000e+00, double* %9
store double 1.000000e+00, double* %10
store double 1.000000e+00, double* %11
%32 = load i32, i32* %5
%33 = call double @sd2eps(i32 %32)
store double %33, double* %12
br label %34
34: ; preds = %41, %27
%35 = load double, double* %8
%36 = load double, double* %9
%37 = fsub double %35, %36
%38 = call double @llvm.fabs.f64(double %37)
%39 = load double, double* %12
%40 = fcmp ogt double %38, %39
br i1 %40, label %41, label %59
41: ; preds = %34
%42 = load double, double* %7
%43 = load double, double* %6
%44 = fmul double %43, %42
store double %44, double* %6
%45 = load double, double* %10
%46 = load double, double* %10
%47 = fadd double %46, 1.000000e+00
%48 = fmul double %45, %47
%49 = load double, double* %11
%50 = fmul double %49, %48
store double %50, double* %11
%51 = load double, double* %10
%52 = fadd double %51, 2.000000e+00
store double %52, double* %10
%53 = load double, double* %8
store double %53, double* %9
%54 = load double, double* %9
%55 = load double, double* %6
%56 = load double, double* %11
%57 = fdiv double %55, %56
%58 = fadd double %54, %57
store double %58, double* %8
br label %34
59: ; preds = %34
%60 = load double, double* %8
store double %60, double* %3
br label %61
61: ; preds = %59, %25, %15
%62 = load double, double* %3
ret double %62
}

LLVM IR (-O2):
@NAN = global double 0.000000e+00
define double @cosinush(double %0, i32 %1) {
%3 = icmp eq i32 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load double, double* @NAN
br label %31
6: ; preds = %2
%7 = tail call i64 @isnan(double %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %31
9: ; preds = %6
%10 = tail call i64 @isinf(double %0)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %31
12: ; preds = %9
%13 = fmul double %0, %0
%14 = tail call double @sd2eps(i32 %1)
%15 = fcmp olt double %14, 1.000000e+00
br i1 %15, label %16, label %31
16: ; preds = %12, %16
%17 = phi double [ %24, %16 ], [ 1.000000e+00, %12 ]
%18 = phi double [ %25, %16 ], [ 1.000000e+00, %12 ]
%19 = phi double [ %27, %16 ], [ 1.000000e+00, %12 ]
%20 = phi double [ %21, %16 ], [ 1.000000e+00, %12 ]
%21 = fmul double %13, %20
%22 = fadd double %18, 1.000000e+00
%23 = fmul double %18, %22
%24 = fmul double %17, %23
%25 = fadd double %18, 2.000000e+00
%26 = fdiv double %21, %24
%27 = fadd double %19, %26
%28 = fsub double %27, %19
%29 = tail call double @llvm.fabs.f64(double %28)
%30 = fcmp ogt double %29, %14
br i1 %30, label %16, label %31
31: ; preds = %16, %12, %6, %9, %4
%32 = phi double [ %5, %4 ], [ %0, %9 ], [ %0, %6 ], [ 1.000000e+00, %12 ], [ %27, %16 ]
ret double %32
}