; LLVM IR from radualexandrub/Optimization-Algorithms-Find-local-minimum :: 2D_Derivative_Quadratic_CostFunctions (Gradienti_conjugati)

; Unoptimized IR (clang -disable-llvm-passes):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@.str.8 = external hidden unnamed_addr constant [11 x i8], align 1
@logfile2 = external dso_local global ptr, align 8

; Function Attrs: nounwind
declare double @pow(double noundef, double noundef) #0

; Function Attrs: nounwind uwtable
declare dso_local void @fprint(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind
declare i32 @fprintf(ptr noundef, ptr noundef, ...) #0

; Function Attrs: nounwind uwtable
declare dso_local void @printVec(i32 noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #2

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_negat(i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_add_coefVec(i32 noundef, double noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_copy(i32 noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @Mat_mult_Vec(i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_Mat_Vec(i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_norm(i32 noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_sprod(i32 noundef, ptr noundef, ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local void @Gradienti_conjugati(i32 noundef %0, ptr noundef %1, ptr noundef %2, double noundef %3, ptr noundef %4) #1 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca double, align 8
  %10 = alloca ptr, align 8
  %11 = alloca double, align 8
  %12 = alloca double, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i64, align 8
  %15 = alloca i64, align 8
  %16 = alloca i64, align 8
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca double, align 8
  %20 = alloca double, align 8
  %21 = alloca ptr, align 8
  %22 = alloca i64, align 8
  store i32 %0, ptr %6, align 4, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !9
  store ptr %2, ptr %8, align 8, !tbaa !9
  store double %3, ptr %9, align 8, !tbaa !11
  store ptr %4, ptr %10, align 8, !tbaa !9
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = zext i32 %23 to i64
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = zext i32 %25 to i64
  %27 = load i32, ptr %6, align 4, !tbaa !5
  %28 = zext i32 %27 to i64
  %29 = load i32, ptr %6, align 4, !tbaa !5
  %30 = zext i32 %29 to i64
  %31 = load ptr, ptr @logfile2, align 8, !tbaa !9
  %32 = call i32 (ptr, ptr, ...) @fprintf(ptr noundef %31, ptr noundef @.str.8) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %33 = load i32, ptr %6, align 4, !tbaa !5
  %34 = zext i32 %33 to i64
  %35 = call ptr @llvm.stacksave.p0()
  store ptr %35, ptr %13, align 8
  %36 = alloca double, i64 %34, align 16
  store i64 %34, ptr %14, align 8
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = zext i32 %37 to i64
  %39 = alloca double, i64 %38, align 16
  store i64 %38, ptr %15, align 8
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = zext i32 %40 to i64
  %42 = alloca double, i64 %41, align 16
  store i64 %41, ptr %16, align 8
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = load i32, ptr %6, align 4, !tbaa !5
  %45 = load ptr, ptr %7, align 8, !tbaa !9
  %46 = load ptr, ptr %10, align 8, !tbaa !9
  call void @Mat_mult_Vec(i32 noundef %43, i32 noundef %44, ptr noundef %45, ptr noundef %46, ptr noundef %42)
  %47 = load ptr, ptr %8, align 8, !tbaa !9
  call void @Vec_add_coefVec(i32 noundef 2, double noundef -1.000000e+00, ptr noundef %42, ptr noundef %47, ptr noundef %36)
  %48 = load i32, ptr %6, align 4, !tbaa !5
  call void @Vec_negat(i32 noundef %48, ptr noundef %36)
  %49 = load i32, ptr %6, align 4, !tbaa !5
  call void @Vec_copy(i32 noundef %49, ptr noundef %36, ptr noundef %39)
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  store i32 0, ptr %17, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %105, %5
  %51 = load i32, ptr %17, align 4, !tbaa !5
  %52 = icmp slt i32 %51, 8
  br i1 %52, label %54, label %53

53:                                               ; preds = %50
  store i32 2, ptr %18, align 4
  br label %108

54:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 8, ptr %19) #4
  %55 = load i32, ptr %6, align 4, !tbaa !5
  %56 = call double @Vec_sprod(i32 noundef %55, ptr noundef %36, ptr noundef %39)
  store double %56, ptr %19, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %20) #4
  %57 = load i32, ptr %6, align 4, !tbaa !5
  %58 = load i32, ptr %6, align 4, !tbaa !5
  %59 = load ptr, ptr %7, align 8, !tbaa !9
  %60 = call double @Vec_Mat_Vec(i32 noundef %57, i32 noundef %58, ptr noundef %39, ptr noundef %59, ptr noundef %39)
  store double %60, ptr %20, align 8, !tbaa !11
  %61 = load double, ptr %19, align 8, !tbaa !11
  %62 = load double, ptr %20, align 8, !tbaa !11
  %63 = fdiv double %61, %62
  store double %63, ptr %11, align 8, !tbaa !11
  %64 = load i32, ptr %6, align 4, !tbaa !5
  %65 = load double, ptr %11, align 8, !tbaa !11
  %66 = load ptr, ptr %10, align 8, !tbaa !9
  %67 = load ptr, ptr %10, align 8, !tbaa !9
  call void @Vec_add_coefVec(i32 noundef %64, double noundef %65, ptr noundef %66, ptr noundef %36, ptr noundef %67)
  %68 = load i32, ptr %6, align 4, !tbaa !5
  %69 = zext i32 %68 to i64
  %70 = call ptr @llvm.stacksave.p0()
  store ptr %70, ptr %21, align 8
  %71 = alloca double, i64 %69, align 16
  store i64 %69, ptr %22, align 8
  %72 = load i32, ptr %6, align 4, !tbaa !5
  call void @Vec_copy(i32 noundef %72, ptr noundef %36, ptr noundef %71)
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = load i32, ptr %6, align 4, !tbaa !5
  %75 = load ptr, ptr %7, align 8, !tbaa !9
  %76 = load ptr, ptr %10, align 8, !tbaa !9
  call void @Mat_mult_Vec(i32 noundef %73, i32 noundef %74, ptr noundef %75, ptr noundef %76, ptr noundef %42)
  %77 = load i32, ptr %6, align 4, !tbaa !5
  %78 = load ptr, ptr %8, align 8, !tbaa !9
  call void @Vec_add_coefVec(i32 noundef %77, double noundef -1.000000e+00, ptr noundef %42, ptr noundef %78, ptr noundef %36)
  %79 = load i32, ptr %6, align 4, !tbaa !5
  call void @Vec_negat(i32 noundef %79, ptr noundef %36)
  %80 = load i32, ptr %6, align 4, !tbaa !5
  %81 = call double @Vec_norm(i32 noundef %80, ptr noundef %36)
  %82 = load double, ptr %9, align 8, !tbaa !11
  %83 = fcmp olt double %81, %82
  br i1 %83, label %84, label %85

84:                                               ; preds = %54
  store i32 2, ptr %18, align 4
  br label %101

85:                                               ; preds = %54
  %86 = load i32, ptr %6, align 4, !tbaa !5
  %87 = load ptr, ptr %10, align 8, !tbaa !9
  call void @printVec(i32 noundef %86, ptr noundef %87)
  %88 = load ptr, ptr @logfile2, align 8, !tbaa !9
  %89 = load ptr, ptr %10, align 8, !tbaa !9
  call void @fprint(ptr noundef %88, ptr noundef %89)
  %90 = load i32, ptr %6, align 4, !tbaa !5
  %91 = call double @Vec_norm(i32 noundef %90, ptr noundef %36)
  %92 = call double @pow(double noundef %91, double noundef 2.000000e+00) #4
  store double %92, ptr %19, align 8, !tbaa !11
  %93 = load i32, ptr %6, align 4, !tbaa !5
  %94 = call double @Vec_norm(i32 noundef %93, ptr noundef %71)
  %95 = call double @pow(double noundef %94, double noundef 2.000000e+00) #4
  store double %95, ptr %20, align 8, !tbaa !11
  %96 = load double, ptr %19, align 8, !tbaa !11
  %97 = load double, ptr %20, align 8, !tbaa !11
  %98 = fdiv double %96, %97
  store double %98, ptr %12, align 8, !tbaa !11
  %99 = load i32, ptr %6, align 4, !tbaa !5
  %100 = load double, ptr %12, align 8, !tbaa !11
  call void @Vec_add_coefVec(i32 noundef %99, double noundef %100, ptr noundef %36, ptr noundef %39, ptr noundef %39)
  store i32 0, ptr %18, align 4
  br label %101

101:                                              ; preds = %85, %84
  %102 = load ptr, ptr %21, align 8
  call void @llvm.stackrestore.p0(ptr %102)
  call void @llvm.lifetime.end.p0(i64 8, ptr %20) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %19) #4
  %103 = load i32, ptr %18, align 4
  switch i32 %103, label %108 [
    i32 0, label %104
  ]

104:                                              ; preds = %101
  br label %105

105:                                              ; preds = %104
  %106 = load i32, ptr %17, align 4, !tbaa !5
  %107 = add nsw i32 %106, 1
  store i32 %107, ptr %17, align 4, !tbaa !5
  br label %50, !llvm.loop !13

108:                                              ; preds = %101, %53
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %109

109:                                              ; preds = %108
  %110 = load ptr, ptr %13, align 8
  call void @llvm.stackrestore.p0(ptr %110)
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  ret void
}

attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Optimized IR (opt -passes='default<O3>'):
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

@.str.8 = external hidden unnamed_addr constant [11 x i8], align 1
@logfile2 = external dso_local local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
declare dso_local void @fprint(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind
declare noundef i32 @fprintf(ptr nocapture noundef, ptr nocapture noundef readonly, ...) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @printVec(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_negat(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_add_coefVec(i32 noundef, double noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @Vec_copy(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @Mat_mult_Vec(i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_Mat_Vec(i32 noundef, i32 noundef, ptr noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #2

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_norm(i32 noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local double @Vec_sprod(i32 noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local void @Gradienti_conjugati(i32 noundef %0, ptr noundef %1, ptr noundef %2, double noundef %3, ptr noundef %4) local_unnamed_addr #0 {
  %6 = load ptr, ptr @logfile2, align 8, !tbaa !5
  %7 = tail call i32 (ptr, ptr, ...) @fprintf(ptr noundef %6, ptr noundef nonnull @.str.8) #3
  %8 = zext i32 %0 to i64
  %9 = alloca double, i64 %8, align 16
  %10 = alloca double, i64 %8, align 16
  %11 = alloca double, i64 %8, align 16
  call void @Mat_mult_Vec(i32 noundef %0, i32 noundef %0, ptr noundef %1, ptr noundef %4, ptr noundef nonnull %11)
  call void @Vec_add_coefVec(i32 noundef 2, double noundef -1.000000e+00, ptr noundef nonnull %11, ptr noundef %2, ptr noundef nonnull %9)
  call void @Vec_negat(i32 noundef %0, ptr noundef nonnull %9)
  call void @Vec_copy(i32 noundef %0, ptr noundef nonnull %9, ptr noundef nonnull %10)
  br label %12

12:                                               ; preds = %5, %20
  %.050 = phi i32 [ 0, %5 ], [ %25, %20 ]
  %13 = call double @Vec_sprod(i32 noundef %0, ptr noundef nonnull %9, ptr noundef nonnull %10)
  %14 = call double @Vec_Mat_Vec(i32 noundef %0, i32 noundef %0, ptr noundef nonnull %10, ptr noundef %1, ptr noundef nonnull %10)
  %15 = fdiv double %13, %14
  call void @Vec_add_coefVec(i32 noundef %0, double noundef %15, ptr noundef %4, ptr noundef nonnull %9, ptr noundef %4)
  %16 = call ptr @llvm.stacksave.p0()
  %17 = alloca double, i64 %8, align 16
  call void @Vec_copy(i32 noundef %0, ptr noundef nonnull %9, ptr noundef nonnull %17)
  call void @Mat_mult_Vec(i32 noundef %0, i32 noundef %0, ptr noundef %1, ptr noundef %4, ptr noundef nonnull %11)
  call void @Vec_add_coefVec(i32 noundef %0, double noundef -1.000000e+00, ptr noundef nonnull %11, ptr noundef %2, ptr noundef nonnull %9)
  call void @Vec_negat(i32 noundef %0, ptr noundef nonnull %9)
  %18 = call double @Vec_norm(i32 noundef %0, ptr noundef nonnull %9)
  %19 = fcmp uge double %18, %3
  br i1 %19, label %20, label %.critedge

20:                                               ; preds = %12
  call void @printVec(i32 noundef %0, ptr noundef %4)
  %21 = load ptr, ptr @logfile2, align 8, !tbaa !5
  call void @fprint(ptr noundef %21, ptr noundef %4)
  %22 = call double @Vec_norm(i32 noundef %0, ptr noundef nonnull %9)
  %square = fmul double %22, %22
  %23 = call double @Vec_norm(i32 noundef %0, ptr noundef nonnull %17)
  %square49 = fmul double %23, %23
  %24 = fdiv double %square, %square49
  call void @Vec_add_coefVec(i32 noundef %0, double noundef %24, ptr noundef nonnull %9, ptr noundef nonnull %10, ptr noundef nonnull %10)
  call void @llvm.stackrestore.p0(ptr %16)
  %25 = add nuw nsw i32 %.050, 1
  %exitcond.not = icmp eq i32 %25, 8
  br i1 %exitcond.not, label %.loopexit, label %12, !llvm.loop !9

.critedge:                                        ; preds = %12
  call void @llvm.stackrestore.p0(ptr %16)
  br label %.loopexit

.loopexit:                                        ; preds = %20, %.critedge
  ret void
}

attributes #0 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nocallback nofree nosync nounwind willreturn }
attributes #3 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"clang version 19.1.0 (git@github.com:llvm/llvm-project.git a4bf6cd7cfb1a1421ba92bca9d017b49936c55e4)"}
!5 = !{!6, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass