; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0080_remove-duplicates-from-sorted-array-ii (removeDuplicates)

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

7:                                                ; preds = %40, %2
  %8 = load i32, ptr %6, align 4, !tbaa !9
  %9 = load i32, ptr %4, align 4, !tbaa !9
  %10 = icmp slt i32 %8, %9
  br i1 %10, label %12, label %11

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

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

15:                                               ; preds = %12
  %16 = load ptr, ptr %3, align 8, !tbaa !5
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i32, ptr %16, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !9
  %23 = sub nsw i32 %22, 2
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i32, ptr %21, i64 %24
  %26 = load i32, ptr %25, align 4, !tbaa !9
  %27 = icmp sgt i32 %20, %26
  br i1 %27, label %28, label %39

28:                                               ; preds = %15, %12
  %29 = load ptr, ptr %3, align 8, !tbaa !5
  %30 = load i32, ptr %6, 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
  %34 = load ptr, ptr %3, align 8, !tbaa !5
  %35 = load i32, ptr %5, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %5, align 4, !tbaa !9
  %37 = sext i32 %35 to i64
  %38 = getelementptr inbounds i32, ptr %34, i64 %37
  store i32 %33, ptr %38, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %28, %15
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %6, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %6, align 4, !tbaa !9
  br label %7, !llvm.loop !11

43:                                               ; preds = %11
  %44 = load i32, ptr %5, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  ret i32 %44
}

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


; 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: readwrite) uwtable
define dso_local i32 @removeDuplicates(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %invariant.gep = getelementptr i8, ptr %0, i64 -8
  %3 = icmp sgt i32 %1, 0
  br i1 %3, label %4, label %._crit_edge

4:                                                ; preds = %2
  %switch = icmp ult i32 %1, 3
  br i1 %switch, label %._crit_edge, label %.lr.ph.peel.next16.preheader

.lr.ph.peel.next16.preheader:                     ; preds = %4
  %wide.trip.count = zext nneg i32 %1 to i64
  %xtraiter = and i64 %wide.trip.count, 1
  %5 = icmp eq i32 %1, 3
  br i1 %5, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.peel.next16.preheader.new

.lr.ph.peel.next16.preheader.new:                 ; preds = %.lr.ph.peel.next16.preheader
  %6 = and i64 %wide.trip.count, 2147483646
  %7 = add nsw i64 %6, -4
  br label %.lr.ph.peel.next16

._crit_edge.loopexit.unr-lcssa:                   ; preds = %32, %.lr.ph.peel.next16.preheader
  %.1.lcssa.ph = phi i32 [ poison, %.lr.ph.peel.next16.preheader ], [ %.1.1, %32 ]
  %indvars.iv.unr = phi i64 [ 2, %.lr.ph.peel.next16.preheader ], [ %indvars.iv.next.1, %32 ]
  %.01213.unr = phi i32 [ 2, %.lr.ph.peel.next16.preheader ], [ %.1.1, %32 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.peel.next16.epil

.lr.ph.peel.next16.epil:                          ; preds = %._crit_edge.loopexit.unr-lcssa
  %8 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.unr
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = sext i32 %.01213.unr to i64
  %gep.epil = getelementptr i32, ptr %invariant.gep, i64 %10
  %11 = load i32, ptr %gep.epil, align 4, !tbaa !5
  %12 = icmp sgt i32 %9, %11
  br i1 %12, label %13, label %._crit_edge

13:                                               ; preds = %.lr.ph.peel.next16.epil
  %14 = add nsw i32 %.01213.unr, 1
  %15 = getelementptr inbounds i32, ptr %0, i64 %10
  store i32 %9, ptr %15, align 4, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %13, %.lr.ph.peel.next16.epil, %4, %2
  %.012.lcssa = phi i32 [ 0, %2 ], [ %1, %4 ], [ %.1.lcssa.ph, %._crit_edge.loopexit.unr-lcssa ], [ %14, %13 ], [ %.01213.unr, %.lr.ph.peel.next16.epil ]
  ret i32 %.012.lcssa

.lr.ph.peel.next16:                               ; preds = %32, %.lr.ph.peel.next16.preheader.new
  %indvars.iv = phi i64 [ 2, %.lr.ph.peel.next16.preheader.new ], [ %indvars.iv.next.1, %32 ]
  %.01213 = phi i32 [ 2, %.lr.ph.peel.next16.preheader.new ], [ %.1.1, %32 ]
  %niter = phi i64 [ 0, %.lr.ph.peel.next16.preheader.new ], [ %niter.next.1, %32 ]
  %16 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = sext i32 %.01213 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %18
  %19 = load i32, ptr %gep, align 4, !tbaa !5
  %20 = icmp sgt i32 %17, %19
  br i1 %20, label %21, label %.lr.ph.peel.next16.1

21:                                               ; preds = %.lr.ph.peel.next16
  %22 = add nsw i32 %.01213, 1
  %23 = getelementptr inbounds i32, ptr %0, i64 %18
  store i32 %17, ptr %23, align 4, !tbaa !5
  br label %.lr.ph.peel.next16.1

.lr.ph.peel.next16.1:                             ; preds = %.lr.ph.peel.next16, %21
  %.1 = phi i32 [ %22, %21 ], [ %.01213, %.lr.ph.peel.next16 ]
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %24 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = sext i32 %.1 to i64
  %gep.1 = getelementptr i32, ptr %invariant.gep, i64 %26
  %27 = load i32, ptr %gep.1, align 4, !tbaa !5
  %28 = icmp sgt i32 %25, %27
  br i1 %28, label %29, label %32

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

32:                                               ; preds = %29, %.lr.ph.peel.next16.1
  %.1.1 = phi i32 [ %30, %29 ], [ %.1, %.lr.ph.peel.next16.1 ]
  %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, %7
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.peel.next16, !llvm.loop !9
}

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

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


; Effective O3 passes: BDCEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass