; LLVM IR from JessonYue/LeetCodeLearning :: Array/zhouhongkun/HomeWork/MatrixOperation/main.ll (Z12createMatrixiiPi)

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

%struct._Matrix = type { i32, i32, ptr }

; Function Attrs: mustprogress nounwind uwtable
define dso_local { i64, ptr } @_Z12createMatrixiiPi(i32 noundef %0, i32 noundef %1, ptr noundef %2) #0 {
  %4 = alloca %struct._Matrix, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %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 i32 %1, ptr %6, align 4, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = mul i64 4, %14
  %16 = call noalias ptr @malloc(i64 noundef %15) #4
  store ptr %16, 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 %17

17:                                               ; preds = %31, %3
  %18 = load i32, ptr %9, align 4, !tbaa !5
  %19 = load i32, ptr %5, align 4, !tbaa !5
  %20 = icmp slt i32 %18, %19
  br i1 %20, label %22, label %21

21:                                               ; preds = %17
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %34

22:                                               ; preds = %17
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = sext i32 %23 to i64
  %25 = mul i64 4, %24
  %26 = call noalias ptr @malloc(i64 noundef %25) #4
  %27 = load ptr, ptr %8, align 8, !tbaa !9
  %28 = load i32, ptr %9, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds ptr, ptr %27, i64 %29
  store ptr %26, ptr %30, align 8, !tbaa !9
  br label %31

31:                                               ; preds = %22
  %32 = load i32, ptr %9, align 4, !tbaa !5
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %9, align 4, !tbaa !5
  br label %17, !llvm.loop !11

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

35:                                               ; preds = %67, %34
  %36 = load i32, ptr %10, align 4, !tbaa !5
  %37 = load i32, ptr %5, align 4, !tbaa !5
  %38 = icmp slt i32 %36, %37
  br i1 %38, label %40, label %39

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

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

41:                                               ; preds = %63, %40
  %42 = load i32, ptr %12, align 4, !tbaa !5
  %43 = load i32, ptr %6, align 4, !tbaa !5
  %44 = icmp slt i32 %42, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %41
  store i32 8, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %66

46:                                               ; preds = %41
  %47 = load ptr, ptr %7, align 8, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !5
  %49 = mul nsw i32 %48, 3
  %50 = load i32, ptr %12, align 4, !tbaa !5
  %51 = add nsw i32 %49, %50
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i32, ptr %47, i64 %52
  %54 = load i32, ptr %53, align 4, !tbaa !5
  %55 = load ptr, ptr %8, align 8, !tbaa !9
  %56 = load i32, ptr %10, align 4, !tbaa !5
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds ptr, ptr %55, i64 %57
  %59 = load ptr, ptr %58, align 8, !tbaa !9
  %60 = load i32, ptr %12, align 4, !tbaa !5
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  store i32 %54, ptr %62, align 4, !tbaa !5
  br label %63

63:                                               ; preds = %46
  %64 = load i32, ptr %12, align 4, !tbaa !5
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %12, align 4, !tbaa !5
  br label %41, !llvm.loop !13

66:                                               ; preds = %45
  br label %67

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

70:                                               ; preds = %39
  %71 = load i32, ptr %5, align 4, !tbaa !5
  %72 = getelementptr inbounds %struct._Matrix, ptr %4, i32 0, i32 0
  store i32 %71, ptr %72, align 8, !tbaa !15
  %73 = load i32, ptr %6, align 4, !tbaa !5
  %74 = getelementptr inbounds %struct._Matrix, ptr %4, i32 0, i32 1
  store i32 %73, ptr %74, align 4, !tbaa !17
  %75 = load ptr, ptr %8, align 8, !tbaa !9
  %76 = getelementptr inbounds %struct._Matrix, ptr %4, i32 0, i32 2
  store ptr %75, ptr %76, align 8, !tbaa !18
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  %77 = load { i64, ptr }, ptr %4, align 8
  ret { i64, ptr } %77
}

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

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

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

attributes #0 = { mustprogress 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 = { 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 #3 = { nounwind }
attributes #4 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}
!15 = !{!16, !6, i64 0}
!16 = !{!"_ZTS7_Matrix", !6, i64 0, !6, i64 4, !10, i64 8}
!17 = !{!16, !6, i64 4}
!18 = !{!16, !10, i64 8}


; 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 uwtable
define dso_local { i64, ptr } @_Z12createMatrixiiPi(i32 noundef %0, i32 noundef %1, ptr nocapture noundef readonly %2) local_unnamed_addr #0 {
  %4 = ptrtoint ptr %2 to i64
  %5 = sext i32 %0 to i64
  %6 = shl nsw i64 %5, 2
  %7 = tail call noalias ptr @malloc(i64 noundef %6) #2
  %8 = icmp sgt i32 %0, 0
  br i1 %8, label %.lr.ph, label %._crit_edge29

.lr.ph:                                           ; preds = %3
  %9 = sext i32 %1 to i64
  %10 = shl nsw i64 %9, 2
  %wide.trip.count = zext nneg i32 %0 to i64
  br label %35

.preheader24:                                     ; preds = %35
  %11 = icmp sgt i32 %1, 0
  %or.cond = and i1 %8, %11
  br i1 %or.cond, label %.preheader.us.preheader, label %._crit_edge29

.preheader.us.preheader:                          ; preds = %.preheader24
  %wide.trip.count40 = zext nneg i32 %0 to i64
  %wide.trip.count35 = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  %n.vec = and i64 %wide.trip.count35, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count35
  %xtraiter = and i64 %wide.trip.count35, 3
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader.us

.preheader.us:                                    ; preds = %.preheader.us.preheader, %._crit_edge.us
  %indvars.iv37 = phi i64 [ 0, %.preheader.us.preheader ], [ %indvars.iv.next38, %._crit_edge.us ]
  %12 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv37
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %.idx = mul i64 %indvars.iv37, 12
  %invariant.gep = getelementptr i8, ptr %2, i64 %.idx
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.memcheck

vector.memcheck:                                  ; preds = %.preheader.us
  %14 = ptrtoint ptr %13 to i64
  %15 = mul nsw i64 %indvars.iv37, -12
  %16 = sub i64 %15, %4
  %17 = add i64 %16, %14
  %diff.check = icmp ult i64 %17, 32
  br i1 %diff.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %vector.memcheck, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %vector.memcheck ]
  %18 = getelementptr i32, ptr %invariant.gep, i64 %index
  %19 = getelementptr i8, ptr %18, i64 16
  %wide.load = load <4 x i32>, ptr %18, align 4, !tbaa !9
  %wide.load42 = load <4 x i32>, ptr %19, align 4, !tbaa !9
  %20 = getelementptr inbounds i32, ptr %13, i64 %index
  %21 = getelementptr inbounds i8, ptr %20, i64 16
  store <4 x i32> %wide.load, ptr %20, align 4, !tbaa !9
  store <4 x i32> %wide.load42, ptr %21, align 4, !tbaa !9
  %index.next = add nuw i64 %index, 8
  %22 = icmp eq i64 %index.next, %n.vec
  br i1 %22, 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, %vector.memcheck, %.preheader.us
  %indvars.iv32.ph = phi i64 [ 0, %vector.memcheck ], [ 0, %.preheader.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, %scalar.ph.prol
  %indvars.iv32.prol = phi i64 [ %indvars.iv.next33.prol, %scalar.ph.prol ], [ %indvars.iv32.ph, %scalar.ph.preheader ]
  %prol.iter = phi i64 [ %prol.iter.next, %scalar.ph.prol ], [ 0, %scalar.ph.preheader ]
  %gep.prol = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv32.prol
  %23 = load i32, ptr %gep.prol, align 4, !tbaa !9
  %24 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv32.prol
  store i32 %23, ptr %24, align 4, !tbaa !9
  %indvars.iv.next33.prol = add nuw nsw i64 %indvars.iv32.prol, 1
  %prol.iter.next = add i64 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i64 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %scalar.ph.prol.loopexit, label %scalar.ph.prol, !llvm.loop !15

scalar.ph.prol.loopexit:                          ; preds = %scalar.ph.prol, %scalar.ph.preheader
  %indvars.iv32.unr = phi i64 [ %indvars.iv32.ph, %scalar.ph.preheader ], [ %indvars.iv.next33.prol, %scalar.ph.prol ]
  %25 = sub nsw i64 %indvars.iv32.ph, %wide.trip.count35
  %26 = icmp ugt i64 %25, -4
  br i1 %26, label %._crit_edge.us, label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.prol.loopexit, %scalar.ph
  %indvars.iv32 = phi i64 [ %indvars.iv.next33.3, %scalar.ph ], [ %indvars.iv32.unr, %scalar.ph.prol.loopexit ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv32
  %27 = load i32, ptr %gep, align 4, !tbaa !9
  %28 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv32
  store i32 %27, ptr %28, align 4, !tbaa !9
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %gep.1 = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.next33
  %29 = load i32, ptr %gep.1, align 4, !tbaa !9
  %30 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.next33
  store i32 %29, ptr %30, align 4, !tbaa !9
  %indvars.iv.next33.1 = add nuw nsw i64 %indvars.iv32, 2
  %gep.2 = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.next33.1
  %31 = load i32, ptr %gep.2, align 4, !tbaa !9
  %32 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.next33.1
  store i32 %31, ptr %32, align 4, !tbaa !9
  %indvars.iv.next33.2 = add nuw nsw i64 %indvars.iv32, 3
  %gep.3 = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv.next33.2
  %33 = load i32, ptr %gep.3, align 4, !tbaa !9
  %34 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv.next33.2
  store i32 %33, ptr %34, align 4, !tbaa !9
  %indvars.iv.next33.3 = add nuw nsw i64 %indvars.iv32, 4
  %exitcond36.not.3 = icmp eq i64 %indvars.iv.next33.3, %wide.trip.count35
  br i1 %exitcond36.not.3, label %._crit_edge.us, label %scalar.ph, !llvm.loop !17

._crit_edge.us:                                   ; preds = %scalar.ph.prol.loopexit, %scalar.ph, %middle.block
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  %exitcond41.not = icmp eq i64 %indvars.iv.next38, %wide.trip.count40
  br i1 %exitcond41.not, label %._crit_edge29, label %.preheader.us, !llvm.loop !18

35:                                               ; preds = %.lr.ph, %35
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %35 ]
  %36 = tail call noalias ptr @malloc(i64 noundef %10) #2
  %37 = getelementptr inbounds ptr, ptr %7, i64 %indvars.iv
  store ptr %36, ptr %37, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.preheader24, label %35, !llvm.loop !19

._crit_edge29:                                    ; preds = %._crit_edge.us, %3, %.preheader24
  %.sroa.2.0.insert.ext = zext i32 %1 to i64
  %.sroa.2.0.insert.shift = shl nuw i64 %.sroa.2.0.insert.ext, 32
  %.sroa.0.0.insert.ext = zext i32 %0 to i64
  %.sroa.0.0.insert.insert = or disjoint i64 %.sroa.2.0.insert.shift, %.sroa.0.0.insert.ext
  %.fca.0.insert = insertvalue { i64, ptr } poison, i64 %.sroa.0.0.insert.insert, 0
  %.fca.1.insert = insertvalue { i64, ptr } %.fca.0.insert, ptr %7, 1
  ret { i64, ptr } %.fca.1.insert
}

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

attributes #0 = { mustprogress nofree 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 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 #2 = { nounwind allocsize(0) }

!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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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, !16}
!16 = !{!"llvm.loop.unroll.disable"}
!17 = distinct !{!17, !12, !13}
!18 = distinct !{!18, !12}
!19 = distinct !{!19, !12}


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