int matlib_invert(int n, double *a) {
  int *ipiv = (int *)malloc(n * sizeof(int));
  int lwork = 64 * 2 * n;
  double *work = (double *)malloc(lwork * sizeof(double));
  int info;
  dgetrf_(&n, &n, a, &n, ipiv, &info);
  if (info) {
    return info;
  }
  dgetri_(&n, a, &n, ipiv, work, &lwork, &info);
  free(ipiv);
  free(work);
  return info;
}


x86 assembly (clang, -O0):
<matlib_invert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movslq -0x8(%rbp),%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x7,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%r8
lea    -0x8(%rbp),%rcx
lea    -0x2c(%rbp),%r9
mov    %rcx,%rdi
mov    %rcx,%rsi
mov    $0x0,%al
call   1060 <dgetrf_@plt>
cmpl   $0x0,-0x2c(%rbp)
je     11ac <matlib_invert+0x6c>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ea <matlib_invert+0xaa>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    -0x8(%rbp),%rdx
lea    -0x1c(%rbp),%r9
lea    -0x2c(%rbp),%rax
mov    %rdx,%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1050 <dgetri_@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matlib_invert>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,0x18(%rsp)
movslq %edi,%rbx
lea    0x0(,%rbx,4),%rdi
call   1040 <malloc@plt>
mov    %rax,%r13
shl    $0x7,%ebx
mov    %ebx,0x1c(%rsp)
movslq %ebx,%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
lea    0x18(%rsp),%rcx
lea    0x14(%rsp),%r12
mov    %rcx,%rdi
mov    %rcx,%rsi
mov    %r14,%rdx
mov    %r13,%r8
mov    %r12,%r9
xor    %eax,%eax
call   1060 <dgetrf_@plt>
mov    0x14(%rsp),%eax
test   %eax,%eax
jne    11da <matlib_invert+0x9a>
mov    %r12,(%rsp)
lea    0x18(%rsp),%rdi
lea    0x1c(%rsp),%r9
mov    %r14,%rsi
mov    %rdi,%rdx
mov    %r13,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   1050 <dgetri_@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    0x14(%rsp),%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @matlib_invert(i32 %0, double* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca double*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca double*
%9 = alloca i32
store i32 %0, i32* %4
store double* %1, double** %5
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = mul i64 %11, 4
%13 = call i8* @malloc(i64 %12)
%14 = bitcast i8* %13 to i32*
store i32* %14, i32** %6
%15 = load i32, i32* %4
%16 = mul nsw i32 128, %15
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 8
%20 = call i8* @malloc(i64 %19)
%21 = bitcast i8* %20 to double*
store double* %21, double** %8
%22 = load double*, double** %5
%23 = load i32*, i32** %6
%24 = call i32 (i32*, i32*, double*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @dgetrf_ to i32 (i32*, i32*, double*, i32*, i32*, i32*, ...)*)(i32* %4, i32* %4, double* %22, i32* %4, i32* %23, i32* %9)
%25 = load i32, i32* %9
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %2
%28 = load i32, i32* %9
store i32 %28, i32* %3
br label %39
29: ; preds = %2
%30 = load double*, double** %5
%31 = load i32*, i32** %6
%32 = load double*, double** %8
%33 = call i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...) bitcast (i32 (...)* @dgetri_ to i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...)*)(i32* %4, double* %30, i32* %4, i32* %31, double* %32, i32* %7, i32* %9)
%34 = load i32*, i32** %6
%35 = bitcast i32* %34 to i8*
%36 = load double*, double** %8
%37 = bitcast double* %36 to i8*
%38 = load i32, i32* %9
store i32 %38, i32* %3
br label %39
39: ; preds = %29, %27
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
define i32 @matlib_invert(i32 %0, double* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %3
%6 = sext i32 %0 to i64
%7 = shl nsw i64 %6, 2
%8 = tail call i8* @malloc(i64 %7)
%9 = bitcast i8* %8 to i32*
%10 = bitcast i32* %4 to i8*
%11 = shl nsw i32 %0, 7
store i32 %11, i32* %4
%12 = sext i32 %11 to i64
%13 = shl nsw i64 %12, 3
%14 = tail call i8* @malloc(i64 %13)
%15 = bitcast i32* %5 to i8*
%16 = call i32 (i32*, i32*, double*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @dgetrf_ to i32 (i32*, i32*, double*, i32*, i32*, i32*, ...)*)(i32* %3, i32* %3, double* %1, i32* %3, i32* %9, i32* %5)
%17 = load i32, i32* %5
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %2
%20 = bitcast i8* %14 to double*
%21 = call i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...) bitcast (i32 (...)* @dgetri_ to i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...)*)(i32* %3, double* %1, i32* %3, i32* %9, double* %20, i32* %4, i32* %5)
%22 = load i32, i32* %5
br label %23
23: ; preds = %2, %19
%24 = phi i32 [ %22, %19 ], [ %17, %2 ]
ret i32 %24
}