; LLVM IR from next-step-for-interview-prep/Coding-Ninjas-DSA-course_code_and_notes_cpp :: Milestone-2=Intro_to_C++/questions and solutions/Test-2 (3-Leaders_in_array)

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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7LeadersPii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = zext i32 %11 to i64
  %13 = call ptr @llvm.stacksave.p0()
  store ptr %13, ptr %5, align 8
  %14 = alloca i32, i64 %12, align 16
  store i64 %12, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #5
  store i32 -2147483648, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  store i32 0, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %15 = load i32, ptr %4, align 4, !tbaa !9
  %16 = sub nsw i32 %15, 1
  store i32 %16, ptr %9, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %46, %2
  %18 = load i32, ptr %9, align 4, !tbaa !9
  %19 = icmp sge i32 %18, 0
  br i1 %19, label %21, label %20

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

21:                                               ; preds = %17
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %22, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = load i32, ptr %7, align 4, !tbaa !9
  %28 = icmp sge i32 %26, %27
  br i1 %28, label %29, label %45

29:                                               ; preds = %21
  %30 = load ptr, ptr %3, align 8, !tbaa !5
  %31 = load i32, ptr %9, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds i32, ptr %30, i64 %32
  %34 = load i32, ptr %33, align 4, !tbaa !9
  %35 = load i32, ptr %8, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %14, i64 %36
  store i32 %34, ptr %37, align 4, !tbaa !9
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = load i32, ptr %9, align 4, !tbaa !9
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds i32, ptr %38, i64 %40
  %42 = load i32, ptr %41, align 4, !tbaa !9
  store i32 %42, ptr %7, align 4, !tbaa !9
  %43 = load i32, ptr %8, align 4, !tbaa !9
  %44 = add nsw i32 %43, 1
  store i32 %44, ptr %8, align 4, !tbaa !9
  br label %45

45:                                               ; preds = %29, %21
  br label %46

46:                                               ; preds = %45
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = add nsw i32 %47, -1
  store i32 %48, ptr %9, align 4, !tbaa !9
  br label %17, !llvm.loop !11

49:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = sub nsw i32 %50, 1
  store i32 %51, ptr %10, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %63, %49
  %53 = load i32, ptr %10, align 4, !tbaa !9
  %54 = icmp sge i32 %53, 0
  br i1 %54, label %56, label %55

55:                                               ; preds = %52
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %66

56:                                               ; preds = %52
  %57 = load i32, ptr %10, align 4, !tbaa !9
  %58 = sext i32 %57 to i64
  %59 = getelementptr inbounds i32, ptr %14, i64 %58
  %60 = load i32, ptr %59, align 4, !tbaa !9
  %61 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %60)
  %62 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %61, ptr noundef @.str)
  br label %63

63:                                               ; preds = %56
  %64 = load i32, ptr %10, align 4, !tbaa !9
  %65 = add nsw i32 %64, -1
  store i32 %65, ptr %10, align 4, !tbaa !9
  br label %52, !llvm.loop !13

66:                                               ; preds = %55
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #5
  %67 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %67)
  ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #1

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

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

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #3

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #4

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #1

attributes #0 = { mustprogress 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 }
attributes #2 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { inlinehint mustprogress 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 #4 = { "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 #5 = { 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}


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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_Z7LeadersPii(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i32 %1 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = icmp sgt i32 %1, 0
  br i1 %5, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %xtraiter = and i64 %3, 1
  %6 = icmp eq i32 %1, 1
  br i1 %6, label %.preheader.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %3, 2147483646
  %invariant.gep = getelementptr i8, ptr %0, i64 -4
  br label %.lr.ph

.preheader.unr-lcssa:                             ; preds = %27, %.lr.ph.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %27 ]
  %indvars.iv.unr = phi i64 [ %3, %.lr.ph.preheader ], [ %indvars.iv.next.1, %27 ]
  %.01824.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.1.1, %27 ]
  %.01923.unr = phi i32 [ -2147483648, %.lr.ph.preheader ], [ %.120.1, %27 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %.preheader.unr-lcssa
  %7 = getelementptr i32, ptr %0, i64 %indvars.iv.unr
  %8 = getelementptr i8, ptr %7, i64 -4
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not.epil = icmp slt i32 %9, %.01923.unr
  br i1 %.not.epil, label %.preheader, label %10

10:                                               ; preds = %.lr.ph.epil
  %11 = sext i32 %.01824.unr to i64
  %12 = getelementptr inbounds i32, ptr %4, i64 %11
  store i32 %9, ptr %12, align 4, !tbaa !5
  %13 = add nsw i32 %.01824.unr, 1
  br label %.preheader

.preheader:                                       ; preds = %.lr.ph.epil, %10, %.preheader.unr-lcssa
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %.preheader.unr-lcssa ], [ %13, %10 ], [ %.01824.unr, %.lr.ph.epil ]
  %14 = icmp sgt i32 %.1.lcssa, 0
  br i1 %14, label %.lr.ph27.preheader, label %._crit_edge

.lr.ph27.preheader:                               ; preds = %.preheader
  %15 = zext nneg i32 %.1.lcssa to i64
  br label %.lr.ph27

.lr.ph:                                           ; preds = %27, %.lr.ph.preheader.new
  %indvars.iv = phi i64 [ %3, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %27 ]
  %.01824 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %27 ]
  %.01923 = phi i32 [ -2147483648, %.lr.ph.preheader.new ], [ %.120.1, %27 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %27 ]
  %gep = getelementptr i32, ptr %invariant.gep, i64 %indvars.iv
  %16 = load i32, ptr %gep, align 4, !tbaa !5
  %.not = icmp slt i32 %16, %.01923
  br i1 %.not, label %.lr.ph.1, label %17

17:                                               ; preds = %.lr.ph
  %18 = sext i32 %.01824 to i64
  %19 = getelementptr inbounds i32, ptr %4, i64 %18
  store i32 %16, ptr %19, align 4, !tbaa !5
  %20 = add nsw i32 %.01824, 1
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %17
  %.120 = phi i32 [ %16, %17 ], [ %.01923, %.lr.ph ]
  %.1 = phi i32 [ %20, %17 ], [ %.01824, %.lr.ph ]
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, -2
  %21 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %.not.1 = icmp slt i32 %22, %.120
  br i1 %.not.1, label %27, label %23

23:                                               ; preds = %.lr.ph.1
  %24 = sext i32 %.1 to i64
  %25 = getelementptr inbounds i32, ptr %4, i64 %24
  store i32 %22, ptr %25, align 4, !tbaa !5
  %26 = add nsw i32 %.1, 1
  br label %27

27:                                               ; preds = %23, %.lr.ph.1
  %.120.1 = phi i32 [ %22, %23 ], [ %.120, %.lr.ph.1 ]
  %.1.1 = phi i32 [ %26, %23 ], [ %.1, %.lr.ph.1 ]
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1.not = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1.not, label %.preheader.unr-lcssa, label %.lr.ph, !llvm.loop !9

._crit_edge:                                      ; preds = %.lr.ph27, %2, %.preheader
  ret void

.lr.ph27:                                         ; preds = %.lr.ph27.preheader, %.lr.ph27
  %indvars.iv29 = phi i64 [ %15, %.lr.ph27.preheader ], [ %indvars.iv.next30, %.lr.ph27 ]
  %indvars.iv.next30 = add nsw i64 %indvars.iv29, -1
  %28 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv.next30
  %29 = load i32, ptr %28, align 4, !tbaa !5
  %30 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %29)
  %31 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %30, ptr noundef nonnull @.str)
  %32 = icmp ugt i64 %indvars.iv29, 1
  br i1 %32, label %.lr.ph27, label %._crit_edge, !llvm.loop !11
}

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #1

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #2

attributes #0 = { mustprogress 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 = { inlinehint mustprogress 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 = { "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" }

!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}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}


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