; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/2962_count-subarrays-where-max-element-appears-at-least-k-times/2962 (countSubarrays)

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

13:                                               ; preds = %36, %3
  %14 = load i32, ptr %11, align 4, !tbaa !9
  %15 = load i32, ptr %5, align 4, !tbaa !9
  %16 = icmp slt i32 %14, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %13
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %39

18:                                               ; preds = %13
  %19 = load i32, ptr %8, align 4, !tbaa !9
  %20 = load ptr, ptr %4, align 8, !tbaa !5
  %21 = load i32, ptr %11, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = getelementptr inbounds i32, ptr %20, i64 %22
  %24 = load i32, ptr %23, align 4, !tbaa !9
  %25 = icmp sgt i32 %19, %24
  br i1 %25, label %26, label %28

26:                                               ; preds = %18
  %27 = load i32, ptr %8, align 4, !tbaa !9
  br label %34

28:                                               ; preds = %18
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %29, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %28, %26
  %35 = phi i32 [ %27, %26 ], [ %33, %28 ]
  store i32 %35, ptr %8, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %34
  %37 = load i32, ptr %11, align 4, !tbaa !9
  %38 = add nsw i32 %37, 1
  store i32 %38, ptr %11, align 4, !tbaa !9
  br label %13, !llvm.loop !13

39:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  store i32 0, ptr %12, align 4, !tbaa !9
  br label %40

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

44:                                               ; preds = %40
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  br label %84

45:                                               ; preds = %40
  %46 = load ptr, ptr %4, align 8, !tbaa !5
  %47 = load i32, ptr %12, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = load i32, ptr %8, align 4, !tbaa !9
  %52 = icmp eq i32 %50, %51
  %53 = zext i1 %52 to i64
  %54 = select i1 %52, i32 1, i32 0
  %55 = load i32, ptr %9, align 4, !tbaa !9
  %56 = add nsw i32 %55, %54
  store i32 %56, ptr %9, align 4, !tbaa !9
  br label %57

57:                                               ; preds = %61, %45
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = icmp sge i32 %58, %59
  br i1 %60, label %61, label %80

61:                                               ; preds = %57
  %62 = load i32, ptr %5, align 4, !tbaa !9
  %63 = load i32, ptr %12, align 4, !tbaa !9
  %64 = sub nsw i32 %62, %63
  %65 = sext i32 %64 to i64
  %66 = load i64, ptr %10, align 8, !tbaa !11
  %67 = add nsw i64 %66, %65
  store i64 %67, ptr %10, align 8, !tbaa !11
  %68 = load ptr, ptr %4, align 8, !tbaa !5
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = add nsw i32 %69, 1
  store i32 %70, ptr %7, align 4, !tbaa !9
  %71 = sext i32 %69 to i64
  %72 = getelementptr inbounds i32, ptr %68, i64 %71
  %73 = load i32, ptr %72, align 4, !tbaa !9
  %74 = load i32, ptr %8, align 4, !tbaa !9
  %75 = icmp eq i32 %73, %74
  %76 = zext i1 %75 to i64
  %77 = select i1 %75, i32 1, i32 0
  %78 = load i32, ptr %9, align 4, !tbaa !9
  %79 = sub nsw i32 %78, %77
  store i32 %79, ptr %9, align 4, !tbaa !9
  br label %57, !llvm.loop !15

80:                                               ; preds = %57
  br label %81

81:                                               ; preds = %80
  %82 = load i32, ptr %12, align 4, !tbaa !9
  %83 = add nsw i32 %82, 1
  store i32 %83, ptr %12, align 4, !tbaa !9
  br label %40, !llvm.loop !16

84:                                               ; preds = %44
  %85 = load i64, ptr %10, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  ret i64 %85
}

; 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 = !{!12, !12, i64 0}
!12 = !{!"long long", !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: nofree norecurse nosync nounwind memory(argmem: read) uwtable
define dso_local i64 @countSubarrays(ptr nocapture noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp sgt i32 %1, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge49

.lr.ph.preheader:                                 ; preds = %3
  %wide.trip.count = zext nneg i32 %1 to i64
  %min.iters.check = icmp ult i32 %1, 8
  br i1 %min.iters.check, label %.lr.ph.preheader68, label %vector.ph

.lr.ph.preheader68:                               ; preds = %middle.block, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec, %middle.block ]
  %.02934.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %10, %middle.block ]
  br label %.lr.ph

vector.ph:                                        ; preds = %.lr.ph.preheader
  %n.vec = and i64 %wide.trip.count, 2147483640
  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 ], [ %7, %vector.body ]
  %vec.phi65 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %8, %vector.body ]
  %5 = getelementptr inbounds i32, ptr %0, i64 %index
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  %wide.load = load <4 x i32>, ptr %5, align 4, !tbaa !5
  %wide.load66 = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %7 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi, <4 x i32> %wide.load)
  %8 = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %vec.phi65, <4 x i32> %wide.load66)
  %index.next = add nuw i64 %index, 8
  %9 = icmp eq i64 %index.next, %n.vec
  br i1 %9, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %rdx.minmax = tail call <4 x i32> @llvm.smax.v4i32(<4 x i32> %7, <4 x i32> %8)
  %10 = tail call i32 @llvm.vector.reduce.smax.v4i32(<4 x i32> %rdx.minmax)
  %cmp.n = icmp eq i64 %n.vec, %wide.trip.count
  br i1 %cmp.n, label %.preheader, label %.lr.ph.preheader68

.preheader:                                       ; preds = %.lr.ph, %middle.block
  %.029..lcssa = phi i32 [ %10, %middle.block ], [ %.029., %.lr.ph ]
  br i1 %4, label %.lr.ph48.preheader, label %._crit_edge49

.lr.ph48.preheader:                               ; preds = %.preheader
  %11 = zext nneg i32 %1 to i64
  %wide.trip.count60 = zext nneg i32 %1 to i64
  br label %.lr.ph48

.lr.ph:                                           ; preds = %.lr.ph.preheader68, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader68 ]
  %.02934 = phi i32 [ %.029., %.lr.ph ], [ %.02934.ph, %.lr.ph.preheader68 ]
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %.029. = tail call i32 @llvm.smax.i32(i32 %.02934, i32 %13)
  %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 %.preheader, label %.lr.ph, !llvm.loop !13

._crit_edge49:                                    ; preds = %._crit_edge, %3, %.preheader
  %.026.lcssa = phi i64 [ 0, %.preheader ], [ 0, %3 ], [ %.1.lcssa, %._crit_edge ]
  ret i64 %.026.lcssa

.lr.ph48:                                         ; preds = %.lr.ph48.preheader, %._crit_edge
  %indvars.iv57 = phi i64 [ 0, %.lr.ph48.preheader ], [ %indvars.iv.next58, %._crit_edge ]
  %.02646 = phi i64 [ 0, %.lr.ph48.preheader ], [ %.1.lcssa, %._crit_edge ]
  %.02745 = phi i32 [ 0, %.lr.ph48.preheader ], [ %.128.lcssa, %._crit_edge ]
  %.03044 = phi i32 [ 0, %.lr.ph48.preheader ], [ %.131.lcssa, %._crit_edge ]
  %14 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv57
  %15 = load i32, ptr %14, align 4, !tbaa !5
  %16 = icmp eq i32 %15, %.029..lcssa
  %17 = zext i1 %16 to i32
  %18 = add nsw i32 %.02745, %17
  %.not36 = icmp slt i32 %18, %2
  br i1 %.not36, label %._crit_edge, label %.lr.ph40

.lr.ph40:                                         ; preds = %.lr.ph48
  %19 = sub nsw i64 %11, %indvars.iv57
  %20 = sext i32 %.03044 to i64
  br label %21

21:                                               ; preds = %.lr.ph40, %21
  %indvars.iv54 = phi i64 [ %20, %.lr.ph40 ], [ %indvars.iv.next55, %21 ]
  %.139 = phi i64 [ %.02646, %.lr.ph40 ], [ %22, %21 ]
  %.12838 = phi i32 [ %18, %.lr.ph40 ], [ %26, %21 ]
  %22 = add nsw i64 %.139, %19
  %indvars.iv.next55 = add nsw i64 %indvars.iv54, 1
  %23 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv54
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = icmp eq i32 %24, %.029..lcssa
  %.neg = sext i1 %25 to i32
  %26 = add i32 %.12838, %.neg
  %.not = icmp slt i32 %26, %2
  br i1 %.not, label %._crit_edge.loopexit, label %21, !llvm.loop !14

._crit_edge.loopexit:                             ; preds = %21
  %27 = trunc nsw i64 %indvars.iv.next55 to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %.lr.ph48
  %.131.lcssa = phi i32 [ %.03044, %.lr.ph48 ], [ %27, %._crit_edge.loopexit ]
  %.128.lcssa = phi i32 [ %18, %.lr.ph48 ], [ %26, %._crit_edge.loopexit ]
  %.1.lcssa = phi i64 [ %.02646, %.lr.ph48 ], [ %22, %._crit_edge.loopexit ]
  %indvars.iv.next58 = add nuw nsw i64 %indvars.iv57, 1
  %exitcond61.not = icmp eq i64 %indvars.iv.next58, %wide.trip.count60
  br i1 %exitcond61.not, label %._crit_edge49, label %.lr.ph48, !llvm.loop !15
}

; 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 <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>) #1

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.smax.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, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !10}


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