; LLVM IR from facebook/redex :: libresource/String16.ll (ZN7android8String166removeEmm)

; 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.android::String16" = type { ptr }

; 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: inlinehint mustprogress uwtable
declare hidden noundef ptr @_ZN7androidL14getEmptyStringEv() #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7android8String167releaseEv(ptr noundef nonnull align 8 dereferenceable(8)) #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK7android8String164sizeEv(ptr noundef nonnull align 8 dereferenceable(8)) #3 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) #3 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #4

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7android8String166removeEmm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, i64 noundef %2) #3 align 2 {
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i64 %1, ptr %6, align 8, !tbaa !9
  store i64 %2, ptr %7, align 8, !tbaa !9
  %14 = load ptr, ptr %5, align 8
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #5
  %15 = call noundef i64 @_ZNK7android8String164sizeEv(ptr noundef nonnull align 8 dereferenceable(8) %14)
  store i64 %15, ptr %8, align 8, !tbaa !9
  %16 = load i64, ptr %7, align 8, !tbaa !9
  %17 = load i64, ptr %8, align 8, !tbaa !9
  %18 = icmp uge i64 %16, %17
  br i1 %18, label %19, label %22

19:                                               ; preds = %3
  call void @_ZN7android8String167releaseEv(ptr noundef nonnull align 8 dereferenceable(8) %14)
  %20 = call noundef ptr @_ZN7androidL14getEmptyStringEv()
  %21 = getelementptr inbounds %"class.android::String16", ptr %14, i32 0, i32 0
  store ptr %20, ptr %21, align 8, !tbaa !11
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %89

22:                                               ; preds = %3
  %23 = load i64, ptr %6, align 8, !tbaa !9
  %24 = load i64, ptr %8, align 8, !tbaa !9
  %25 = icmp ugt i64 %23, %24
  br i1 %25, label %32, label %26

26:                                               ; preds = %22
  %27 = load i64, ptr %6, align 8, !tbaa !9
  %28 = load i64, ptr %8, align 8, !tbaa !9
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = sub i64 %28, %29
  %31 = icmp ugt i64 %27, %30
  br i1 %31, label %32, label %36

32:                                               ; preds = %26, %22
  %33 = load i64, ptr %8, align 8, !tbaa !9
  %34 = load i64, ptr %7, align 8, !tbaa !9
  %35 = sub i64 %33, %34
  store i64 %35, ptr %6, align 8, !tbaa !9
  br label %36

36:                                               ; preds = %32, %26
  %37 = load i64, ptr %7, align 8, !tbaa !9
  %38 = icmp eq i64 %37, 0
  br i1 %38, label %39, label %44

39:                                               ; preds = %36
  %40 = load i64, ptr %6, align 8, !tbaa !9
  %41 = load i64, ptr %8, align 8, !tbaa !9
  %42 = icmp eq i64 %40, %41
  br i1 %42, label %43, label %44

43:                                               ; preds = %39
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %89

44:                                               ; preds = %39, %36
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = icmp ugt i64 %45, 0
  br i1 %46, label %47, label %72

47:                                               ; preds = %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %48 = load i64, ptr %8, align 8, !tbaa !9
  %49 = add i64 %48, 1
  %50 = mul i64 %49, 2
  %51 = call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %14, i64 noundef %50)
  store ptr %51, ptr %10, align 8, !tbaa !5
  %52 = load ptr, ptr %10, align 8, !tbaa !5
  %53 = icmp ne ptr %52, null
  br i1 %53, label %55, label %54

54:                                               ; preds = %47
  store i32 -12, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %69

55:                                               ; preds = %47
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #5
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %56)
  store ptr %57, ptr %11, align 8, !tbaa !5
  %58 = load ptr, ptr %11, align 8, !tbaa !5
  %59 = load ptr, ptr %11, align 8, !tbaa !5
  %60 = load i64, ptr %7, align 8, !tbaa !9
  %61 = getelementptr inbounds i16, ptr %59, i64 %60
  %62 = load i64, ptr %8, align 8, !tbaa !9
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = sub i64 %62, %63
  %65 = add i64 %64, 1
  %66 = mul i64 %65, 2
  call void @llvm.memmove.p0.p0.i64(ptr align 2 %58, ptr align 2 %61, i64 %66, i1 false)
  %67 = load ptr, ptr %11, align 8, !tbaa !5
  %68 = getelementptr inbounds %"class.android::String16", ptr %14, i32 0, i32 0
  store ptr %67, ptr %68, align 8, !tbaa !11
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #5
  store i32 0, ptr %9, align 4
  br label %69

69:                                               ; preds = %55, %54
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  %70 = load i32, ptr %9, align 4
  switch i32 %70, label %89 [
    i32 0, label %71
  ]

71:                                               ; preds = %69
  br label %72

72:                                               ; preds = %71, %44
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %73 = load i64, ptr %6, align 8, !tbaa !9
  %74 = add i64 %73, 1
  %75 = mul i64 %74, 2
  %76 = call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %14, i64 noundef %75)
  store ptr %76, ptr %12, align 8, !tbaa !5
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = icmp ne ptr %77, null
  br i1 %78, label %79, label %87

79:                                               ; preds = %72
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #5
  %80 = load ptr, ptr %12, align 8, !tbaa !5
  %81 = call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %80)
  store ptr %81, ptr %13, align 8, !tbaa !5
  %82 = load ptr, ptr %13, align 8, !tbaa !5
  %83 = load i64, ptr %6, align 8, !tbaa !9
  %84 = getelementptr inbounds i16, ptr %82, i64 %83
  store i16 0, ptr %84, align 2, !tbaa !13
  %85 = load ptr, ptr %13, align 8, !tbaa !5
  %86 = getelementptr inbounds %"class.android::String16", ptr %14, i32 0, i32 0
  store ptr %85, ptr %86, align 8, !tbaa !11
  store i32 0, ptr %4, align 4
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #5
  br label %88

87:                                               ; preds = %72
  store i32 -12, ptr %4, align 4
  store i32 1, ptr %9, align 4
  br label %88

88:                                               ; preds = %87, %79
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %89

89:                                               ; preds = %88, %69, %43, %19
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #5
  %90 = load i32, ptr %4, align 4
  ret i32 %90
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { inlinehint 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 #2 = { inlinehint 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 #3 = { 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #5 = { 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 = !{!"long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSN7android8String16E", !6, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"char16_t", !7, i64 0}


; 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: inlinehint mustprogress uwtable
declare hidden noundef ptr @_ZN7androidL14getEmptyStringEv() local_unnamed_addr #0

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #1 align 2

; Function Attrs: mustprogress uwtable
declare dso_local void @_ZN7android8String167releaseEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef i64 @_ZNK7android8String164sizeEv(ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress uwtable
declare dso_local noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8), i64 noundef) local_unnamed_addr #2 align 2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #3

; Function Attrs: mustprogress uwtable
define dso_local noundef range(i32 -12, 1) i32 @_ZN7android8String166removeEmm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, i64 noundef %2) local_unnamed_addr #2 align 2 {
  %4 = tail call noundef i64 @_ZNK7android8String164sizeEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %.not = icmp ugt i64 %4, %2
  br i1 %.not, label %7, label %5

5:                                                ; preds = %3
  tail call void @_ZN7android8String167releaseEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %6 = tail call noundef ptr @_ZN7androidL14getEmptyStringEv()
  br label %.critedge.sink.split

7:                                                ; preds = %3
  %8 = icmp ult i64 %4, %1
  %9 = sub i64 %4, %2
  %10 = tail call i64 @llvm.umin.i64(i64 %9, i64 %1)
  %.030 = select i1 %8, i64 %9, i64 %10
  %11 = icmp eq i64 %2, 0
  %12 = icmp eq i64 %.030, %4
  %or.cond40 = and i1 %11, %12
  br i1 %or.cond40, label %.critedge, label %13

13:                                               ; preds = %7
  br i1 %11, label %23, label %14

14:                                               ; preds = %13
  %15 = shl i64 %4, 1
  %16 = add i64 %15, 2
  %17 = tail call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %16)
  %.not37.not = icmp eq ptr %17, null
  br i1 %.not37.not, label %.critedge, label %18

18:                                               ; preds = %14
  %19 = tail call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %17)
  %20 = getelementptr inbounds i16, ptr %19, i64 %2
  %21 = shl i64 %9, 1
  %22 = add i64 %21, 2
  tail call void @llvm.memmove.p0.p0.i64(ptr align 2 %19, ptr nonnull align 2 %20, i64 %22, i1 false)
  store ptr %19, ptr %0, align 8, !tbaa !5
  br label %23

23:                                               ; preds = %18, %13
  %24 = shl i64 %.030, 1
  %25 = add i64 %24, 2
  %26 = tail call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %25)
  %.not38 = icmp eq ptr %26, null
  br i1 %.not38, label %.critedge, label %27

27:                                               ; preds = %23
  %28 = tail call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %26)
  %29 = getelementptr inbounds i16, ptr %28, i64 %.030
  store i16 0, ptr %29, align 2, !tbaa !10
  br label %.critedge.sink.split

.critedge.sink.split:                             ; preds = %5, %27
  %.sink = phi ptr [ %28, %27 ], [ %6, %5 ]
  store ptr %.sink, ptr %0, align 8, !tbaa !5
  br label %.critedge

.critedge:                                        ; preds = %.critedge.sink.split, %14, %23, %7
  %.2 = phi i32 [ 0, %7 ], [ -12, %23 ], [ -12, %14 ], [ 0, %.critedge.sink.split ]
  ret i32 %.2
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.umin.i64(i64, i64) #4

attributes #0 = { inlinehint 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 = { inlinehint 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 = { 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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 = !{!"_ZTSN7android8String16E", !7, i64 0}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!11, !11, i64 0}
!11 = !{!"char16_t", !8, i64 0}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass