; LLVM IR from JessonYue/LeetCodeLearning :: Array/zhangjunwei/6.28-7.4 (numOfSubarrays)

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

20:                                               ; preds = %56, %4
  %21 = load i32, ptr %11, align 4, !tbaa !9
  %22 = load i32, ptr %10, align 4, !tbaa !9
  %23 = icmp slt i32 %21, %22
  br i1 %23, label %25, label %24

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

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #2
  %26 = load i32, ptr %11, align 4, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = add nsw i32 %26, %27
  store i32 %28, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #2
  store i32 0, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #2
  %29 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %29, ptr %15, align 4, !tbaa !9
  br label %30

30:                                               ; preds = %43, %25
  %31 = load i32, ptr %15, align 4, !tbaa !9
  %32 = load i32, ptr %13, align 4, !tbaa !9
  %33 = icmp slt i32 %31, %32
  br i1 %33, label %35, label %34

34:                                               ; preds = %30
  store i32 5, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #2
  br label %46

35:                                               ; preds = %30
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = load i32, ptr %15, align 4, !tbaa !9
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i32, ptr %36, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !9
  %41 = load i32, ptr %14, align 4, !tbaa !9
  %42 = add nsw i32 %41, %40
  store i32 %42, ptr %14, align 4, !tbaa !9
  br label %43

43:                                               ; preds = %35
  %44 = load i32, ptr %15, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr %15, align 4, !tbaa !9
  br label %30, !llvm.loop !11

46:                                               ; preds = %34
  %47 = load i32, ptr %14, align 4, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = load i32, ptr %8, align 4, !tbaa !9
  %50 = mul nsw i32 %48, %49
  %51 = icmp sge i32 %47, %50
  br i1 %51, label %52, label %55

52:                                               ; preds = %46
  %53 = load i32, ptr %9, align 4, !tbaa !9
  %54 = add nsw i32 %53, 1
  store i32 %54, ptr %9, align 4, !tbaa !9
  br label %55

55:                                               ; preds = %52, %46
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #2
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %11, align 4, !tbaa !9
  br label %20, !llvm.loop !13

59:                                               ; preds = %24
  %60 = load i32, ptr %9, align 4, !tbaa !9
  store i32 1, ptr %12, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  ret i32 %60
}

; 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

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 = { 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 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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 i32 @numOfSubarrays1(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #0 {
  %.not24 = icmp slt i32 %1, %2
  br i1 %.not24, label %._crit_edge29, label %.lr.ph28

.lr.ph28:                                         ; preds = %4
  %5 = mul nsw i32 %3, %2
  %6 = add i32 %1, 1
  %7 = sub i32 %6, %2
  %wide.trip.count = zext i32 %7 to i64
  %8 = icmp sgt i32 %2, 0
  br label %9

._crit_edge29:                                    ; preds = %._crit_edge, %4
  %.019.lcssa = phi i32 [ 0, %4 ], [ %spec.select, %._crit_edge ]
  ret i32 %.019.lcssa

9:                                                ; preds = %.lr.ph28, %._crit_edge
  %indvars.iv = phi i64 [ 0, %.lr.ph28 ], [ %indvars.iv.next, %._crit_edge ]
  %.01926 = phi i32 [ 0, %.lr.ph28 ], [ %spec.select, %._crit_edge ]
  %10 = trunc i64 %indvars.iv to i32
  %11 = add i32 %10, %2
  %12 = trunc i64 %indvars.iv to i32
  %13 = add i32 %12, 1
  %smax = tail call i32 @llvm.smax.i32(i32 %11, i32 %13)
  %14 = trunc i64 %indvars.iv to i32
  %15 = xor i32 %14, -1
  %16 = add i32 %smax, %15
  %17 = zext i32 %16 to i64
  %18 = add nuw nsw i64 %17, 1
  %indvars34 = trunc i64 %indvars.iv to i32
  %19 = add nsw i32 %indvars34, %2
  br i1 %8, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %9
  %min.iters.check = icmp ult i32 %16, 7
  br i1 %min.iters.check, label %.lr.ph.preheader37, label %vector.ph

.lr.ph.preheader37:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv31.ph = phi i64 [ %indvars.iv, %.lr.ph.preheader ], [ %ind.end, %middle.block ]
  %.01822.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %26, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %18, 8589934584
  %ind.end = add nuw i64 %indvars.iv, %n.vec
  %20 = getelementptr i32, ptr %0, i64 %indvars.iv
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %23, %vector.body ]
  %vec.phi35 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %24, %vector.body ]
  %21 = getelementptr i32, ptr %20, i64 %index
  %22 = getelementptr inbounds i8, ptr %21, i64 16
  %wide.load = load <4 x i32>, ptr %21, align 4, !tbaa !5
  %wide.load36 = load <4 x i32>, ptr %22, align 4, !tbaa !5
  %23 = add <4 x i32> %wide.load, %vec.phi
  %24 = add <4 x i32> %wide.load36, %vec.phi35
  %index.next = add nuw i64 %index, 8
  %25 = icmp eq i64 %index.next, %n.vec
  br i1 %25, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %24, %23
  %26 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %18, %n.vec
  br i1 %cmp.n, label %._crit_edge, label %.lr.ph.preheader37

._crit_edge:                                      ; preds = %.lr.ph, %middle.block, %9
  %.018.lcssa = phi i32 [ 0, %9 ], [ %26, %middle.block ], [ %30, %.lr.ph ]
  %.not21 = icmp sge i32 %.018.lcssa, %5
  %27 = zext i1 %.not21 to i32
  %spec.select = add nuw nsw i32 %.01926, %27
  %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 %._crit_edge29, label %9, !llvm.loop !13

.lr.ph:                                           ; preds = %.lr.ph.preheader37, %.lr.ph
  %indvars.iv31 = phi i64 [ %indvars.iv.next32, %.lr.ph ], [ %indvars.iv31.ph, %.lr.ph.preheader37 ]
  %.01822 = phi i32 [ %30, %.lr.ph ], [ %.01822.ph, %.lr.ph.preheader37 ]
  %28 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv31
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = add nsw i32 %29, %.01822
  %indvars.iv.next32 = add nuw nsw i64 %indvars.iv31, 1
  %31 = trunc nuw i64 %indvars.iv.next32 to i32
  %32 = icmp sgt i32 %19, %31
  br i1 %32, label %.lr.ph, label %._crit_edge, !llvm.loop !14
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #1

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" }
attributes #1 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10, !12, !11}


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