#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 *chebyshev_coefs(int);
double chebyshev_multiplier_by_dct(double *, int, int);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ memset(double *, int /*<<< orphan*/, int);
int /*<<< orphan*/ polynomial_scalar_mul(double *, int, double, double *);
int /*<<< orphan*/ polynomial_x_substitution(double *, int, double *, int,
                                             double *);
void polynomial_fit_on_points_by_dct(double *y, int p_count, double start,
                                     double end, double *c, int degree) {
  int id;
  double cm, *cc, xs[2];
  memset(c, 0, (degree + 1) * sizeof(double));
  xs[0] = -2. * start / (end - start) - 1.;
  xs[1] = 2. / (end - start);
  for (id = 0; id <= degree; id++) {
    cm = chebyshev_multiplier_by_dct(y, p_count, id);
    cc = chebyshev_coefs(id);
    polynomial_scalar_mul(cc, id, cm, cc);
    polynomial_x_substitution(cc, id, xs, 1, c);
    free(cc);
  }
}


x86 assembly (clang, -O0):
<polynomial_fit_on_points_by_dct>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
movsd  %xmm1,-0x20(%rbp)
mov    %rdx,-0x28(%rbp)
mov    %ecx,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movsd  0xe6f(%rip),%xmm0
mulsd  -0x18(%rbp),%xmm0
movsd  -0x20(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe4c(%rip),%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0x50(%rbp)
movsd  -0x20(%rbp),%xmm1
subsd  -0x18(%rbp),%xmm1
movsd  0xe29(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x48(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jg     1258 <polynomial_fit_on_points_by_dct+0xf8>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x30(%rbp),%edx
call   1070 <chebyshev_multiplier_by_dct@plt>
movsd  %xmm0,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <chebyshev_coefs@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
movsd  -0x38(%rbp),%xmm0
mov    -0x40(%rbp),%rdx
call   1080 <polynomial_scalar_mul@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%esi
lea    -0x50(%rbp),%rdx
mov    -0x28(%rbp),%r8
mov    $0x1,%ecx
call   1060 <polynomial_x_substitution@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11e7 <polynomial_fit_on_points_by_dct+0x87>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<polynomial_fit_on_points_by_dct>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %ecx,%ebx
mov    %rdx,%r14
movsd  %xmm1,(%rsp)
movsd  %xmm0,0x8(%rsp)
mov    %esi,%r15d
mov    %rdi,%r12
lea    0x8(,%rbx,8),%edx
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movsd  (%rsp),%xmm0
movsd  0x8(%rsp),%xmm1
subsd  %xmm1,%xmm0
addsd  %xmm1,%xmm1
divsd  %xmm0,%xmm1
movapd %xmm1,%xmm2
movapd %xmm0,%xmm1
movsd  0xe42(%rip),%xmm0
subsd  %xmm2,%xmm0
movsd  %xmm0,0x10(%rsp)
movsd  0xe38(%rip),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,0x18(%rsp)
test   %ebx,%ebx
js     1244 <polynomial_fit_on_points_by_dct+0xe4>
add    $0x1,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r15d,%esi
mov    %ebp,%edx
call   1070 <chebyshev_multiplier_by_dct@plt>
movsd  %xmm0,(%rsp)
mov    %ebp,%edi
call   1040 <chebyshev_coefs@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %ebp,%esi
movsd  (%rsp),%xmm0
mov    %rax,%rdx
call   1080 <polynomial_scalar_mul@plt>
mov    %r13,%rdi
mov    %ebp,%esi
lea    0x10(%rsp),%rdx
mov    $0x1,%ecx
mov    %r14,%r8
call   1060 <polynomial_x_substitution@plt>
mov    %r13,%rdi
call   1030 <free@plt>
add    $0x1,%ebp
cmp    %ebp,%ebx
jne    11f0 <polynomial_fit_on_points_by_dct+0x90>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @polynomial_fit_on_points_by_dct(double* %0, i32 %1, double %2, double %3, double* %4, i32 %5) {
%7 = alloca double*
%8 = alloca i32
%9 = alloca double
%10 = alloca double
%11 = alloca double*
%12 = alloca i32
%13 = alloca i32
%14 = alloca double
%15 = alloca double*
%16 = alloca [2 x double]
store double* %0, double** %7
store i32 %1, i32* %8
store double %2, double* %9
store double %3, double* %10
store double* %4, double** %11
store i32 %5, i32* %12
%17 = load double*, double** %11
%18 = load i32, i32* %12
%19 = add nsw i32 %18, 1
%20 = sext i32 %19 to i64
%21 = mul i64 %20, 8
%22 = trunc i64 %21 to i32
%23 = call i32 @memset(double* %17, i32 0, i32 %22)
%24 = load double, double* %9
%25 = fmul double -2.000000e+00, %24
%26 = load double, double* %10
%27 = load double, double* %9
%28 = fsub double %26, %27
%29 = fdiv double %25, %28
%30 = fsub double %29, 1.000000e+00
%31 = getelementptr inbounds [2 x double], [2 x double]* %16, i64 0, i64 0
store double %30, double* %31
%32 = load double, double* %10
%33 = load double, double* %9
%34 = fsub double %32, %33
%35 = fdiv double 2.000000e+00, %34
%36 = getelementptr inbounds [2 x double], [2 x double]* %16, i64 0, i64 1
store double %35, double* %36
store i32 0, i32* %13
br label %37
37: ; preds = %60, %6
%38 = load i32, i32* %13
%39 = load i32, i32* %12
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %63
41: ; preds = %37
%42 = load double*, double** %7
%43 = load i32, i32* %8
%44 = load i32, i32* %13
%45 = call double @chebyshev_multiplier_by_dct(double* %42, i32 %43, i32 %44)
store double %45, double* %14
%46 = load i32, i32* %13
%47 = call double* @chebyshev_coefs(i32 %46)
store double* %47, double** %15
%48 = load double*, double** %15
%49 = load i32, i32* %13
%50 = load double, double* %14
%51 = load double*, double** %15
%52 = call i32 @polynomial_scalar_mul(double* %48, i32 %49, double %50, double* %51)
%53 = load double*, double** %15
%54 = load i32, i32* %13
%55 = getelementptr inbounds [2 x double], [2 x double]* %16, i64 0, i64 0
%56 = load double*, double** %11
%57 = call i32 @polynomial_x_substitution(double* %53, i32 %54, double* %55, i32 1, double* %56)
%58 = load double*, double** %15
%59 = call i32 @free(double* %58)
br label %60
60: ; preds = %41
%61 = load i32, i32* %13
%62 = add nsw i32 %61, 1
store i32 %62, i32* %13
br label %37
63: ; preds = %37
ret void
}

LLVM IR (-O2):
define void @polynomial_fit_on_points_by_dct(double* %0, i32 %1, double %2, double %3, double* %4, i32 %5) {
%7 = alloca [2 x double]
%8 = bitcast [2 x double]* %7 to i8*
%9 = shl i32 %5, 3
%10 = add i32 %9, 8
%11 = tail call i32 @memset(double* %4, i32 0, i32 %10)
%12 = fmul double %2, 2.000000e+00
%13 = fsub double %3, %2
%14 = fdiv double %12, %13
%15 = fsub double -1.000000e+00, %14
%16 = getelementptr inbounds [2 x double], [2 x double]* %7, i64 0, i64 0
store double %15, double* %16
%17 = fdiv double 2.000000e+00, %13
%18 = getelementptr inbounds [2 x double], [2 x double]* %7, i64 0, i64 1
store double %17, double* %18
%19 = icmp slt i32 %5, 0
br i1 %19, label %29, label %20
20: ; preds = %6, %20
%21 = phi i32 [ %27, %20 ], [ 0, %6 ]
%22 = call double @chebyshev_multiplier_by_dct(double* %0, i32 %1, i32 %21)
%23 = call double* @chebyshev_coefs(i32 %21)
%24 = call i32 @polynomial_scalar_mul(double* %23, i32 %21, double %22, double* %23)
%25 = call i32 @polynomial_x_substitution(double* %23, i32 %21, double* %16, i32 1, double* %4)
%26 = call i32 @free(double* %23)
%27 = add nuw i32 %21, 1
%28 = icmp eq i32 %21, %5
br i1 %28, label %29, label %20
29: ; preds = %20, %6
ret void
}