; LLVM IR from div-bargali/Data-Structures-and-Algorithms :: C++/Algorithms/Searching-Algorithms (Find_kth_element)

; 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 nounwind uwtable
define dso_local noundef i32 @_Z15findKthPositiveRSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca [10001 x i32], align 16
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 40004, ptr %5) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  store i32 0, ptr %7, align 4, !tbaa !9
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %8

8:                                                ; preds = %16, %2
  %9 = load i32, ptr %6, align 4, !tbaa !9
  %10 = icmp sle i32 %9, 10000
  br i1 %10, label %11, label %19

11:                                               ; preds = %8
  %12 = load i32, ptr %6, align 4, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [10001 x i32], ptr %5, i64 0, i64 %14
  store i32 %12, ptr %15, align 4, !tbaa !9
  br label %16

16:                                               ; preds = %11
  %17 = load i32, ptr %6, align 4, !tbaa !9
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %6, align 4, !tbaa !9
  br label %8, !llvm.loop !11

19:                                               ; preds = %8
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %34, %19
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = sext i32 %21 to i64
  %23 = load ptr, ptr %3, align 8, !tbaa !5
  %24 = call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %23) #2
  %25 = icmp ult i64 %22, %24
  br i1 %25, label %26, label %37

26:                                               ; preds = %20
  %27 = load ptr, ptr %3, align 8, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %27, i64 noundef %29) #2
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [10001 x i32], ptr %5, i64 0, i64 %32
  store i32 -1, ptr %33, align 4, !tbaa !9
  br label %34

34:                                               ; preds = %26
  %35 = load i32, ptr %6, align 4, !tbaa !9
  %36 = add nsw i32 %35, 1
  store i32 %36, ptr %6, align 4, !tbaa !9
  br label %20, !llvm.loop !13

37:                                               ; preds = %20
  store i32 1, ptr %6, align 4, !tbaa !9
  br label %38

38:                                               ; preds = %56, %37
  %39 = load i32, ptr %6, align 4, !tbaa !9
  %40 = icmp sle i32 %39, 10000
  br i1 %40, label %41, label %59

41:                                               ; preds = %38
  %42 = load i32, ptr %6, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [10001 x i32], ptr %5, i64 0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !9
  %46 = icmp ne i32 %45, -1
  br i1 %46, label %47, label %50

47:                                               ; preds = %41
  %48 = load i32, ptr %4, align 4, !tbaa !9
  %49 = add nsw i32 %48, -1
  store i32 %49, ptr %4, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %47, %41
  %51 = load i32, ptr %4, align 4, !tbaa !9
  %52 = icmp eq i32 %51, 0
  br i1 %52, label %53, label %55

53:                                               ; preds = %50
  %54 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %54, ptr %7, align 4, !tbaa !9
  br label %59

55:                                               ; preds = %50
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %6, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %6, align 4, !tbaa !9
  br label %38, !llvm.loop !14

59:                                               ; preds = %53, %38
  %60 = load i32, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 40004, ptr %5) #2
  ret i32 %60
}

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) #0 align 2

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

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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"}
!13 = distinct !{!13, !12}
!14 = distinct !{!14, !12}


; 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 @_Z15findKthPositiveRSt6vectorIiSaIiEEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #0 {
vector.ph:
  %2 = alloca [10001 x i32], align 16
  call void @llvm.lifetime.start.p0(i64 40004, ptr nonnull %2) #2
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next.1, %vector.body ]
  %vec.ind = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %vector.ph ], [ %vec.ind.next.1, %vector.body ]
  %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
  %3 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %index
  %4 = getelementptr inbounds i8, ptr %3, i64 16
  store <4 x i32> %vec.ind, ptr %3, align 16, !tbaa !5
  store <4 x i32> %step.add, ptr %4, align 16, !tbaa !5
  %index.next = or disjoint i64 %index, 8
  %vec.ind.next = add <4 x i32> %vec.ind, <i32 8, i32 8, i32 8, i32 8>
  %step.add.1 = add <4 x i32> %vec.ind, <i32 12, i32 12, i32 12, i32 12>
  %5 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %index.next
  %6 = getelementptr inbounds i8, ptr %5, i64 16
  store <4 x i32> %vec.ind.next, ptr %5, align 16, !tbaa !5
  store <4 x i32> %step.add.1, ptr %6, align 16, !tbaa !5
  %index.next.1 = add nuw nsw i64 %index, 16
  %vec.ind.next.1 = add <4 x i32> %vec.ind, <i32 16, i32 16, i32 16, i32 16>
  %7 = icmp eq i64 %index.next.1, 10000
  br i1 %7, label %scalar.ph, label %vector.body, !llvm.loop !9

