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

; 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.AString = 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 @_ZN7AString10RemoveCharEc(ptr noundef nonnull align 8 dereferenceable(16) %0, i8 noundef signext %1) #1 align 2 {
  %3 = alloca ptr, align 8
  %4 = alloca i8, align 1
  %5 = alloca ptr, align 8
  %6 = alloca i8, align 1
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca i8, align 1
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i8 %1, ptr %4, align 1, !tbaa !9
  %10 = load ptr, ptr %3, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = getelementptr inbounds %class.AString, ptr %10, i32 0, i32 0
  %12 = load ptr, ptr %11, align 8, !tbaa !10
  store ptr %12, ptr %5, align 8, !tbaa !5
  br label %13

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

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

21:                                               ; preds = %13
  %22 = load i8, ptr %6, align 1, !tbaa !9
  %23 = sext i8 %22 to i32
  %24 = load i8, ptr %4, align 1, !tbaa !9
  %25 = sext i8 %24 to i32
  %26 = icmp eq i32 %23, %25
  br i1 %26, label %27, label %28

27:                                               ; preds = %21
  store i32 2, ptr %7, align 4
  br label %29

28:                                               ; preds = %21
  store i32 0, ptr %7, align 4
  br label %29

29:                                               ; preds = %28, %27, %20
  call void @llvm.lifetime.end.p0(i64 1, ptr %6) #2
  %30 = load i32, ptr %7, align 4
  switch i32 %30, label %67 [
    i32 0, label %31
    i32 2, label %32
  ]

31:                                               ; preds = %29
  br label %13, !llvm.loop !13

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

35:                                               ; preds = %56, %32
  call void @llvm.lifetime.start.p0(i64 1, ptr %9) #2
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds i8, ptr %36, i32 1
  store ptr %37, ptr %5, align 8, !tbaa !5
  %38 = load i8, ptr %36, align 1, !tbaa !9
  store i8 %38, ptr %9, align 1, !tbaa !9
  %39 = load i8, ptr %9, align 1, !tbaa !9
  %40 = sext i8 %39 to i32
  %41 = icmp eq i32 %40, 0
  br i1 %41, label %42, label %43

42:                                               ; preds = %35
  store i32 4, ptr %7, align 4
  br label %54

43:                                               ; preds = %35
  %44 = load i8, ptr %9, align 1, !tbaa !9
  %45 = sext i8 %44 to i32
  %46 = load i8, ptr %4, align 1, !tbaa !9
  %47 = sext i8 %46 to i32
  %48 = icmp ne i32 %45, %47
  br i1 %48, label %49, label %53

49:                                               ; preds = %43
  %50 = load i8, ptr %9, align 1, !tbaa !9
  %51 = load ptr, ptr %8, align 8, !tbaa !5
  %52 = getelementptr inbounds i8, ptr %51, i32 1
  store ptr %52, ptr %8, align 8, !tbaa !5
  store i8 %50, ptr %51, align 1, !tbaa !9
  br label %53

53:                                               ; preds = %49, %43
  store i32 0, ptr %7, align 4
  br label %54

54:                                               ; preds = %53, %42
  call void @llvm.lifetime.end.p0(i64 1, ptr %9) #2
  %55 = load i32, ptr %7, align 4
  switch i32 %55, label %70 [
    i32 0, label %56
    i32 4, label %57
  ]

56:                                               ; preds = %54
  br label %35, !llvm.loop !15

57:                                               ; preds = %54
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  store i8 0, ptr %58, align 1, !tbaa !9
  %59 = load ptr, ptr %8, align 8, !tbaa !5
  %60 = getelementptr inbounds %class.AString, ptr %10, i32 0, i32 0
  %61 = load ptr, ptr %60, align 8, !tbaa !10
  %62 = ptrtoint ptr %59 to i64
  %63 = ptrtoint ptr %61 to i64
  %64 = sub i64 %62, %63
  %65 = trunc i64 %64 to i32
  %66 = getelementptr inbounds %class.AString, ptr %10, i32 0, i32 1
  store i32 %65, ptr %66, align 8, !tbaa !16
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  store i32 0, ptr %7, align 4
  br label %67

67:                                               ; preds = %57, %29
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  %68 = load i32, ptr %7, align 4
  switch i32 %68, label %70 [
    i32 0, label %69
    i32 1, label %69
  ]

69:                                               ; preds = %67, %67
  ret void

70:                                               ; preds = %67, %54
  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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 0}
!11 = !{!"_ZTS7AString", !6, i64 0, !12, i64 8, !12, i64 12}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = !{!11, !12, 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 @_ZN7AString10RemoveCharEc(ptr nocapture noundef nonnull align 8 dereferenceable(16) %0, i8 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 1
  %6 = load i8, ptr %.0, align 1, !tbaa !11
  %7 = icmp eq i8 %6, 0
  %8 = icmp eq i8 %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 !12

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

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

.lr.ph:                                           ; preds = %.lr.ph.preheader, %16
  %12 = phi i8 [ %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 i8 %12, %1
  br i1 %.not, label %16, label %14

14:                                               ; preds = %.lr.ph
  %15 = getelementptr inbounds i8, ptr %.01527, i64 1
  store i8 %12, ptr %.01527, align 1, !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 1
  %18 = load i8, ptr %13, align 1, !tbaa !11
  %19 = icmp eq i8 %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 i8 0, ptr %.015.lcssa, align 1, !tbaa !11
  %20 = load ptr, ptr %0, align 8, !tbaa !5
  %21 = ptrtoint ptr %.015.lcssa to i64
  %22 = ptrtoint ptr %20 to i64
  %23 = sub i64 %21, %22
  %24 = trunc i64 %23 to i32
  %25 = getelementptr inbounds i8, ptr %0, i64 8
  store i32 %24, ptr %25, align 8, !tbaa !14
  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 = !{!"_ZTS7AString", !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 = !{!8, !8, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!6, !10, i64 8}


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