int pcapack_inverse(const int n, double *restrict x) {
  int info = 0;
  int *ipiv;
  int lwork;
  double tmp;
  double *work;
  ipiv = malloc(n * sizeof(*ipiv));
  dgetrf_(&n, &n, x, &n, ipiv, &info);
  if (info != 0)
    goto cleanup;
  lwork = -1;
  dgetri_(&n, x, &n, ipiv, &tmp, &lwork, &info);
  if (info != 0)
    goto cleanup;
  lwork = (int)tmp;
  work = malloc(lwork * sizeof(*work));
  dgetri_(&n, x, &n, ipiv, work, &lwork, &info);
  free(work);
cleanup:
  free(ipiv);
  return info;
}


x86 assembly (clang, -O0):
<pcapack_inverse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movslq -0x4(%rbp),%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%r8
lea    -0x4(%rbp),%rcx
lea    -0x14(%rbp),%r9
mov    %rcx,%rdi
mov    %rcx,%rsi
mov    $0x0,%al
call   1060 <dgetrf_@plt>
cmpl   $0x0,-0x14(%rbp)
je     1193 <pcapack_inverse+0x53>
jmp    1217 <pcapack_inverse+0xd7>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
lea    -0x4(%rbp),%rdx
lea    -0x30(%rbp),%r8
lea    -0x24(%rbp),%r9
lea    -0x14(%rbp),%rax
mov    %rdx,%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1050 <dgetri_@plt>
cmpl   $0x0,-0x14(%rbp)
je     11cf <pcapack_inverse+0x8f>
jmp    1217 <pcapack_inverse+0xd7>
cvttsd2si -0x30(%rbp),%eax
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x38(%rbp),%r8
lea    -0x4(%rbp),%rdx
lea    -0x24(%rbp),%r9
lea    -0x14(%rbp),%rax
mov    %rdx,%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1050 <dgetri_@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pcapack_inverse>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
mov    %edi,0x1c(%rsp)
movl   $0x0,0x14(%rsp)
movslq %edi,%rdi
shl    $0x2,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbx
lea    0x1c(%rsp),%rcx
lea    0x14(%rsp),%r15
mov    %rcx,%rdi
mov    %rcx,%rsi
mov    %r14,%rdx
mov    %rax,%r8
mov    %r15,%r9
xor    %eax,%eax
call   1060 <dgetrf_@plt>
mov    0x14(%rsp),%ebp
test   %ebp,%ebp
jne    1209 <pcapack_inverse+0xc9>
movl   $0xffffffff,0x18(%rsp)
ff
mov    %r15,(%rsp)
lea    0x1c(%rsp),%rdi
lea    0x20(%rsp),%r8
lea    0x18(%rsp),%r9
mov    %r14,%rsi
mov    %rdi,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <dgetri_@plt>
mov    0x14(%rsp),%ebp
test   %ebp,%ebp
jne    1209 <pcapack_inverse+0xc9>
cvttsd2si 0x20(%rsp),%eax
mov    %eax,0x18(%rsp)
movslq %eax,%rdi
shl    $0x3,%rdi
call   1040 <malloc@plt>
mov    %rax,%rbp
mov    %r15,(%rsp)
lea    0x1c(%rsp),%rdi
lea    0x18(%rsp),%r9
mov    %r14,%rsi
mov    %rdi,%rdx
mov    %rbx,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1050 <dgetri_@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    0x14(%rsp),%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @pcapack_inverse(i32 %0, double* noalias %1) {
%3 = alloca i32
%4 = alloca double*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca double
%9 = alloca double*
store i32 %0, i32* %3
store double* %1, double** %4
store i32 0, i32* %5
%10 = load i32, i32* %3
%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 double*, double** %4
%16 = load i32*, i32** %6
%17 = call i32 (i32*, i32*, double*, i32*, i32*, i32*, ...) bitcast (i32 (...)* @dgetrf_ to i32 (i32*, i32*, double*, i32*, i32*, i32*, ...)*)(i32* %3, i32* %3, double* %15, i32* %3, i32* %16, i32* %5)
%18 = load i32, i32* %5
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %2
br label %42
21: ; preds = %2
store i32 -1, i32* %7
%22 = load double*, double** %4
%23 = load i32*, i32** %6
%24 = call i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...) bitcast (i32 (...)* @dgetri_ to i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...)*)(i32* %3, double* %22, i32* %3, i32* %23, double* %8, i32* %7, i32* %5)
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
br label %42
28: ; preds = %21
%29 = load double, double* %8
%30 = fptosi double %29 to i32
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = mul i64 %32, 8
%34 = call i8* @malloc(i64 %33)
%35 = bitcast i8* %34 to double*
store double* %35, double** %9
%36 = load double*, double** %4
%37 = load i32*, i32** %6
%38 = load double*, double** %9
%39 = call i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...) bitcast (i32 (...)* @dgetri_ to i32 (i32*, double*, i32*, i32*, double*, i32*, i32*, ...)*)(i32* %3, double* %36, i32* %3, i32* %37, double* %38, i32* %7, i32* %5)
%40 = load double*, double** %9
%41 = bitcast double* %40 to i8*
br label %42
42: ; preds = %28, %27, %20
%43 = load i32*, i32** %6
%44 = bitcast i32* %43 to i8*
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
define i32 @pcapack_inverse(i32 %0, double* noalias %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
store i32 %0, i32* %3
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = bitcast i32* %5 to i8*
%9 = bitcast double* %6 to i8*
%10 = sext i32 %0 to i64
%11 = shl nsw i64 %10, 2
%12 = tail call i8* @malloc(i64 %11)
%13 = bitcast i8* %12 to i32*
%14 = 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* %13, i32* %4)
%15 = load i32, i32* %4
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %30
17: ; preds = %2
store i32 -1, i32* %5
%18 = 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* %13, double* %6, i32* %5, i32* %4)
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = load double, double* %6
%23 = fptosi double %22 to i32
store i32 %23, i32* %5
%24 = sext i32 %23 to i64
%25 = shl nsw i64 %24, 3
%26 = call i8* @malloc(i64 %25)
%27 = bitcast i8* %26 to double*
%28 = 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* %13, double* %27, i32* %5, i32* %4)
%29 = load i32, i32* %4
br label %30
30: ; preds = %17, %2, %21
%31 = phi i32 [ %19, %17 ], [ %15, %2 ], [ %29, %21 ]
ret i32 %31
}