; LLVM IR from facebook/redex :: libresource/VectorImpl.ll (ZN7android16SortedVectorImpl5mergeERKNS_10VectorImplE)

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

; 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 @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40)) #1 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN7android16SortedVectorImpl5mergeERKNS_10VectorImplE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(40) %1) #3 align 2 {
  %3 = alloca i64, align 8
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i64, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  %12 = load ptr, ptr %4, align 8
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  %14 = call noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40) %13)
  br i1 %14, label %50, label %15

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = call noundef ptr @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40) %16)
  store ptr %17, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  %18 = call noundef i64 @_ZNK7android10VectorImpl8itemSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %12)
  store i64 %18, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = call noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40) %19)
  store i64 %20, ptr %8, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  store i64 0, ptr %9, align 8, !tbaa !9
  br label %21

21:                                               ; preds = %41, %15
  %22 = load i64, ptr %9, align 8, !tbaa !9
  %23 = load i64, ptr %8, align 8, !tbaa !9
  %24 = icmp ult i64 %22, %23
  br i1 %24, label %26, label %25

25:                                               ; preds = %21
  store i32 2, ptr %10, align 4
  br label %44

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  %27 = load ptr, ptr %6, align 8, !tbaa !5
  %28 = load i64, ptr %9, align 8, !tbaa !9
  %29 = load i64, ptr %7, align 8, !tbaa !9
  %30 = mul i64 %28, %29
  %31 = getelementptr inbounds i8, ptr %27, i64 %30
  %32 = call noundef i64 @_ZN7android16SortedVectorImpl3addEPKv(ptr noundef nonnull align 8 dereferenceable(40) %12, ptr noundef %31)
  store i64 %32, ptr %11, align 8, !tbaa !9
  %33 = load i64, ptr %11, align 8, !tbaa !9
  %34 = icmp slt i64 %33, 0
  br i1 %34, label %35, label %37

35:                                               ; preds = %26
  %36 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %36, ptr %3, align 8
  store i32 1, ptr %10, align 4
  br label %38

37:                                               ; preds = %26
  store i32 0, ptr %10, align 4
  br label %38

38:                                               ; preds = %37, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  %39 = load i32, ptr %10, align 4
  switch i32 %39, label %44 [
    i32 0, label %40
  ]

40:                                               ; preds = %38
  br label %41

41:                                               ; preds = %40
  %42 = load i64, ptr %9, align 8, !tbaa !9
  %43 = add i64 %42, 1
  store i64 %43, ptr %9, align 8, !tbaa !9
  br label %21, !llvm.loop !11

44:                                               ; preds = %38, %25
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %45 = load i32, ptr %10, align 4
  switch i32 %45, label %47 [
    i32 2, label %46
  ]

46:                                               ; preds = %44
  store i32 0, ptr %10, align 4
  br label %47

47:                                               ; preds = %46, %44
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  %48 = load i32, ptr %10, align 4
  switch i32 %48, label %53 [
    i32 0, label %49
    i32 1, label %51
  ]

49:                                               ; preds = %47
  br label %50

50:                                               ; preds = %49, %2
  store i64 0, ptr %3, align 8
  br label %51

51:                                               ; preds = %50, %47
  %52 = load i64, ptr %3, align 8
  ret i64 %52

53:                                               ; preds = %47
  unreachable
}

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 = { 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 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


; 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 @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40)) local_unnamed_addr #0 align 2

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

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_ZN7android16SortedVectorImpl5mergeERKNS_10VectorImplE(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef nonnull align 8 dereferenceable(40) %1) local_unnamed_addr #2 align 2 {
  %3 = tail call noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40) %1)
  br i1 %3, label %.thread, label %4

4:                                                ; preds = %2
  %5 = tail call noundef ptr @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40) %1)
  %6 = tail call noundef i64 @_ZNK7android10VectorImpl8itemSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %7 = tail call noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40) %1)
  %.not22.not = icmp eq i64 %7, 0
  br i1 %.not22.not, label %.thread, label %.lr.ph

8:                                                ; preds = %.lr.ph
  %9 = add nuw i64 %.01823, 1
  %exitcond.not = icmp eq i64 %9, %7
  br i1 %exitcond.not, label %.thread, label %.lr.ph, !llvm.loop !5

.lr.ph:                                           ; preds = %4, %8
  %.01823 = phi i64 [ %9, %8 ], [ 0, %4 ]
  %10 = mul i64 %.01823, %6
  %11 = getelementptr inbounds i8, ptr %5, i64 %10
  %12 = tail call noundef i64 @_ZN7android16SortedVectorImpl3addEPKv(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %11)
  %13 = icmp sgt i64 %12, -1
  br i1 %13, label %8, label %.thread

.thread:                                          ; preds = %.lr.ph, %8, %4, %2
  %.3 = phi i64 [ 0, %2 ], [ 0, %4 ], [ %12, %.lr.ph ], [ 0, %8 ]
  ret 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 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" }

!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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass