; LLVM IR from codee-com/open-catalog :: Checks/PWR043 (solution)

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

; Function Attrs: nounwind uwtable
define dso_local void @matmul_improved(i32 noundef %0, ptr noundef %1, ptr noundef %2, ptr noundef %3) #0 {
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %5, align 4, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !9
  store ptr %2, ptr %7, align 8, !tbaa !9
  store ptr %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %15

15:                                               ; preds = %39, %4
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = icmp slt i32 %16, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %15
  store i32 2, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %42

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

21:                                               ; preds = %35, %20
  %22 = load i32, ptr %11, align 4, !tbaa !5
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 5, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %38

26:                                               ; preds = %21
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = mul nsw i32 %28, %29
  %31 = load i32, ptr %11, align 4, !tbaa !5
  %32 = add nsw i32 %30, %31
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds double, ptr %27, i64 %33
  store double 0.000000e+00, ptr %34, align 8, !tbaa !11
  br label %35

35:                                               ; preds = %26
  %36 = load i32, ptr %11, align 4, !tbaa !5
  %37 = add nsw i32 %36, 1
  store i32 %37, ptr %11, align 4, !tbaa !5
  br label %21, !llvm.loop !13

38:                                               ; preds = %25
  br label %39

39:                                               ; preds = %38
  %40 = load i32, ptr %9, align 4, !tbaa !5
  %41 = add nsw i32 %40, 1
  store i32 %41, ptr %9, align 4, !tbaa !5
  br label %15, !llvm.loop !15

42:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %43

43:                                               ; preds = %97, %42
  %44 = load i32, ptr %12, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = icmp slt i32 %44, %45
  br i1 %46, label %48, label %47

47:                                               ; preds = %43
  store i32 8, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %100

48:                                               ; preds = %43
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %93, %48
  %50 = load i32, ptr %13, align 4, !tbaa !5
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = icmp slt i32 %50, %51
  br i1 %52, label %54, label %53

53:                                               ; preds = %49
  store i32 11, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %96

54:                                               ; preds = %49
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %89, %54
  %56 = load i32, ptr %14, align 4, !tbaa !5
  %57 = load i32, ptr %5, align 4, !tbaa !5
  %58 = icmp slt i32 %56, %57
  br i1 %58, label %60, label %59

59:                                               ; preds = %55
  store i32 14, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  br label %92

60:                                               ; preds = %55
  %61 = load ptr, ptr %6, align 8, !tbaa !9
  %62 = load i32, ptr %12, align 4, !tbaa !5
  %63 = load i32, ptr %5, align 4, !tbaa !5
  %64 = mul nsw i32 %62, %63
  %65 = load i32, ptr %13, align 4, !tbaa !5
  %66 = add nsw i32 %64, %65
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds double, ptr %61, i64 %67
  %69 = load double, ptr %68, align 8, !tbaa !11
  %70 = load ptr, ptr %7, align 8, !tbaa !9
  %71 = load i32, ptr %13, align 4, !tbaa !5
  %72 = load i32, ptr %5, align 4, !tbaa !5
  %73 = mul nsw i32 %71, %72
  %74 = load i32, ptr %14, align 4, !tbaa !5
  %75 = add nsw i32 %73, %74
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds double, ptr %70, i64 %76
  %78 = load double, ptr %77, align 8, !tbaa !11
  %79 = load ptr, ptr %8, align 8, !tbaa !9
  %80 = load i32, ptr %12, align 4, !tbaa !5
  %81 = load i32, ptr %5, align 4, !tbaa !5
  %82 = mul nsw i32 %80, %81
  %83 = load i32, ptr %14, align 4, !tbaa !5
  %84 = add nsw i32 %82, %83
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds double, ptr %79, i64 %85
  %87 = load double, ptr %86, align 8, !tbaa !11
  %88 = call double @llvm.fmuladd.f64(double %69, double %78, double %87)
  store double %88, ptr %86, align 8, !tbaa !11
  br label %89

89:                                               ; preds = %60
  %90 = load i32, ptr %14, align 4, !tbaa !5
  %91 = add nsw i32 %90, 1
  store i32 %91, ptr %14, align 4, !tbaa !5
  br label %55, !llvm.loop !16

92:                                               ; preds = %59
  br label %93

93:                                               ; preds = %92
  %94 = load i32, ptr %13, align 4, !tbaa !5
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %13, align 4, !tbaa !5
  br label %49, !llvm.loop !17

96:                                               ; preds = %53
  br label %97

97:                                               ; preds = %96
  %98 = load i32, ptr %12, align 4, !tbaa !5
  %99 = add nsw i32 %98, 1
  store i32 %99, ptr %12, align 4, !tbaa !5
  br label %43, !llvm.loop !18

100:                                              ; preds = %47
  ret void
}

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

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

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"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"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}
!17 = distinct !{!17, !14}
!18 = distinct !{!18, !14}


; 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: nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @matmul_improved(i32 noundef %0, ptr nocapture noundef readonly %1, ptr nocapture noundef readonly %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = icmp sgt i32 %0, 0
  br i1 %5, label %.preheader35.us.preheader, label %._crit_edge

.preheader35.us.preheader:                        ; preds = %4
  %6 = zext nneg i32 %0 to i64
  %7 = mul nuw nsw i64 %6, %6
  %8 = shl i64 %7, 3
  tail call void @llvm.memset.p0.i64(ptr align 8 %3, i8 0, i64 %8, i1 false), !tbaa !5
  %9 = zext nneg i32 %0 to i64
  %10 = shl nuw nsw i64 %9, 3
  %11 = shl nuw nsw i64 %9, 3
  %12 = mul nuw nsw i64 %9, %9
  %13 = shl i64 %12, 3
  %scevgep65 = getelementptr i8, ptr %2, i64 %13
  %min.iters.check = icmp ult i32 %0, 4
  %n.vec = and i64 %9, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %9
  %xtraiter = and i64 %9, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %14 = add nsw i64 %9, -1
  br label %.preheader35.us

.preheader35.us:                                  ; preds = %.preheader35.us.preheader, %._crit_edge42.split.us.us
  %indvars.iv53 = phi i64 [ 0, %.preheader35.us.preheader ], [ %indvars.iv.next54, %._crit_edge42.split.us.us ]
  %15 = mul i64 %10, %indvars.iv53
  %scevgep = getelementptr i8, ptr %3, i64 %15
  %16 = add i64 %11, %15
  %scevgep62 = getelementptr i8, ptr %3, i64 %16
  %scevgep63 = getelementptr i8, ptr %1, i64 %15
  %scevgep64 = getelementptr i8, ptr %1, i64 %16
  %17 = mul nuw nsw i64 %indvars.iv53, %9
  %invariant.gep60 = getelementptr inbounds double, ptr %1, i64 %17
  %invariant.gep58 = getelementptr inbounds double, ptr %3, i64 %17
  %bound0 = icmp ult ptr %scevgep, %scevgep64
  %bound1 = icmp ult ptr %scevgep63, %scevgep62
  %found.conflict = and i1 %bound0, %bound1
  %bound066 = icmp ult ptr %scevgep, %scevgep65
  %bound167 = icmp ugt ptr %scevgep62, %2
  %found.conflict68 = and i1 %bound066, %bound167
  %conflict.rdx = or i1 %found.conflict, %found.conflict68
  br label %.preheader.us.us

.preheader.us.us:                                 ; preds = %._crit_edge.us43.us, %.preheader35.us
  %indvars.iv48 = phi i64 [ %indvars.iv.next49, %._crit_edge.us43.us ], [ 0, %.preheader35.us ]
  %gep61 = getelementptr inbounds double, ptr %invariant.gep60, i64 %indvars.iv48
  %18 = mul nuw nsw i64 %indvars.iv48, %9
  %invariant.gep = getelementptr inbounds double, ptr %2, i64 %18
  %brmerge = select i1 %min.iters.check, i1 true, i1 %conflict.rdx
  br i1 %brmerge, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.preheader.us.us
  %19 = load double, ptr %gep61, align 8, !tbaa !5, !alias.scope !9
  %broadcast.splatinsert72 = insertelement <2 x double> poison, double %19, i64 0
  %broadcast.splat73 = shufflevector <2 x double> %broadcast.splatinsert72, <2 x double> poison, <2 x i32> zeroinitializer
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %20 = getelementptr inbounds double, ptr %invariant.gep, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  %wide.load = load <2 x double>, ptr %20, align 8, !tbaa !5, !alias.scope !12
  %wide.load69 = load <2 x double>, ptr %21, align 8, !tbaa !5, !alias.scope !12
  %22 = getelementptr inbounds double, ptr %invariant.gep58, i64 %index
  %23 = getelementptr inbounds i8, ptr %22, i64 16
  %wide.load70 = load <2 x double>, ptr %22, align 8, !tbaa !5, !alias.scope !14, !noalias !16
  %wide.load71 = load <2 x double>, ptr %23, align 8, !tbaa !5, !alias.scope !14, !noalias !16
  %24 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat73, <2 x double> %wide.load, <2 x double> %wide.load70)
  %25 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat73, <2 x double> %wide.load69, <2 x double> %wide.load71)
  store <2 x double> %24, ptr %22, align 8, !tbaa !5, !alias.scope !14, !noalias !16
  store <2 x double> %25, ptr %23, align 8, !tbaa !5, !alias.scope !14, !noalias !16
  %index.next = add nuw i64 %index, 4
  %26 = icmp eq i64 %index.next, %n.vec
  br i1 %26, label %middle.block, label %vector.body, !llvm.loop !17

middle.block:                                     ; preds = %vector.body
  br i1 %cmp.n, label %._crit_edge.us43.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %.preheader.us.us, %middle.block
  %indvars.iv.ph = phi i64 [ 0, %.preheader.us.us ], [ %n.vec, %middle.block ]
  br i1 %lcmp.mod.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol

scalar.ph.prol:                                   ; preds = %scalar.ph.preheader
  %27 = load double, ptr %gep61, align 8, !tbaa !5
  %gep.prol = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.ph
  %28 = load double, ptr %gep.prol, align 8, !tbaa !5
  %gep59.prol = getelementptr inbounds double, ptr %invariant.gep58, i64 %indvars.iv.ph
  %29 = load double, ptr %gep59.prol, align 8, !tbaa !5
  %30 = tail call double @llvm.fmuladd.f64(double %27, double %28, double %29)
  store double %30, ptr %gep59.prol, align 8, !tbaa !5
  %indvars.iv.next.prol = or disjoint i64 %indvars.iv.ph, 1
  br label %scalar.ph.prol.loopexit

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv.unr = phi i64 [ %indvars.iv.ph, %scalar.ph.preheader ], [ %indvars.iv.next.prol, %scalar.ph.prol ]
  %31 = icmp eq i64 %indvars.iv.ph, %14
  br i1 %31, label %._crit_edge.us43.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %scalar.ph ], [ %indvars.iv.unr, %scalar.ph.prol.loopexit ]
  %32 = load double, ptr %gep61, align 8, !tbaa !5
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv
  %33 = load double, ptr %gep, align 8, !tbaa !5
  %gep59 = getelementptr inbounds double, ptr %invariant.gep58, i64 %indvars.iv
  %34 = load double, ptr %gep59, align 8, !tbaa !5
  %35 = tail call double @llvm.fmuladd.f64(double %32, double %33, double %34)
  store double %35, ptr %gep59, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %36 = load double, ptr %gep61, align 8, !tbaa !5
  %gep.1 = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.next
  %37 = load double, ptr %gep.1, align 8, !tbaa !5
  %gep59.1 = getelementptr inbounds double, ptr %invariant.gep58, i64 %indvars.iv.next
  %38 = load double, ptr %gep59.1, align 8, !tbaa !5
  %39 = tail call double @llvm.fmuladd.f64(double %36, double %37, double %38)
  store double %39, ptr %gep59.1, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %9
  br i1 %exitcond.not.1, label %._crit_edge.us43.us, label %scalar.ph, !llvm.loop !21

._crit_edge.us43.us:                              ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next49 = add nuw nsw i64 %indvars.iv48, 1
  %exitcond52.not = icmp eq i64 %indvars.iv.next49, %9
  br i1 %exitcond52.not, label %._crit_edge42.split.us.us, label %.preheader.us.us, !llvm.loop !22

._crit_edge42.split.us.us:                        ; preds = %._crit_edge.us43.us
  %indvars.iv.next54 = add nuw nsw i64 %indvars.iv53, 1
  %exitcond57.not = icmp eq i64 %indvars.iv.next54, %9
  br i1 %exitcond57.not, label %._crit_edge, label %.preheader35.us, !llvm.loop !23

._crit_edge:                                      ; preds = %._crit_edge42.split.us.us, %4
  ret void
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare <2 x double> @llvm.fmuladd.v2f64(<2 x double>, <2 x double>, <2 x double>) #3

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: readwrite) 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10}
!10 = distinct !{!10, !11}
!11 = distinct !{!11, !"LVerDomain"}
!12 = !{!13}
!13 = distinct !{!13, !11}
!14 = !{!15}
!15 = distinct !{!15, !11}
!16 = !{!10, !13}
!17 = distinct !{!17, !18, !19, !20}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.isvectorized", i32 1}
!20 = !{!"llvm.loop.unroll.runtime.disable"}
!21 = distinct !{!21, !18, !19}
!22 = distinct !{!22, !18}
!23 = distinct !{!23, !18}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass