; LLVM IR from stan-dev/math :: lib/sundials_6.1.1/src/sundials (sundials_iterative)

; 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"

%struct._SUNQRData = type { ptr, ptr, ptr }

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

declare double @N_VDotProd(ptr noundef, ptr noundef) #1

; Function Attrs: nounwind
declare double @sqrt(double noundef) #2

declare void @N_VLinearSum(double noundef, ptr noundef, double noundef, ptr noundef, ptr noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local i32 @SUNQRAdd_MGS(ptr noundef %0, ptr noundef %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr noundef %5) #3 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store ptr %2, ptr %9, align 8, !tbaa !5
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  store ptr %5, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %15 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %15, ptr %14, align 8, !tbaa !5
  %16 = load ptr, ptr %9, align 8, !tbaa !5
  %17 = load ptr, ptr %14, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct._SUNQRData, ptr %17, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  call void @N_VScale(double noundef 1.000000e+00, ptr noundef %16, ptr noundef %19)
  store i64 0, ptr %13, align 8, !tbaa !13
  br label %20

20:                                               ; preds = %62, %6
  %21 = load i64, ptr %13, align 8, !tbaa !13
  %22 = load i32, ptr %10, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = icmp slt i64 %21, %23
  br i1 %24, label %25, label %65

25:                                               ; preds = %20
  %26 = load ptr, ptr %7, align 8, !tbaa !5
  %27 = load i64, ptr %13, align 8, !tbaa !13
  %28 = getelementptr inbounds ptr, ptr %26, i64 %27
  %29 = load ptr, ptr %28, align 8, !tbaa !5
  %30 = load ptr, ptr %14, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct._SUNQRData, ptr %30, i32 0, i32 0
  %32 = load ptr, ptr %31, align 8, !tbaa !11
  %33 = call double @N_VDotProd(ptr noundef %29, ptr noundef %32)
  %34 = load ptr, ptr %8, align 8, !tbaa !5
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = load i32, ptr %11, align 4, !tbaa !9
  %37 = mul nsw i32 %35, %36
  %38 = sext i32 %37 to i64
  %39 = load i64, ptr %13, align 8, !tbaa !13
  %40 = add nsw i64 %38, %39
  %41 = getelementptr inbounds double, ptr %34, i64 %40
  store double %33, ptr %41, align 8, !tbaa !15
  %42 = load ptr, ptr %14, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct._SUNQRData, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !11
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = load i32, ptr %11, align 4, !tbaa !9
  %48 = mul nsw i32 %46, %47
  %49 = sext i32 %48 to i64
  %50 = load i64, ptr %13, align 8, !tbaa !13
  %51 = add nsw i64 %49, %50
  %52 = getelementptr inbounds double, ptr %45, i64 %51
  %53 = load double, ptr %52, align 8, !tbaa !15
  %54 = fneg double %53
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = load i64, ptr %13, align 8, !tbaa !13
  %57 = getelementptr inbounds ptr, ptr %55, i64 %56
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = load ptr, ptr %14, align 8, !tbaa !5
  %60 = getelementptr inbounds %struct._SUNQRData, ptr %59, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !11
  call void @N_VLinearSum(double noundef 1.000000e+00, ptr noundef %44, double noundef %54, ptr noundef %58, ptr noundef %61)
  br label %62

62:                                               ; preds = %25
  %63 = load i64, ptr %13, align 8, !tbaa !13
  %64 = add nsw i64 %63, 1
  store i64 %64, ptr %13, align 8, !tbaa !13
  br label %20, !llvm.loop !17

65:                                               ; preds = %20
  %66 = load ptr, ptr %14, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct._SUNQRData, ptr %66, i32 0, i32 0
  %68 = load ptr, ptr %67, align 8, !tbaa !11
  %69 = load ptr, ptr %14, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct._SUNQRData, ptr %69, i32 0, i32 0
  %71 = load ptr, ptr %70, align 8, !tbaa !11
  %72 = call double @N_VDotProd(ptr noundef %68, ptr noundef %71)
  %73 = fcmp ole double %72, 0.000000e+00
  br i1 %73, label %74, label %75

74:                                               ; preds = %65
  br label %84

75:                                               ; preds = %65
  %76 = load ptr, ptr %14, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct._SUNQRData, ptr %76, i32 0, i32 0
  %78 = load ptr, ptr %77, align 8, !tbaa !11
  %79 = load ptr, ptr %14, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct._SUNQRData, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !11
  %82 = call double @N_VDotProd(ptr noundef %78, ptr noundef %81)
  %83 = call double @sqrt(double noundef %82) #4
  br label %84

84:                                               ; preds = %75, %74
  %85 = phi double [ 0.000000e+00, %74 ], [ %83, %75 ]
  %86 = load ptr, ptr %8, align 8, !tbaa !5
  %87 = load i32, ptr %10, align 4, !tbaa !9
  %88 = load i32, ptr %11, align 4, !tbaa !9
  %89 = mul nsw i32 %87, %88
  %90 = load i32, ptr %10, align 4, !tbaa !9
  %91 = add nsw i32 %89, %90
  %92 = sext i32 %91 to i64
  %93 = getelementptr inbounds double, ptr %86, i64 %92
  store double %85, ptr %93, align 8, !tbaa !15
  %94 = load ptr, ptr %8, align 8, !tbaa !5
  %95 = load i32, ptr %10, align 4, !tbaa !9
  %96 = load i32, ptr %11, align 4, !tbaa !9
  %97 = mul nsw i32 %95, %96
  %98 = load i32, ptr %10, align 4, !tbaa !9
  %99 = add nsw i32 %97, %98
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds double, ptr %94, i64 %100
  %102 = load double, ptr %101, align 8, !tbaa !15
  %103 = fdiv double 1.000000e+00, %102
  %104 = load ptr, ptr %14, align 8, !tbaa !5
  %105 = getelementptr inbounds %struct._SUNQRData, ptr %104, i32 0, i32 0
  %106 = load ptr, ptr %105, align 8, !tbaa !11
  %107 = load ptr, ptr %7, align 8, !tbaa !5
  %108 = load i32, ptr %10, align 4, !tbaa !9
  %109 = sext i32 %108 to i64
  %110 = getelementptr inbounds ptr, ptr %107, i64 %109
  %111 = load ptr, ptr %110, align 8, !tbaa !5
  call void @N_VScale(double noundef %103, ptr noundef %106, ptr noundef %111)
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  ret i32 0
}

