double determinant(double M[3][3], int ndims) {
  if (ndims == 1) {
    return (M[0][0]);
  } else if (ndims == 2) {
    return (M[0][0] * M[1][1] - M[0][1] * M[1][0]);
  } else if (ndims == 3) {
    return (M[0][0] * (M[1][1] * M[2][2] - M[2][1] * M[1][2]) -
            M[1][0] * (M[0][1] * M[2][2] - M[2][1] * M[0][2]) +
            M[2][0] * (M[0][1] * M[1][2] - M[1][1] * M[0][2]));
  } else {
    return (0.0);
  }
}


x86 assembly (clang, -O0):
<determinant>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x1,-0x14(%rbp)
jne    1127 <determinant+0x27>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a7 <determinant+0x1a7>
cmpl   $0x2,-0x14(%rbp)
jne    117d <determinant+0x7d>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  0x20(%rax),%xmm2
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm1
mov    -0x10(%rbp),%rax
mulsd  0x18(%rax),%xmm1
movq   %xmm1,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm1
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a7 <determinant+0x1a7>
cmpl   $0x3,-0x14(%rbp)
jne    129f <determinant+0x19f>
mov    -0x10(%rbp),%rax
movsd  (%rax),%xmm1
mov    -0x10(%rbp),%rax
movsd  0x20(%rax),%xmm2
mov    -0x10(%rbp),%rax
movsd  0x40(%rax),%xmm3
mov    -0x10(%rbp),%rax
movsd  0x38(%rax),%xmm0
mov    -0x10(%rbp),%rax
mulsd  0x28(%rax),%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm3,%xmm2
addsd  %xmm0,%xmm2
mov    -0x10(%rbp),%rax
movsd  0x18(%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm3
mov    -0x10(%rbp),%rax
movsd  0x40(%rax),%xmm5
mov    -0x10(%rbp),%rax
movsd  0x38(%rax),%xmm4
mov    -0x10(%rbp),%rax
mulsd  0x10(%rax),%xmm4
movq   %xmm4,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm4
mulsd  %xmm5,%xmm3
addsd  %xmm4,%xmm3
mulsd  %xmm3,%xmm0
movq   %xmm0,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm0
mulsd  %xmm2,%xmm1
addsd  %xmm0,%xmm1
mov    -0x10(%rbp),%rax
movsd  0x30(%rax),%xmm0
mov    -0x10(%rbp),%rax
movsd  0x8(%rax),%xmm2
mov    -0x10(%rbp),%rax
movsd  0x28(%rax),%xmm4
mov    -0x10(%rbp),%rax
movsd  0x20(%rax),%xmm3
mov    -0x10(%rbp),%rax
mulsd  0x10(%rax),%xmm3
movq   %xmm3,%rax
movabs $0x8000000000000000,%rcx
xor    %rcx,%rax
movq   %rax,%xmm3
mulsd  %xmm4,%xmm2
addsd  %xmm3,%xmm2
mulsd  %xmm2,%xmm0
addsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12a7 <determinant+0x1a7>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
pop    %rbp
ret

x86 assembly (clang, -O2):
<determinant>:
cmp    $0x3,%esi
je     1118 <determinant+0x18>
cmp    $0x2,%esi
je     1175 <determinant+0x75>
xorpd  %xmm0,%xmm0
cmp    $0x1,%esi
jne    1174 <determinant+0x74>
movsd  (%rdi),%xmm0
ret
movsd  0x20(%rdi),%xmm1
movsd  0x40(%rdi),%xmm2
movsd  0x38(%rdi),%xmm3
movsd  0x28(%rdi),%xmm4
movapd %xmm1,%xmm5
mulsd  %xmm2,%xmm5
movsd  0x8(%rdi),%xmm0
mulsd  %xmm0,%xmm2
mulsd  %xmm4,%xmm0
mulsd  %xmm3,%xmm4
subsd  %xmm4,%xmm5
movsd  0x10(%rdi),%xmm4
mulsd  %xmm4,%xmm3
subsd  %xmm3,%xmm2
mulsd  0x18(%rdi),%xmm2
mulsd  (%rdi),%xmm5
subsd  %xmm2,%xmm5
mulsd  %xmm1,%xmm4
subsd  %xmm4,%xmm0
mulsd  0x30(%rdi),%xmm0
addsd  %xmm5,%xmm0
ret
movsd  0x18(%rdi),%xmm1
mulsd  0x8(%rdi),%xmm1
movsd  (%rdi),%xmm0
mulsd  0x20(%rdi),%xmm0
subsd  %xmm1,%xmm0
ret

LLVM IR (-O0):
define double @determinant([3 x double]* %0, i32 %1) {
%3 = alloca double
%4 = alloca [3 x double]*
%5 = alloca i32
store [3 x double]* %0, [3 x double]** %4
store i32 %1, i32* %5
%6 = load i32, i32* %5
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %13
8: ; preds = %2
%9 = load [3 x double]*, [3 x double]** %4
%10 = getelementptr inbounds [3 x double], [3 x double]* %9, i64 0
%11 = getelementptr inbounds [3 x double], [3 x double]* %10, i64 0, i64 0
%12 = load double, double* %11
store double %12, double* %3
br label %114
13: ; preds = %2
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = load [3 x double]*, [3 x double]** %4
%18 = getelementptr inbounds [3 x double], [3 x double]* %17, i64 0
%19 = getelementptr inbounds [3 x double], [3 x double]* %18, i64 0, i64 0
%20 = load double, double* %19
%21 = load [3 x double]*, [3 x double]** %4
%22 = getelementptr inbounds [3 x double], [3 x double]* %21, i64 1
%23 = getelementptr inbounds [3 x double], [3 x double]* %22, i64 0, i64 1
%24 = load double, double* %23
%25 = load [3 x double]*, [3 x double]** %4
%26 = getelementptr inbounds [3 x double], [3 x double]* %25, i64 0
%27 = getelementptr inbounds [3 x double], [3 x double]* %26, i64 0, i64 1
%28 = load double, double* %27
%29 = load [3 x double]*, [3 x double]** %4
%30 = getelementptr inbounds [3 x double], [3 x double]* %29, i64 1
%31 = getelementptr inbounds [3 x double], [3 x double]* %30, i64 0, i64 0
%32 = load double, double* %31
%33 = fmul double %28, %32
%34 = fneg double %33
%35 = call double @llvm.fmuladd.f64(double %20, double %24, double %34)
store double %35, double* %3
br label %114
36: ; preds = %13
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, 3
br i1 %38, label %39, label %113
39: ; preds = %36
%40 = load [3 x double]*, [3 x double]** %4
%41 = getelementptr inbounds [3 x double], [3 x double]* %40, i64 0
%42 = getelementptr inbounds [3 x double], [3 x double]* %41, i64 0, i64 0
%43 = load double, double* %42
%44 = load [3 x double]*, [3 x double]** %4
%45 = getelementptr inbounds [3 x double], [3 x double]* %44, i64 1
%46 = getelementptr inbounds [3 x double], [3 x double]* %45, i64 0, i64 1
%47 = load double, double* %46
%48 = load [3 x double]*, [3 x double]** %4
%49 = getelementptr inbounds [3 x double], [3 x double]* %48, i64 2
%50 = getelementptr inbounds [3 x double], [3 x double]* %49, i64 0, i64 2
%51 = load double, double* %50
%52 = load [3 x double]*, [3 x double]** %4
%53 = getelementptr inbounds [3 x double], [3 x double]* %52, i64 2
%54 = getelementptr inbounds [3 x double], [3 x double]* %53, i64 0, i64 1
%55 = load double, double* %54
%56 = load [3 x double]*, [3 x double]** %4
%57 = getelementptr inbounds [3 x double], [3 x double]* %56, i64 1
%58 = getelementptr inbounds [3 x double], [3 x double]* %57, i64 0, i64 2
%59 = load double, double* %58
%60 = fmul double %55, %59
%61 = fneg double %60
%62 = call double @llvm.fmuladd.f64(double %47, double %51, double %61)
%63 = load [3 x double]*, [3 x double]** %4
%64 = getelementptr inbounds [3 x double], [3 x double]* %63, i64 1
%65 = getelementptr inbounds [3 x double], [3 x double]* %64, i64 0, i64 0
%66 = load double, double* %65
%67 = load [3 x double]*, [3 x double]** %4
%68 = getelementptr inbounds [3 x double], [3 x double]* %67, i64 0
%69 = getelementptr inbounds [3 x double], [3 x double]* %68, i64 0, i64 1
%70 = load double, double* %69
%71 = load [3 x double]*, [3 x double]** %4
%72 = getelementptr inbounds [3 x double], [3 x double]* %71, i64 2
%73 = getelementptr inbounds [3 x double], [3 x double]* %72, i64 0, i64 2
%74 = load double, double* %73
%75 = load [3 x double]*, [3 x double]** %4
%76 = getelementptr inbounds [3 x double], [3 x double]* %75, i64 2
%77 = getelementptr inbounds [3 x double], [3 x double]* %76, i64 0, i64 1
%78 = load double, double* %77
%79 = load [3 x double]*, [3 x double]** %4
%80 = getelementptr inbounds [3 x double], [3 x double]* %79, i64 0
%81 = getelementptr inbounds [3 x double], [3 x double]* %80, i64 0, i64 2
%82 = load double, double* %81
%83 = fmul double %78, %82
%84 = fneg double %83
%85 = call double @llvm.fmuladd.f64(double %70, double %74, double %84)
%86 = fmul double %66, %85
%87 = fneg double %86
%88 = call double @llvm.fmuladd.f64(double %43, double %62, double %87)
%89 = load [3 x double]*, [3 x double]** %4
%90 = getelementptr inbounds [3 x double], [3 x double]* %89, i64 2
%91 = getelementptr inbounds [3 x double], [3 x double]* %90, i64 0, i64 0
%92 = load double, double* %91
%93 = load [3 x double]*, [3 x double]** %4
%94 = getelementptr inbounds [3 x double], [3 x double]* %93, i64 0
%95 = getelementptr inbounds [3 x double], [3 x double]* %94, i64 0, i64 1
%96 = load double, double* %95
%97 = load [3 x double]*, [3 x double]** %4
%98 = getelementptr inbounds [3 x double], [3 x double]* %97, i64 1
%99 = getelementptr inbounds [3 x double], [3 x double]* %98, i64 0, i64 2
%100 = load double, double* %99
%101 = load [3 x double]*, [3 x double]** %4
%102 = getelementptr inbounds [3 x double], [3 x double]* %101, i64 1
%103 = getelementptr inbounds [3 x double], [3 x double]* %102, i64 0, i64 1
%104 = load double, double* %103
%105 = load [3 x double]*, [3 x double]** %4
%106 = getelementptr inbounds [3 x double], [3 x double]* %105, i64 0
%107 = getelementptr inbounds [3 x double], [3 x double]* %106, i64 0, i64 2
%108 = load double, double* %107
%109 = fmul double %104, %108
%110 = fneg double %109
%111 = call double @llvm.fmuladd.f64(double %96, double %100, double %110)
%112 = call double @llvm.fmuladd.f64(double %92, double %111, double %88)
store double %112, double* %3
br label %114
113: ; preds = %36
store double 0.000000e+00, double* %3
br label %114
114: ; preds = %113, %39, %16, %8
%115 = load double, double* %3
ret double %115
}

LLVM IR (-O2):
define double @determinant([3 x double]* nocapture %0, i32 %1) {
switch i32 %1, label %49 [
i32 1, label %3
i32 2, label %6
i32 3, label %18
]
3: ; preds = %2
%4 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 0
%5 = load double, double* %4
br label %49
6: ; preds = %2
%7 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 0
%8 = load double, double* %7
%9 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 1
%10 = load double, double* %9
%11 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 1
%12 = load double, double* %11
%13 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 0
%14 = load double, double* %13
%15 = fneg double %12
%16 = fmul double %14, %15
%17 = tail call double @llvm.fmuladd.f64(double %8, double %10, double %16)
br label %49
18: ; preds = %2
%19 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 0
%20 = load double, double* %19
%21 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 1
%22 = load double, double* %21
%23 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 2
%24 = load double, double* %23
%25 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 1
%26 = load double, double* %25
%27 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 2
%28 = load double, double* %27
%29 = fneg double %26
%30 = fmul double %28, %29
%31 = tail call double @llvm.fmuladd.f64(double %22, double %24, double %30)
%32 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 1, i64 0
%33 = load double, double* %32
%34 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 1
%35 = load double, double* %34
%36 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 0, i64 2
%37 = load double, double* %36
%38 = fmul double %37, %29
%39 = tail call double @llvm.fmuladd.f64(double %35, double %24, double %38)
%40 = fneg double %33
%41 = fmul double %39, %40
%42 = tail call double @llvm.fmuladd.f64(double %20, double %31, double %41)
%43 = getelementptr inbounds [3 x double], [3 x double]* %0, i64 2, i64 0
%44 = load double, double* %43
%45 = fneg double %22
%46 = fmul double %37, %45
%47 = tail call double @llvm.fmuladd.f64(double %35, double %28, double %46)
%48 = tail call double @llvm.fmuladd.f64(double %44, double %47, double %42)
br label %49
49: ; preds = %2, %18, %6, %3
%50 = phi double [ %5, %3 ], [ %17, %6 ], [ %48, %18 ], [ 0.000000e+00, %2 ]
ret double %50
}