scalar.ph:                                        ; preds = %vector.body
  %8 = getelementptr inbounds i8, ptr %2, i64 40000
  store i32 10000, ptr %8, align 16, !tbaa !5
  %9 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #2
  %.not24 = icmp eq i64 %9, 0
  br i1 %.not24, label %.preheader.preheader, label %.lr.ph

.lr.ph:                                           ; preds = %scalar.ph, %.lr.ph
  %indvars.iv26 = phi i64 [ %indvars.iv.next27, %.lr.ph ], [ 0, %scalar.ph ]
  %10 = tail call noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24) %0, i64 noundef %indvars.iv26) #2
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %12
  store i32 -1, ptr %13, align 4, !tbaa !5
  %indvars.iv.next27 = add nuw nsw i64 %indvars.iv26, 1
  %14 = tail call noundef i64 @_ZNKSt6vectorIiSaIiEE4sizeEv(ptr noundef nonnull align 8 dereferenceable(24) %0) #2
  %15 = icmp ugt i64 %14, %indvars.iv.next27
  br i1 %15, label %.lr.ph, label %.preheader.preheader, !llvm.loop !13

.preheader.preheader:                             ; preds = %.lr.ph, %scalar.ph
  br label %.preheader

.preheader:                                       ; preds = %24, %.preheader.preheader
  %indvars.iv29 = phi i64 [ 1, %.preheader.preheader ], [ %indvars.iv.next30.1, %24 ]
  %.01622 = phi i32 [ %1, %.preheader.preheader ], [ %spec.select.1, %24 ]
  %16 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %indvars.iv29
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %.not = icmp ne i32 %17, -1
  %18 = sext i1 %.not to i32
  %spec.select = add nsw i32 %.01622, %18
  %19 = icmp eq i32 %spec.select, 0
  br i1 %19, label %.split.loop.exit, label %.preheader.1

.preheader.1:                                     ; preds = %.preheader
  %indvars.iv.next30 = add nuw nsw i64 %indvars.iv29, 1
  %20 = getelementptr inbounds [10001 x i32], ptr %2, i64 0, i64 %indvars.iv.next30
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %.not.1 = icmp ne i32 %21, -1
  %22 = sext i1 %.not.1 to i32
  %spec.select.1 = add nsw i32 %spec.select, %22
  %23 = icmp eq i32 %spec.select.1, 0
  br i1 %23, label %.split.loop.exit, label %24

24:                                               ; preds = %.preheader.1
  %indvars.iv.next30.1 = add nuw nsw i64 %indvars.iv29, 2
  %exitcond32.not.1 = icmp eq i64 %indvars.iv.next30.1, 10001
  br i1 %exitcond32.not.1, label %.split.loop.exit33, label %.preheader, !llvm.loop !14

.split.loop.exit:                                 ; preds = %.preheader.1, %.preheader
  %indvars.iv29.lcssa = phi i64 [ %indvars.iv29, %.preheader ], [ %indvars.iv.next30, %.preheader.1 ]
  %25 = trunc nuw nsw i64 %indvars.iv29.lcssa to i32
  br label %.split.loop.exit33

.split.loop.exit33:                               ; preds = %24, %.split.loop.exit
  %.0 = phi i32 [ %25, %.split.loop.exit ], [ 0, %24 ]
  call void @llvm.lifetime.end.p0(i64 40004, ptr nonnull %2) #2
  ret i32 %.0
}

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZNSt6vectorIiSaIiEEixEm(ptr noundef nonnull align 8 dereferenceable(24), i64 noundef) local_unnamed_addr #0 align 2

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

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 = { 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, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10}
!14 = distinct !{!14, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LCSSAPass,LICMPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass