; LLVM IR from Py-Contributors/AlgorithmsAndDataStructure :: C++/Algorithms/BinarySearch/KthElementOfSortedArrays.ll (Z10kthelementPiS_iii)

; 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: mustprogress uwtable
define dso_local noundef i32 @_Z10kthelementPiS_iii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) #0 {
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca i32, align 4
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  %22 = alloca i32, align 4
  store ptr %0, ptr %7, align 8, !tbaa !5
  store ptr %1, ptr %8, align 8, !tbaa !5
  store i32 %2, ptr %9, align 4, !tbaa !9
  store i32 %3, ptr %10, align 4, !tbaa !9
  store i32 %4, ptr %11, align 4, !tbaa !9
  %23 = load i32, ptr %9, align 4, !tbaa !9
  %24 = load i32, ptr %10, align 4, !tbaa !9
  %25 = icmp sgt i32 %23, %24
  br i1 %25, label %26, label %33

26:                                               ; preds = %5
  %27 = load ptr, ptr %8, align 8, !tbaa !5
  %28 = load ptr, ptr %7, align 8, !tbaa !5
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = load i32, ptr %11, align 4, !tbaa !9
  %32 = call noundef i32 @_Z10kthelementPiS_iii(ptr noundef %27, ptr noundef %28, i32 noundef %29, i32 noundef %30, i32 noundef %31)
  store i32 %32, ptr %6, align 4
  br label %128

33:                                               ; preds = %5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %34 = load i32, ptr %11, align 4, !tbaa !9
  %35 = load i32, ptr %9, align 4, !tbaa !9
  %36 = sub nsw i32 %34, %35
  store i32 %36, ptr %14, align 4, !tbaa !9
  %37 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %13, ptr noundef nonnull align 4 dereferenceable(4) %14)
  %38 = load i32, ptr %37, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  store i32 %38, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #3
  %39 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %11, ptr noundef nonnull align 4 dereferenceable(4) %10)
  %40 = load i32, ptr %39, align 4, !tbaa !9
  store i32 %40, ptr %15, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %125, %33
  %42 = load i32, ptr %12, align 4, !tbaa !9
  %43 = load i32, ptr %15, align 4, !tbaa !9
  %44 = icmp sle i32 %42, %43
  br i1 %44, label %45, label %126

45:                                               ; preds = %41
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #3
  %46 = load i32, ptr %12, align 4, !tbaa !9
  %47 = load i32, ptr %15, align 4, !tbaa !9
  %48 = add nsw i32 %46, %47
  %49 = ashr i32 %48, 1
  store i32 %49, ptr %16, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #3
  %50 = load i32, ptr %11, align 4, !tbaa !9
  %51 = load i32, ptr %16, align 4, !tbaa !9
  %52 = sub nsw i32 %50, %51
  store i32 %52, ptr %17, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #3
  %53 = load i32, ptr %16, align 4, !tbaa !9
  %54 = icmp eq i32 %53, 0
  br i1 %54, label %55, label %56

55:                                               ; preds = %45
  br label %63

56:                                               ; preds = %45
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = load i32, ptr %16, align 4, !tbaa !9
  %59 = sub nsw i32 %58, 1
  %60 = sext i32 %59 to i64
  %61 = getelementptr inbounds i32, ptr %57, i64 %60
  %62 = load i32, ptr %61, align 4, !tbaa !9
  br label %63

63:                                               ; preds = %56, %55
  %64 = phi i32 [ -2147483648, %55 ], [ %62, %56 ]
  store i32 %64, ptr %18, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #3
  %65 = load i32, ptr %17, align 4, !tbaa !9
  %66 = icmp eq i32 %65, 0
  br i1 %66, label %67, label %68

67:                                               ; preds = %63
  br label %75

68:                                               ; preds = %63
  %69 = load ptr, ptr %8, align 8, !tbaa !5
  %70 = load i32, ptr %17, align 4, !tbaa !9
  %71 = sub nsw i32 %70, 1
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds i32, ptr %69, i64 %72
  %74 = load i32, ptr %73, align 4, !tbaa !9
  br label %75

