; LLVM IR from microsoft/LightAIMD :: src (einsum)

; 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 speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

; 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 double @einsum_mn_nm(ptr noundef %0, ptr noundef %1, i64 noundef %2, i64 noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca double, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  %13 = alloca i64, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store i64 %2, ptr %7, align 8, !tbaa !9
  store i64 %3, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  store double 0.000000e+00, ptr %9, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %49, %4
  %15 = load i64, ptr %10, align 8, !tbaa !9
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = icmp ult i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 2, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  br label %52

19:                                               ; preds = %14
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %20 = load i64, ptr %10, align 8, !tbaa !9
  %21 = load i64, ptr %8, align 8, !tbaa !9
  %22 = mul i64 %20, %21
  store i64 %22, ptr %12, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #3
  store i64 0, ptr %13, align 8, !tbaa !9
  br label %23

23:                                               ; preds = %45, %19
  %24 = load i64, ptr %13, align 8, !tbaa !9
  %25 = load i64, ptr %8, align 8, !tbaa !9
  %26 = icmp ult i64 %24, %25
  br i1 %26, label %28, label %27

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

28:                                               ; preds = %23
  %29 = load ptr, ptr %5, align 8, !tbaa !5
  %30 = load i64, ptr %12, align 8, !tbaa !9
  %31 = load i64, ptr %13, align 8, !tbaa !9
  %32 = add i64 %30, %31
  %33 = getelementptr inbounds double, ptr %29, i64 %32
  %34 = load double, ptr %33, align 8, !tbaa !11
  %35 = load ptr, ptr %6, align 8, !tbaa !5
  %36 = load i64, ptr %13, align 8, !tbaa !9
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = mul i64 %36, %37
  %39 = load i64, ptr %10, align 8, !tbaa !9
  %40 = add i64 %38, %39
  %41 = getelementptr inbounds double, ptr %35, i64 %40
  %42 = load double, ptr %41, align 8, !tbaa !11
  %43 = load double, ptr %9, align 8, !tbaa !11
  %44 = call double @llvm.fmuladd.f64(double %34, double %42, double %43)
  store double %44, ptr %9, align 8, !tbaa !11
  br label %45

45:                                               ; preds = %28
  %46 = load i64, ptr %13, align 8, !tbaa !9
  %47 = add i64 %46, 1
  store i64 %47, ptr %13, align 8, !tbaa !9
  br label %23, !llvm.loop !13

48:                                               ; preds = %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %49

49:                                               ; preds = %48
  %50 = load i64, ptr %10, align 8, !tbaa !9
  %51 = add i64 %50, 1
  store i64 %51, ptr %10, align 8, !tbaa !9
  br label %14, !llvm.loop !15

52:                                               ; preds = %18
  %53 = load double, ptr %9, align 8, !tbaa !11
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  ret double %53
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #2 = { 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !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}


; 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 nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #0

; Function Attrs: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local double @einsum_mn_nm(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %.not = icmp eq i64 %2, 0
  %.not27 = icmp eq i64 %3, 0
  %or.cond = or i1 %.not, %.not27
  br i1 %or.cond, label %._crit_edge25, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %4
  %5 = add i64 %3, -1
  %xtraiter = and i64 %3, 3
  %6 = icmp ult i64 %5, 3
  %unroll_iter = and i64 %3, -4
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.01622.us = phi double [ %.lcssa, %._crit_edge.us ], [ 0.000000e+00, %.lr.ph.us.preheader ]
  %.01721.us = phi i64 [ %39, %._crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %7 = mul i64 %.01721.us, %3
  %invariant.gep.us = getelementptr double, ptr %1, i64 %.01721.us
  %8 = getelementptr double, ptr %0, i64 %7
  br i1 %6, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new

.lr.ph.us.new:                                    ; preds = %.lr.ph.us, %.lr.ph.us.new
  %.020.us = phi i64 [ %32, %.lr.ph.us.new ], [ 0, %.lr.ph.us ]
  %.119.us = phi double [ %31, %.lr.ph.us.new ], [ %.01622.us, %.lr.ph.us ]
  %niter = phi i64 [ %niter.next.3, %.lr.ph.us.new ], [ 0, %.lr.ph.us ]
  %9 = getelementptr double, ptr %8, i64 %.020.us
  %10 = load double, ptr %9, align 8, !tbaa !5
  %11 = mul i64 %.020.us, %2
  %gep.us = getelementptr double, ptr %invariant.gep.us, i64 %11
  %12 = load double, ptr %gep.us, align 8, !tbaa !5
  %13 = tail call double @llvm.fmuladd.f64(double %10, double %12, double %.119.us)
  %14 = or disjoint i64 %.020.us, 1
  %15 = getelementptr double, ptr %8, i64 %14
  %16 = load double, ptr %15, align 8, !tbaa !5
  %17 = mul i64 %14, %2
  %gep.us.1 = getelementptr double, ptr %invariant.gep.us, i64 %17
  %18 = load double, ptr %gep.us.1, align 8, !tbaa !5
  %19 = tail call double @llvm.fmuladd.f64(double %16, double %18, double %13)
  %20 = or disjoint i64 %.020.us, 2
  %21 = getelementptr double, ptr %8, i64 %20
  %22 = load double, ptr %21, align 8, !tbaa !5
  %23 = mul i64 %20, %2
  %gep.us.2 = getelementptr double, ptr %invariant.gep.us, i64 %23
  %24 = load double, ptr %gep.us.2, align 8, !tbaa !5
  %25 = tail call double @llvm.fmuladd.f64(double %22, double %24, double %19)
  %26 = or disjoint i64 %.020.us, 3
  %27 = getelementptr double, ptr %8, i64 %26
  %28 = load double, ptr %27, align 8, !tbaa !5
  %29 = mul i64 %26, %2
  %gep.us.3 = getelementptr double, ptr %invariant.gep.us, i64 %29
  %30 = load double, ptr %gep.us.3, align 8, !tbaa !5
  %31 = tail call double @llvm.fmuladd.f64(double %28, double %30, double %25)
  %32 = add nuw i64 %.020.us, 4
  %niter.next.3 = add i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %._crit_edge.us.unr-lcssa, label %.lr.ph.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %.lr.ph.us.new, %.lr.ph.us
  %.lcssa.ph = phi double [ poison, %.lr.ph.us ], [ %31, %.lr.ph.us.new ]
  %.020.us.unr = phi i64 [ 0, %.lr.ph.us ], [ %32, %.lr.ph.us.new ]
  %.119.us.unr = phi double [ %.01622.us, %.lr.ph.us ], [ %31, %.lr.ph.us.new ]
  br i1 %lcmp.mod.not, label %._crit_edge.us, label %.epil.preheader

.epil.preheader:                                  ; preds = %._crit_edge.us.unr-lcssa, %.epil.preheader
  %.020.us.epil = phi i64 [ %38, %.epil.preheader ], [ %.020.us.unr, %._crit_edge.us.unr-lcssa ]
  %.119.us.epil = phi double [ %37, %.epil.preheader ], [ %.119.us.unr, %._crit_edge.us.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %._crit_edge.us.unr-lcssa ]
  %33 = getelementptr double, ptr %8, i64 %.020.us.epil
  %34 = load double, ptr %33, align 8, !tbaa !5
  %35 = mul i64 %.020.us.epil, %2
  %gep.us.epil = getelementptr double, ptr %invariant.gep.us, i64 %35
  %36 = load double, ptr %gep.us.epil, align 8, !tbaa !5
  %37 = tail call double @llvm.fmuladd.f64(double %34, double %36, double %.119.us.epil)
  %38 = add nuw i64 %.020.us.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 double [ %.lcssa.ph, %._crit_edge.us.unr-lcssa ], [ %37, %.epil.preheader ]
  %39 = add nuw i64 %.01721.us, 1
  %exitcond29.not = icmp eq i64 %39, %2
  br i1 %exitcond29.not, label %._crit_edge25, label %.lr.ph.us, !llvm.loop !13

._crit_edge25:                                    ; preds = %._crit_edge.us, %4
  %.016.lcssa = phi double [ 0.000000e+00, %4 ], [ %.lcssa, %._crit_edge.us ]
  ret double %.016.lcssa
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #1 = { 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 = !{!"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, !12}
!12 = !{!"llvm.loop.unroll.disable"}
!13 = distinct !{!13, !10}


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