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

; 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 { i64, i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z7findKthxx(i64 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca %class.anon, align 8
  %6 = alloca i64, align 8
  %7 = alloca i64, align 8
  %8 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  %9 = getelementptr inbounds %class.anon, ptr %5, i32 0, i32 0
  %10 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %10, ptr %9, align 8, !tbaa !9
  %11 = getelementptr inbounds %class.anon, ptr %5, i32 0, i32 1
  %12 = load i64, ptr %4, align 8, !tbaa !5
  store i64 %12, ptr %11, align 8, !tbaa !11
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store i64 1, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = load i64, ptr %3, align 8, !tbaa !5
  %15 = mul nsw i64 %13, %14
  %16 = add nsw i64 %15, 1
  store i64 %16, ptr %7, align 8, !tbaa !5
  br label %17

17:                                               ; preds = %35, %2
  %18 = load i64, ptr %6, align 8, !tbaa !5
  %19 = add nsw i64 %18, 1
  %20 = load i64, ptr %7, align 8, !tbaa !5
  %21 = icmp slt i64 %19, %20
  br i1 %21, label %22, label %36

22:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %23 = load i64, ptr %6, align 8, !tbaa !5
  %24 = load i64, ptr %7, align 8, !tbaa !5
  %25 = load i64, ptr %6, align 8, !tbaa !5
  %26 = sub nsw i64 %24, %25
  %27 = sdiv i64 %26, 2
  %28 = add nsw i64 %23, %27
  store i64 %28, ptr %8, align 8, !tbaa !5
  %29 = load i64, ptr %8, align 8, !tbaa !5
  %30 = call noundef zeroext i1 @"_ZZ7findKthxxENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(16) %5, i64 noundef %29)
  br i1 %30, label %31, label %33

31:                                               ; preds = %22
  %32 = load i64, ptr %8, align 8, !tbaa !5
  store i64 %32, ptr %6, align 8, !tbaa !5
  br label %35

33:                                               ; preds = %22
  %34 = load i64, ptr %8, align 8, !tbaa !5
  store i64 %34, ptr %7, align 8, !tbaa !5
  br label %35

35:                                               ; preds = %33, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %17, !llvm.loop !12

36:                                               ; preds = %17
  %37 = load i64, ptr %6, align 8, !tbaa !5
  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 i64 %37
}

; 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 @"_ZZ7findKthxxENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(16), 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSZ7findKthxxE3$_0", !6, i64 0, !6, i64 8}
!11 = !{!10, !6, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"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 { i64, i64 }

; Function Attrs: mustprogress uwtable
define dso_local noundef i64 @_Z7findKthxx(i64 noundef %0, i64 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 i64 %0, ptr %3, align 8, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  store i64 %1, ptr %4, align 8, !tbaa !10
  %5 = mul nsw i64 %0, %0
  %6 = icmp ugt i64 %5, 1
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %2
  %7 = add nuw nsw i64 %5, 1
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.016 = phi i64 [ %..0, %.lr.ph ], [ 1, %.lr.ph.preheader ]
  %.01315 = phi i64 [ %.013., %.lr.ph ], [ %7, %.lr.ph.preheader ]
  %8 = sub nsw i64 %.01315, %.016
  %9 = sdiv i64 %8, 2
  %10 = add nsw i64 %9, %.016
  %11 = call noundef zeroext i1 @"_ZZ7findKthxxENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(16) %3, i64 noundef %10)
  %.013. = select i1 %11, i64 %.01315, i64 %10
  %..0 = select i1 %11, i64 %10, i64 %.016
  %12 = add nsw i64 %..0, 1
  %13 = icmp slt i64 %12, %.013.
  br i1 %13, label %.lr.ph, label %._crit_edge, !llvm.loop !11

._crit_edge:                                      ; preds = %.lr.ph, %2
  %.0.lcssa = phi i64 [ 1, %2 ], [ %..0, %.lr.ph ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  ret i64 %.0.lcssa
}

; 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 @"_ZZ7findKthxxENK3$_0clEx"(ptr noundef nonnull align 8 dereferenceable(16), 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, !7, i64 0}
!6 = !{!"_ZTSZ7findKthxxE3$_0", !7, i64 0, !7, i64 8}
!7 = !{!"long long", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!6, !7, i64 8}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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