; LLVM IR from Beliavsky/FortranTip :: sum_mat (sum_mat)

; 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: 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: nounwind uwtable
define dso_local float @sum_mat(i32 noundef %0, i32 noundef %1, ptr noundef %2) #1 {
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca float, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  store ptr %2, ptr %6, align 8, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !5
  %12 = zext i32 %11 to i64
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = zext i32 %13 to i64
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store float 0.000000e+00, ptr %7, align 4, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !5
  br label %15

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

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

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

21:                                               ; preds = %38, %20
  %22 = load i32, ptr %10, 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 %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  br label %41

26:                                               ; preds = %21
  %27 = load ptr, ptr %6, align 8, !tbaa !9
  %28 = load i32, ptr %8, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = mul nsw i64 %29, %14
  %31 = getelementptr inbounds float, ptr %27, i64 %30
  %32 = load i32, ptr %10, align 4, !tbaa !5
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds float, ptr %31, i64 %33
  %35 = load float, ptr %34, align 4, !tbaa !11
  %36 = load float, ptr %7, align 4, !tbaa !11
  %37 = fadd float %36, %35
  store float %37, ptr %7, align 4, !tbaa !11
  br label %38

38:                                               ; preds = %26
  %39 = load i32, ptr %10, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %10, align 4, !tbaa !5
  br label %21, !llvm.loop !13

41:                                               ; preds = %25
  br label %42

42:                                               ; preds = %41
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %8, align 4, !tbaa !5
  br label %15, !llvm.loop !15

45:                                               ; preds = %19
  %46 = load float, ptr %7, align 4, !tbaa !11
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret float %46
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"float", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !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: read) uwtable
define dso_local float @sum_mat(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = zext i32 %1 to i64
  %5 = icmp sgt i32 %0, 0
  %6 = icmp sgt i32 %1, 0
  %or.cond = and i1 %5, %6
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge18

.preheader.us.preheader:                          ; preds = %3
  %wide.trip.count25 = zext nneg i32 %0 to i64
  %xtraiter = and i64 %4, 7
  %7 = icmp ult i32 %1, 8
  %unroll_iter = and i64 %4, 2147483640
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv22 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next23, %._crit_edge.us ]
  %.01316.us = phi float [ 0.000000e+00, %.preheader.us.preheader ], [ %.lcssa, %._crit_edge.us ]
  %8 = mul nuw nsw i64 %indvars.iv22, %4
  %9 = getelementptr inbounds float, ptr %2, i64 %8
  br i1 %7, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new

.preheader.us.new:                                ; preds = %.preheader.us, %.preheader.us.new
  %indvars.iv = phi i64 [ %indvars.iv.next.7, %.preheader.us.new ], [ 0, %.preheader.us ]
  %.114.us = phi float [ %33, %.preheader.us.new ], [ %.01316.us, %.preheader.us ]
  %niter = phi i64 [ %niter.next.7, %.preheader.us.new ], [ 0, %.preheader.us ]
  %10 = getelementptr inbounds float, ptr %9, i64 %indvars.iv
  %11 = load float, ptr %10, align 4, !tbaa !5
  %12 = fadd float %.114.us, %11
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %13 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next
  %14 = load float, ptr %13, align 4, !tbaa !5
  %15 = fadd float %12, %14
  %indvars.iv.next.1 = or disjoint i64 %indvars.iv, 2
  %16 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.1
  %17 = load float, ptr %16, align 4, !tbaa !5
  %18 = fadd float %15, %17
  %indvars.iv.next.2 = or disjoint i64 %indvars.iv, 3
  %19 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.2
  %20 = load float, ptr %19, align 4, !tbaa !5
  %21 = fadd float %18, %20
  %indvars.iv.next.3 = or disjoint i64 %indvars.iv, 4
  %22 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.3
  %23 = load float, ptr %22, align 4, !tbaa !5
  %24 = fadd float %21, %23
  %indvars.iv.next.4 = or disjoint i64 %indvars.iv, 5
  %25 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.4
  %26 = load float, ptr %25, align 4, !tbaa !5
  %27 = fadd float %24, %26
  %indvars.iv.next.5 = or disjoint i64 %indvars.iv, 6
  %28 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.5
  %29 = load float, ptr %28, align 4, !tbaa !5
  %30 = fadd float %27, %29
  %indvars.iv.next.6 = or disjoint i64 %indvars.iv, 7
  %31 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.next.6
  %32 = load float, ptr %31, align 4, !tbaa !5
  %33 = fadd float %30, %32
  %indvars.iv.next.7 = add nuw nsw i64 %indvars.iv, 8
  %niter.next.7 = add i64 %niter, 8
  %niter.ncmp.7 = icmp eq i64 %niter.next.7, %unroll_iter
  br i1 %niter.ncmp.7, label %._crit_edge.us.unr-lcssa, label %.preheader.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %.preheader.us.new, %.preheader.us
  %.lcssa.ph = phi float [ poison, %.preheader.us ], [ %33, %.preheader.us.new ]
  %indvars.iv.unr = phi i64 [ 0, %.preheader.us ], [ %indvars.iv.next.7, %.preheader.us.new ]
  %.114.us.unr = phi float [ %.01316.us, %.preheader.us ], [ %33, %.preheader.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %._crit_edge.us.unr-lcssa ]
  %.114.us.epil = phi float [ %36, %.epil.preheader ], [ %.114.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %34 = getelementptr inbounds float, ptr %9, i64 %indvars.iv.epil
  %35 = load float, ptr %34, align 4, !tbaa !5
  %36 = fadd float %.114.us.epil, %35
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %._crit_edge.us, label %.epil.preheader, !llvm.loop !11

._crit_edge.us:                                   ; preds = %.epil.preheader, %._crit_edge.us.unr-lcssa
  %.lcssa = phi float [ %.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %36, %.epil.preheader ]
  %indvars.iv.next23 = add nuw nsw i64 %indvars.iv22, 1
  %exitcond26.not = icmp eq i64 %indvars.iv.next23, %wide.trip.count25
  br i1 %exitcond26.not, label %._crit_edge18, label %.preheader.us, !llvm.loop !13

._crit_edge18:                                    ; preds = %._crit_edge.us, %3
  %.013.lcssa = phi float [ 0.000000e+00, %3 ], [ %.lcssa, %._crit_edge.us ]
  ret float %.013.lcssa
}

attributes #0 = { nofree norecurse nosync nounwind memory(argmem: read) 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" }

!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 = !{!"float", !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, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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