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

; 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: mustprogress uwtable
define dso_local noundef i64 @_ZNK7android16SortedVectorImpl13_indexOrderOfEPKvPm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, ptr noundef %2) #3 align 2 {
  %4 = alloca i64, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i64, align 8
  %9 = alloca i64, align 8
  %10 = alloca i64, align 8
  %11 = alloca i64, align 8
  %12 = alloca ptr, align 8
  %13 = alloca i64, align 8
  %14 = alloca ptr, align 8
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %17 = load ptr, ptr %5, align 8
  %18 = load ptr, ptr %7, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %22

20:                                               ; preds = %3
  %21 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 0, ptr %21, align 8, !tbaa !9
  br label %22

22:                                               ; preds = %20, %3
  %23 = call noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40) %17)
  br i1 %23, label %24, label %25

24:                                               ; preds = %22
  store i64 -2, ptr %4, align 8
  br label %78

25:                                               ; preds = %22
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  store i64 -2, 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %26 = call noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40) %17)
  %27 = sub i64 %26, 1
  store i64 %27, ptr %10, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #4
  %28 = call noundef ptr @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40) %17)
  store ptr %28, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #4
  %29 = call noundef i64 @_ZNK7android10VectorImpl8itemSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %17)
  store i64 %29, ptr %13, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %69, %25
  %31 = load i64, ptr %9, align 8, !tbaa !9
  %32 = load i64, ptr %10, align 8, !tbaa !9
  %33 = icmp sle i64 %31, %32
  br i1 %33, label %34, label %70

34:                                               ; preds = %30
  %35 = load i64, ptr %9, align 8, !tbaa !9
  %36 = load i64, ptr %10, align 8, !tbaa !9
  %37 = load i64, ptr %9, align 8, !tbaa !9
  %38 = sub nsw i64 %36, %37
  %39 = sdiv i64 %38, 2
  %40 = add nsw i64 %35, %39
  store i64 %40, ptr %11, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %14) #4
  %41 = load ptr, ptr %12, align 8, !tbaa !5
  %42 = load i64, ptr %11, align 8, !tbaa !9
  %43 = load i64, ptr %13, align 8, !tbaa !9
  %44 = mul i64 %42, %43
  %45 = getelementptr inbounds i8, ptr %41, i64 %44
  store ptr %45, ptr %14, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  %46 = load ptr, ptr %14, align 8, !tbaa !5
  %47 = load ptr, ptr %6, align 8, !tbaa !5
  %48 = load ptr, ptr %17, align 8, !tbaa !11
  %49 = getelementptr inbounds ptr, ptr %48, i64 8
  %50 = load ptr, ptr %49, align 8
  %51 = call noundef i32 %50(ptr noundef nonnull align 8 dereferenceable(40) %17, ptr noundef %46, ptr noundef %47)
  store i32 %51, ptr %15, align 4, !tbaa !13
  %52 = load i32, ptr %15, align 4, !tbaa !13
  %53 = icmp eq i32 %52, 0
  br i1 %53, label %54, label %56

54:                                               ; preds = %34
  %55 = load i64, ptr %11, align 8, !tbaa !9
  store i64 %55, ptr %9, align 8, !tbaa !9
  store i64 %55, ptr %8, align 8, !tbaa !9
  store i32 3, ptr %16, align 4
  br label %67

56:                                               ; preds = %34
  %57 = load i32, ptr %15, align 4, !tbaa !13
  %58 = icmp slt i32 %57, 0
  br i1 %58, label %59, label %62

59:                                               ; preds = %56
  %60 = load i64, ptr %11, align 8, !tbaa !9
  %61 = add nsw i64 %60, 1
  store i64 %61, ptr %9, align 8, !tbaa !9
  br label %65

62:                                               ; preds = %56
  %63 = load i64, ptr %11, align 8, !tbaa !9
  %64 = sub nsw i64 %63, 1
  store i64 %64, ptr %10, align 8, !tbaa !9
  br label %65

65:                                               ; preds = %62, %59
  br label %66

66:                                               ; preds = %65
  store i32 0, ptr %16, align 4
  br label %67

67:                                               ; preds = %66, %54
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %14) #4
  %68 = load i32, ptr %16, align 4
  switch i32 %68, label %80 [
    i32 0, label %69
    i32 3, label %70
  ]

69:                                               ; preds = %67
  br label %30, !llvm.loop !15

70:                                               ; preds = %67, %30
  %71 = load ptr, ptr %7, align 8, !tbaa !5
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %76

73:                                               ; preds = %70
  %74 = load i64, ptr %9, align 8, !tbaa !9
  %75 = load ptr, ptr %7, align 8, !tbaa !5
  store i64 %74, ptr %75, align 8, !tbaa !9
  br label %76

76:                                               ; preds = %73, %70
  %77 = load i64, ptr %8, align 8, !tbaa !9
  store i64 %77, ptr %4, align 8
  store i32 1, ptr %16, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  br label %78

78:                                               ; preds = %76, %24
  %79 = load i64, ptr %4, align 8
  ret i64 %79

80:                                               ; preds = %67
  unreachable
}

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

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 = !{!12, !12, i64 0}
!12 = !{!"vtable pointer", !8, i64 0}
!13 = !{!14, !14, i64 0}
!14 = !{!"int", !7, i64 0}
!15 = distinct !{!15, !16}
!16 = !{!"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: mustprogress uwtable
define dso_local noundef i64 @_ZNK7android16SortedVectorImpl13_indexOrderOfEPKvPm(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %1, ptr noundef writeonly %2) local_unnamed_addr #2 align 2 {
  %.not = icmp eq ptr %2, null
  br i1 %.not, label %5, label %4

4:                                                ; preds = %3
  store i64 0, ptr %2, align 8, !tbaa !5
  br label %5

5:                                                ; preds = %4, %3
  %6 = tail call noundef zeroext i1 @_ZNK7android10VectorImpl7isEmptyEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  br i1 %6, label %27, label %7

7:                                                ; preds = %5
  %8 = tail call noundef i64 @_ZNK7android10VectorImpl4sizeEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %9 = add i64 %8, -1
  %10 = tail call noundef ptr @_ZNK7android10VectorImpl9arrayImplEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %11 = tail call noundef i64 @_ZNK7android10VectorImpl8itemSizeEv(ptr noundef nonnull align 8 dereferenceable(40) %0)
  %.not3440 = icmp slt i64 %9, 0
  br i1 %.not3440, label %.thread, label %.lr.ph

.lr.ph:                                           ; preds = %7, %22
  %.02542 = phi i64 [ %.126, %22 ], [ 0, %7 ]
  %.02841 = phi i64 [ %.129, %22 ], [ %9, %7 ]
  %12 = sub nsw i64 %.02841, %.02542
  %13 = sdiv i64 %12, 2
  %14 = add nsw i64 %13, %.02542
  %15 = mul i64 %14, %11
  %16 = getelementptr inbounds i8, ptr %10, i64 %15
  %17 = load ptr, ptr %0, align 8, !tbaa !9
  %18 = getelementptr inbounds i8, ptr %17, i64 64
  %19 = load ptr, ptr %18, align 8
  %20 = tail call noundef i32 %19(ptr noundef nonnull align 8 dereferenceable(40) %0, ptr noundef %16, ptr noundef %1)
  %21 = icmp eq i32 %20, 0
  br i1 %21, label %.thread, label %22

22:                                               ; preds = %.lr.ph
  %23 = icmp slt i32 %20, 0
  %24 = add nsw i64 %14, 1
  %25 = add nsw i64 %14, -1
  %.129 = select i1 %23, i64 %.02841, i64 %25
  %.126 = select i1 %23, i64 %24, i64 %.02542
  %.not34 = icmp sgt i64 %.126, %.129
  br i1 %.not34, label %.thread, label %.lr.ph

.thread:                                          ; preds = %22, %.lr.ph, %7
  %.3 = phi i64 [ 0, %7 ], [ %14, %.lr.ph ], [ %.126, %22 ]
  %.2 = phi i64 [ -2, %7 ], [ %14, %.lr.ph ], [ -2, %22 ]
  br i1 %.not, label %27, label %26

26:                                               ; preds = %.thread
  store i64 %.3, ptr %2, align 8, !tbaa !5
  br label %27

27:                                               ; preds = %.thread, %26, %5
  %.023 = phi i64 [ -2, %5 ], [ %.2, %26 ], [ %.2, %.thread ]
  ret i64 %.023
}

; 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

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 = !{!6, !6, i64 0}
!6 = !{!"long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"vtable pointer", !8, i64 0}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass