; LLVM IR from codee-com/open-catalog :: Checks/PWR039 (example)

; 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(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
  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 %13

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

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

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

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

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

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

25:                                               ; preds = %59, %24
  %26 = load i32, ptr %12, align 4, !tbaa !5
  %27 = load i32, ptr %5, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

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

30:                                               ; preds = %25
  %31 = load ptr, ptr %6, align 8, !tbaa !9
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = load i32, ptr %5, align 4, !tbaa !5
  %34 = mul nsw i32 %32, %33
  %35 = load i32, ptr %12, align 4, !tbaa !5
  %36 = add nsw i32 %34, %35
  %37 = sext i32 %36 to i64
  %38 = getelementptr inbounds double, ptr %31, i64 %37
  %39 = load double, ptr %38, align 8, !tbaa !11
  %40 = load ptr, ptr %7, align 8, !tbaa !9
  %41 = load i32, ptr %12, align 4, !tbaa !5
  %42 = load i32, ptr %5, align 4, !tbaa !5
  %43 = mul nsw i32 %41, %42
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = add nsw i32 %43, %44
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds double, ptr %40, i64 %46
  %48 = load double, ptr %47, align 8, !tbaa !11
  %49 = load ptr, ptr %8, align 8, !tbaa !9
  %50 = load i32, ptr %9, align 4, !tbaa !5
  %51 = load i32, ptr %5, align 4, !tbaa !5
  %52 = mul nsw i32 %50, %51
  %53 = load i32, ptr %11, align 4, !tbaa !5
  %54 = add nsw i32 %52, %53
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds double, ptr %49, i64 %55
  %57 = load double, ptr %56, align 8, !tbaa !11
  %58 = call double @llvm.fmuladd.f64(double %39, double %48, double %57)
  store double %58, ptr %56, align 8, !tbaa !11
  br label %59

59:                                               ; preds = %30
  %60 = load i32, ptr %12, align 4, !tbaa !5
  %61 = add nsw i32 %60, 1
  store i32 %61, ptr %12, align 4, !tbaa !5
  br label %25, !llvm.loop !13

62:                                               ; preds = %29
  br label %63

63:                                               ; preds = %62
  %64 = load i32, ptr %11, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %11, align 4, !tbaa !5
  br label %19, !llvm.loop !15

66:                                               ; preds = %23
  br label %67

67:                                               ; preds = %66
  %68 = load i32, ptr %9, align 4, !tbaa !5
  %69 = add nsw i32 %68, 1
  store i32 %69, ptr %9, align 4, !tbaa !5
  br label %13, !llvm.loop !16

70:                                               ; preds = %17
  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 speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #2

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

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}


; 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(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 %.preheader23.us.preheader, label %._crit_edge

.preheader23.us.preheader:                        ; preds = %4
  %6 = zext nneg i32 %0 to i64
  %xtraiter = and i64 %6, 1
  %7 = icmp eq i32 %0, 1
  %unroll_iter = and i64 %6, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader23.us

.preheader23.us:                                  ; preds = %.preheader23.us.preheader, %._crit_edge26.split.us.us
  %indvars.iv34 = phi i64 [ 0, %.preheader23.us.preheader ], [ %indvars.iv.next35, %._crit_edge26.split.us.us ]
  %8 = mul nuw nsw i64 %indvars.iv34, %6
  %invariant.gep41 = getelementptr inbounds double, ptr %3, i64 %8
  %invariant.gep = getelementptr inbounds double, ptr %1, i64 %8
  br label %.preheader.us.us

.preheader.us.us:                                 ; preds = %._crit_edge.us.us, %.preheader23.us
  %indvars.iv29 = phi i64 [ %indvars.iv.next30, %._crit_edge.us.us ], [ 0, %.preheader23.us ]
  %gep42 = getelementptr inbounds double, ptr %invariant.gep41, i64 %indvars.iv29
  %.promoted.us.us = load double, ptr %gep42, align 8, !tbaa !5
  %invariant.gep39 = getelementptr inbounds double, ptr %2, i64 %indvars.iv29
  br i1 %7, label %._crit_edge.us.us.unr-lcssa, label %.preheader.us.us.new

.preheader.us.us.new:                             ; preds = %.preheader.us.us, %.preheader.us.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %.preheader.us.us.new ], [ 0, %.preheader.us.us ]
  %9 = phi double [ %17, %.preheader.us.us.new ], [ %.promoted.us.us, %.preheader.us.us ]
  %niter = phi i64 [ %niter.next.1, %.preheader.us.us.new ], [ 0, %.preheader.us.us ]
  %gep = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv
  %10 = load double, ptr %gep, align 8, !tbaa !5
  %11 = mul nuw nsw i64 %indvars.iv, %6
  %gep40 = getelementptr inbounds double, ptr %invariant.gep39, i64 %11
  %12 = load double, ptr %gep40, align 8, !tbaa !5
  %13 = tail call double @llvm.fmuladd.f64(double %10, double %12, double %9)
  store double %13, ptr %gep42, align 8, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %gep.1 = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.next
  %14 = load double, ptr %gep.1, align 8, !tbaa !5
  %15 = mul nuw nsw i64 %indvars.iv.next, %6
  %gep40.1 = getelementptr inbounds double, ptr %invariant.gep39, i64 %15
  %16 = load double, ptr %gep40.1, align 8, !tbaa !5
  %17 = tail call double @llvm.fmuladd.f64(double %14, double %16, double %13)
  store double %17, ptr %gep42, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.us.unr-lcssa, label %.preheader.us.us.new, !llvm.loop !9

._crit_edge.us.us.unr-lcssa:                      ; preds = %.preheader.us.us.new, %.preheader.us.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us.us ], [ %indvars.iv.next.1, %.preheader.us.us.new ]
  %.unr = phi double [ %.promoted.us.us, %.preheader.us.us ], [ %17, %.preheader.us.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us.us, label %._crit_edge.us.us.epilog-lcssa

._crit_edge.us.us.epilog-lcssa:                   ; preds = %._crit_edge.us.us.unr-lcssa
  %gep.epil = getelementptr inbounds double, ptr %invariant.gep, i64 %indvars.iv.unr
  %18 = load double, ptr %gep.epil, align 8, !tbaa !5
  %19 = mul nuw nsw i64 %indvars.iv.unr, %6
  %gep40.epil = getelementptr inbounds double, ptr %invariant.gep39, i64 %19
  %20 = load double, ptr %gep40.epil, align 8, !tbaa !5
  %21 = tail call double @llvm.fmuladd.f64(double %18, double %20, double %.unr)
  store double %21, ptr %gep42, align 8, !tbaa !5
  br label %._crit_edge.us.us

._crit_edge.us.us:                                ; preds = %._crit_edge.us.us.unr-lcssa, %._crit_edge.us.us.epilog-lcssa
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %exitcond33.not = icmp eq i64 %indvars.iv.next30, %6
  br i1 %exitcond33.not, label %._crit_edge26.split.us.us, label %.preheader.us.us, !llvm.loop !11

._crit_edge26.split.us.us:                        ; preds = %._crit_edge.us.us
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next35, %6
  br i1 %exitcond38.not, label %._crit_edge, label %.preheader23.us, !llvm.loop !12

._crit_edge:                                      ; preds = %._crit_edge26.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

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) }

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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