; LLVM IR from smv1999/CompetitiveProgrammingQuestionBank :: Recursion/remove_duplicates.ll (Z16removeduplicatesPc)

; 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 uwtable
define dso_local void @_Z16removeduplicatesPc(ptr noundef %0) #0 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %4 = load ptr, ptr %2, align 8, !tbaa !5
  %5 = getelementptr inbounds i8, ptr %4, i64 0
  %6 = load i8, ptr %5, align 1, !tbaa !9
  %7 = sext i8 %6 to i32
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %9, label %10

9:                                                ; preds = %1
  br label %49

10:                                               ; preds = %1
  %11 = load ptr, ptr %2, align 8, !tbaa !5
  %12 = getelementptr inbounds i8, ptr %11, i64 0
  %13 = load i8, ptr %12, align 1, !tbaa !9
  %14 = sext i8 %13 to i32
  %15 = load ptr, ptr %2, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 1
  %17 = load i8, ptr %16, align 1, !tbaa !9
  %18 = sext i8 %17 to i32
  %19 = icmp eq i32 %14, %18
  br i1 %19, label %20, label %46

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

21:                                               ; preds = %41, %20
  %22 = load ptr, ptr %2, align 8, !tbaa !5
  %23 = load i32, ptr %3, align 4, !tbaa !10
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i8, ptr %22, i64 %24
  %26 = load i8, ptr %25, align 1, !tbaa !9
  %27 = sext i8 %26 to i32
  %28 = icmp ne i32 %27, 0
  br i1 %28, label %30, label %29

29:                                               ; preds = %21
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  br label %44

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

41:                                               ; preds = %30
  %42 = load i32, ptr %3, align 4, !tbaa !10
  %43 = add nsw i32 %42, 1
  store i32 %43, ptr %3, align 4, !tbaa !10
  br label %21, !llvm.loop !12

44:                                               ; preds = %29
  %45 = load ptr, ptr %2, align 8, !tbaa !5
  call void @_Z16removeduplicatesPc(ptr noundef %45)
  br label %49

46:                                               ; preds = %10
  %47 = load ptr, ptr %2, align 8, !tbaa !5
  %48 = getelementptr inbounds i8, ptr %47, i64 1
  call void @_Z16removeduplicatesPc(ptr noundef %48)
  br label %49

49:                                               ; preds = %46, %44, %9
  ret void
}

; 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 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 = !{!7, !7, i64 0}
!10 = !{!11, !11, i64 0}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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: mustprogress nofree norecurse nosync nounwind memory(argmem: readwrite) uwtable
define dso_local void @_Z16removeduplicatesPc(ptr nocapture noundef %0) local_unnamed_addr #0 {
  %2 = load i8, ptr %0, align 1, !tbaa !5
  %3 = icmp eq i8 %2, 0
  br i1 %3, label %tailrecurse.outer._crit_edge, label %.lr.ph14

tailrecurse.outer.loopexit:                       ; preds = %8
  %4 = icmp eq i8 %10, 0
  br i1 %4, label %tailrecurse.outer._crit_edge, label %.lr.ph14

.lr.ph14:                                         ; preds = %1, %tailrecurse.outer.loopexit
  %5 = phi i8 [ %10, %tailrecurse.outer.loopexit ], [ %2, %1 ]
  %.tr.ph15 = phi ptr [ %6, %tailrecurse.outer.loopexit ], [ %0, %1 ]
  %6 = getelementptr inbounds i8, ptr %.tr.ph15, i64 1
  br label %8

tailrecurse.loopexit:                             ; preds = %.lr.ph
  %.pre = load i8, ptr %.tr.ph15, align 1, !tbaa !5
  %7 = icmp eq i8 %.pre, 0
  br i1 %7, label %tailrecurse.outer._crit_edge, label %8

8:                                                ; preds = %.lr.ph14, %tailrecurse.loopexit
  %9 = phi i8 [ %5, %.lr.ph14 ], [ %.pre, %tailrecurse.loopexit ]
  %10 = load i8, ptr %6, align 1, !tbaa !5
  %11 = icmp eq i8 %9, %10
  br i1 %11, label %.preheader, label %tailrecurse.outer.loopexit

.preheader:                                       ; preds = %8
  %12 = load i8, ptr %.tr.ph15, align 1, !tbaa !5
  %.not12 = icmp eq i8 %12, 0
  br i1 %.not12, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %.lr.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %.lr.ph ], [ 0, %.preheader ]
  %13 = getelementptr inbounds i8, ptr %.tr.ph15, i64 %indvars.iv
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %14 = getelementptr inbounds i8, ptr %.tr.ph15, i64 %indvars.iv.next
  %15 = load i8, ptr %14, align 1, !tbaa !5
  store i8 %15, ptr %13, align 1, !tbaa !5
  %.not = icmp eq i8 %15, 0
  br i1 %.not, label %tailrecurse.loopexit, label %.lr.ph, !llvm.loop !8

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer.loopexit, %tailrecurse.loopexit, %.preheader, %1
  ret void
}

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

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}


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