; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 05. Arrays/27 Find Equlibrium Point/27a Find Equlibrium Point.ll (Z13isEquilibriumPii)

; 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: mustprogress nounwind uwtable
define dso_local noundef zeroext i1 @_Z13isEquilibriumPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i1, align 1
  %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 i32, align 4
  %11 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %12

12:                                               ; preds = %62, %2
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr %5, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  store i32 2, ptr %7, align 4
  br label %65

17:                                               ; preds = %12
  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
  br label %18

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

22:                                               ; preds = %18
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %34

23:                                               ; preds = %18
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = load i32, ptr %9, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %24, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !9
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = add nsw i32 %29, %28
  store i32 %30, ptr %8, align 4, !tbaa !9
  br label %31

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

34:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #2
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %11, align 4, !tbaa !9
  br label %37

37:                                               ; preds = %50, %34
  %38 = load i32, ptr %11, align 4, !tbaa !9
  %39 = load i32, ptr %5, align 4, !tbaa !9
  %40 = icmp slt i32 %38, %39
  br i1 %40, label %42, label %41

41:                                               ; preds = %37
  store i32 8, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  br label %53

42:                                               ; preds = %37
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i32, ptr %11, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %43, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !9
  %48 = load i32, ptr %10, align 4, !tbaa !9
  %49 = add nsw i32 %48, %47
  store i32 %49, ptr %10, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %42
  %51 = load i32, ptr %11, align 4, !tbaa !9
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %11, align 4, !tbaa !9
  br label %37, !llvm.loop !13

53:                                               ; preds = %41
  %54 = load i32, ptr %8, align 4, !tbaa !9
  %55 = load i32, ptr %10, align 4, !tbaa !9
  %56 = icmp eq i32 %54, %55
  br i1 %56, label %57, label %58

57:                                               ; preds = %53
  store i1 true, ptr %3, align 1
  store i32 1, ptr %7, align 4
  br label %59

58:                                               ; preds = %53
  store i32 0, ptr %7, align 4
  br label %59

59:                                               ; preds = %58, %57
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  %60 = load i32, ptr %7, align 4
  switch i32 %60, label %65 [
    i32 0, label %61
  ]

61:                                               ; preds = %59
  br label %62

62:                                               ; preds = %61
  %63 = load i32, ptr %6, align 4, !tbaa !9
  %64 = add nsw i32 %63, 1
  store i32 %64, ptr %6, align 4, !tbaa !9
  br label %12, !llvm.loop !14

65:                                               ; preds = %59, %16
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %66 = load i32, ptr %7, align 4
  switch i32 %66, label %70 [
    i32 2, label %67
    i32 1, label %68
  ]

67:                                               ; preds = %65
  store i1 false, ptr %3, align 1
  br label %68

68:                                               ; preds = %67, %65
  %69 = load i1, ptr %3, align 1
  ret i1 %69

70:                                               ; preds = %65
  unreachable
}

; 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 = { 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 }

!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}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !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: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local noundef zeroext i1 @_Z13isEquilibriumPii(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %smax = tail call i32 @llvm.smax.i32(i32 %1, i32 0)
  %wide.trip.count50 = zext nneg i32 %smax to i64
  %wide.trip.count45 = zext i32 %1 to i64
  br label %3

3:                                                ; preds = %._crit_edge35, %2
  %indvars.iv47 = phi i64 [ %indvars.iv.next48, %._crit_edge35 ], [ 0, %2 ]
  %indvars.iv40 = phi i64 [ %indvars.iv.next41, %._crit_edge35 ], [ 1, %2 ]
  %4 = xor i64 %indvars.iv47, -1
  %5 = add nsw i64 %4, %wide.trip.count45
  %exitcond51.not = icmp eq i64 %indvars.iv47, %wide.trip.count50
  br i1 %exitcond51.not, label %27, label %.preheader

.preheader:                                       ; preds = %3
  %.not37 = icmp eq i64 %indvars.iv47, 0
  br i1 %.not37, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %min.iters.check58 = icmp ult i64 %indvars.iv47, 8
  br i1 %min.iters.check58, label %.lr.ph.preheader74, label %vector.ph59

.lr.ph.preheader74:                               ; preds = %middle.block56, %.lr.ph.preheader
  %indvars.iv.ph = phi i64 [ 0, %.lr.ph.preheader ], [ %n.vec61, %middle.block56 ]
  %.02229.ph = phi i32 [ 0, %.lr.ph.preheader ], [ %11, %middle.block56 ]
  br label %.lr.ph

vector.ph59:                                      ; preds = %.lr.ph.preheader
  %n.vec61 = and i64 %indvars.iv47, 9223372036854775800
  br label %vector.body63

vector.body63:                                    ; preds = %vector.body63, %vector.ph59
  %index64 = phi i64 [ 0, %vector.ph59 ], [ %index.next69, %vector.body63 ]
  %vec.phi65 = phi <4 x i32> [ zeroinitializer, %vector.ph59 ], [ %8, %vector.body63 ]
  %vec.phi66 = phi <4 x i32> [ zeroinitializer, %vector.ph59 ], [ %9, %vector.body63 ]
  %6 = getelementptr inbounds i32, ptr %0, i64 %index64
  %7 = getelementptr inbounds i8, ptr %6, i64 16
  %wide.load67 = load <4 x i32>, ptr %6, align 4, !tbaa !5
  %wide.load68 = load <4 x i32>, ptr %7, align 4, !tbaa !5
  %8 = add <4 x i32> %wide.load67, %vec.phi65
  %9 = add <4 x i32> %wide.load68, %vec.phi66
  %index.next69 = add nuw i64 %index64, 8
  %10 = icmp eq i64 %index.next69, %n.vec61
  br i1 %10, label %middle.block56, label %vector.body63, !llvm.loop !9

middle.block56:                                   ; preds = %vector.body63
  %bin.rdx70 = add <4 x i32> %9, %8
  %11 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx70)
  %cmp.n71 = icmp eq i64 %indvars.iv47, %n.vec61
  br i1 %cmp.n71, label %._crit_edge, label %.lr.ph.preheader74

._crit_edge:                                      ; preds = %.lr.ph, %middle.block56, %.preheader
  %.022.lcssa = phi i32 [ 0, %.preheader ], [ %11, %middle.block56 ], [ %23, %.lr.ph ]
  %indvars.iv.next48 = add nuw nsw i64 %indvars.iv47, 1
  %12 = trunc nuw i64 %indvars.iv.next48 to i32
  %13 = icmp slt i32 %12, %1
  br i1 %13, label %.lr.ph34.preheader, label %._crit_edge35

.lr.ph34.preheader:                               ; preds = %._crit_edge
  %min.iters.check = icmp ult i64 %5, 8
  br i1 %min.iters.check, label %.lr.ph34.preheader73, label %vector.ph

.lr.ph34.preheader73:                             ; preds = %middle.block, %.lr.ph34.preheader
  %indvars.iv42.ph = phi i64 [ %indvars.iv40, %.lr.ph34.preheader ], [ %ind.end, %middle.block ]
  %.02031.ph = phi i32 [ 0, %.lr.ph34.preheader ], [ %20, %middle.block ]
  br label %.lr.ph34

vector.ph:                                        ; preds = %.lr.ph34.preheader
  %n.vec = and i64 %5, -8
  %ind.end = add i64 %indvars.iv40, %n.vec
  %14 = getelementptr i32, ptr %0, i64 %indvars.iv40
  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 ], [ %17, %vector.body ]
  %vec.phi54 = phi <4 x i32> [ zeroinitializer, %vector.ph ], [ %18, %vector.body ]
  %15 = getelementptr i32, ptr %14, i64 %index
  %16 = getelementptr inbounds i8, ptr %15, i64 16
  %wide.load = load <4 x i32>, ptr %15, align 4, !tbaa !5
  %wide.load55 = load <4 x i32>, ptr %16, align 4, !tbaa !5
  %17 = add <4 x i32> %wide.load, %vec.phi
  %18 = add <4 x i32> %wide.load55, %vec.phi54
  %index.next = add nuw i64 %index, 8
  %19 = icmp eq i64 %index.next, %n.vec
  br i1 %19, label %middle.block, label %vector.body, !llvm.loop !13

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %18, %17
  %20 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  %cmp.n = icmp eq i64 %5, %n.vec
  br i1 %cmp.n, label %._crit_edge35, label %.lr.ph34.preheader73

.lr.ph:                                           ; preds = %.lr.ph.preheader74, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ %indvars.iv.ph, %.lr.ph.preheader74 ]
  %.02229 = phi i32 [ %23, %.lr.ph ], [ %.02229.ph, %.lr.ph.preheader74 ]
  %21 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = add nsw i32 %22, %.02229
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv47
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge35:                                    ; preds = %.lr.ph34, %middle.block, %._crit_edge
  %.020.lcssa = phi i32 [ 0, %._crit_edge ], [ %20, %middle.block ], [ %26, %.lr.ph34 ]
  %.not = icmp eq i32 %.022.lcssa, %.020.lcssa
  %indvars.iv.next41 = add nuw nsw i64 %indvars.iv40, 1
  br i1 %.not, label %27, label %3, !llvm.loop !15

.lr.ph34:                                         ; preds = %.lr.ph34.preheader73, %.lr.ph34
  %indvars.iv42 = phi i64 [ %indvars.iv.next43, %.lr.ph34 ], [ %indvars.iv42.ph, %.lr.ph34.preheader73 ]
  %.02031 = phi i32 [ %26, %.lr.ph34 ], [ %.02031.ph, %.lr.ph34.preheader73 ]
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv42
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = add nsw i32 %25, %.02031
  %indvars.iv.next43 = add nuw nsw i64 %indvars.iv42, 1
  %exitcond46.not = icmp eq i64 %indvars.iv.next43, %wide.trip.count45
  br i1 %exitcond46.not, label %._crit_edge35, label %.lr.ph34, !llvm.loop !16

27:                                               ; preds = %3, %._crit_edge35
  %28 = sext i32 %1 to i64
  %.not28 = icmp slt i64 %indvars.iv47, %28
  ret i1 %.not28
}

; 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 = { mustprogress nofree norecurse nosync nounwind willreturn 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++ 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, !11, !12}
!14 = distinct !{!14, !10, !12, !11}
!15 = distinct !{!15, !10}
!16 = distinct !{!16, !10, !12, !11}


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