#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 *);
int iterate_conjgrad(double *, int, int, int, double *, double *, double *,
                     double *, double *, double *);
double *malloc(int);
int conjugate_gradient(double *A, int m, int n, int stride, double *b,
                       double *x0) {
  double *y, *res, *dir, *tmp;
  int err;
  err = 0;
  y = NULL;
  if (m >= n) {
    tmp = malloc(m * sizeof tmp[0]);
    dir = malloc(m * sizeof dir[0]);
    res = malloc(m * sizeof res[0]);
    if ((err = iterate_conjgrad(A, m, n, stride, b, x0, res, dir, tmp, NULL)) ==
        -1) {
      err = -1;
      goto err_out;
    }
  } else {
    y = malloc(m * sizeof y[0]);
    tmp = malloc(m * sizeof tmp[0]);
    dir = malloc(n * sizeof dir[0]);
    res = malloc(m * sizeof res[0]);
    if ((err = iterate_conjgrad(A, m, n, stride, b, y, res, dir, tmp, x0)) ==
        -1) {
      err = -1;
      goto err_out;
    }
  }
err_out:
  if (y != NULL)
    free(y);
  free(res);
  free(dir);
  free(tmp);
  return err;
}


x86 assembly (clang, -O0):
<conjugate_gradient>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x78,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x54(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jl     11fe <conjugate_gradient+0xce>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x50(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x30(%rbp),%r9
mov    -0x40(%rbp),%r11
mov    -0x48(%rbp),%r10
mov    -0x50(%rbp),%rax
xor    %ebx,%ebx
mov    %r11,(%rsp)
mov    %r10,0x8(%rsp)
mov    %rax,0x10(%rsp)
movq   $0x0,0x18(%rsp)
call   1040 <iterate_conjgrad@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <conjugate_gradient+0xc9>
movl   $0xffffffff,-0x54(%rbp)
jmp    12a9 <conjugate_gradient+0x179>
jmp    12a4 <conjugate_gradient+0x174>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x50(%rbp)
movslq -0x18(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%r8
mov    -0x38(%rbp),%r9
mov    -0x40(%rbp),%rbx
mov    -0x48(%rbp),%r11
mov    -0x50(%rbp),%r10
mov    -0x30(%rbp),%rax
mov    %rbx,(%rsp)
mov    %r11,0x8(%rsp)
mov    %r10,0x10(%rsp)
mov    %rax,0x18(%rsp)
call   1040 <iterate_conjgrad@plt>
mov    %eax,-0x54(%rbp)
cmp    $0xffffffff,%eax
jne    129f <conjugate_gradient+0x16f>
movl   $0xffffffff,-0x54(%rbp)
jmp    12a9 <conjugate_gradient+0x179>
jmp    12a4 <conjugate_gradient+0x174>
jmp    12a9 <conjugate_gradient+0x179>
cmpq   $0x0,-0x38(%rbp)
je     12bd <conjugate_gradient+0x18d>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rdi
call   1030 <free@plt>
mov    -0x54(%rbp),%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<conjugate_gradient>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,0x18(%rsp)
mov    %r8,0x10(%rsp)
mov    %ecx,0x4(%rsp)
mov    %edx,%r12d
mov    %esi,%r13d
mov    %rdi,0x8(%rsp)
lea    0x0(,%r13,8),%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
cmp    %r12d,%r13d
jge    11d9 <conjugate_gradient+0xa9>
lea    0x0(,%r12,8),%edi
call   1050 <malloc@plt>
mov    %rax,0x20(%rsp)
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    0x20(%rsp),%r12
mov    0x4(%rsp),%ecx
mov    0x10(%rsp),%r8
mov    %rbx,%r9
push   0x18(%rsp)
push   %r14
push   %r12
push   %rax
call   1040 <iterate_conjgrad@plt>
add    $0x20,%rsp
mov    %eax,%ebp
test   %rbx,%rbx
je     11d1 <conjugate_gradient+0xa1>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rbx
mov    %r12,%r14
jmp    120d <conjugate_gradient+0xdd>
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
mov    0x8(%rsp),%rdi
mov    %r13d,%esi
mov    %r12d,%edx
mov    0x4(%rsp),%ecx
mov    0x10(%rsp),%r8
mov    0x18(%rsp),%r9
push   $0x0
push   %rbx
push   %r14
push   %rax
call   1040 <iterate_conjgrad@plt>
add    $0x20,%rsp
mov    %eax,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @conjugate_gradient(double* %0, i32 %1, i32 %2, i32 %3, double* %4, double* %5) {
%7 = alloca double*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca double*
%12 = alloca double*
%13 = alloca double*
%14 = alloca double*
%15 = alloca double*
%16 = alloca double*
%17 = alloca i32
store double* %0, double** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store double* %4, double** %11
store double* %5, double** %12
store i32 0, i32* %17
store double* null, double** %13
%18 = load i32, i32* %8
%19 = load i32, i32* %9
%20 = icmp sge i32 %18, %19
br i1 %20, label %21, label %50
21: ; preds = %6
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = mul i64 %23, 8
%25 = trunc i64 %24 to i32
%26 = call double* @malloc(i32 %25)
store double* %26, double** %16
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = mul i64 %28, 8
%30 = trunc i64 %29 to i32
%31 = call double* @malloc(i32 %30)
store double* %31, double** %15
%32 = load i32, i32* %8
%33 = sext i32 %32 to i64
%34 = mul i64 %33, 8
%35 = trunc i64 %34 to i32
%36 = call double* @malloc(i32 %35)
store double* %36, double** %14
%37 = load double*, double** %7
%38 = load i32, i32* %8
%39 = load i32, i32* %9
%40 = load i32, i32* %10
%41 = load double*, double** %11
%42 = load double*, double** %12
%43 = load double*, double** %14
%44 = load double*, double** %15
%45 = load double*, double** %16
%46 = call i32 @iterate_conjgrad(double* %37, i32 %38, i32 %39, i32 %40, double* %41, double* %42, double* %43, double* %44, double* %45, double* null)
store i32 %46, i32* %17
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %49
48: ; preds = %21
store i32 -1, i32* %17
br label %86
49: ; preds = %21
br label %85
50: ; preds = %6
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = mul i64 %52, 8
%54 = trunc i64 %53 to i32
%55 = call double* @malloc(i32 %54)
store double* %55, double** %13
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = mul i64 %57, 8
%59 = trunc i64 %58 to i32
%60 = call double* @malloc(i32 %59)
store double* %60, double** %16
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = mul i64 %62, 8
%64 = trunc i64 %63 to i32
%65 = call double* @malloc(i32 %64)
store double* %65, double** %15
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = mul i64 %67, 8
%69 = trunc i64 %68 to i32
%70 = call double* @malloc(i32 %69)
store double* %70, double** %14
%71 = load double*, double** %7
%72 = load i32, i32* %8
%73 = load i32, i32* %9
%74 = load i32, i32* %10
%75 = load double*, double** %11
%76 = load double*, double** %13
%77 = load double*, double** %14
%78 = load double*, double** %15
%79 = load double*, double** %16
%80 = load double*, double** %12
%81 = call i32 @iterate_conjgrad(double* %71, i32 %72, i32 %73, i32 %74, double* %75, double* %76, double* %77, double* %78, double* %79, double* %80)
store i32 %81, i32* %17
%82 = icmp eq i32 %81, -1
br i1 %82, label %83, label %84
83: ; preds = %50
store i32 -1, i32* %17
br label %86
84: ; preds = %50
br label %85
85: ; preds = %84, %49
br label %86
86: ; preds = %85, %83, %48
%87 = load double*, double** %13
%88 = icmp ne double* %87, null
br i1 %88, label %89, label %92
89: ; preds = %86
%90 = load double*, double** %13
%91 = call i32 @free(double* %90)
br label %92
92: ; preds = %89, %86
%93 = load double*, double** %14
%94 = call i32 @free(double* %93)
%95 = load double*, double** %15
%96 = call i32 @free(double* %95)
%97 = load double*, double** %16
%98 = call i32 @free(double* %97)
%99 = load i32, i32* %17
ret i32 %99
}

LLVM IR (-O2):
define i32 @conjugate_gradient(double* %0, i32 %1, i32 %2, i32 %3, double* %4, double* %5) {
%7 = icmp slt i32 %1, %2
%8 = shl i32 %1, 3
%9 = tail call double* @malloc(i32 %8)
%10 = tail call double* @malloc(i32 %8)
br i1 %7, label %14, label %11
11: ; preds = %6
%12 = tail call double* @malloc(i32 %8)
%13 = tail call i32 @iterate_conjgrad(double* %0, i32 %1, i32 %2, i32 %3, double* %4, double* %5, double* %12, double* %10, double* %9, double* null)
br label %22
14: ; preds = %6
%15 = shl i32 %2, 3
%16 = tail call double* @malloc(i32 %15)
%17 = tail call double* @malloc(i32 %8)
%18 = tail call i32 @iterate_conjgrad(double* %0, i32 %1, i32 %2, i32 %3, double* %4, double* %9, double* %17, double* %16, double* %10, double* %5)
%19 = icmp eq double* %9, null
br i1 %19, label %22, label %20
20: ; preds = %14
%21 = tail call i32 @free(double* %9)
br label %22
22: ; preds = %11, %20, %14
%23 = phi i32 [ %13, %11 ], [ %18, %20 ], [ %18, %14 ]
%24 = phi double* [ %9, %11 ], [ %10, %20 ], [ %10, %14 ]
%25 = phi double* [ %10, %11 ], [ %16, %20 ], [ %16, %14 ]
%26 = phi double* [ %12, %11 ], [ %17, %20 ], [ %17, %14 ]
%27 = tail call i32 @free(double* %26)
%28 = tail call i32 @free(double* %25)
%29 = tail call i32 @free(double* %24)
ret i32 %23
}