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

; 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 nounwind uwtable
declare dso_local noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) #1 align 2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #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 i32 @_ZN7android8String165setToEPKDsm(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i64 noundef) #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) #2

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

18:                                               ; preds = %4
  %19 = load ptr, ptr %8, align 8, !tbaa !5
  %20 = load i64, ptr %9, align 8, !tbaa !9
  %21 = call noundef i32 @_ZN7android8String165setToEPKDsm(ptr noundef nonnull align 8 dereferenceable(8) %14, ptr noundef %19, i64 noundef %20)
  store i32 %21, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %77

22:                                               ; preds = %4
  %23 = load i64, ptr %9, align 8, !tbaa !9
  %24 = icmp eq i64 %23, 0
  br i1 %24, label %25, label %26

25:                                               ; preds = %22
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %77

26:                                               ; preds = %22
  br label %27

27:                                               ; preds = %26
  %28 = load i64, ptr %7, align 8, !tbaa !9
  %29 = load i64, ptr %10, align 8, !tbaa !9
  %30 = icmp ugt i64 %28, %29
  br i1 %30, label %31, label %33

31:                                               ; preds = %27
  %32 = load i64, ptr %10, align 8, !tbaa !9
  store i64 %32, ptr %7, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %31, %27
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %34 = load i64, ptr %10, align 8, !tbaa !9
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = add i64 %34, %35
  %37 = add i64 %36, 1
  %38 = mul i64 %37, 2
  %39 = call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %14, i64 noundef %38)
  store ptr %39, ptr %12, align 8, !tbaa !5
  %40 = load ptr, ptr %12, align 8, !tbaa !5
  %41 = icmp ne ptr %40, null
  br i1 %41, label %42, label %75

42:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %43 = load ptr, ptr %12, align 8, !tbaa !5
  %44 = call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %43)
  store ptr %44, ptr %13, align 8, !tbaa !5
  %45 = load i64, ptr %7, align 8, !tbaa !9
  %46 = load i64, ptr %10, align 8, !tbaa !9
  %47 = icmp ult i64 %45, %46
  br i1 %47, label %48, label %61

48:                                               ; preds = %42
  %49 = load ptr, ptr %13, align 8, !tbaa !5
  %50 = load i64, ptr %7, align 8, !tbaa !9
  %51 = getelementptr inbounds i16, ptr %49, i64 %50
  %52 = load i64, ptr %9, align 8, !tbaa !9
  %53 = getelementptr inbounds i16, ptr %51, i64 %52
  %54 = load ptr, ptr %13, align 8, !tbaa !5
  %55 = load i64, ptr %7, align 8, !tbaa !9
  %56 = getelementptr inbounds i16, ptr %54, i64 %55
  %57 = load i64, ptr %10, align 8, !tbaa !9
  %58 = load i64, ptr %7, align 8, !tbaa !9
  %59 = sub i64 %57, %58
  %60 = mul i64 %59, 2
  call void @llvm.memmove.p0.p0.i64(ptr align 2 %53, ptr align 2 %56, i64 %60, i1 false)
  br label %61

61:                                               ; preds = %48, %42
  %62 = load ptr, ptr %13, align 8, !tbaa !5
  %63 = load i64, ptr %7, align 8, !tbaa !9
  %64 = getelementptr inbounds i16, ptr %62, i64 %63
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load i64, ptr %9, align 8, !tbaa !9
  %67 = mul i64 %66, 2
  call void @llvm.memcpy.p0.p0.i64(ptr align 2 %64, ptr align 2 %65, i64 %67, i1 false)
  %68 = load ptr, ptr %13, align 8, !tbaa !5
  %69 = load i64, ptr %10, align 8, !tbaa !9
  %70 = load i64, ptr %9, align 8, !tbaa !9
  %71 = add i64 %69, %70
  %72 = getelementptr inbounds i16, ptr %68, i64 %71
  store i16 0, ptr %72, align 2, !tbaa !11
  %73 = load ptr, ptr %13, align 8, !tbaa !5
  %74 = getelementptr inbounds %"class.android::String16", ptr %14, i32 0, i32 0
  store ptr %73, ptr %74, align 8, !tbaa !13
  store i32 0, ptr %5, align 4
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  br label %76