declare void @N_VScale(double noundef, ptr noundef, ptr noundef) #1

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { "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 = { 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 #3 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_SUNQRData", !6, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!14, !14, i64 0}
!14 = !{!"long", !7, i64 0}
!15 = !{!16, !16, i64 0}
!16 = !{!"double", !7, i64 0}
!17 = distinct !{!17, !18}
!18 = !{!"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"

declare double @N_VDotProd(ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @sqrt(double noundef) local_unnamed_addr #1

declare void @N_VLinearSum(double noundef, ptr noundef, double noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @SUNQRAdd_MGS(ptr nocapture noundef readonly %0, ptr nocapture noundef writeonly %1, ptr noundef %2, i32 noundef %3, i32 noundef %4, ptr nocapture noundef readonly %5) local_unnamed_addr #2 {
  %7 = load ptr, ptr %5, align 8, !tbaa !5
  tail call void @N_VScale(double noundef 1.000000e+00, ptr noundef %2, ptr noundef %7) #3
  %8 = sext i32 %3 to i64
  %9 = icmp sgt i32 %3, 0
  br i1 %9, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6
  %10 = mul nsw i32 %4, %3
  %11 = sext i32 %10 to i64
  %invariant.gep = getelementptr double, ptr %1, i64 %11
  br label %12

12:                                               ; preds = %.lr.ph, %12
  %.036 = phi i64 [ 0, %.lr.ph ], [ %20, %12 ]
  %13 = getelementptr inbounds ptr, ptr %0, i64 %.036
  %14 = load ptr, ptr %13, align 8, !tbaa !10
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = tail call double @N_VDotProd(ptr noundef %14, ptr noundef %15) #3
  %gep = getelementptr double, ptr %invariant.gep, i64 %.036
  store double %16, ptr %gep, align 8, !tbaa !11
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = fneg double %16
  %19 = load ptr, ptr %13, align 8, !tbaa !10
  tail call void @N_VLinearSum(double noundef 1.000000e+00, ptr noundef %17, double noundef %18, ptr noundef %19, ptr noundef %17) #3
  %20 = add nuw nsw i64 %.036, 1
  %exitcond.not = icmp eq i64 %20, %8
  br i1 %exitcond.not, label %._crit_edge, label %12, !llvm.loop !13

._crit_edge:                                      ; preds = %12, %6
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = tail call double @N_VDotProd(ptr noundef %21, ptr noundef %21) #3
  %23 = fcmp ugt double %22, 0.000000e+00
  br i1 %23, label %24, label %28

24:                                               ; preds = %._crit_edge
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = tail call double @N_VDotProd(ptr noundef %25, ptr noundef %25) #3
  %27 = tail call double @sqrt(double noundef %26) #3
  br label %28

28:                                               ; preds = %._crit_edge, %24
  %29 = phi double [ %27, %24 ], [ 0.000000e+00, %._crit_edge ]
  %30 = add i32 %4, 1
  %31 = mul i32 %30, %3
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds double, ptr %1, i64 %32
  store double %29, ptr %33, align 8, !tbaa !11
  %34 = fdiv double 1.000000e+00, %29
  %35 = load ptr, ptr %5, align 8, !tbaa !5
  %36 = getelementptr inbounds ptr, ptr %0, i64 %8
  %37 = load ptr, ptr %36, align 8, !tbaa !10
  tail call void @N_VScale(double noundef %34, ptr noundef %35, ptr noundef %37) #3
  ret i32 0
}

declare void @N_VScale(double noundef, ptr noundef, ptr noundef) local_unnamed_addr #0

attributes #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 = { mustprogress nofree nounwind willreturn memory(write) "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 = { 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 #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, !7, i64 0}
!6 = !{!"_SUNQRData", !7, i64 0, !7, i64 8, !7, i64 16}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!7, !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"double", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass