; LLVM IR from JessonYue/LeetCodeLearning :: Array/wuchao/stack/6-19删除字符串中所有相邻的字符 (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"

%struct.MyStack = type { ptr, i32 }

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @myStackCreate() #1

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @myStackEmpty(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local void @myStackPush(ptr noundef, i8 noundef signext) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @myStackPop(ptr noundef) #1

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @myStackTop(ptr noundef) #1

; Function Attrs: nounwind uwtable
define dso_local ptr @removeDuplicates(ptr noundef %0) #1 {
  %2 = alloca ptr, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #3
  %8 = call ptr @myStackCreate()
  store ptr %8, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %9 = load ptr, ptr %2, align 8, !tbaa !5
  %10 = call i64 @strlen(ptr noundef %9) #4
  store i64 %10, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %11

11:                                               ; preds = %39, %1
  %12 = load i64, ptr %5, align 8, !tbaa !9
  %13 = load i64, ptr %4, align 8, !tbaa !9
  %14 = icmp ult i64 %12, %13
  br i1 %14, label %15, label %42

15:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 1, ptr %6) #3
  %16 = load ptr, ptr %2, align 8, !tbaa !5
  %17 = load i64, ptr %5, align 8, !tbaa !9
  %18 = getelementptr inbounds i8, ptr %16, i64 %17
  %19 = load i8, ptr %18, align 1, !tbaa !11
  store i8 %19, ptr %6, align 1, !tbaa !11
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = call zeroext i1 @myStackEmpty(ptr noundef %20)
  br i1 %21, label %22, label %25

22:                                               ; preds = %15
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = load i8, ptr %6, align 1, !tbaa !11
  call void @myStackPush(ptr noundef %23, i8 noundef signext %24)
  br label %39

25:                                               ; preds = %15
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = call signext i8 @myStackTop(ptr noundef %26)
  %28 = sext i8 %27 to i32
  %29 = load i8, ptr %6, align 1, !tbaa !11
  %30 = sext i8 %29 to i32
  %31 = icmp eq i32 %28, %30
  br i1 %31, label %32, label %35

32:                                               ; preds = %25
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = call signext i8 @myStackPop(ptr noundef %33)
  br label %38

35:                                               ; preds = %25
  %36 = load ptr, ptr %3, align 8, !tbaa !5
  %37 = load i8, ptr %6, align 1, !tbaa !11
  call void @myStackPush(ptr noundef %36, i8 noundef signext %37)
  br label %38

38:                                               ; preds = %35, %32
  br label %39

39:                                               ; preds = %38, %22
  %40 = load i64, ptr %5, align 8, !tbaa !9
  %41 = add i64 %40, 1
  store i64 %41, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #3
  br label %11, !llvm.loop !12

42:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  %43 = load ptr, ptr %3, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.MyStack, ptr %43, i32 0, i32 1
  %45 = load i32, ptr %44, align 8, !tbaa !14
  store i32 %45, ptr %7, align 4, !tbaa !17
  store i64 0, ptr %5, align 8, !tbaa !9
  br label %46

46:                                               ; preds = %51, %42
  %47 = load i64, ptr %5, align 8, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !17
  %49 = sext i32 %48 to i64
  %50 = icmp ult i64 %47, %49
  br i1 %50, label %51, label %63

51:                                               ; preds = %46
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = call signext i8 @myStackPop(ptr noundef %52)
  %54 = load ptr, ptr %2, align 8, !tbaa !5
  %55 = load i32, ptr %7, align 4, !tbaa !17
  %56 = sext i32 %55 to i64
  %57 = load i64, ptr %5, align 8, !tbaa !9
  %58 = sub i64 %56, %57
  %59 = sub i64 %58, 1
  %60 = getelementptr inbounds i8, ptr %54, i64 %59
  store i8 %53, ptr %60, align 1, !tbaa !11
  %61 = load i64, ptr %5, align 8, !tbaa !9
  %62 = add i64 %61, 1
  store i64 %62, ptr %5, align 8, !tbaa !9
  br label %46, !llvm.loop !18

63:                                               ; preds = %46
  %64 = load i64, ptr %5, align 8, !tbaa !9
  %65 = load i64, ptr %4, align 8, !tbaa !9
  %66 = icmp ult i64 %64, %65
  br i1 %66, label %67, label %71

67:                                               ; preds = %63
  %68 = load ptr, ptr %2, align 8, !tbaa !5
  %69 = load i64, ptr %5, align 8, !tbaa !9
  %70 = getelementptr inbounds i8, ptr %68, i64 %69
  store i8 0, ptr %70, align 1, !tbaa !11
  br label %71

71:                                               ; preds = %67, %63
  %72 = load ptr, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #3
  ret ptr %72
}

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #2

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

!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 = !{!"long", !7, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !16, i64 8}
!15 = !{!"", !6, i64 0, !16, i64 8}
!16 = !{!"int", !7, i64 0}
!17 = !{!16, !16, i64 0}
!18 = distinct !{!18, !13}


; 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: nounwind uwtable
declare dso_local ptr @myStackCreate() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local zeroext i1 @myStackEmpty(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local void @myStackPush(ptr noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @myStackPop(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local signext i8 @myStackTop(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @removeDuplicates(ptr noundef returned %0) local_unnamed_addr #0 {
  %2 = tail call ptr @myStackCreate()
  %3 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %0) #2
  %.not = icmp eq i64 %3, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1, %14
  %.028 = phi i64 [ %15, %14 ], [ 0, %1 ]
  %4 = getelementptr inbounds i8, ptr %0, i64 %.028
  %5 = load i8, ptr %4, align 1, !tbaa !5
  %6 = tail call zeroext i1 @myStackEmpty(ptr noundef %2)
  br i1 %6, label %7, label %8

7:                                                ; preds = %.lr.ph
  tail call void @myStackPush(ptr noundef %2, i8 noundef signext %5)
  br label %14

8:                                                ; preds = %.lr.ph
  %9 = tail call signext i8 @myStackTop(ptr noundef %2)
  %10 = icmp eq i8 %9, %5
  br i1 %10, label %11, label %13

11:                                               ; preds = %8
  %12 = tail call signext i8 @myStackPop(ptr noundef %2)
  br label %14

13:                                               ; preds = %8
  tail call void @myStackPush(ptr noundef %2, i8 noundef signext %5)
  br label %14

14:                                               ; preds = %11, %13, %7
  %15 = add nuw i64 %.028, 1
  %exitcond.not = icmp eq i64 %15, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !8

._crit_edge:                                      ; preds = %14, %1
  %16 = getelementptr inbounds i8, ptr %2, i64 8
  %17 = load i32, ptr %16, align 8, !tbaa !10
  %18 = sext i32 %17 to i64
  %invariant.gep = getelementptr i8, ptr %0, i64 %18
  %.not33 = icmp eq i32 %17, 0
  br i1 %.not33, label %._crit_edge32, label %.lr.ph31

.lr.ph31:                                         ; preds = %._crit_edge, %.lr.ph31
  %.129 = phi i64 [ %21, %.lr.ph31 ], [ 0, %._crit_edge ]
  %19 = tail call signext i8 @myStackPop(ptr noundef %2)
  %20 = xor i64 %.129, -1
  %gep = getelementptr i8, ptr %invariant.gep, i64 %20
  store i8 %19, ptr %gep, align 1, !tbaa !5
  %21 = add nuw i64 %.129, 1
  %exitcond34.not = icmp eq i64 %21, %18
  br i1 %exitcond34.not, label %._crit_edge32, label %.lr.ph31, !llvm.loop !14

._crit_edge32:                                    ; preds = %.lr.ph31, %._crit_edge
  %22 = icmp ugt i64 %3, %18
  br i1 %22, label %23, label %25

23:                                               ; preds = %._crit_edge32
  %24 = getelementptr inbounds i8, ptr %0, i64 %18
  store i8 0, ptr %24, align 1, !tbaa !5
  br label %25

25:                                               ; preds = %23, %._crit_edge32
  ret ptr %0
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #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 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 = { nounwind willreturn memory(read) }

!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/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !13, i64 8}
!11 = !{!"", !12, i64 0, !13, i64 8}
!12 = !{!"any pointer", !6, i64 0}
!13 = !{!"int", !6, i64 0}
!14 = distinct !{!14, !9}


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