; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: Arrays/remove_duplicates_from_sorted_array.ll (Z16remove_duplicatePii)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; 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.end.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z16remove_duplicatePii(ptr noundef %0, i32 noundef %1) #2 {
  %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
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = zext i32 %10 to i64
  %12 = call ptr @llvm.stacksave.p0()
  store ptr %12, ptr %5, align 8
  %13 = alloca i32, i64 %11, align 16
  store i64 %11, ptr %6, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 0, ptr %8, align 4, !tbaa !9
  br label %14

14:                                               ; preds = %44, %2
  %15 = load i32, ptr %8, align 4, !tbaa !9
  %16 = load i32, ptr %4, align 4, !tbaa !9
  %17 = sub nsw i32 %16, 1
  %18 = icmp slt i32 %15, %17
  br i1 %18, label %20, label %19

19:                                               ; preds = %14
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %47

20:                                               ; preds = %14
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i32, ptr %8, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds i32, ptr %21, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !9
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = load i32, ptr %8, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %26, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = icmp ne i32 %25, %31
  br i1 %32, label %33, label %43

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

43:                                               ; preds = %33, %20
  br label %44

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

47:                                               ; preds = %19
  %48 = load ptr, ptr %3, align 8, !tbaa !5
  %49 = load i32, ptr %4, align 4, !tbaa !9
  %50 = sub nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds i32, ptr %48, i64 %51
  %53 = load i32, ptr %52, align 4, !tbaa !9
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %7, align 4, !tbaa !9
  %56 = sext i32 %54 to i64
  %57 = getelementptr inbounds i32, ptr %13, i64 %56
  store i32 %53, ptr %57, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  store i32 0, ptr %9, align 4, !tbaa !9
  br label %58

58:                                               ; preds = %72, %47
  %59 = load i32, ptr %9, align 4, !tbaa !9
  %60 = load i32, ptr %7, align 4, !tbaa !9
  %61 = icmp slt i32 %59, %60
  br i1 %61, label %63, label %62

62:                                               ; preds = %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  br label %75

63:                                               ; preds = %58
  %64 = load i32, ptr %9, align 4, !tbaa !9
  %65 = sext i32 %64 to i64
  %66 = getelementptr inbounds i32, ptr %13, i64 %65
  %67 = load i32, ptr %66, align 4, !tbaa !9
  %68 = load ptr, ptr %3, align 8, !tbaa !5
  %69 = load i32, ptr %9, align 4, !tbaa !9
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds i32, ptr %68, i64 %70
  store i32 %67, ptr %71, align 4, !tbaa !9
  br label %72

72:                                               ; preds = %63
  %73 = load i32, ptr %9, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  store i32 %74, ptr %9, align 4, !tbaa !9
  br label %58, !llvm.loop !13

75:                                               ; preds = %62
  %76 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  %77 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %77)
  ret i32 %76
}

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

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

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local noundef range(i32 -2147483647, -2147483648) i32 @_Z16remove_duplicatePii(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i32 %1 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = add i32 %1, -1
  %6 = icmp sgt i32 %1, 1
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %2
  %7 = sext i32 %5 to i64
  %8 = getelementptr inbounds i32, ptr %0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  store i32 %9, ptr %4, align 16, !tbaa !5
  br label %.lr.ph33.preheader

.lr.ph.preheader:                                 ; preds = %2
  %wide.trip.count = zext i32 %5 to i64
  %.pre = load i32, ptr %0, align 4, !tbaa !5
  %xtraiter = and i64 %wide.trip.count, 1
  %10 = icmp eq i32 %5, 1
  br i1 %10, label %._crit_edge.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %wide.trip.count, 4294967294
  br label %.lr.ph

._crit_edge.unr-lcssa.loopexit:                   ; preds = %39
  %11 = add nuw nsw i64 %indvars.iv, 3
  br label %._crit_edge.unr-lcssa

._crit_edge.unr-lcssa:                            ; preds = %._crit_edge.unr-lcssa.loopexit, %.lr.ph.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.unr-lcssa.loopexit ]
  %.unr = phi i32 [ %.pre, %.lr.ph.preheader ], [ %34, %._crit_edge.unr-lcssa.loopexit ]
  %indvars.iv.unr = phi i64 [ 1, %.lr.ph.preheader ], [ %11, %._crit_edge.unr-lcssa.loopexit ]
  %.02227.unr = phi i32 [ 0, %.lr.ph.preheader ], [ %.1.1, %._crit_edge.unr-lcssa.loopexit ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.unr-lcssa
  %12 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %.not26.epil = icmp eq i32 %.unr, %13
  br i1 %.not26.epil, label %._crit_edge, label %14

14:                                               ; preds = %.lr.ph.epil
  %15 = add nsw i32 %.02227.unr, 1
  %16 = sext i32 %.02227.unr to i64
  %17 = getelementptr inbounds i32, ptr %4, i64 %16
  store i32 %.unr, ptr %17, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %14, %._crit_edge.unr-lcssa
  %.1.lcssa = phi i32 [ %.1.lcssa.ph, %._crit_edge.unr-lcssa ], [ %15, %14 ], [ %.02227.unr, %.lr.ph.epil ]
  %18 = sext i32 %5 to i64
  %19 = getelementptr inbounds i32, ptr %0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = sext i32 %.1.lcssa to i64
  %22 = getelementptr inbounds i32, ptr %4, i64 %21
  store i32 %20, ptr %22, align 4, !tbaa !5
  %.not30 = icmp slt i32 %.1.lcssa, 0
  br i1 %.not30, label %._crit_edge.._crit_edge34_crit_edge, label %.lr.ph33.preheader

._crit_edge.._crit_edge34_crit_edge:              ; preds = %._crit_edge
  %.pre39 = add nsw i32 %.1.lcssa, 1
  br label %._crit_edge34

.lr.ph33.preheader:                               ; preds = %._crit_edge.thread, %._crit_edge
  %.022.lcssa42 = phi i32 [ 0, %._crit_edge.thread ], [ %.1.lcssa, %._crit_edge ]
  %23 = add nuw i32 %.022.lcssa42, 1
  %24 = zext i32 %23 to i64
  %25 = shl nuw nsw i64 %24, 2
  call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 4 dereferenceable(1) %0, ptr noundef nonnull align 16 dereferenceable(1) %4, i64 %25, i1 false), !tbaa !5
  br label %._crit_edge34

.lr.ph:                                           ; preds = %39, %.lr.ph.preheader.new
  %26 = phi i32 [ %.pre, %.lr.ph.preheader.new ], [ %34, %39 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %39 ]
  %.02227 = phi i32 [ 0, %.lr.ph.preheader.new ], [ %.1.1, %39 ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %39 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %.not26 = icmp eq i32 %26, %28
  br i1 %.not26, label %.lr.ph.1, label %29

29:                                               ; preds = %.lr.ph
  %30 = add nsw i32 %.02227, 1
  %31 = sext i32 %.02227 to i64
  %32 = getelementptr inbounds i32, ptr %4, i64 %31
  store i32 %26, ptr %32, align 4, !tbaa !5
  br label %.lr.ph.1

.lr.ph.1:                                         ; preds = %.lr.ph, %29
  %.1 = phi i32 [ %30, %29 ], [ %.02227, %.lr.ph ]
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %33 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %34 = load i32, ptr %33, align 4, !tbaa !5
  %.not26.1 = icmp eq i32 %28, %34
  br i1 %.not26.1, label %39, label %35

35:                                               ; preds = %.lr.ph.1
  %36 = add nsw i32 %.1, 1
  %37 = sext i32 %.1 to i64
  %38 = getelementptr inbounds i32, ptr %4, i64 %37
  store i32 %28, ptr %38, align 4, !tbaa !5
  br label %39

39:                                               ; preds = %35, %.lr.ph.1
  %.1.1 = phi i32 [ %36, %35 ], [ %.1, %.lr.ph.1 ]
  %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.unr-lcssa.loopexit, label %.lr.ph, !llvm.loop !9

._crit_edge34:                                    ; preds = %._crit_edge.._crit_edge34_crit_edge, %.lr.ph33.preheader
  %.pre-phi = phi i32 [ %.pre39, %._crit_edge.._crit_edge34_crit_edge ], [ %23, %.lr.ph33.preheader ]
  ret i32 %.pre-phi
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #1

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

!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"}


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