; LLVM IR from next-step-for-interview-prep/Coding-Ninjas-DSA-course_code_and_notes_cpp :: Milestone-2=Intro_to_C++/questions and solutions/Lec-8=Arrays (Find_unique)

; 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 i32 @_Z10findUniquePii(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %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
  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 %10

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

14:                                               ; preds = %10
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %56

15:                                               ; preds = %10
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %49, %15
  %17 = load i32, ptr %8, align 4, !tbaa !9
  %18 = load i32, ptr %5, align 4, !tbaa !9
  %19 = icmp slt i32 %17, %18
  br i1 %19, label %21, label %20

20:                                               ; preds = %16
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  br label %52

21:                                               ; preds = %16
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = load i32, ptr %8, align 4, !tbaa !9
  %24 = icmp eq i32 %22, %23
  br i1 %24, label %25, label %26

25:                                               ; preds = %21
  br label %49

26:                                               ; preds = %21
  %27 = load ptr, ptr %4, align 8, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = load i32, ptr %8, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i32, ptr %32, i64 %34
  %36 = load i32, ptr %35, align 4, !tbaa !9
  %37 = icmp eq i32 %31, %36
  br i1 %37, label %38, label %47

38:                                               ; preds = %26
  %39 = load ptr, ptr %4, align 8, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = sext i32 %40 to i64
  %42 = getelementptr inbounds i32, ptr %39, i64 %41
  store i32 -1, ptr %42, align 4, !tbaa !9
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = load i32, ptr %8, align 4, !tbaa !9
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %43, i64 %45
  store i32 -1, ptr %46, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %38, %26
  br label %48

48:                                               ; preds = %47
  br label %49

49:                                               ; preds = %48, %25
  %50 = load i32, ptr %8, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %8, align 4, !tbaa !9
  br label %16, !llvm.loop !11

52:                                               ; preds = %20
  br label %53

53:                                               ; preds = %52
  %54 = load i32, ptr %6, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %6, align 4, !tbaa !9
  br label %10, !llvm.loop !13

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

57:                                               ; preds = %76, %56
  %58 = load i32, ptr %9, align 4, !tbaa !9
  %59 = load i32, ptr %5, align 4, !tbaa !9
  %60 = icmp slt i32 %58, %59
  br i1 %60, label %62, label %61

61:                                               ; preds = %57
  store i32 8, ptr %7, align 4
  br label %79

62:                                               ; preds = %57
  %63 = load ptr, ptr %4, align 8, !tbaa !5
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i32, ptr %63, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = icmp slt i32 %67, 0
  br i1 %68, label %69, label %70

69:                                               ; preds = %62
  br label %76

70:                                               ; preds = %62
  %71 = load ptr, ptr %4, align 8, !tbaa !5
  %72 = load i32, ptr %9, align 4, !tbaa !9
  %73 = sext i32 %72 to i64
  %74 = getelementptr inbounds i32, ptr %71, i64 %73
  %75 = load i32, ptr %74, align 4, !tbaa !9
  store i32 %75, ptr %3, align 4
  store i32 1, ptr %7, align 4
  br label %79

76:                                               ; preds = %69
  %77 = load i32, ptr %9, align 4, !tbaa !9
  %78 = add nsw i32 %77, 1
  store i32 %78, ptr %9, align 4, !tbaa !9
  br label %57, !llvm.loop !14

79:                                               ; preds = %70, %61
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  %80 = load i32, ptr %7, align 4
  switch i32 %80, label %84 [
    i32 8, label %81
    i32 1, label %82
  ]

81:                                               ; preds = %79
  unreachable

82:                                               ; preds = %79
  %83 = load i32, ptr %3, align 4
  ret i32 %83

84:                                               ; preds = %79
  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 memory(argmem: readwrite, inaccessiblemem: write) uwtable
define dso_local noundef i32 @_Z10findUniquePii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %.preheader28.us.preheader, label %.preheader

.preheader28.us.preheader:                        ; preds = %2
  %wide.trip.count35 = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count35, 1
  %4 = icmp eq i32 %1, 1
  %unroll_iter = and i64 %wide.trip.count35, 2147483646
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br label %.preheader28.us

.preheader28.us:                                  ; preds = %.preheader28.us.preheader, %._crit_edge.us
  %indvars.iv32 = phi i64 [ 0, %.preheader28.us.preheader ], [ %indvars.iv.next33, %._crit_edge.us ]
  %5 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv32
  br i1 %4, label %._crit_edge.us.unr-lcssa, label %.preheader28.us.new

.preheader28.us.new:                              ; preds = %.preheader28.us, %21
  %indvars.iv = phi i64 [ %indvars.iv.next.1, %21 ], [ 0, %.preheader28.us ]
  %niter = phi i64 [ %niter.next.1, %21 ], [ 0, %.preheader28.us ]
  %6 = icmp eq i64 %indvars.iv32, %indvars.iv
  br i1 %6, label %13, label %7

7:                                                ; preds = %.preheader28.us.new
  %8 = load i32, ptr %5, align 4, !tbaa !5
  %9 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = icmp eq i32 %8, %10
  br i1 %11, label %12, label %13

12:                                               ; preds = %7
  store i32 -1, ptr %5, align 4, !tbaa !5
  store i32 -1, ptr %9, align 4, !tbaa !5
  br label %13

13:                                               ; preds = %12, %7, %.preheader28.us.new
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %14 = icmp eq i64 %indvars.iv32, %indvars.iv.next
  br i1 %14, label %21, label %15

15:                                               ; preds = %13
  %16 = load i32, ptr %5, align 4, !tbaa !5
  %17 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp eq i32 %16, %18
  br i1 %19, label %20, label %21

20:                                               ; preds = %15
  store i32 -1, ptr %5, align 4, !tbaa !5
  store i32 -1, ptr %17, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %20, %15, %13
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.us.unr-lcssa, label %.preheader28.us.new, !llvm.loop !9

._crit_edge.us.unr-lcssa:                         ; preds = %21, %.preheader28.us
  %indvars.iv.unr = phi i64 [ 0, %.preheader28.us ], [ %indvars.iv.next.1, %21 ]
  %22 = icmp eq i64 %indvars.iv32, %indvars.iv.unr
  %or.cond = select i1 %lcmp.mod.not, i1 true, i1 %22
  br i1 %or.cond, label %._crit_edge.us, label %23

23:                                               ; preds = %._crit_edge.us.unr-lcssa
  %24 = load i32, ptr %5, align 4, !tbaa !5
  %25 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = icmp eq i32 %24, %26
  br i1 %27, label %28, label %._crit_edge.us

28:                                               ; preds = %23
  store i32 -1, ptr %5, align 4, !tbaa !5
  store i32 -1, ptr %25, align 4, !tbaa !5
  br label %._crit_edge.us

._crit_edge.us:                                   ; preds = %23, %28, %._crit_edge.us.unr-lcssa
  %indvars.iv.next33 = add nuw nsw i64 %indvars.iv32, 1
  %exitcond36.not = icmp eq i64 %indvars.iv.next33, %wide.trip.count35
  br i1 %exitcond36.not, label %.preheader, label %.preheader28.us, !llvm.loop !11

.preheader:                                       ; preds = %._crit_edge.us, %2
  %29 = sext i32 %1 to i64
  br label %30

30:                                               ; preds = %30, %.preheader
  %indvars.iv37 = phi i64 [ %indvars.iv.next38, %30 ], [ 0, %.preheader ]
  %31 = icmp slt i64 %indvars.iv37, %29
  tail call void @llvm.assume(i1 %31)
  %32 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv37
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = icmp slt i32 %33, 0
  %indvars.iv.next38 = add nuw nsw i64 %indvars.iv37, 1
  br i1 %34, label %30, label %35, !llvm.loop !12

35:                                               ; preds = %30
  ret i32 %33
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #1

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite, inaccessiblemem: write) 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(inaccessiblemem: write) }

!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}
!12 = distinct !{!12, !10}


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