double bspline_derivative(unsigned int n, unsigned int i, unsigned int p,
                          const double tau[n + 1], double x) {
  assert(p > 0);
  double a = frac(p, tau[i + p] - tau[i]);
  double b = frac(p, tau[i + p + 1] - tau[i + 1]);
  return a * bspline(n, i, p - 1, tau, x) -
         b * bspline(n, i + 1, p - 1, tau, x);
}


x86 assembly (clang, -O0):
<bspline_derivative>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
cmpl   $0x0,-0xc(%rbp)
seta   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    -0xc(%rbp),%ecx
mov    %ecx,%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
subsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%al
call   1050 <frac@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,%ecx
subsd  (%rax,%rcx,8),%xmm0
mov    $0x1,%al
call   1050 <frac@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x30(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%al
call   1030 <bspline@plt>
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x38(%rbp)
movsd  -0x30(%rbp),%xmm0
movsd  %xmm0,-0x48(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
add    $0x1,%esi
mov    -0xc(%rbp),%edx
sub    $0x1,%edx
mov    -0x18(%rbp),%rcx
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%al
call   1030 <bspline@plt>
movsd  -0x48(%rbp),%xmm1
movsd  -0x40(%rbp),%xmm0
movsd  -0x38(%rbp),%xmm2
cvtsi2sd %eax,%xmm3
mulsd  %xmm3,%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bspline_derivative>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  %xmm0,0x8(%rsp)
mov    %rcx,%r12
mov    %edx,%ebx
mov    %esi,%ebp
mov    %edi,%r14d
xor    %edi,%edi
mov    %edx,%r15d
add    $0xffffffff,%r15d
setb   %dil
xor    %eax,%eax
call   1040 <assert@plt>
lea    (%rbx,%rbp,1),%eax
movsd  (%r12,%rax,8),%xmm0
mov    %ebp,%eax
subsd  (%r12,%rax,8),%xmm0
mov    %ebx,%edi
mov    $0x1,%al
call   1050 <frac@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,(%rsp)
lea    (%rbx,%rbp,1),%eax
add    $0x1,%eax
movsd  (%r12,%rax,8),%xmm0
lea    0x1(%rbp),%r13d
subsd  (%r12,%r13,8),%xmm0
mov    %ebx,%edi
mov    $0x1,%al
call   1050 <frac@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,0x10(%rsp)
mov    %r14d,%edi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r12,%rcx
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1030 <bspline@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
mulsd  (%rsp),%xmm0
movsd  %xmm0,(%rsp)
mov    %r14d,%edi
mov    %r13d,%esi
mov    %r15d,%edx
mov    %r12,%rcx
movsd  0x8(%rsp),%xmm0
mov    $0x1,%al
call   1030 <bspline@plt>
cvtsi2sd %eax,%xmm1
mulsd  0x10(%rsp),%xmm1
movsd  (%rsp),%xmm0
subsd  %xmm1,%xmm0
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @bspline_derivative(i32 %0, i32 %1, i32 %2, double* %3, double %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca double*
%10 = alloca double
%11 = alloca double
%12 = alloca double
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store double* %3, double** %9
store double %4, double* %10
%13 = load i32, i32* %6
%14 = add i32 %13, 1
%15 = zext i32 %14 to i64
%16 = load i32, i32* %8
%17 = icmp ugt i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %18)
%20 = load i32, i32* %8
%21 = load double*, double** %9
%22 = load i32, i32* %7
%23 = load i32, i32* %8
%24 = add i32 %22, %23
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds double, double* %21, i64 %25
%27 = load double, double* %26
%28 = load double*, double** %9
%29 = load i32, i32* %7
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds double, double* %28, i64 %30
%32 = load double, double* %31
%33 = fsub double %27, %32
%34 = call i32 (i32, double, ...) bitcast (i32 (...)* @frac to i32 (i32, double, ...)*)(i32 %20, double %33)
%35 = sitofp i32 %34 to double
store double %35, double* %11
%36 = load i32, i32* %8
%37 = load double*, double** %9
%38 = load i32, i32* %7
%39 = load i32, i32* %8
%40 = add i32 %38, %39
%41 = add i32 %40, 1
%42 = zext i32 %41 to i64
%43 = getelementptr inbounds double, double* %37, i64 %42
%44 = load double, double* %43
%45 = load double*, double** %9
%46 = load i32, i32* %7
%47 = add i32 %46, 1
%48 = zext i32 %47 to i64
%49 = getelementptr inbounds double, double* %45, i64 %48
%50 = load double, double* %49
%51 = fsub double %44, %50
%52 = call i32 (i32, double, ...) bitcast (i32 (...)* @frac to i32 (i32, double, ...)*)(i32 %36, double %51)
%53 = sitofp i32 %52 to double
store double %53, double* %12
%54 = load double, double* %11
%55 = load i32, i32* %6
%56 = load i32, i32* %7
%57 = load i32, i32* %8
%58 = sub i32 %57, 1
%59 = load double*, double** %9
%60 = load double, double* %10
%61 = call i32 (i32, i32, i32, double*, double, ...) bitcast (i32 (...)* @bspline to i32 (i32, i32, i32, double*, double, ...)*)(i32 %55, i32 %56, i32 %58, double* %59, double %60)
%62 = sitofp i32 %61 to double
%63 = load double, double* %12
%64 = load i32, i32* %6
%65 = load i32, i32* %7
%66 = add i32 %65, 1
%67 = load i32, i32* %8
%68 = sub i32 %67, 1
%69 = load double*, double** %9
%70 = load double, double* %10
%71 = call i32 (i32, i32, i32, double*, double, ...) bitcast (i32 (...)* @bspline to i32 (i32, i32, i32, double*, double, ...)*)(i32 %64, i32 %66, i32 %68, double* %69, double %70)
%72 = sitofp i32 %71 to double
%73 = fmul double %63, %72
%74 = fneg double %73
%75 = call double @llvm.fmuladd.f64(double %54, double %62, double %74)
ret double %75
}

LLVM IR (-O2):
define double @bspline_derivative(i32 %0, i32 %1, i32 %2, double* %3, double %4) {
%6 = icmp ne i32 %2, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %7)
%9 = add i32 %2, %1
%10 = zext i32 %9 to i64
%11 = getelementptr inbounds double, double* %3, i64 %10
%12 = load double, double* %11
%13 = zext i32 %1 to i64
%14 = getelementptr inbounds double, double* %3, i64 %13
%15 = load double, double* %14
%16 = fsub double %12, %15
%17 = tail call i32 (i32, double, ...) bitcast (i32 (...)* @frac to i32 (i32, double, ...)*)(i32 %2, double %16)
%18 = sitofp i32 %17 to double
%19 = add i32 %9, 1
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds double, double* %3, i64 %20
%22 = load double, double* %21
%23 = add i32 %1, 1
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds double, double* %3, i64 %24
%26 = load double, double* %25
%27 = fsub double %22, %26
%28 = tail call i32 (i32, double, ...) bitcast (i32 (...)* @frac to i32 (i32, double, ...)*)(i32 %2, double %27)
%29 = sitofp i32 %28 to double
%30 = add i32 %2, -1
%31 = tail call i32 (i32, i32, i32, double*, double, ...) bitcast (i32 (...)* @bspline to i32 (i32, i32, i32, double*, double, ...)*)(i32 %0, i32 %1, i32 %30, double* %3, double %4)
%32 = sitofp i32 %31 to double
%33 = tail call i32 (i32, i32, i32, double*, double, ...) bitcast (i32 (...)* @bspline to i32 (i32, i32, i32, double*, double, ...)*)(i32 %0, i32 %23, i32 %30, double* %3, double %4)
%34 = sitofp i32 %33 to double
%35 = fneg double %29
%36 = fmul double %35, %34
%37 = tail call double @llvm.fmuladd.f64(double %18, double %32, double %36)
ret double %37
}