; LLVM IR from SSBMTonberry/pocketlzma :: ref_spec/lzma1900/CPP/Common/MyString.ll (ZN7UString10RemoveCharEw)

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

%class.UString = type { ptr, i32, 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: mustprogress nounwind uwtable
define dso_local void @_ZN7UString10RemoveCharEw(ptr noundef nonnull align 8 dereferenceable(16) %0, i32 noundef signext %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %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 ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = getelementptr inbounds %class.UString, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !11
  store ptr %12, ptr %5, align 8, !tbaa !5
  br label %13

13:                                               ; preds = %28, %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %14 = load ptr, ptr %5, align 8, !tbaa !5
  %15 = getelementptr inbounds i32, ptr %14, i32 1
  store ptr %15, ptr %5, align 8, !tbaa !5
  %16 = load i32, ptr %14, align 4, !tbaa !9
  store i32 %16, ptr %6, align 4, !tbaa !9
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = icmp eq i32 %17, 0
  br i1 %18, label %19, label %20

19:                                               ; preds = %13
  store i32 1, ptr %7, align 4
  br label %26

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

24:                                               ; preds = %20
  store i32 2, ptr %7, align 4
  br label %26

25:                                               ; preds = %20
  store i32 0, ptr %7, align 4
  br label %26

26:                                               ; preds = %25, %24, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  %27 = load i32, ptr %7, align 4
  switch i32 %27, label %62 [
    i32 0, label %28
    i32 2, label %29
  ]

28:                                               ; preds = %26
  br label %13, !llvm.loop !14

29:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %30 = load ptr, ptr %5, align 8, !tbaa !5
  %31 = getelementptr inbounds i32, ptr %30, i64 -1
  store ptr %31, ptr %8, align 8, !tbaa !5
  br label %32

32:                                               ; preds = %50, %29
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  %33 = load ptr, ptr %5, align 8, !tbaa !5
  %34 = getelementptr inbounds i32, ptr %33, i32 1
  store ptr %34, ptr %5, align 8, !tbaa !5
  %35 = load i32, ptr %33, align 4, !tbaa !9
  store i32 %35, ptr %9, align 4, !tbaa !9
  %36 = load i32, ptr %9, align 4, !tbaa !9
  %37 = icmp eq i32 %36, 0
  br i1 %37, label %38, label %39

38:                                               ; preds = %32
  store i32 4, ptr %7, align 4
  br label %48

39:                                               ; preds = %32
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load i32, ptr %4, align 4, !tbaa !9
  %42 = icmp ne i32 %40, %41
  br i1 %42, label %43, label %47

43:                                               ; preds = %39
  %44 = load i32, ptr %9, align 4, !tbaa !9
  %45 = load ptr, ptr %8, align 8, !tbaa !5
  %46 = getelementptr inbounds i32, ptr %45, i32 1
  store ptr %46, ptr %8, align 8, !tbaa !5
  store i32 %44, ptr %45, align 4, !tbaa !9
  br label %47

47:                                               ; preds = %43, %39
  store i32 0, ptr %7, align 4
  br label %48

48:                                               ; preds = %47, %38
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  %49 = load i32, ptr %7, align 4
  switch i32 %49, label %65 [
    i32 0, label %50
    i32 4, label %51
  ]

50:                                               ; preds = %48
  br label %32, !llvm.loop !16

51:                                               ; preds = %48
  %52 = load ptr, ptr %8, align 8, !tbaa !5
  store i32 0, ptr %52, align 4, !tbaa !9
  %53 = load ptr, ptr %8, align 8, !tbaa !5
  %54 = getelementptr inbounds %class.UString, ptr %10, i32 0, i32 0
  %55 = load ptr, ptr %54, align 8, !tbaa !11
  %56 = ptrtoint ptr %53 to i64
  %57 = ptrtoint ptr %55 to i64
  %58 = sub i64 %56, %57
  %59 = sdiv exact i64 %58, 4
  %60 = trunc i64 %59 to i32
  %61 = getelementptr inbounds %class.UString, ptr %10, i32 0, i32 1
  store i32 %60, ptr %61, align 8, !tbaa !17
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  store i32 0, ptr %7, align 4
  br label %62

62:                                               ; preds = %51, %26
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %63 = load i32, ptr %7, align 4
  switch i32 %63, label %65 [
    i32 0, label %64
    i32 1, label %64
  ]

64:                                               ; preds = %62, %62
  ret void

65:                                               ; preds = %62, %48
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"wchar_t", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTS7UString", !6, i64 0, !13, i64 8, !13, i64 12}
!13 = !{!"int", !7, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!12, !13, i64 8}


; 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(readwrite, inaccessiblemem: none) uwtable
define dso_local void @_ZN7UString10RemoveCharEw(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, i32 noundef signext %1) local_unnamed_addr #0 align 2 {
  %3 = load ptr, ptr %0, align 8, !tbaa !5
  br label %4

4:                                                ; preds = %4, %2
  %.0 = phi ptr [ %3, %2 ], [ %5, %4 ]
  %5 = getelementptr inbounds i8, ptr %.0, i64 4
  %6 = load i32, ptr %.0, align 4, !tbaa !11
  %7 = icmp eq i32 %6, 0
  %8 = icmp eq i32 %6, %1
  %. = select i1 %8, i32 2, i32 0
  %.017 = select i1 %7, i32 1, i32 %.
  switch i32 %.017, label %.loopexit [
    i32 0, label %4
    i32 2, label %.preheader
  ], !llvm.loop !13

.preheader:                                       ; preds = %4
  %9 = load i32, ptr %5, align 4, !tbaa !11
  %10 = icmp eq i32 %9, 0
  br i1 %10, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %.preheader
  %11 = getelementptr inbounds i8, ptr %.0, i64 8
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %12 = phi i32 [ %18, %16 ], [ %9, %.lr.ph.preheader ]
  %13 = phi ptr [ %17, %16 ], [ %11, %.lr.ph.preheader ]
  %.01527 = phi ptr [ %.2, %16 ], [ %.0, %.lr.ph.preheader ]
  %.not = icmp eq i32 %12, %1
  br i1 %.not, label %16, label %14

14:                                               ; preds = %.lr.ph
  %15 = getelementptr inbounds i8, ptr %.01527, i64 4
  store i32 %12, ptr %.01527, align 4, !tbaa !11
  br label %16

16:                                               ; preds = %.lr.ph, %14
  %.2 = phi ptr [ %15, %14 ], [ %.01527, %.lr.ph ]
  %17 = getelementptr inbounds i8, ptr %13, i64 4
  %18 = load i32, ptr %13, align 4, !tbaa !11
  %19 = icmp eq i32 %18, 0
  br i1 %19, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %16, %.preheader
  %.015.lcssa = phi ptr [ %.0, %.preheader ], [ %.2, %16 ]
  store i32 0, ptr %.015.lcssa, align 4, !tbaa !11
  %20 = ptrtoint ptr %.015.lcssa to i64
  %21 = ptrtoint ptr %3 to i64
  %22 = sub i64 %20, %21
  %23 = lshr exact i64 %22, 2
  %24 = trunc i64 %23 to i32
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %24, ptr %25, align 8, !tbaa !15
  br label %.loopexit

.loopexit:                                        ; preds = %4, %._crit_edge
  ret void
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, inaccessiblemem: none) 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, !7, i64 0}
!6 = !{!"_ZTS7UString", !7, i64 0, !10, i64 8, !10, i64 12}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"wchar_t", !8, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!6, !10, i64 8}


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