75:                                               ; preds = %33
  store i32 -12, ptr %5, align 4
  store i32 1, ptr %11, align 4
  br label %76

76:                                               ; preds = %75, %61
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  br label %77

77:                                               ; preds = %76, %25, %18
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  %78 = load i32, ptr %5, align 4
  ret i32 %78
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !12, i64 0}
!12 = !{!"char16_t", !7, i64 0}
!13 = !{!14, !6, i64 0}
!14 = !{!"_ZTSN7android8String16E", !6, 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 nounwind uwtable
declare dso_local noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24)) local_unnamed_addr #0 align 2

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

; 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 i32 @_ZN7android8String165setToEPKDsm(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef, i64 noundef) 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) #1

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_ZN7android8String166insertEmPKDsm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %1, ptr noundef %2, i64 noundef %3) local_unnamed_addr #2 align 2 {
  %5 = tail call noundef i64 @_ZNK7android8String164sizeEv(ptr noundef nonnull align 8 dereferenceable(8) %0)
  %6 = icmp eq i64 %5, 0
  br i1 %6, label %7, label %9

7:                                                ; preds = %4
  %8 = tail call noundef i32 @_ZN7android8String165setToEPKDsm(ptr noundef nonnull align 8 dereferenceable(8) %0, ptr noundef %2, i64 noundef %3)
  br label %28

9:                                                ; preds = %4
  %10 = icmp eq i64 %3, 0
  br i1 %10, label %28, label %11

11:                                               ; preds = %9
  %spec.select = tail call i64 @llvm.umin.i64(i64 %5, i64 %1)
  %12 = add i64 %5, %3
  %13 = shl i64 %12, 1
  %14 = add i64 %13, 2
  %15 = tail call noundef ptr @_ZN7android8String1610editResizeEm(ptr noundef nonnull align 8 dereferenceable(8) %0, i64 noundef %14)
  %.not = icmp eq ptr %15, null
  br i1 %.not, label %28, label %16

16:                                               ; preds = %11
  %17 = tail call noundef ptr @_ZN7android12SharedBuffer4dataEv(ptr noundef nonnull align 8 dereferenceable(24) %15)
  %18 = icmp ugt i64 %5, %1
  br i1 %18, label %19, label %24

19:                                               ; preds = %16
  %20 = getelementptr inbounds i16, ptr %17, i64 %spec.select
  %21 = getelementptr inbounds i16, ptr %20, i64 %3
  %22 = sub i64 %5, %spec.select
  %23 = shl i64 %22, 1
  tail call void @llvm.memmove.p0.p0.i64(ptr nonnull align 2 %21, ptr align 2 %20, i64 %23, i1 false)
  br label %24

24:                                               ; preds = %19, %16
  %25 = getelementptr inbounds i16, ptr %17, i64 %spec.select
  %26 = shl i64 %3, 1
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 2 %25, ptr align 2 %2, i64 %26, i1 false)
  %27 = getelementptr inbounds i16, ptr %17, i64 %12
  store i16 0, ptr %27, align 2, !tbaa !5
  store ptr %17, ptr %0, align 8, !tbaa !9
  br label %28

28:                                               ; preds = %24, %11, %9, %7
  %.1 = phi i32 [ %8, %7 ], [ 0, %9 ], [ 0, %24 ], [ -12, %11 ]
  ret i32 %.1
}

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

attributes #0 = { 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 #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: readwrite) }
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 = { 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, !6, i64 0}
!6 = !{!"char16_t", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !11, i64 0}
!10 = !{!"_ZTSN7android8String16E", !11, i64 0}
!11 = !{!"any pointer", !7, i64 0}


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