#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 */

/* Variables and functions */
int fabs(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ mat3_det(double **);
int /*<<< orphan*/ mat3_mul_vec(double *, double **, double *);
int /*<<< orphan*/ mat3_solve(double *, double **, double *);
int /*<<< orphan*/ printf(char *);
double rnd_double();
int /*<<< orphan*/ rnd_init();
int /*<<< orphan*/ stderr;
scalar_t__ vec3_cmp(double *, double *);
int /*<<< orphan*/ vec3_print(double *);
int test_solve(void) {
  int i, j, k;
  double A[3][3], b[3], x[3], solved[3];
  rnd_init();
  for (i = 0; i < 10000; i++) {
    for (j = 0; j < 3; j++) {
      for (k = 0; k < 3; k++)
        A[j][k] = rnd_double();
      x[j] = rnd_double();
    }
    mat3_mul_vec(b, A, x);
    if (fabs(mat3_det(A)) < 1e-10)
      continue;
    mat3_solve(solved, A, b);
    if (vec3_cmp(solved, x)) {
      fprintf(stderr, "Failed to solve 3x3 system!\n");
      printf("Got:\n");
      vec3_print(solved);
      printf("Expected:\n");
      vec3_print(x);
      return -1;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<test_solve>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    $0x0,%al
call   1050 <rnd_init@plt>
movl   $0x0,-0x8(%rbp)
cmpl   $0x2710,-0x8(%rbp)
jge    130b <test_solve+0x16b>
movl   $0x0,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jge    1237 <test_solve+0x97>
movl   $0x0,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
jge    1215 <test_solve+0x75>
mov    $0x0,%al
call   10b0 <rnd_double@plt>
movslq -0xc(%rbp),%rcx
lea    -0x60(%rbp),%rax
imul   $0x18,%rcx,%rcx
add    %rcx,%rax
movslq -0x10(%rbp),%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11de <test_solve+0x3e>
mov    $0x0,%al
call   10b0 <rnd_double@plt>
movslq -0xc(%rbp),%rax
movsd  %xmm0,-0xa0(%rbp,%rax,8)
ff ff
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11cd <test_solve+0x2d>
lea    -0x80(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0xa0(%rbp),%rdx
call   10a0 <mat3_mul_vec@plt>
lea    -0x60(%rbp),%rdi
call   1030 <mat3_det@plt>
mov    %eax,%edi
call   1070 <fabs@plt>
cvtsi2sd %eax,%xmm1
movsd  0xd99(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1276 <test_solve+0xd6>
jmp    12fd <test_solve+0x15d>
lea    -0xc0(%rbp),%rdi
lea    -0x60(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1080 <mat3_solve@plt>
lea    -0xc0(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   10c0 <vec3_cmp@plt>
cmp    $0x0,%rax
je     12f8 <test_solve+0x158>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
lea    0xd51(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xd62(%rip),%rdi
call   1040 <printf@plt>
lea    -0xc0(%rbp),%rdi
call   1060 <vec3_print@plt>
lea    0xd50(%rip),%rdi
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rdi
call   1060 <vec3_print@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1312 <test_solve+0x172>
jmp    12fd <test_solve+0x15d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    11b9 <test_solve+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_solve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0xb0,%rsp
xor    %eax,%eax
call   1050 <rnd_init@plt>
mov    $0x2710,%ebp
lea    0x90(%rsp),%r14
lea    0x20(%rsp),%rbx
mov    %rsp,%r15
lea    0x70(%rsp),%r12
jmp    11e9 <test_solve+0x49>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0xffffffff,%ebp
je     1311 <test_solve+0x171>
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x20(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x28(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x30(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x38(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x40(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x48(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x8(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x50(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x58(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x60(%rsp)
xor    %eax,%eax
call   10b0 <rnd_double@plt>
movsd  %xmm0,0x10(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
call   10a0 <mat3_mul_vec@plt>
mov    %rbx,%rdi
call   1030 <mat3_det@plt>
mov    %eax,%edi
call   1070 <fabs@plt>
test   %eax,%eax
jle    11e0 <test_solve+0x40>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1080 <mat3_solve@plt>
mov    %r12,%rdi
mov    %r15,%rsi
call   10c0 <vec3_cmp@plt>
test   %rax,%rax
je     11e0 <test_solve+0x40>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
call   1090 <fprintf@plt>
lea    0xd36(%rip),%rdi
call   1040 <puts@plt>
lea    0x70(%rsp),%rdi
call   1060 <vec3_print@plt>
lea    0xd25(%rip),%rdi
call   1040 <puts@plt>
mov    %rsp,%rdi
call   1060 <vec3_print@plt>
mov    $0xffffffff,%eax
jmp    1313 <test_solve+0x173>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [29 x i8] c"Failed to solve 3x3 system!\0A\00"
@.str.1 = [6 x i8] c"Got:\0A\00"
@.str.2 = [11 x i8] c"Expected:\0A\00"
define i32 @test_solve() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [3 x [3 x double]]
%6 = alloca [3 x double]
%7 = alloca [3 x double]
%8 = alloca [3 x double]
%9 = call i32 (...) @rnd_init()
store i32 0, i32* %2
br label %10
10: ; preds = %73, %0
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 10000
br i1 %12, label %13, label %76
13: ; preds = %10
store i32 0, i32* %3
br label %14
14: ; preds = %37, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 3
br i1 %16, label %17, label %40
17: ; preds = %14
store i32 0, i32* %4
br label %18
18: ; preds = %29, %17
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %32
21: ; preds = %18
%22 = call double (...) @rnd_double()
%23 = load i32, i32* %3
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %5, i64 0, i64 %24
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [3 x double], [3 x double]* %25, i64 0, i64 %27
store double %22, double* %28
br label %29
29: ; preds = %21
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %18
32: ; preds = %18
%33 = call double (...) @rnd_double()
%34 = load i32, i32* %3
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 %35
store double %33, double* %36
br label %37
37: ; preds = %32
%38 = load i32, i32* %3
%39 = add nsw i32 %38, 1
store i32 %39, i32* %3
br label %14
40: ; preds = %14
%41 = getelementptr inbounds [3 x double], [3 x double]* %6, i64 0, i64 0
%42 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %5, i64 0, i64 0
%43 = bitcast [3 x double]* %42 to double**
%44 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%45 = call i32 @mat3_mul_vec(double* %41, double** %43, double* %44)
%46 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %5, i64 0, i64 0
%47 = bitcast [3 x double]* %46 to double**
%48 = call i32 @mat3_det(double** %47)
%49 = call i32 @fabs(i32 %48)
%50 = sitofp i32 %49 to double
%51 = fcmp olt double %50, 1.000000e-10
br i1 %51, label %52, label %53
52: ; preds = %40
br label %73
53: ; preds = %40
%54 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%55 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %5, i64 0, i64 0
%56 = bitcast [3 x double]* %55 to double**
%57 = getelementptr inbounds [3 x double], [3 x double]* %6, i64 0, i64 0
%58 = call i32 @mat3_solve(double* %54, double** %56, double* %57)
%59 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%60 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%61 = call i64 @vec3_cmp(double* %59, double* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %72
63: ; preds = %53
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%66 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%67 = getelementptr inbounds [3 x double], [3 x double]* %8, i64 0, i64 0
%68 = call i32 @vec3_print(double* %67)
%69 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%70 = getelementptr inbounds [3 x double], [3 x double]* %7, i64 0, i64 0
%71 = call i32 @vec3_print(double* %70)
store i32 -1, i32* %1
br label %77
72: ; preds = %53
br label %73
73: ; preds = %72, %52
%74 = load i32, i32* %2
%75 = add nsw i32 %74, 1
store i32 %75, i32* %2
br label %10
76: ; preds = %10
store i32 0, i32* %1
br label %77
77: ; preds = %76, %63
%78 = load i32, i32* %1
ret i32 %78
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [29 x i8] c"Failed to solve 3x3 system!\0A\00"
@str = [5 x i8] c"Got:\00"
@str.3 = [10 x i8] c"Expected:\00"
define i32 @test_solve() {
%1 = alloca [3 x [3 x double]]
%2 = alloca [3 x double]
%3 = alloca [3 x double]
%4 = alloca [3 x double]
%5 = bitcast [3 x [3 x double]]* %1 to i8*
%6 = bitcast [3 x double]* %2 to i8*
%7 = bitcast [3 x double]* %3 to i8*
%8 = bitcast [3 x double]* %4 to i8*
%9 = tail call i32 (...) @rnd_init()
%10 = getelementptr inbounds [3 x double], [3 x double]* %2, i64 0, i64 0
%11 = bitcast [3 x [3 x double]]* %1 to double**
%12 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 0
%13 = getelementptr inbounds [3 x double], [3 x double]* %4, i64 0, i64 0
%14 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 0, i64 0
%15 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 0, i64 1
%16 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 0, i64 2
%17 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 1, i64 0
%18 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 1, i64 1
%19 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 1, i64 2
%20 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 1
%21 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 2, i64 0
%22 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 2, i64 1
%23 = getelementptr inbounds [3 x [3 x double]], [3 x [3 x double]]* %1, i64 0, i64 2, i64 2
%24 = getelementptr inbounds [3 x double], [3 x double]* %3, i64 0, i64 2
br label %25
25: ; preds = %0, %54
%26 = phi i32 [ 0, %0 ], [ %55, %54 ]
%27 = call double (...) @rnd_double()
store double %27, double* %14
%28 = call double (...) @rnd_double()
store double %28, double* %15
%29 = call double (...) @rnd_double()
store double %29, double* %16
%30 = call double (...) @rnd_double()
store double %30, double* %12
%31 = call double (...) @rnd_double()
store double %31, double* %17
%32 = call double (...) @rnd_double()
store double %32, double* %18
%33 = call double (...) @rnd_double()
store double %33, double* %19
%34 = call double (...) @rnd_double()
store double %34, double* %20
%35 = call double (...) @rnd_double()
store double %35, double* %21
%36 = call double (...) @rnd_double()
store double %36, double* %22
%37 = call double (...) @rnd_double()
store double %37, double* %23
%38 = call double (...) @rnd_double()
store double %38, double* %24
%39 = call i32 @mat3_mul_vec(double* %10, double** %11, double* %12)
%40 = call i32 @mat3_det(double** %11)
%41 = call i32 @fabs(i32 %40)
%42 = icmp slt i32 %41, 1
br i1 %42, label %54, label %43
43: ; preds = %25
%44 = call i32 @mat3_solve(double* %13, double** %11, double* %10)
%45 = call i64 @vec3_cmp(double* %13, double* %12)
%46 = icmp eq i64 %45, 0
br i1 %46, label %54, label %47
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%51 = call i32 @vec3_print(double* %13)
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.3, i64 0, i64 0))
%53 = call i32 @vec3_print(double* %12)
br label %57
54: ; preds = %43, %25
%55 = add nuw nsw i32 %26, 1
%56 = icmp eq i32 %55, 10000
br i1 %56, label %57, label %25
57: ; preds = %54, %47
%58 = phi i32 [ -1, %47 ], [ 0, %54 ]
ret i32 %58
}