75:                                               ; preds = %68, %67
  %76 = phi i32 [ -2147483648, %67 ], [ %74, %68 ]
  store i32 %76, ptr %19, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #3
  %77 = load i32, ptr %16, align 4, !tbaa !9
  %78 = load i32, ptr %10, align 4, !tbaa !9
  %79 = icmp eq i32 %77, %78
  br i1 %79, label %80, label %81

80:                                               ; preds = %75
  br label %87

81:                                               ; preds = %75
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = load i32, ptr %16, align 4, !tbaa !9
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds i32, ptr %82, i64 %84
  %86 = load i32, ptr %85, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %81, %80
  %88 = phi i32 [ 2147483647, %80 ], [ %86, %81 ]
  store i32 %88, ptr %20, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #3
  %89 = load i32, ptr %17, align 4, !tbaa !9
  %90 = load i32, ptr %9, align 4, !tbaa !9
  %91 = icmp eq i32 %89, %90
  br i1 %91, label %92, label %93

92:                                               ; preds = %87
  br label %99

93:                                               ; preds = %87
  %94 = load ptr, ptr %8, align 8, !tbaa !5
  %95 = load i32, ptr %17, align 4, !tbaa !9
  %96 = sext i32 %95 to i64
  %97 = getelementptr inbounds i32, ptr %94, i64 %96
  %98 = load i32, ptr %97, align 4, !tbaa !9
  br label %99

99:                                               ; preds = %93, %92
  %100 = phi i32 [ 2147483647, %92 ], [ %98, %93 ]
  store i32 %100, ptr %21, align 4, !tbaa !9
  %101 = load i32, ptr %18, align 4, !tbaa !9
  %102 = load i32, ptr %21, align 4, !tbaa !9
  %103 = icmp sle i32 %101, %102
  br i1 %103, label %104, label %111

104:                                              ; preds = %99
  %105 = load i32, ptr %19, align 4, !tbaa !9
  %106 = load i32, ptr %20, align 4, !tbaa !9
  %107 = icmp sle i32 %105, %106
  br i1 %107, label %108, label %111

108:                                              ; preds = %104
  %109 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %18, ptr noundef nonnull align 4 dereferenceable(4) %19)
  %110 = load i32, ptr %109, align 4, !tbaa !9
  store i32 %110, ptr %6, align 4
  store i32 1, ptr %22, align 4
  br label %123

111:                                              ; preds = %104, %99
  %112 = load i32, ptr %18, align 4, !tbaa !9
  %113 = load i32, ptr %21, align 4, !tbaa !9
  %114 = icmp sgt i32 %112, %113
  br i1 %114, label %115, label %118

115:                                              ; preds = %111
  %116 = load i32, ptr %16, align 4, !tbaa !9
  %117 = sub nsw i32 %116, 1
  store i32 %117, ptr %15, align 4, !tbaa !9
  br label %121

118:                                              ; preds = %111
  %119 = load i32, ptr %16, align 4, !tbaa !9
  %120 = add nsw i32 %119, 1
  store i32 %120, ptr %12, align 4, !tbaa !9
  br label %121

121:                                              ; preds = %118, %115
  br label %122

122:                                              ; preds = %121
  store i32 0, ptr %22, align 4
  br label %123

123:                                              ; preds = %122, %108
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #3
  %124 = load i32, ptr %22, align 4
  switch i32 %124, label %127 [
    i32 0, label %125
  ]

125:                                              ; preds = %123
  br label %41, !llvm.loop !11

126:                                              ; preds = %41
  store i32 1, ptr %6, align 4
  store i32 1, ptr %22, align 4
  br label %127

127:                                              ; preds = %126, %123
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #3
  br label %128

128:                                              ; preds = %127, %26
  %129 = load i32, ptr %6, align 4
  ret i32 %129
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

attributes #0 = { 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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"int", !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: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z10kthelementPiS_iii(ptr nocapture noundef readonly %0, ptr nocapture noundef readonly %1, i32 noundef %2, i32 noundef %3, i32 noundef %4) local_unnamed_addr #0 {
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %4, ptr %7, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %tailrecurse, %5
  %.tr = phi ptr [ %0, %5 ], [ %.tr49, %tailrecurse ]
  %.tr49 = phi ptr [ %1, %5 ], [ %.tr, %tailrecurse ]
  %.tr50 = phi i32 [ %2, %5 ], [ %.tr51, %tailrecurse ]
  %.tr51 = phi i32 [ %3, %5 ], [ %.tr50, %tailrecurse ]
  %12 = icmp sgt i32 %.tr50, %.tr51
  br i1 %12, label %tailrecurse, label %13

