extern int matinv(double *A, int n) {
  double d, *B;
  int i, j, *indx;
  indx = imat(n, 1);
  B = mat(n, n);
  matcpy(B, A, n, n);
  if (ludcmp(B, n, indx, &d)) {
    free(indx);
    free(B);
    return -1;
  }
  for (j = 0; j < n; j++) {
    for (i = 0; i < n; i++)
      A[i + j * n] = 0.0;
    A[j + j * n] = 1.0;
    lubksb(B, n, indx, A + j * n);
  }
  free(indx);
  free(B);
  return 0;
}


x86 assembly (clang, -O0):
<matinv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
mov    $0x0,%al
call   1060 <imat@plt>
cltq
mov    %rax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    $0x0,%al
call   1050 <mat@plt>
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    $0x0,%al
call   1080 <matcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
lea    -0x20(%rbp),%rcx
mov    $0x0,%al
call   1040 <ludcmp@plt>
cmp    $0x0,%eax
je     11e9 <matinv+0x89>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12a5 <matinv+0x145>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    128c <matinv+0x12c>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1238 <matinv+0xd8>
mov    -0x10(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%edx
imul   -0x14(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
xorps  %xmm0,%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1203 <matinv+0xa3>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%ecx
mov    -0x30(%rbp),%edx
imul   -0x14(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movsd  0xdad(%rip),%xmm0
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x30(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x3,%rax
add    %rax,%rcx
mov    $0x0,%al
call   1070 <lubksb@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11f0 <matinv+0x90>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matinv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
mov    %esi,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1070 <imat@plt>
movslq %eax,%r14
mov    %ebx,%edi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <mat@plt>
movslq %eax,%r12
mov    %r12,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
mov    %ebx,%ecx
xor    %eax,%eax
call   1090 <matcpy@plt>
lea    0x30(%rsp),%rcx
mov    %r12,%rdi
mov    %ebx,0xc(%rsp)
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <ludcmp@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
jne    127c <matinv+0x10c>
mov    $0x0,%ebx
cmpl   $0x0,0xc(%rsp)
jle    127c <matinv+0x10c>
mov    0xc(%rsp),%ebp
lea    0x0(,%rbp,8),%r13
lea    0x8(,%rbp,8),%rax
mov    %rax,0x28(%rsp)
mov    %r14,0x18(%rsp)
mov    %r15,%r14
mov    %r12,0x10(%rsp)
mov    %r15,0x20(%rsp)
mov    %r15,%r12
xor    %r15d,%r15d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movslq %r15d,%r15
mov    0x20(%rsp),%rax
lea    (%rax,%r15,8),%rdi
xor    %esi,%esi
mov    %r13,%rdx
call   1060 <memset@plt>
movabs $0x3ff0000000000000,%rax
mov    %rax,(%r12)
mov    0x10(%rsp),%rdi
mov    %ebp,%esi
mov    0x18(%rsp),%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1080 <lubksb@plt>
add    $0x1,%rbx
add    0xc(%rsp),%r15d
add    0x28(%rsp),%r12
add    %r13,%r14
cmp    %rbx,%rbp
jne    1220 <matinv+0xb0>
mov    0x18(%rsp),%r14
mov    0x10(%rsp),%r12
xor    %ebx,%ebx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @matinv(double* %0, i32 %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca double
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
store double* %0, double** %4
store i32 %1, i32* %5
%11 = load i32, i32* %5
%12 = call i32 (i32, i32, ...) bitcast (i32 (...)* @imat to i32 (i32, i32, ...)*)(i32 %11, i32 1)
%13 = sext i32 %12 to i64
%14 = inttoptr i64 %13 to i32*
store i32* %14, i32** %10
%15 = load i32, i32* %5
%16 = load i32, i32* %5
%17 = call i32 (i32, i32, ...) bitcast (i32 (...)* @mat to i32 (i32, i32, ...)*)(i32 %15, i32 %16)
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to double*
store double* %19, double** %7
%20 = load double*, double** %7
%21 = load double*, double** %4
%22 = load i32, i32* %5
%23 = load i32, i32* %5
%24 = call i32 (double*, double*, i32, i32, ...) bitcast (i32 (...)* @matcpy to i32 (double*, double*, i32, i32, ...)*)(double* %20, double* %21, i32 %22, i32 %23)
%25 = load double*, double** %7
%26 = load i32, i32* %5
%27 = load i32*, i32** %10
%28 = call i32 (double*, i32, i32*, double*, ...) bitcast (i32 (...)* @ludcmp to i32 (double*, i32, i32*, double*, ...)*)(double* %25, i32 %26, i32* %27, double* %6)
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %2
%31 = load i32*, i32** %10
%32 = bitcast i32* %31 to i8*
%33 = load double*, double** %7
%34 = bitcast double* %33 to i8*
store i32 -1, i32* %3
br label %84
35: ; preds = %2
store i32 0, i32* %9
br label %36
36: ; preds = %76, %35
%37 = load i32, i32* %9
%38 = load i32, i32* %5
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %79
40: ; preds = %36
store i32 0, i32* %8
br label %41
41: ; preds = %54, %40
%42 = load i32, i32* %8
%43 = load i32, i32* %5
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load double*, double** %4
%47 = load i32, i32* %8
%48 = load i32, i32* %9
%49 = load i32, i32* %5
%50 = mul nsw i32 %48, %49
%51 = add nsw i32 %47, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds double, double* %46, i64 %52
store double 0.000000e+00, double* %53
br label %54
54: ; preds = %45
%55 = load i32, i32* %8
%56 = add nsw i32 %55, 1
store i32 %56, i32* %8
br label %41
57: ; preds = %41
%58 = load double*, double** %4
%59 = load i32, i32* %9
%60 = load i32, i32* %9
%61 = load i32, i32* %5
%62 = mul nsw i32 %60, %61
%63 = add nsw i32 %59, %62
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds double, double* %58, i64 %64
store double 1.000000e+00, double* %65
%66 = load double*, double** %7
%67 = load i32, i32* %5
%68 = load i32*, i32** %10
%69 = load double*, double** %4
%70 = load i32, i32* %9
%71 = load i32, i32* %5
%72 = mul nsw i32 %70, %71
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds double, double* %69, i64 %73
%75 = call i32 (double*, i32, i32*, double*, ...) bitcast (i32 (...)* @lubksb to i32 (double*, i32, i32*, double*, ...)*)(double* %66, i32 %67, i32* %68, double* %74)
br label %76
76: ; preds = %57
%77 = load i32, i32* %9
%78 = add nsw i32 %77, 1
store i32 %78, i32* %9
br label %36
79: ; preds = %36
%80 = load i32*, i32** %10
%81 = bitcast i32* %80 to i8*
%82 = load double*, double** %7
%83 = bitcast double* %82 to i8*
store i32 0, i32* %3
br label %84
84: ; preds = %79, %30
%85 = load i32, i32* %3
ret i32 %85
}

LLVM IR (-O2):
define i32 @matinv(double* %0, i32 %1) {
%3 = alloca double
%4 = bitcast double* %3 to i8*
%5 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @imat to i32 (i32, i32, ...)*)(i32 %1, i32 1)
%6 = sext i32 %5 to i64
%7 = inttoptr i64 %6 to i32*
%8 = tail call i32 (i32, i32, ...) bitcast (i32 (...)* @mat to i32 (i32, i32, ...)*)(i32 %1, i32 %1)
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to double*
%11 = tail call i32 (double*, double*, i32, i32, ...) bitcast (i32 (...)* @matcpy to i32 (double*, double*, i32, i32, ...)*)(double* %10, double* %0, i32 %1, i32 %1)
%12 = call i32 (double*, i32, i32*, double*, ...) bitcast (i32 (...)* @ludcmp to i32 (double*, i32, i32*, double*, ...)*)(double* %10, i32 %1, i32* %7, double* %3)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %34
14: ; preds = %2
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %34
16: ; preds = %14
%17 = zext i32 %1 to i64
%18 = shl nuw nsw i64 %17, 3
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %16, %20
%21 = phi i64 [ 0, %16 ], [ %32, %20 ]
%22 = trunc i64 %21 to i32
%23 = mul i32 %22, %1
%24 = sext i32 %23 to i64
%25 = getelementptr double, double* %0, i64 %24
%26 = bitcast double* %25 to i8*
%27 = mul nsw i64 %21, %19
%28 = add nuw nsw i64 %27, %21
%29 = getelementptr inbounds double, double* %0, i64 %28
store double 1.000000e+00, double* %29
%30 = getelementptr inbounds double, double* %0, i64 %27
%31 = call i32 (double*, i32, i32*, double*, ...) bitcast (i32 (...)* @lubksb to i32 (double*, i32, i32*, double*, ...)*)(double* %10, i32 %1, i32* %7, double* %30)
%32 = add nuw nsw i64 %21, 1
%33 = icmp eq i64 %32, %17
br i1 %33, label %34, label %20
34: ; preds = %20, %14, %2
%35 = phi i32 [ -1, %2 ], [ 0, %14 ], [ 0, %20 ]
%36 = inttoptr i64 %6 to i8*
%37 = inttoptr i64 %9 to i8*
ret i32 %35
}