; LLVM IR from VeloxChem/VeloxChem :: src/math/DenseMatrix.ll (ZN12CDenseMatrix18symmetrizeAndScaleEd)

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

%class.CDenseMatrix = type { i32, i32, %"class.std::vector" }
%"class.std::vector" = type { %"struct.std::_Vector_base" }
%"struct.std::_Vector_base" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl" = type { %"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" }
%"struct.std::_Vector_base<double, std::allocator<double>>::_Vector_impl_data" = 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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNSt6vectorIdSaIdEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN12CDenseMatrix18symmetrizeAndScaleEd(ptr noundef nonnull align 8 dereferenceable(32) %0, double noundef %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca double, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca double, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store double %1, ptr %4, align 8, !tbaa !9
  %12 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %13 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 2
  %14 = call noundef ptr @_ZNSt6vectorIdSaIdEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %13) #2
  store ptr %14, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 1
  %18 = load i32, ptr %17, align 4, !tbaa !18
  %19 = icmp eq i32 %16, %18
  br i1 %19, label %20, label %79

20:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !19
  br label %21

21:                                               ; preds = %75, %20
  %22 = load i32, ptr %6, align 4, !tbaa !19
  %23 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 0
  %24 = load i32, ptr %23, align 8, !tbaa !11
  %25 = icmp slt i32 %22, %24
  br i1 %25, label %27, label %26

26:                                               ; preds = %21
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %78

27:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %28 = load i32, ptr %6, align 4, !tbaa !19
  store i32 %28, ptr %8, align 4, !tbaa !19
  br label %29

29:                                               ; preds = %71, %27
  %30 = load i32, ptr %8, align 4, !tbaa !19
  %31 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 0
  %32 = load i32, ptr %31, align 8, !tbaa !11
  %33 = icmp slt i32 %30, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %29
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %74

35:                                               ; preds = %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %36 = load i32, ptr %6, align 4, !tbaa !19
  %37 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 1
  %38 = load i32, ptr %37, align 4, !tbaa !18
  %39 = mul nsw i32 %36, %38
  %40 = load i32, ptr %8, align 4, !tbaa !19
  %41 = add nsw i32 %39, %40
  store i32 %41, ptr %9, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  %42 = load i32, ptr %8, align 4, !tbaa !19
  %43 = getelementptr inbounds %class.CDenseMatrix, ptr %12, i32 0, i32 1
  %44 = load i32, ptr %43, align 4, !tbaa !18
  %45 = mul nsw i32 %42, %44
  %46 = load i32, ptr %6, align 4, !tbaa !19
  %47 = add nsw i32 %45, %46
  store i32 %47, ptr %10, align 4, !tbaa !19
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  %48 = load double, ptr %4, align 8, !tbaa !9
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = load i32, ptr %9, align 4, !tbaa !19
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds double, ptr %49, i64 %51
  %53 = load double, ptr %52, align 8, !tbaa !9
  %54 = load ptr, ptr %5, align 8, !tbaa !5
  %55 = load i32, ptr %10, align 4, !tbaa !19
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds double, ptr %54, i64 %56
  %58 = load double, ptr %57, align 8, !tbaa !9
  %59 = fadd double %53, %58
  %60 = fmul double %48, %59
  store double %60, ptr %11, align 8, !tbaa !9
  %61 = load double, ptr %11, align 8, !tbaa !9
  %62 = load ptr, ptr %5, align 8, !tbaa !5
  %63 = load i32, ptr %9, align 4, !tbaa !19
  %64 = sext i32 %63 to i64
  %65 = getelementptr inbounds double, ptr %62, i64 %64
  store double %61, ptr %65, align 8, !tbaa !9
  %66 = load double, ptr %11, align 8, !tbaa !9
  %67 = load ptr, ptr %5, align 8, !tbaa !5
  %68 = load i32, ptr %10, align 4, !tbaa !19
  %69 = sext i32 %68 to i64
  %70 = getelementptr inbounds double, ptr %67, i64 %69
  store double %66, ptr %70, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %71

71:                                               ; preds = %35
  %72 = load i32, ptr %8, align 4, !tbaa !19
  %73 = add nsw i32 %72, 1
  store i32 %73, ptr %8, align 4, !tbaa !19
  br label %29, !llvm.loop !20

74:                                               ; preds = %34
  br label %75

75:                                               ; preds = %74
  %76 = load i32, ptr %6, align 4, !tbaa !19
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %6, align 4, !tbaa !19
  br label %21, !llvm.loop !22

78:                                               ; preds = %26
  br label %79

79:                                               ; preds = %78, %2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret void
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress 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 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !13, i64 0}
!12 = !{!"_ZTS12CDenseMatrix", !13, i64 0, !13, i64 4, !14, i64 8}
!13 = !{!"int", !7, i64 0}
!14 = !{!"_ZTSSt6vectorIdSaIdEE", !15, i64 0}
!15 = !{!"_ZTSSt12_Vector_baseIdSaIdEE", !16, i64 0}
!16 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE12_Vector_implE", !17, i64 0}
!17 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE17_Vector_impl_dataE", !6, i64 0, !6, i64 8, !6, i64 16}
!18 = !{!12, !13, i64 4}
!19 = !{!13, !13, i64 0}
!20 = distinct !{!20, !21}
!21 = !{!"llvm.loop.mustprogress"}
!22 = distinct !{!22, !21}


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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZNSt6vectorIdSaIdEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_ZN12CDenseMatrix18symmetrizeAndScaleEd(ptr noundef nonnull align 8 dereferenceable(32) %0, double noundef %1) local_unnamed_addr #0 align 2 {
  %3 = getelementptr inbounds i8, ptr %0, i64 8
  %4 = tail call noundef ptr @_ZNSt6vectorIdSaIdEE4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %3) #1
  %5 = load i32, ptr %0, align 8, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %0, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !15
  %8 = icmp eq i32 %5, %7
  %9 = icmp sgt i32 %5, 0
  %or.cond = and i1 %8, %9
  br i1 %or.cond, label %.preheader.preheader, label %.loopexit

.preheader.preheader:                             ; preds = %2
  %10 = zext nneg i32 %5 to i64
  %wide.trip.count28 = zext nneg i32 %5 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %12
  %indvars.iv = phi i64 [ 0, %.preheader.preheader ], [ %indvars.iv.next, %12 ]
  %11 = mul nuw nsw i64 %indvars.iv, %10
  %invariant.gep = getelementptr inbounds double, ptr %4, i64 %11
  %invariant.gep30 = getelementptr inbounds double, ptr %4, i64 %indvars.iv
  br label %13

12:                                               ; preds = %13
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond29.not = icmp eq i64 %indvars.iv.next, %wide.trip.count28
  br i1 %exitcond29.not, label %.loopexit, label %.preheader, !llvm.loop !16

13:                                               ; preds = %.preheader, %13
  %indvars.iv24 = phi i64 [ %indvars.iv, %.preheader ], [ %indvars.iv.next25, %13 ]
  %14 = mul nuw nsw i64 %indvars.iv24, %10
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv24
  %15 = load double, ptr %gep, align 8, !tbaa !18
  %gep31 = getelementptr inbounds double, ptr %invariant.gep30, i64 %14
  %16 = load double, ptr %gep31, align 8, !tbaa !18
  %17 = fadd double %15, %16
  %18 = fmul double %17, %1
  store double %18, ptr %gep, align 8, !tbaa !18
  store double %18, ptr %gep31, align 8, !tbaa !18
  %indvars.iv.next25 = add nuw nsw i64 %indvars.iv24, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next25, %wide.trip.count28
  br i1 %exitcond.not, label %12, label %13, !llvm.loop !20

.loopexit:                                        ; preds = %12, %2
  ret void
}

attributes #0 = { mustprogress 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 = { 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 = !{!"_ZTS12CDenseMatrix", !7, i64 0, !7, i64 4, !10, i64 8}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"_ZTSSt6vectorIdSaIdEE", !11, i64 0}
!11 = !{!"_ZTSSt12_Vector_baseIdSaIdEE", !12, i64 0}
!12 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE12_Vector_implE", !13, i64 0}
!13 = !{!"_ZTSNSt12_Vector_baseIdSaIdEE17_Vector_impl_dataE", !14, i64 0, !14, i64 8, !14, i64 16}
!14 = !{!"any pointer", !8, i64 0}
!15 = !{!6, !7, i64 4}
!16 = distinct !{!16, !17}
!17 = !{!"llvm.loop.mustprogress"}
!18 = !{!19, !19, i64 0}
!19 = !{!"double", !8, i64 0}
!20 = distinct !{!20, !17}


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