#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 */
double *allocate_real_vector(size_t);
double bilinear_form(size_t, double *, double **, double *);
int /*<<< orphan*/ free(double *);
int /*<<< orphan*/ mat_vec_prod(size_t, double *, double **, double *);
int /*<<< orphan*/ vec_add(size_t, double *, double *);
int /*<<< orphan*/ vec_mul(size_t, double, double *);
double vec_prod(size_t, double *, double *);
int /*<<< orphan*/ vec_set(size_t, double *, double *);
int /*<<< orphan*/ vec_sub(size_t, double *, double *);
void solve_CG(size_t n, double *x, double **a, double *b) {
  double *tmp = allocate_real_vector(n);
  double *r = allocate_real_vector(n);
  double *p = allocate_real_vector(n);
  double *alpha_p = allocate_real_vector(n);
  mat_vec_prod(n, tmp, a, x);
  vec_add(n, r, b);
  vec_sub(n, r, tmp);
  vec_add(n, p, r);
  for (size_t iter = 0; iter < n; iter++) {
    double r_sqr = vec_prod(n, r, r);
    double alpha = r_sqr / bilinear_form(n, p, a, p);
    vec_set(n, alpha_p, p);
    vec_mul(n, alpha, alpha_p);
    vec_add(n, x, alpha_p);
    mat_vec_prod(n, tmp, a, alpha_p);
    vec_sub(n, r, tmp);
    if (vec_prod(n, r, r) < 1.0e-14)
      break;
    double beta = vec_prod(n, r, r) / r_sqr;
    vec_set(n, tmp, p);
    vec_set(n, p, r);
    vec_mul(n, beta, tmp);
    vec_add(n, p, tmp);
  }
  free(alpha_p);
  free(p);
  free(r);
  free(tmp);
}


x86 assembly (clang, -O0):
<solve_CG>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1060 <mat_vec_prod@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1070 <vec_add@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10b0 <vec_sub@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <vec_add@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1376 <solve_CG+0x1e6>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <vec_prod@plt>
movsd  %xmm0,-0x50(%rbp)
movsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x68(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x38(%rbp),%rcx
call   1050 <bilinear_form@plt>
movaps %xmm0,%xmm1
movsd  -0x68(%rbp),%xmm0
divsd  %xmm1,%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   10a0 <vec_set@plt>
mov    -0x8(%rbp),%rdi
movsd  -0x58(%rbp),%xmm0
mov    -0x40(%rbp),%rsi
call   1090 <vec_mul@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1070 <vec_add@plt>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1060 <mat_vec_prod@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   10b0 <vec_sub@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <vec_prod@plt>
movaps %xmm0,%xmm1
movsd  0xd0a(%rip),%xmm0
ucomisd %xmm1,%xmm0
jbe    1305 <solve_CG+0x175>
jmp    1376 <solve_CG+0x1e6>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <vec_prod@plt>
divsd  -0x50(%rbp),%xmm0
movsd  %xmm0,-0x60(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   10a0 <vec_set@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <vec_set@plt>
mov    -0x8(%rbp),%rdi
movsd  -0x60(%rbp),%xmm0
mov    -0x28(%rbp),%rsi
call   1090 <vec_mul@plt>
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <vec_add@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    122c <solve_CG+0x9c>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solve_CG>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x8(%rsp)
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <allocate_real_vector@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   1040 <allocate_real_vector@plt>
mov    %rax,0x10(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r14,0x28(%rsp)
mov    %r14,%rdx
mov    %r15,0x20(%rsp)
mov    %r15,%rcx
call   1060 <mat_vec_prod@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    0x8(%rsp),%rdx
call   1070 <vec_add@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r13,%rdx
call   10b0 <vec_sub@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   1070 <vec_add@plt>
test   %rbx,%rbx
je     1351 <solve_CG+0x1c1>
mov    %rbx,0x18(%rsp)
mov    0x28(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rbp,%rdx
call   1080 <vec_prod@plt>
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1050 <bilinear_form@plt>
movsd  0x8(%rsp),%xmm1
divsd  %xmm0,%xmm1
movsd  %xmm1,0x30(%rsp)
mov    %rbx,%rdi
mov    %r13,%r14
mov    %r12,%r13
mov    0x10(%rsp),%r12
mov    %r12,%rsi
mov    %r13,%rdx
call   10a0 <vec_set@plt>
mov    %rbx,%rdi
movsd  0x30(%rsp),%xmm0
mov    %r12,%rsi
call   1090 <vec_mul@plt>
mov    %rbx,%rdi
mov    0x20(%rsp),%rsi
mov    %r12,%rdx
call   1070 <vec_add@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
mov    %r13,%r12
mov    %r14,%r13
call   1060 <mat_vec_prod@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r14,%rdx
call   10b0 <vec_sub@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rbp,%rdx
call   1080 <vec_prod@plt>
movsd  0xd16(%rip),%xmm1
ucomisd %xmm0,%xmm1
ja     1351 <solve_CG+0x1c1>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %rbp,%rdx
call   1080 <vec_prod@plt>
divsd  0x8(%rsp),%xmm0
movsd  %xmm0,0x8(%rsp)
mov    %rbx,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   10a0 <vec_set@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %rbp,%rdx
call   10a0 <vec_set@plt>
mov    %rbx,%rdi
movsd  0x8(%rsp),%xmm0
mov    %r13,%rsi
call   1090 <vec_mul@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <vec_add@plt>
addq   $0xffffffffffffffff,0x18(%rsp)
jne    1240 <solve_CG+0xb0>
mov    0x10(%rsp),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %r13,%rdi
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @solve_CG(i64 %0, double* %1, double** %2, double* %3) {
%5 = alloca i64
%6 = alloca double*
%7 = alloca double**
%8 = alloca double*
%9 = alloca double*
%10 = alloca double*
%11 = alloca double*
%12 = alloca double*
%13 = alloca i64
%14 = alloca double
%15 = alloca double
%16 = alloca double
store i64 %0, i64* %5
store double* %1, double** %6
store double** %2, double*** %7
store double* %3, double** %8
%17 = load i64, i64* %5
%18 = call double* @allocate_real_vector(i64 %17)
store double* %18, double** %9
%19 = load i64, i64* %5
%20 = call double* @allocate_real_vector(i64 %19)
store double* %20, double** %10
%21 = load i64, i64* %5
%22 = call double* @allocate_real_vector(i64 %21)
store double* %22, double** %11
%23 = load i64, i64* %5
%24 = call double* @allocate_real_vector(i64 %23)
store double* %24, double** %12
%25 = load i64, i64* %5
%26 = load double*, double** %9
%27 = load double**, double*** %7
%28 = load double*, double** %6
%29 = call i32 @mat_vec_prod(i64 %25, double* %26, double** %27, double* %28)
%30 = load i64, i64* %5
%31 = load double*, double** %10
%32 = load double*, double** %8
%33 = call i32 @vec_add(i64 %30, double* %31, double* %32)
%34 = load i64, i64* %5
%35 = load double*, double** %10
%36 = load double*, double** %9
%37 = call i32 @vec_sub(i64 %34, double* %35, double* %36)
%38 = load i64, i64* %5
%39 = load double*, double** %11
%40 = load double*, double** %10
%41 = call i32 @vec_add(i64 %38, double* %39, double* %40)
store i64 0, i64* %13
br label %42
42: ; preds = %108, %4
%43 = load i64, i64* %13
%44 = load i64, i64* %5
%45 = icmp ult i64 %43, %44
br i1 %45, label %46, label %111
46: ; preds = %42
%47 = load i64, i64* %5
%48 = load double*, double** %10
%49 = load double*, double** %10
%50 = call double @vec_prod(i64 %47, double* %48, double* %49)
store double %50, double* %14
%51 = load double, double* %14
%52 = load i64, i64* %5
%53 = load double*, double** %11
%54 = load double**, double*** %7
%55 = load double*, double** %11
%56 = call double @bilinear_form(i64 %52, double* %53, double** %54, double* %55)
%57 = fdiv double %51, %56
store double %57, double* %15
%58 = load i64, i64* %5
%59 = load double*, double** %12
%60 = load double*, double** %11
%61 = call i32 @vec_set(i64 %58, double* %59, double* %60)
%62 = load i64, i64* %5
%63 = load double, double* %15
%64 = load double*, double** %12
%65 = call i32 @vec_mul(i64 %62, double %63, double* %64)
%66 = load i64, i64* %5
%67 = load double*, double** %6
%68 = load double*, double** %12
%69 = call i32 @vec_add(i64 %66, double* %67, double* %68)
%70 = load i64, i64* %5
%71 = load double*, double** %9
%72 = load double**, double*** %7
%73 = load double*, double** %12
%74 = call i32 @mat_vec_prod(i64 %70, double* %71, double** %72, double* %73)
%75 = load i64, i64* %5
%76 = load double*, double** %10
%77 = load double*, double** %9
%78 = call i32 @vec_sub(i64 %75, double* %76, double* %77)
%79 = load i64, i64* %5
%80 = load double*, double** %10
%81 = load double*, double** %10
%82 = call double @vec_prod(i64 %79, double* %80, double* %81)
%83 = fcmp olt double %82, 0x3D06849B86A12B9B
br i1 %83, label %84, label %85
84: ; preds = %46
br label %111
85: ; preds = %46
%86 = load i64, i64* %5
%87 = load double*, double** %10
%88 = load double*, double** %10
%89 = call double @vec_prod(i64 %86, double* %87, double* %88)
%90 = load double, double* %14
%91 = fdiv double %89, %90
store double %91, double* %16
%92 = load i64, i64* %5
%93 = load double*, double** %9
%94 = load double*, double** %11
%95 = call i32 @vec_set(i64 %92, double* %93, double* %94)
%96 = load i64, i64* %5
%97 = load double*, double** %11
%98 = load double*, double** %10
%99 = call i32 @vec_set(i64 %96, double* %97, double* %98)
%100 = load i64, i64* %5
%101 = load double, double* %16
%102 = load double*, double** %9
%103 = call i32 @vec_mul(i64 %100, double %101, double* %102)
%104 = load i64, i64* %5
%105 = load double*, double** %11
%106 = load double*, double** %9
%107 = call i32 @vec_add(i64 %104, double* %105, double* %106)
br label %108
108: ; preds = %85
%109 = load i64, i64* %13
%110 = add i64 %109, 1
store i64 %110, i64* %13
br label %42
111: ; preds = %84, %42
%112 = load double*, double** %12
%113 = call i32 @free(double* %112)
%114 = load double*, double** %11
%115 = call i32 @free(double* %114)
%116 = load double*, double** %10
%117 = call i32 @free(double* %116)
%118 = load double*, double** %9
%119 = call i32 @free(double* %118)
ret void
}

LLVM IR (-O2):
define void @solve_CG(i64 %0, double* %1, double** %2, double* %3) {
%5 = tail call double* @allocate_real_vector(i64 %0)
%6 = tail call double* @allocate_real_vector(i64 %0)
%7 = tail call double* @allocate_real_vector(i64 %0)
%8 = tail call double* @allocate_real_vector(i64 %0)
%9 = tail call i32 @mat_vec_prod(i64 %0, double* %5, double** %2, double* %1)
%10 = tail call i32 @vec_add(i64 %0, double* %6, double* %3)
%11 = tail call i32 @vec_sub(i64 %0, double* %6, double* %5)
%12 = tail call i32 @vec_add(i64 %0, double* %7, double* %6)
%13 = icmp eq i64 %0, 0
br i1 %13, label %35, label %14
14: ; preds = %4, %26
%15 = phi i64 [ %33, %26 ], [ 0, %4 ]
%16 = tail call double @vec_prod(i64 %0, double* %6, double* %6)
%17 = tail call double @bilinear_form(i64 %0, double* %7, double** %2, double* %7)
%18 = fdiv double %16, %17
%19 = tail call i32 @vec_set(i64 %0, double* %8, double* %7)
%20 = tail call i32 @vec_mul(i64 %0, double %18, double* %8)
%21 = tail call i32 @vec_add(i64 %0, double* %1, double* %8)
%22 = tail call i32 @mat_vec_prod(i64 %0, double* %5, double** %2, double* %8)
%23 = tail call i32 @vec_sub(i64 %0, double* %6, double* %5)
%24 = tail call double @vec_prod(i64 %0, double* %6, double* %6)
%25 = fcmp olt double %24, 0x3D06849B86A12B9B
br i1 %25, label %35, label %26
26: ; preds = %14
%27 = tail call double @vec_prod(i64 %0, double* %6, double* %6)
%28 = fdiv double %27, %16
%29 = tail call i32 @vec_set(i64 %0, double* %5, double* %7)
%30 = tail call i32 @vec_set(i64 %0, double* %7, double* %6)
%31 = tail call i32 @vec_mul(i64 %0, double %28, double* %5)
%32 = tail call i32 @vec_add(i64 %0, double* %7, double* %5)
%33 = add nuw i64 %15, 1
%34 = icmp eq i64 %33, %0
br i1 %34, label %35, label %14
35: ; preds = %26, %14, %4
%36 = tail call i32 @free(double* %8)
%37 = tail call i32 @free(double* %7)
%38 = tail call i32 @free(double* %6)
%39 = tail call i32 @free(double* %5)
ret void
}