13:                                               ; preds = %tailrecurse
  store i32 %.tr51, ptr %6, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %8) #3
  store i32 0, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %9) #3
  %14 = sub nsw i32 %4, %.tr50
  store i32 %14, ptr %9, align 4, !tbaa !5
  %15 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %8, ptr noundef nonnull align 4 dereferenceable(4) %9)
  %16 = load i32, ptr %15, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %8) #3
  %17 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %7, ptr noundef nonnull align 4 dereferenceable(4) %6)
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %invariant.gep = getelementptr i8, ptr %.tr, i64 -4
  %invariant.gep53 = getelementptr i8, ptr %.tr49, i64 -4
  %.not55 = icmp sgt i32 %16, %18
  br i1 %.not55, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %13
  %19 = load i32, ptr %7, align 4, !tbaa !5
  %20 = load i32, ptr %6, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %.lr.ph, %53
  %.03057 = phi i32 [ %16, %.lr.ph ], [ %.131, %53 ]
  %.03356 = phi i32 [ %18, %.lr.ph ], [ %.134, %53 ]
  %22 = add nsw i32 %.03057, %.03356
  %23 = ashr i32 %22, 1
  %24 = sub nsw i32 %19, %23
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %10) #3
  %25 = icmp ult i32 %22, 2
  br i1 %25, label %29, label %26

26:                                               ; preds = %21
  %27 = sext i32 %23 to i64
  %gep = getelementptr i32, ptr %invariant.gep, i64 %27
  %28 = load i32, ptr %gep, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %21, %26
  %30 = phi i32 [ %28, %26 ], [ -2147483648, %21 ]
  store i32 %30, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %11) #3
  %31 = icmp eq i32 %19, %23
  br i1 %31, label %35, label %32

32:                                               ; preds = %29
  %33 = sext i32 %24 to i64
  %gep54 = getelementptr i32, ptr %invariant.gep53, i64 %33
  %34 = load i32, ptr %gep54, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %29, %32
  %36 = phi i32 [ %34, %32 ], [ -2147483648, %29 ]
  store i32 %36, ptr %11, align 4, !tbaa !5
  %37 = icmp eq i32 %23, %20
  br i1 %37, label %42, label %38

38:                                               ; preds = %35
  %39 = sext i32 %23 to i64
  %40 = getelementptr inbounds i32, ptr %.tr, i64 %39
  %41 = load i32, ptr %40, align 4, !tbaa !5
  br label %42

42:                                               ; preds = %35, %38
  %43 = phi i32 [ %41, %38 ], [ 2147483647, %35 ]
  %44 = icmp eq i32 %24, %.tr50
  br i1 %44, label %49, label %45

45:                                               ; preds = %42
  %46 = sext i32 %24 to i64
  %47 = getelementptr inbounds i32, ptr %.tr49, i64 %46
  %48 = load i32, ptr %47, align 4, !tbaa !5
  br label %49

49:                                               ; preds = %42, %45
  %50 = phi i32 [ %48, %45 ], [ 2147483647, %42 ]
  %.not43 = icmp sgt i32 %30, %50
  %.not44 = icmp sgt i32 %36, %43
  %or.cond = select i1 %.not43, i1 true, i1 %.not44
  br i1 %or.cond, label %53, label %.thread

.thread:                                          ; preds = %49
  %51 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %10, ptr noundef nonnull align 4 dereferenceable(4) %11)
  %52 = load i32, ptr %51, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %10) #3
  br label %.loopexit

53:                                               ; preds = %49
  %54 = add nsw i32 %23, -1
  %55 = add nsw i32 %23, 1
  %.134 = select i1 %.not43, i32 %54, i32 %.03356
  %.131 = select i1 %.not43, i32 %.03057, i32 %55
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %10) #3
  %.not = icmp sgt i32 %.131, %.134
  br i1 %.not, label %.loopexit, label %21, !llvm.loop !9

.loopexit:                                        ; preds = %53, %13, %.thread
  %.3 = phi i32 [ %52, %.thread ], [ 1, %13 ], [ 1, %53 ]
  ret i32 %.3
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

attributes #0 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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