#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;

/* Variables and functions */
int /*<<< orphan*/ eliminate_by_row(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                    int, int);
int /*<<< orphan*/ get_identity_matrix(int /*<<< orphan*/ *, int);
int get_pivot_index(int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ **, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ normalize_pivot_row(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *, int, int);
int /*<<< orphan*/ switch_columns(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                  int, int, int);
void invert_matrix(uint8_t *matrix, uint8_t *result, int size) {
  int row;
  int pivotIndex;
  uint8_t currentRow[size];
  int currentRowSize = size * sizeof(uint8_t);
  get_identity_matrix(result, size);
  for (row = 0; row < size; row++) {
    int index = row;
    memcpy(&currentRow, matrix + row * size, currentRowSize);
    pivotIndex = get_pivot_index(currentRow, index, size);
    if (pivotIndex != row) {
      switch_columns(matrix, result, index, pivotIndex, size);
    }
    normalize_pivot_row(matrix, result, index, size);
    eliminate_by_row(matrix, result, row, size);
  }
}


x86 assembly (clang, -O0):
<invert_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x2,%rax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <get_identity_matrix@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1253 <invert_matrix+0xf3>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%eax
imul   -0x14(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x34(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1050 <get_pivot_index@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     121f <invert_matrix+0xbf>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x14(%rbp),%r8d
call   1080 <switch_columns@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1040 <normalize_pivot_row@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
call   1070 <eliminate_by_row@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11b8 <invert_matrix+0x58>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<invert_matrix>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edx,%ebx
mov    %rdi,-0x38(%rbp)
mov    %edx,%r14d
mov    %rsp,%rcx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rsi,-0x48(%rbp)
mov    %rsi,%rdi
mov    %edx,%esi
call   1030 <get_identity_matrix@plt>
mov    %rbx,%rax
mov    %rbx,-0x30(%rbp)
test   %ebx,%ebx
jle    125a <invert_matrix+0xfa>
mov    -0x30(%rbp),%rax
lea    0x0(,%rax,4),%eax
mov    %eax,-0x3c(%rbp)
lea    0x0(,%r14,4),%rax
mov    %rax,-0x58(%rbp)
xor    %r12d,%r12d
mov    -0x38(%rbp),%r15
mov    %r14,-0x50(%rbp)
mov    -0x48(%rbp),%r13
jmp    121b <invert_matrix+0xbb>
nopl   0x0(%rax,%rax,1)
mov    -0x38(%rbp),%rbx
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
mov    -0x30(%rbp),%r14
mov    %r14d,%ecx
call   1040 <normalize_pivot_row@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12d,%edx
mov    %r14d,%ecx
call   1070 <eliminate_by_row@plt>
add    $0x1,%r12
add    -0x58(%rbp),%r15
mov    -0x50(%rbp),%r14
cmp    %r12,%r14
je     125a <invert_matrix+0xfa>
mov    -0x60(%rbp),%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    -0x3c(%rbp),%edx
call   1060 <memcpy@plt>
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
call   1050 <get_pivot_index@plt>
mov    %eax,%ecx
cmp    %rcx,%r12
je     11e0 <invert_matrix+0x80>
mov    -0x38(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    %r12d,%edx
mov    %eax,%ecx
mov    -0x30(%rbp),%r8
call   1080 <switch_columns@plt>
jmp    11e0 <invert_matrix+0x80>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @invert_matrix(i32* %0, i32* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%13 = load i32, i32* %6
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i32, i64 %14
store i64 %14, i64* %10
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
store i32 %20, i32* %11
%21 = load i32*, i32** %5
%22 = load i32, i32* %6
%23 = call i32 @get_identity_matrix(i32* %21, i32 %22)
store i32 0, i32* %7
br label %24
24: ; preds = %63, %3
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %66
28: ; preds = %24
%29 = load i32, i32* %7
store i32 %29, i32* %12
%30 = bitcast i32* %16 to i32**
%31 = load i32*, i32** %4
%32 = load i32, i32* %7
%33 = load i32, i32* %6
%34 = mul nsw i32 %32, %33
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i32, i32* %31, i64 %35
%37 = load i32, i32* %11
%38 = call i32 @memcpy(i32** %30, i32* %36, i32 %37)
%39 = load i32, i32* %12
%40 = load i32, i32* %6
%41 = call i32 @get_pivot_index(i32* %16, i32 %39, i32 %40)
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = icmp ne i32 %42, %43
br i1 %44, label %45, label %52
45: ; preds = %28
%46 = load i32*, i32** %4
%47 = load i32*, i32** %5
%48 = load i32, i32* %12
%49 = load i32, i32* %8
%50 = load i32, i32* %6
%51 = call i32 @switch_columns(i32* %46, i32* %47, i32 %48, i32 %49, i32 %50)
br label %52
52: ; preds = %45, %28
%53 = load i32*, i32** %4
%54 = load i32*, i32** %5
%55 = load i32, i32* %12
%56 = load i32, i32* %6
%57 = call i32 @normalize_pivot_row(i32* %53, i32* %54, i32 %55, i32 %56)
%58 = load i32*, i32** %4
%59 = load i32*, i32** %5
%60 = load i32, i32* %7
%61 = load i32, i32* %6
%62 = call i32 @eliminate_by_row(i32* %58, i32* %59, i32 %60, i32 %61)
br label %63
63: ; preds = %52
%64 = load i32, i32* %7
%65 = add nsw i32 %64, 1
store i32 %65, i32* %7
br label %24
66: ; preds = %24
%67 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
define void @invert_matrix(i32* %0, i32* %1, i32 %2) {
%4 = zext i32 %2 to i64
%5 = alloca i32, i64 %4
%6 = shl i32 %2, 2
%7 = tail call i32 @get_identity_matrix(i32* %1, i32 %2)
%8 = icmp sgt i32 %2, 0
br i1 %8, label %9, label %28
9: ; preds = %3
%10 = bitcast i32* %5 to i32**
%11 = zext i32 %2 to i64
br label %12
12: ; preds = %9, %23
%13 = phi i64 [ 0, %9 ], [ %26, %23 ]
%14 = mul nsw i64 %13, %11
%15 = getelementptr inbounds i32, i32* %0, i64 %14
%16 = call i32 @memcpy(i32** %10, i32* %15, i32 %6)
%17 = trunc i64 %13 to i32
%18 = call i32 @get_pivot_index(i32* %5, i32 %17, i32 %2)
%19 = zext i32 %18 to i64
%20 = icmp eq i64 %13, %19
br i1 %20, label %23, label %21
21: ; preds = %12
%22 = call i32 @switch_columns(i32* %0, i32* %1, i32 %17, i32 %18, i32 %2)
br label %23
23: ; preds = %21, %12
%24 = call i32 @normalize_pivot_row(i32* %0, i32* %1, i32 %17, i32 %2)
%25 = call i32 @eliminate_by_row(i32* %0, i32* %1, i32 %17, i32 %2)
%26 = add nuw nsw i64 %13, 1
%27 = icmp eq i64 %26, %4
br i1 %27, label %28, label %12
28: ; preds = %23, %3
ret void
}