#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 cos(int);
int /*<<< orphan*/ printf(char *, ...);
double scale(int /*<<< orphan*/);
int /*<<< orphan*/ smoothstep(int /*<<< orphan*/);
int /*<<< orphan*/ triangle(unsigned short);
int main(int argc, char *argv[]) {
  printf("x\tcos(x)\tsmoothstepCos(x)\terror\n");
  for (int i = 0; i < 65536; i++) {
    unsigned short x = (unsigned short)i;
    double smoothstepCos = scale(smoothstep(triangle(x)));
    double builtinCos = cos(2 * 3.14159265358979323846 * x / 65536.0);
    printf("0x%.4hx\t%e\t%e\t%e\n", x, builtinCos, smoothstepCos,
           builtinCos != 0 ? (smoothstepCos - builtinCos) / builtinCos
                           : smoothstepCos - builtinCos);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xea3(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x10000,-0x14(%rbp)
jge    1267 <main+0x117>
mov    -0x14(%rbp),%eax
mov    %ax,-0x16(%rbp)
movzwl -0x16(%rbp),%edi
call   1040 <triangle@plt>
mov    %eax,%edi
call   1030 <smoothstep@plt>
mov    %eax,%edi
call   1060 <scale@plt>
movsd  %xmm0,-0x20(%rbp)
movzwl -0x16(%rbp),%eax
cvtsi2sd %eax,%xmm1
movsd  0xe4d(%rip),%xmm0
mulsd  %xmm1,%xmm0
movsd  0xe39(%rip),%xmm1
divsd  %xmm1,%xmm0
cvttsd2si %xmm0,%edi
call   1070 <cos@plt>
movsd  %xmm0,-0x28(%rbp)
movzwl -0x16(%rbp),%eax
mov    %eax,-0x3c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x20(%rbp),%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1211 <main+0xc1>
jp     1211 <main+0xc1>
jmp    122a <main+0xda>
movsd  -0x20(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
divsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
jmp    1239 <main+0xe9>
movsd  -0x20(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
movsd  -0x30(%rbp),%xmm1
movsd  -0x38(%rbp),%xmm0
mov    -0x3c(%rbp),%esi
movsd  -0x48(%rbp),%xmm2
lea    0xddf(%rip),%rdi
mov    $0x3,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117b <main+0x2b>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xec4(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
lea    0xea4(%rip),%r14
xorpd  %xmm0,%xmm0
movsd  %xmm0,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
movzwl %bx,%edi
call   1050 <triangle@plt>
mov    %eax,%edi
call   1040 <smoothstep@plt>
mov    %eax,%edi
call   1070 <scale@plt>
movsd  %xmm0,0x10(%rsp)
movsd  0x8(%rsp),%xmm0
mulsd  0xe46(%rip),%xmm0
mulsd  0xe46(%rip),%xmm0
cvttsd2si %xmm0,%edi
call   1080 <cos@plt>
movsd  0x10(%rsp),%xmm1
movapd %xmm1,%xmm2
subsd  %xmm0,%xmm2
movapd %xmm0,%xmm5
cmpneqsd 0xe32(%rip),%xmm5
movapd %xmm5,%xmm3
andpd  %xmm0,%xmm3
movsd  0xe1a(%rip),%xmm4
andnpd %xmm4,%xmm5
orpd   %xmm3,%xmm5
divsd  %xmm5,%xmm2
mov    %r14,%rdi
mov    %ebx,%esi
mov    $0x3,%al
call   1060 <printf@plt>
movsd  0x8(%rsp),%xmm0
movsd  0xdf4(%rip),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x8(%rsp)
add    $0x1,%ebx
cmp    $0x10000,%ebx
jne    1190 <main+0x30>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [33 x i8] c"x\09cos(x)\09smoothstepCos(x)\09error\0A\00"
@.str.1 = [18 x i8] c"0x%.4hx\09%e\09%e\09%e\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i16
%8 = alloca double
%9 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %11
11: ; preds = %47, %2
%12 = load i32, i32* %6
%13 = icmp slt i32 %12, 65536
br i1 %13, label %14, label %50
14: ; preds = %11
%15 = load i32, i32* %6
%16 = trunc i32 %15 to i16
store i16 %16, i16* %7
%17 = load i16, i16* %7
%18 = call i32 @triangle(i16 zeroext %17)
%19 = call i32 @smoothstep(i32 %18)
%20 = call double @scale(i32 %19)
store double %20, double* %8
%21 = load i16, i16* %7
%22 = zext i16 %21 to i32
%23 = sitofp i32 %22 to double
%24 = fmul double 0x401921FB54442D18, %23
%25 = fdiv double %24, 6.553600e+04
%26 = fptosi double %25 to i32
%27 = call double @cos(i32 %26)
store double %27, double* %9
%28 = load i16, i16* %7
%29 = zext i16 %28 to i32
%30 = load double, double* %9
%31 = load double, double* %8
%32 = load double, double* %9
%33 = fcmp une double %32, 0.000000e+00
br i1 %33, label %34, label %40
34: ; preds = %14
%35 = load double, double* %8
%36 = load double, double* %9
%37 = fsub double %35, %36
%38 = load double, double* %9
%39 = fdiv double %37, %38
br label %44
40: ; preds = %14
%41 = load double, double* %8
%42 = load double, double* %9
%43 = fsub double %41, %42
br label %44
44: ; preds = %40, %34
%45 = phi double [ %39, %34 ], [ %43, %40 ]
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %29, double %30, double %31, double %45)
br label %47
47: ; preds = %44
%48 = load i32, i32* %6
%49 = add nsw i32 %48, 1
store i32 %49, i32* %6
br label %11
50: ; preds = %11
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"0x%.4hx\09%e\09%e\09%e\0A\00"
@str = [32 x i8] c"x\09cos(x)\09smoothstepCos(x)\09error\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %5
4: ; preds = %5
ret i32 0
5: ; preds = %2, %5
%6 = phi i32 [ 0, %2 ], [ %21, %5 ]
%7 = trunc i32 %6 to i16
%8 = tail call i32 @triangle(i16 zeroext %7)
%9 = tail call i32 @smoothstep(i32 %8)
%10 = tail call double @scale(i32 %9)
%11 = sitofp i32 %6 to double
%12 = fmul double %11, 0x401921FB54442D18
%13 = fmul double %12, 0x3EF0000000000000
%14 = fptosi double %13 to i32
%15 = tail call double @cos(i32 %14)
%16 = fcmp une double %15, 0.000000e+00
%17 = fsub double %10, %15
%18 = select i1 %16, double %15, double 1.000000e+00
%19 = fdiv double %17, %18
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %6, double %15, double %10, double %19)
%21 = add nuw nsw i32 %6, 1
%22 = icmp eq i32 %21, 65536
br i1 %22, label %4, label %5
}