double bezier_surface(double u, double v, unsigned int N, unsigned int M,
                      const double k[N + 1][M + 1]) {
  double coeff[N + 1];
  for (unsigned int i = 0; i <= N; i++)
    coeff[i] = bezier_curve(u, M, k[i]);
  return bezier_curve(v, N, coeff);
}


x86 assembly (clang, -O0):
<bezier_surface>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movsd  %xmm0,-0x8(%rbp)
movsd  %xmm1,-0x10(%rbp)
mov    %edi,-0x14(%rbp)
mov    %esi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    -0x18(%rbp),%ecx
add    $0x1,%ecx
mov    %rcx,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x14(%rbp),%eax
ja     11b8 <bezier_surface+0xa8>
mov    -0x48(%rbp),%rcx
movsd  -0x8(%rbp),%xmm0
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x34(%rbp),%eax
imul   %rcx,%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    $0x1,%al
call   1030 <bezier_curve@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
cvtsi2sd %ecx,%xmm0
mov    -0x34(%rbp),%ecx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1167 <bezier_surface+0x57>
mov    -0x40(%rbp),%rsi
movsd  -0x10(%rbp),%xmm0
mov    -0x14(%rbp),%edi
mov    $0x1,%al
call   1030 <bezier_curve@plt>
cvtsi2sd %eax,%xmm0
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bezier_surface>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,-0x48(%rbp)
mov    %esi,%r12d
movsd  %xmm1,-0x38(%rbp)
movsd  %xmm0,-0x40(%rbp)
mov    %rdi,%rax
mov    %rdi,-0x30(%rbp)
lea    0x1(%rdi),%eax
lea    0x1(%r12),%r13d
mov    %rsp,%rbx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
xor    %r14d,%r14d
nopl   0x0(%rax)
mov    %r14d,%r15d
mov    %r15,%rax
imul   %r13,%rax
mov    -0x48(%rbp),%rcx
lea    (%rcx,%rax,8),%rsi
movsd  -0x40(%rbp),%xmm0
mov    %r12d,%edi
mov    $0x1,%al
call   1030 <bezier_curve@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
movsd  %xmm0,(%rbx,%r15,8)
add    $0x1,%r14d
cmp    -0x30(%rbp),%r14d
jbe    1160 <bezier_surface+0x50>
movsd  -0x38(%rbp),%xmm0
mov    -0x30(%rbp),%rdi
mov    %rbx,%rsi
mov    $0x1,%al
call   1030 <bezier_curve@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define double @bezier_surface(double %0, double %1, i32 %2, i32 %3, double* %4) {
%6 = alloca double
%7 = alloca double
%8 = alloca i32
%9 = alloca i32
%10 = alloca double*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
store double %0, double* %6
store double %1, double* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store double* %4, double** %10
%14 = load i32, i32* %8
%15 = add i32 %14, 1
%16 = zext i32 %15 to i64
%17 = load i32, i32* %9
%18 = add i32 %17, 1
%19 = zext i32 %18 to i64
%20 = load i32, i32* %8
%21 = add i32 %20, 1
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %11
%24 = alloca double, i64 %22
store i64 %22, i64* %12
store i32 0, i32* %13
br label %25
25: ; preds = %42, %5
%26 = load i32, i32* %13
%27 = load i32, i32* %8
%28 = icmp ule i32 %26, %27
br i1 %28, label %29, label %45
29: ; preds = %25
%30 = load double, double* %6
%31 = load i32, i32* %9
%32 = load double*, double** %10
%33 = load i32, i32* %13
%34 = zext i32 %33 to i64
%35 = mul nsw i64 %34, %19
%36 = getelementptr inbounds double, double* %32, i64 %35
%37 = call i32 (double, i32, double*, ...) bitcast (i32 (...)* @bezier_curve to i32 (double, i32, double*, ...)*)(double %30, i32 %31, double* %36)
%38 = sitofp i32 %37 to double
%39 = load i32, i32* %13
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds double, double* %24, i64 %40
store double %38, double* %41
br label %42
42: ; preds = %29
%43 = load i32, i32* %13
%44 = add i32 %43, 1
store i32 %44, i32* %13
br label %25
45: ; preds = %25
%46 = load double, double* %7
%47 = load i32, i32* %8
%48 = call i32 (double, i32, double*, ...) bitcast (i32 (...)* @bezier_curve to i32 (double, i32, double*, ...)*)(double %46, i32 %47, double* %24)
%49 = sitofp i32 %48 to double
%50 = load i8*, i8** %11
ret double %49
}

LLVM IR (-O2):
define double @bezier_surface(double %0, double %1, i32 %2, i32 %3, double* %4) {
%6 = add i32 %2, 1
%7 = add i32 %3, 1
%8 = zext i32 %7 to i64
%9 = zext i32 %6 to i64
%10 = alloca double, i64 %9
br label %14
11: ; preds = %14
%12 = call i32 (double, i32, double*, ...) bitcast (i32 (...)* @bezier_curve to i32 (double, i32, double*, ...)*)(double %1, i32 %2, double* %10)
%13 = sitofp i32 %12 to double
ret double %13
14: ; preds = %5, %14
%15 = phi i32 [ 0, %5 ], [ %22, %14 ]
%16 = zext i32 %15 to i64
%17 = mul nuw nsw i64 %16, %8
%18 = getelementptr inbounds double, double* %4, i64 %17
%19 = tail call i32 (double, i32, double*, ...) bitcast (i32 (...)* @bezier_curve to i32 (double, i32, double*, ...)*)(double %0, i32 %3, double* %18)
%20 = sitofp i32 %19 to double
%21 = getelementptr inbounds double, double* %10, i64 %16
store double %20, double* %21
%22 = add i32 %15, 1
%23 = icmp ugt i32 %22, %2
br i1 %23, label %11, label %14
}