#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 /*<<< orphan*/ free(double *);
double *mat(int, int);
int matinv(double *, int);
int /*<<< orphan*/ matmul(char *, int, int, int, double, double const *,
                          double const *, double, double *);
extern int lsq(const double *A, const double *y, int n, int m, double *x,
               double *Q) {
  double *Ay;
  int info;
  if (m < n)
    return -1;
  Ay = mat(n, 1);
  matmul("NN", n, 1, m, 1.0, A, y, 0.0, Ay);
  matmul("NT", n, n, m, 1.0, A, A, 0.0, Q);
  if (!(info = matinv(Q, n)))
    matmul("NN", n, 1, n, 1.0, Q, Ay, 0.0, x);
  free(Ay);
  return info;
}


x86 assembly (clang, -O0):
<lsq>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1176 <lsq+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    1242 <lsq+0x102>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1040 <mat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    -0x38(%rbp),%rax
lea    0xe68(%rip),%rdi
mov    $0x1,%edx
movsd  0xe53(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    %rax,(%rsp)
call   1060 <matmul@plt>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    -0x10(%rbp),%r8
mov    -0x10(%rbp),%r9
mov    -0x30(%rbp),%rax
lea    0xe36(%rip),%rdi
movsd  0xe23(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    %rax,(%rsp)
call   1060 <matmul@plt>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1050 <matinv@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jne    1233 <lsq+0xf3>
mov    -0x1c(%rbp),%esi
mov    -0x1c(%rbp),%ecx
mov    -0x30(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    -0x28(%rbp),%rax
lea    0xdee(%rip),%rdi
mov    $0x1,%edx
movsd  0xdd9(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    %rax,(%rsp)
call   1060 <matmul@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lsq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r15d
cmp    %edx,%ecx
jl     1219 <lsq+0xd9>
mov    %r9,%r12
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%r13
mov    %r8,0x10(%rsp)
mov    %edx,%edi
mov    $0x1,%esi
call   1040 <mat@plt>
mov    %rax,%r15
mov    %rax,(%rsp)
lea    0xe80(%rip),%rdi
movsd  0xe70(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    %ebp,%esi
mov    $0x1,%edx
mov    %ebx,%ecx
mov    %r13,%r8
mov    %r14,%r9
call   1060 <matmul@plt>
mov    %r12,(%rsp)
lea    0xe59(%rip),%rdi
xorps  %xmm1,%xmm1
mov    %ebp,%esi
mov    %ebp,%edx
mov    %ebx,%ecx
mov    %r15,%rbx
movsd  0xe3a(%rip),%xmm0
mov    %r13,%r8
mov    %r13,%r9
call   1060 <matmul@plt>
mov    %r12,%rdi
mov    %ebp,%esi
call   1050 <matinv@plt>
mov    %eax,%r15d
test   %eax,%eax
jne    1211 <lsq+0xd1>
mov    0x10(%rsp),%rax
mov    %rax,(%rsp)
lea    0xe16(%rip),%rdi
movsd  0xe06(%rip),%xmm0
xorps  %xmm1,%xmm1
mov    %ebp,%esi
mov    $0x1,%edx
mov    %ebp,%ecx
mov    %r12,%r8
mov    %rbx,%r9
call   1060 <matmul@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"NN\00"
@.str.1 = [3 x i8] c"NT\00"
define i32 @lsq(double* %0, double* %1, i32 %2, i32 %3, double* %4, double* %5) {
%7 = alloca i32
%8 = alloca double*
%9 = alloca double*
%10 = alloca i32
%11 = alloca i32
%12 = alloca double*
%13 = alloca double*
%14 = alloca double*
%15 = alloca i32
store double* %0, double** %8
store double* %1, double** %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store double* %4, double** %12
store double* %5, double** %13
%16 = load i32, i32* %11
%17 = load i32, i32* %10
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %6
store i32 -1, i32* %7
br label %51
20: ; preds = %6
%21 = load i32, i32* %10
%22 = call double* @mat(i32 %21, i32 1)
store double* %22, double** %14
%23 = load i32, i32* %10
%24 = load i32, i32* %11
%25 = load double*, double** %8
%26 = load double*, double** %9
%27 = load double*, double** %14
%28 = call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %23, i32 1, i32 %24, double 1.000000e+00, double* %25, double* %26, double 0.000000e+00, double* %27)
%29 = load i32, i32* %10
%30 = load i32, i32* %10
%31 = load i32, i32* %11
%32 = load double*, double** %8
%33 = load double*, double** %8
%34 = load double*, double** %13
%35 = call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30, i32 %31, double 1.000000e+00, double* %32, double* %33, double 0.000000e+00, double* %34)
%36 = load double*, double** %13
%37 = load i32, i32* %10
%38 = call i32 @matinv(double* %36, i32 %37)
store i32 %38, i32* %15
%39 = icmp ne i32 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %20
%41 = load i32, i32* %10
%42 = load i32, i32* %10
%43 = load double*, double** %13
%44 = load double*, double** %14
%45 = load double*, double** %12
%46 = call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %41, i32 1, i32 %42, double 1.000000e+00, double* %43, double* %44, double 0.000000e+00, double* %45)
br label %47
47: ; preds = %40, %20
%48 = load double*, double** %14
%49 = call i32 @free(double* %48)
%50 = load i32, i32* %15
store i32 %50, i32* %7
br label %51
51: ; preds = %47, %19
%52 = load i32, i32* %7
ret i32 %52
}

LLVM IR (-O2):
@.str = [3 x i8] c"NN\00"
@.str.1 = [3 x i8] c"NT\00"
define i32 @lsq(double* %0, double* %1, i32 %2, i32 %3, double* %4, double* %5) {
%7 = icmp slt i32 %3, %2
br i1 %7, label %18, label %8
8: ; preds = %6
%9 = tail call double* @mat(i32 %2, i32 1)
%10 = tail call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2, i32 1, i32 %3, double 1.000000e+00, double* %0, double* %1, double 0.000000e+00, double* %9)
%11 = tail call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %2, i32 %3, double 1.000000e+00, double* %0, double* %0, double 0.000000e+00, double* %5)
%12 = tail call i32 @matinv(double* %5, i32 %2)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @matmul(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %2, i32 1, i32 %2, double 1.000000e+00, double* %5, double* %9, double 0.000000e+00, double* %4)
br label %16
16: ; preds = %14, %8
%17 = tail call i32 @free(double* %9)
br label %18
18: ; preds = %6, %16
%19 = phi i32 [ %12, %16 ], [ -1, %6 ]
ret i32 %19
}