; LLVM IR from KhawajaAbaid/machine-learning-in-c :: linalg (vector_outer_product)

; 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: nounwind allocsize(0,1)
declare noalias ptr @calloc(i64 noundef, i64 noundef) #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 ptr @vector_outer_product(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 ptr, align 8
  %10 = alloca i64, align 8
  %11 = alloca i32, align 4
  %12 = 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) #4
  %13 = load i64, ptr %7, align 8, !tbaa !9
  %14 = mul i64 %13, 8
  %15 = call noalias ptr @malloc(i64 noundef %14) #5
  store ptr %15, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  store i64 0, ptr %10, align 8, !tbaa !9
  br label %16

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

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

21:                                               ; preds = %16
  %22 = load i64, ptr %8, align 8, !tbaa !9
  %23 = call noalias ptr @calloc(i64 noundef %22, i64 noundef 8) #6
  %24 = load ptr, ptr %9, align 8, !tbaa !5
  %25 = load i64, ptr %10, align 8, !tbaa !9
  %26 = getelementptr inbounds ptr, ptr %24, i64 %25
  store ptr %23, ptr %26, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  store i64 0, ptr %12, align 8, !tbaa !9
  br label %27

27:                                               ; preds = %48, %21
  %28 = load i64, ptr %12, align 8, !tbaa !9
  %29 = load i64, ptr %8, align 8, !tbaa !9
  %30 = icmp ult i64 %28, %29
  br i1 %30, label %32, label %31

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

32:                                               ; preds = %27
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = load i64, ptr %10, align 8, !tbaa !9
  %35 = getelementptr inbounds double, ptr %33, i64 %34
  %36 = load double, ptr %35, align 8, !tbaa !11
  %37 = load ptr, ptr %6, align 8, !tbaa !5
  %38 = load i64, ptr %12, align 8, !tbaa !9
  %39 = getelementptr inbounds double, ptr %37, i64 %38
  %40 = load double, ptr %39, align 8, !tbaa !11
  %41 = fmul double %36, %40
  %42 = load ptr, ptr %9, align 8, !tbaa !5
  %43 = load i64, ptr %10, align 8, !tbaa !9
  %44 = getelementptr inbounds ptr, ptr %42, i64 %43
  %45 = load ptr, ptr %44, align 8, !tbaa !5
  %46 = load i64, ptr %12, align 8, !tbaa !9
  %47 = getelementptr inbounds double, ptr %45, i64 %46
  store double %41, ptr %47, align 8, !tbaa !11
  br label %48

48:                                               ; preds = %32
  %49 = load i64, ptr %12, align 8, !tbaa !9
  %50 = add i64 %49, 1
  store i64 %50, ptr %12, align 8, !tbaa !9
  br label %27, !llvm.loop !13

51:                                               ; preds = %31
  br label %52

52:                                               ; preds = %51
  %53 = load i64, ptr %10, align 8, !tbaa !9
  %54 = add i64 %53, 1
  store i64 %54, ptr %10, align 8, !tbaa !9
  br label %16, !llvm.loop !15

55:                                               ; preds = %20
  %56 = load ptr, ptr %9, align 8, !tbaa !5
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  ret ptr %56
}

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #3

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(0,1) "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 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 allocsize(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 #4 = { nounwind }
attributes #5 = { nounwind allocsize(0) }
attributes #6 = { nounwind allocsize(0,1) }

!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 nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @calloc(i64 noundef, i64 noundef) local_unnamed_addr #0

; Function Attrs: nofree nounwind memory(write, argmem: read, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @vector_outer_product(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i64 noundef %2, i64 noundef %3) local_unnamed_addr #1 {
  %5 = shl i64 %2, 3
  %6 = tail call noalias ptr @malloc(i64 noundef %5) #3
  %.not = icmp eq i64 %2, 0
  br i1 %.not, label %._crit_edge22, label %.lr.ph21

.lr.ph21:                                         ; preds = %4
  %.not23 = icmp eq i64 %3, 0
  br i1 %.not23, label %.lr.ph21.split, label %.lr.ph.us.preheader

.lr.ph.us.preheader:                              ; preds = %.lr.ph21
  %min.iters.check = icmp ult i64 %3, 4
  %n.vec = and i64 %3, -4
  %cmp.n = icmp eq i64 %n.vec, %3
  br label %.lr.ph.us

.lr.ph.us:                                        ; preds = %.lr.ph.us.preheader, %._crit_edge.us
  %.01719.us = phi i64 [ %23, %._crit_edge.us ], [ 0, %.lr.ph.us.preheader ]
  %7 = tail call noalias ptr @calloc(i64 noundef %3, i64 noundef 8) #4
  %8 = getelementptr inbounds ptr, ptr %6, i64 %.01719.us
  store ptr %7, ptr %8, align 8, !tbaa !5
  %9 = getelementptr inbounds double, ptr %0, i64 %.01719.us
  %10 = load double, ptr %9, align 8, !tbaa !9
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

vector.ph:                                        ; preds = %.lr.ph.us
  %broadcast.splatinsert = insertelement <2 x double> poison, double %10, i64 0
  %broadcast.splat = shufflevector <2 x double> %broadcast.splatinsert, <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 ]
  %11 = getelementptr inbounds double, ptr %1, i64 %index
  %12 = getelementptr inbounds i8, ptr %11, i64 16
  %wide.load = load <2 x double>, ptr %11, align 8, !tbaa !9
  %wide.load29 = load <2 x double>, ptr %12, align 8, !tbaa !9
  %13 = fmul <2 x double> %broadcast.splat, %wide.load
  %14 = fmul <2 x double> %broadcast.splat, %wide.load29
  %15 = getelementptr inbounds double, ptr %7, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  store <2 x double> %13, ptr %15, align 8, !tbaa !9
  store <2 x double> %14, ptr %16, align 8, !tbaa !9
  %index.next = add nuw i64 %index, 4
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !11

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

scalar.ph.preheader:                              ; preds = %middle.block, %.lr.ph.us
  %.018.us.ph = phi i64 [ 0, %.lr.ph.us ], [ %n.vec, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %.018.us = phi i64 [ %22, %scalar.ph ], [ %.018.us.ph, %scalar.ph.preheader ]
  %18 = getelementptr inbounds double, ptr %1, i64 %.018.us
  %19 = load double, ptr %18, align 8, !tbaa !9
  %20 = fmul double %10, %19
  %21 = getelementptr inbounds double, ptr %7, i64 %.018.us
  store double %20, ptr %21, align 8, !tbaa !9
  %22 = add nuw i64 %.018.us, 1
  %exitcond.not = icmp eq i64 %22, %3
  br i1 %exitcond.not, label %._crit_edge.us, label %scalar.ph, !llvm.loop !15

._crit_edge.us:                                   ; preds = %scalar.ph, %middle.block
  %23 = add nuw i64 %.01719.us, 1
  %exitcond25.not = icmp eq i64 %23, %2
  br i1 %exitcond25.not, label %._crit_edge22, label %.lr.ph.us, !llvm.loop !16

._crit_edge22:                                    ; preds = %._crit_edge.us, %.lr.ph21.split, %4
  ret ptr %6

.lr.ph21.split:                                   ; preds = %.lr.ph21, %.lr.ph21.split
  %.01719 = phi i64 [ %26, %.lr.ph21.split ], [ 0, %.lr.ph21 ]
  %24 = tail call noalias ptr @calloc(i64 noundef 0, i64 noundef 8) #4
  %25 = getelementptr inbounds ptr, ptr %6, i64 %.01719
  store ptr %24, ptr %25, align 8, !tbaa !5
  %26 = add nuw i64 %.01719, 1
  %exitcond26.not = icmp eq i64 %26, %2
  br i1 %exitcond26.not, label %._crit_edge22, label %.lr.ph21.split, !llvm.loop !16
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress nofree nounwind willreturn allockind("alloc,zeroed") allocsize(0,1) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 = { nofree nounwind memory(write, argmem: read, inaccessiblemem: 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 #2 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(0) }
attributes #4 = { nounwind allocsize(0,1) }

!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 = distinct !{!11, !12, !13, !14}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!"llvm.loop.isvectorized", i32 1}
!14 = !{!"llvm.loop.unroll.runtime.disable"}
!15 = distinct !{!15, !12, !14, !13}
!16 = distinct !{!16, !12}


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