; LLVM IR from chachaxw/data-structure-and-algorithm :: leetcode/binary_search/sqrt.ll (Z4sqrti)

; 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 @_Z4sqrti(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = icmp slt i32 %8, 0
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  store i32 -1, ptr %2, align 4
  br label %57

11:                                               ; preds = %1
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp eq i32 %12, 0
  br i1 %13, label %14, label %15

14:                                               ; preds = %11
  store i32 0, ptr %2, align 4
  br label %57

15:                                               ; preds = %11
  br label %16

16:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #2
  store i64 1, ptr %4, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %17 = load i32, ptr %3, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  store i64 %18, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  br label %19

19:                                               ; preds = %52, %16
  %20 = load i64, ptr %4, align 8, !tbaa !9
  %21 = add nsw i64 %20, 1
  %22 = load i64, ptr %5, align 8, !tbaa !9
  %23 = icmp slt i64 %21, %22
  br i1 %23, label %24, label %53

24:                                               ; preds = %19
  %25 = load i64, ptr %4, align 8, !tbaa !9
  %26 = load i64, ptr %5, align 8, !tbaa !9
  %27 = load i64, ptr %4, align 8, !tbaa !9
  %28 = sub nsw i64 %26, %27
  %29 = sdiv i64 %28, 2
  %30 = add nsw i64 %25, %29
  store i64 %30, ptr %6, align 8, !tbaa !9
  %31 = load i64, ptr %6, align 8, !tbaa !9
  %32 = load i64, ptr %6, align 8, !tbaa !9
  %33 = mul nsw i64 %31, %32
  %34 = load i32, ptr %3, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = icmp eq i64 %33, %35
  br i1 %36, label %37, label %40

37:                                               ; preds = %24
  %38 = load i64, ptr %6, align 8, !tbaa !9
  %39 = trunc i64 %38 to i32
  store i32 %39, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %56

40:                                               ; preds = %24
  %41 = load i64, ptr %6, align 8, !tbaa !9
  %42 = load i64, ptr %6, align 8, !tbaa !9
  %43 = mul nsw i64 %41, %42
  %44 = load i32, ptr %3, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = icmp sgt i64 %43, %45
  br i1 %46, label %47, label %49

47:                                               ; preds = %40
  %48 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %48, ptr %5, align 8, !tbaa !9
  br label %51

49:                                               ; preds = %40
  %50 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %50, ptr %4, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %49, %47
  br label %52

52:                                               ; preds = %51
  br label %19, !llvm.loop !11

53:                                               ; preds = %19
  %54 = load i64, ptr %4, align 8, !tbaa !9
  %55 = trunc i64 %54 to i32
  store i32 %55, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %56

56:                                               ; preds = %53, %37
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #2
  br label %57

57:                                               ; preds = %56, %14, %10
  %58 = load i32, ptr %2, align 4
  ret i32 %58
}

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

; 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !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 nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i32 @_Z4sqrti(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 0
  br i1 %2, label %._crit_edge, label %3

3:                                                ; preds = %1
  %4 = icmp eq i32 %0, 0
  br i1 %4, label %._crit_edge, label %5

5:                                                ; preds = %3
  %6 = zext nneg i32 %0 to i64
  %7 = icmp ugt i32 %0, 2
  br i1 %7, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %5, %13
  %.01927 = phi i64 [ %..019, %13 ], [ %6, %5 ]
  %.02126 = phi i64 [ %.021., %13 ], [ 1, %5 ]
  %8 = sub nsw i64 %.01927, %.02126
  %9 = sdiv i64 %8, 2
  %10 = add nsw i64 %9, %.02126
  %11 = mul nsw i64 %10, %10
  %12 = icmp eq i64 %11, %6
  br i1 %12, label %._crit_edge.loopexit, label %13

13:                                               ; preds = %.lr.ph
  %14 = icmp ugt i64 %11, %6
  %.021. = select i1 %14, i64 %.02126, i64 %10
  %..019 = select i1 %14, i64 %10, i64 %.01927
  %15 = add nsw i64 %.021., 1
  %16 = icmp slt i64 %15, %..019
  br i1 %16, label %.lr.ph, label %._crit_edge.loopexit, !llvm.loop !5

._crit_edge.loopexit:                             ; preds = %13, %.lr.ph
  %.0.in.ph = phi i64 [ %10, %.lr.ph ], [ %.021., %13 ]
  %17 = trunc i64 %.0.in.ph to i32
  br label %._crit_edge

._crit_edge:                                      ; preds = %5, %._crit_edge.loopexit, %3, %1
  %.1 = phi i32 [ -1, %1 ], [ 0, %3 ], [ 1, %5 ], [ %17, %._crit_edge.loopexit ]
  ret i32 %.1
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


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