; LLVM IR from forestLoop/Learning-ITMO-Academy-Pilot-Course :: binary-search/5-A.ll (Z7findKthRKSt6vectorISt4pairIiiESaIS1_EEi)

; 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.anon = type <{ ptr, i32, [4 x i8] }>

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7findKthRKSt6vectorISt4pairIiiESaIS1_EEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca %class.anon, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  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 16, ptr %5) #3
  %9 = getelementptr inbounds %class.anon, ptr %5, i32 0, i32 0
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %10, ptr %9, align 8, !tbaa !5
  %11 = getelementptr inbounds %class.anon, ptr %5, i32 0, i32 1
  %12 = load i32, ptr %4, align 4, !tbaa !9
  store i32 %12, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 -2000000000, ptr %6, align 8, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  store i64 2000000001, ptr %7, align 8, !tbaa !13
  br label %13

13:                                               ; preds = %31, %2
  %14 = load i64, ptr %6, align 8, !tbaa !13
  %15 = add nsw i64 %14, 1
  %16 = load i64, ptr %7, align 8, !tbaa !13
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %18, label %32

18:                                               ; preds = %13
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %19 = load i64, ptr %6, align 8, !tbaa !13
  %20 = load i64, ptr %7, align 8, !tbaa !13
  %21 = load i64, ptr %6, align 8, !tbaa !13
  %22 = sub nsw i64 %20, %21
  %23 = sdiv i64 %22, 2
  %24 = add nsw i64 %19, %23
  store i64 %24, ptr %8, align 8, !tbaa !13
  %25 = load i64, ptr %8, align 8, !tbaa !13
  %26 = call noundef zeroext i1 @"_ZZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(12) %5, i64 noundef %25)
  br i1 %26, label %27, label %29

27:                                               ; preds = %18
  %28 = load i64, ptr %8, align 8, !tbaa !13
  store i64 %28, ptr %6, align 8, !tbaa !13
  br label %31

29:                                               ; preds = %18
  %30 = load i64, ptr %8, align 8, !tbaa !13
  store i64 %30, ptr %7, align 8, !tbaa !13
  br label %31

31:                                               ; preds = %29, %27
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %13, !llvm.loop !15

32:                                               ; preds = %13
  %33 = load i64, ptr %6, align 8, !tbaa !13
  %34 = trunc i64 %33 to i32
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  ret i32 %34
}

; 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 uwtable
declare hidden noundef zeroext i1 @"_ZZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(12), i64 noundef) #2 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 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 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 = !{!12, !10, i64 8}
!12 = !{!"_ZTSZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiE3$_0", !6, i64 0, !10, i64 8}
!13 = !{!14, !14, i64 0}
!14 = !{!"long long", !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"

%class.anon = type <{ ptr, i32, [4 x i8] }>

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z7findKthRKSt6vectorISt4pairIiiESaIS1_EEi(ptr noundef nonnull align 8 dereferenceable(24) %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca %class.anon, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  store ptr %0, ptr %3, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  store i32 %1, ptr %4, align 8, !tbaa !9
  br label %5

5:                                                ; preds = %2, %5
  %.013 = phi i64 [ -2000000000, %2 ], [ %..0, %5 ]
  %.01012 = phi i64 [ 2000000001, %2 ], [ %.010., %5 ]
  %6 = sub nsw i64 %.01012, %.013
  %7 = sdiv i64 %6, 2
  %8 = add nsw i64 %7, %.013
  %9 = call noundef zeroext i1 @"_ZZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(12) %3, i64 noundef %8)
  %.010. = select i1 %9, i64 %.01012, i64 %8
  %..0 = select i1 %9, i64 %8, i64 %.013
  %10 = add nsw i64 %..0, 1
  %11 = icmp slt i64 %10, %.010.
  br i1 %11, label %5, label %12, !llvm.loop !12

12:                                               ; preds = %5
  %13 = trunc i64 %..0 to i32
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  ret i32 %13
}

; 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 uwtable
declare hidden noundef zeroext i1 @"_ZZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(12), i64 noundef) local_unnamed_addr #2 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 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 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, !11, i64 8}
!10 = !{!"_ZTSZ7findKthRKSt6vectorISt4pairIiiESaIS1_EEiE3$_0", !6, i64 0, !11, i64 8}
!11 = !{!"int", !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,ReassociatePass,SROAPass,SimplifyCFGPass