; LLVM IR from attractivechaos/plb2 :: src/c (matmul)

; 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
declare dso_local ptr @mat_alloc(i32 noundef, i32 noundef) #0

; 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: nounwind uwtable
define dso_local ptr @mat_mul(i32 noundef %0, i32 noundef %1, ptr noundef %2, i32 noundef %3, ptr noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store i32 %0, ptr %6, align 4, !tbaa !5
  store i32 %1, ptr %7, align 4, !tbaa !5
  store ptr %2, ptr %8, align 8, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !5
  store ptr %4, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %15 = load i32, ptr %6, align 4, !tbaa !5
  %16 = load i32, ptr %9, align 4, !tbaa !5
  %17 = call ptr @mat_alloc(i32 noundef %15, i32 noundef %16)
  store ptr %17, ptr %11, align 8, !tbaa !9
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %69, %5
  %19 = load i32, ptr %12, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  %21 = icmp slt i32 %19, %20
  br i1 %21, label %22, label %72

22:                                               ; preds = %18
  store i32 0, ptr %14, align 4, !tbaa !5
  br label %23

23:                                               ; preds = %65, %22
  %24 = load i32, ptr %14, align 4, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !5
  %26 = icmp slt i32 %24, %25
  br i1 %26, label %27, label %68

27:                                               ; preds = %23
  store i32 0, ptr %13, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %61, %27
  %29 = load i32, ptr %13, align 4, !tbaa !5
  %30 = load i32, ptr %9, align 4, !tbaa !5
  %31 = icmp slt i32 %29, %30
  br i1 %31, label %32, label %64

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

61:                                               ; preds = %32
  %62 = load i32, ptr %13, align 4, !tbaa !5
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %13, align 4, !tbaa !5
  br label %28, !llvm.loop !13

64:                                               ; preds = %28
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %14, align 4, !tbaa !5
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %14, align 4, !tbaa !5
  br label %23, !llvm.loop !15

68:                                               ; preds = %23
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %12, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %12, align 4, !tbaa !5
  br label %18, !llvm.loop !16

72:                                               ; preds = %18
  %73 = load ptr, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  ret ptr %73
}

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


; 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: nounwind uwtable
declare dso_local ptr @mat_alloc(i32 noundef, i32 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local ptr @mat_mul(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2, i32 noundef %3, ptr nocapture noundef readonly %4) local_unnamed_addr #0 {
  %6 = tail call ptr @mat_alloc(i32 noundef %0, i32 noundef %3)
  %7 = icmp sgt i32 %0, 0
  %8 = icmp sgt i32 %1, 0
  %or.cond = and i1 %7, %8
  %9 = icmp sgt i32 %3, 0
  %or.cond44 = and i1 %or.cond, %9
  br i1 %or.cond44, label %.preheader22.us.us.preheader, label %._crit_edge

.preheader22.us.us.preheader:                     ; preds = %5
  %wide.trip.count42 = zext nneg i32 %0 to i64
  %wide.trip.count37 = zext nneg i32 %1 to i64
  %wide.trip.count = zext nneg i32 %3 to i64
  %10 = shl nuw nsw i64 %wide.trip.count, 3
  %11 = shl nuw nsw i64 %wide.trip.count37, 3
  %min.iters.check = icmp ult i32 %3, 4
  %n.vec = and i64 %wide.trip.count, 2147483644
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  %xtraiter = and i64 %wide.trip.count, 1
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  %12 = add nsw i64 %wide.trip.count, -1
  br label %.preheader22.us.us

.preheader22.us.us:                               ; preds = %.preheader22.us.us.preheader, %._crit_edge25.split.us.us.us
  %indvars.iv39 = phi i64 [ 0, %.preheader22.us.us.preheader ], [ %indvars.iv.next40, %._crit_edge25.split.us.us.us ]
  %13 = getelementptr inbounds ptr, ptr %2, i64 %indvars.iv39
  %14 = getelementptr inbounds ptr, ptr %6, i64 %indvars.iv39
  %15 = load ptr, ptr %13, align 8, !tbaa !5
  %16 = load ptr, ptr %14, align 8, !tbaa !5
  %scevgep = getelementptr i8, ptr %16, i64 %10
  %scevgep45 = getelementptr i8, ptr %15, i64 %11
  %bound0 = icmp ult ptr %16, %scevgep45
  %bound1 = icmp ult ptr %15, %scevgep
  %found.conflict = and i1 %bound0, %bound1
  br label %.preheader.us.us.us

.preheader.us.us.us:                              ; preds = %._crit_edge.us.us.us, %.preheader22.us.us
  %indvars.iv34 = phi i64 [ %indvars.iv.next35, %._crit_edge.us.us.us ], [ 0, %.preheader22.us.us ]
  %17 = getelementptr inbounds double, ptr %15, i64 %indvars.iv34
  %18 = getelementptr inbounds ptr, ptr %4, i64 %indvars.iv34
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.preheader.us.us.us
  %scevgep46 = getelementptr i8, ptr %19, i64 %10
  %bound047 = icmp ult ptr %16, %scevgep46
  %bound148 = icmp ult ptr %19, %scevgep
  %found.conflict49 = and i1 %bound047, %bound148
  %conflict.rdx = or i1 %found.conflict, %found.conflict49
  br i1 %conflict.rdx, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %vector.memcheck
  %20 = load double, ptr %17, align 8, !tbaa !9, !alias.scope !11
  %broadcast.splatinsert53 = insertelement <2 x double> poison, double %20, i64 0
  %broadcast.splat54 = shufflevector <2 x double> %broadcast.splatinsert53, <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 ]
  %21 = getelementptr inbounds double, ptr %19, i64 %index
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load = load <2 x double>, ptr %21, align 8, !tbaa !9, !alias.scope !14
  %wide.load50 = load <2 x double>, ptr %22, align 8, !tbaa !9, !alias.scope !14
  %23 = getelementptr inbounds double, ptr %16, i64 %index
  %24 = getelementptr inbounds i8, ptr %23, i64 16
  %wide.load51 = load <2 x double>, ptr %23, align 8, !tbaa !9, !alias.scope !16, !noalias !18
  %wide.load52 = load <2 x double>, ptr %24, align 8, !tbaa !9, !alias.scope !16, !noalias !18
  %25 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat54, <2 x double> %wide.load, <2 x double> %wide.load51)
  %26 = tail call <2 x double> @llvm.fmuladd.v2f64(<2 x double> %broadcast.splat54, <2 x double> %wide.load50, <2 x double> %wide.load52)
  store <2 x double> %25, ptr %23, align 8, !tbaa !9, !alias.scope !16, !noalias !18
  store <2 x double> %26, ptr %24, align 8, !tbaa !9, !alias.scope !16, !noalias !18
  %index.next = add nuw i64 %index, 4
  %27 = icmp eq i64 %index.next, %n.vec
  br i1 %27, label %middle.block, label %vector.body, !llvm.loop !19

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

scalar.ph.preheader:                              ; preds = %middle.block, %vector.memcheck, %.preheader.us.us.us
  %indvars.iv.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.preheader.us.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
  %28 = load double, ptr %17, align 8, !tbaa !9
  %29 = getelementptr inbounds double, ptr %19, i64 %indvars.iv.ph
  %30 = load double, ptr %29, align 8, !tbaa !9
  %31 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.ph
  %32 = load double, ptr %31, align 8, !tbaa !9
  %33 = tail call double @llvm.fmuladd.f64(double %28, double %30, double %32)
  store double %33, ptr %31, align 8, !tbaa !9
  %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 ]
  %34 = icmp eq i64 %indvars.iv.ph, %12
  br i1 %34, label %._crit_edge.us.us.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 ]
  %35 = load double, ptr %17, align 8, !tbaa !9
  %36 = getelementptr inbounds double, ptr %19, i64 %indvars.iv
  %37 = load double, ptr %36, align 8, !tbaa !9
  %38 = getelementptr inbounds double, ptr %16, i64 %indvars.iv
  %39 = load double, ptr %38, align 8, !tbaa !9
  %40 = tail call double @llvm.fmuladd.f64(double %35, double %37, double %39)
  store double %40, ptr %38, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %41 = load double, ptr %17, align 8, !tbaa !9
  %42 = getelementptr inbounds double, ptr %19, i64 %indvars.iv.next
  %43 = load double, ptr %42, align 8, !tbaa !9
  %44 = getelementptr inbounds double, ptr %16, i64 %indvars.iv.next
  %45 = load double, ptr %44, align 8, !tbaa !9
  %46 = tail call double @llvm.fmuladd.f64(double %41, double %43, double %45)
  store double %46, ptr %44, align 8, !tbaa !9
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %exitcond.not.1 = icmp eq i64 %indvars.iv.next.1, %wide.trip.count
  br i1 %exitcond.not.1, label %._crit_edge.us.us.us, label %scalar.ph, !llvm.loop !23

._crit_edge.us.us.us:                             ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next35 = add nuw nsw i64 %indvars.iv34, 1
  %exitcond38.not = icmp eq i64 %indvars.iv.next35, %wide.trip.count37
  br i1 %exitcond38.not, label %._crit_edge25.split.us.us.us, label %.preheader.us.us.us, !llvm.loop !24

._crit_edge25.split.us.us.us:                     ; preds = %._crit_edge.us.us.us
  %indvars.iv.next40 = add nuw nsw i64 %indvars.iv39, 1
  %exitcond43.not = icmp eq i64 %indvars.iv.next40, %wide.trip.count42
  br i1 %exitcond43.not, label %._crit_edge, label %.preheader22.us.us, !llvm.loop !25

._crit_edge:                                      ; preds = %._crit_edge25.split.us.us.us, %5
  ret ptr %6
}

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

; 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>) #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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12}
!12 = distinct !{!12, !13}
!13 = distinct !{!13, !"LVerDomain"}
!14 = !{!15}
!15 = distinct !{!15, !13}
!16 = !{!17}
!17 = distinct !{!17, !13}
!18 = !{!12, !15}
!19 = distinct !{!19, !20, !21, !22}
!20 = !{!"llvm.loop.mustprogress"}
!21 = !{!"llvm.loop.isvectorized", i32 1}
!22 = !{!"llvm.loop.unroll.runtime.disable"}
!23 = distinct !{!23, !20, !21}
!24 = distinct !{!24, !20}
!25 = distinct !{!25, !20}


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