; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 30. CPP Programs/Square _root _of _a_number.ll (Z9floorSqrtx)

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

11:                                               ; preds = %1
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = icmp eq i64 %12, 1
  br i1 %13, label %14, label %16

14:                                               ; preds = %11, %1
  %15 = load i64, ptr %3, align 8, !tbaa !5
  store i64 %15, ptr %2, align 8
  br label %54

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

18:                                               ; preds = %50, %16
  %19 = load i64, ptr %4, align 8, !tbaa !5
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = icmp sle i64 %19, %20
  br i1 %21, label %22, label %51

22:                                               ; preds = %18
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %23 = load i64, ptr %4, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = add nsw i64 %23, %24
  %26 = sdiv i64 %25, 2
  store i64 %26, ptr %7, align 8, !tbaa !5
  %27 = load i64, ptr %7, align 8, !tbaa !5
  %28 = load i64, ptr %7, align 8, !tbaa !5
  %29 = mul nsw i64 %27, %28
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = icmp eq i64 %29, %30
  br i1 %31, label %32, label %34

32:                                               ; preds = %22
  %33 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %33, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %48

34:                                               ; preds = %22
  %35 = load i64, ptr %7, align 8, !tbaa !5
  %36 = load i64, ptr %7, align 8, !tbaa !5
  %37 = mul nsw i64 %35, %36
  %38 = load i64, ptr %3, align 8, !tbaa !5
  %39 = icmp slt i64 %37, %38
  br i1 %39, label %40, label %44

40:                                               ; preds = %34
  %41 = load i64, ptr %7, align 8, !tbaa !5
  %42 = add nsw i64 %41, 1
  store i64 %42, ptr %4, align 8, !tbaa !5
  %43 = load i64, ptr %7, align 8, !tbaa !5
  store i64 %43, ptr %6, align 8, !tbaa !5
  br label %47

44:                                               ; preds = %34
  %45 = load i64, ptr %7, align 8, !tbaa !5
  %46 = sub nsw i64 %45, 1
  store i64 %46, ptr %5, align 8, !tbaa !5
  br label %47

47:                                               ; preds = %44, %40
  store i32 0, ptr %8, align 4
  br label %48

48:                                               ; preds = %47, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %49 = load i32, ptr %8, align 4
  switch i32 %49, label %53 [
    i32 0, label %50
  ]

50:                                               ; preds = %48
  br label %18, !llvm.loop !9

51:                                               ; preds = %18
  %52 = load i64, ptr %6, align 8, !tbaa !5
  store i64 %52, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %53

53:                                               ; preds = %51, %48
  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 %54

54:                                               ; preds = %53, %14
  %55 = load i64, ptr %2, align 8
  ret i64 %55
}

; 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"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 i64 @_Z9floorSqrtx(i64 noundef %0) local_unnamed_addr #0 {
  %or.cond = icmp ult i64 %0, 2
  br i1 %or.cond, label %.thread, label %.preheader

.preheader:                                       ; preds = %1
  %.not39 = icmp sgt i64 %0, 0
  tail call void @llvm.assume(i1 %.not39)
  br label %.lr.ph

.lr.ph:                                           ; preds = %.preheader, %5
  %.02342 = phi i64 [ %.124, %5 ], [ undef, %.preheader ]
  %.02641 = phi i64 [ %.127, %5 ], [ %0, %.preheader ]
  %.02940 = phi i64 [ %.130, %5 ], [ 1, %.preheader ]
  %2 = add nuw nsw i64 %.02641, %.02940
  %3 = lshr i64 %2, 1
  %4 = mul nuw nsw i64 %3, %3
  %.not34 = icmp eq i64 %4, %0
  br i1 %.not34, label %.thread, label %5

5:                                                ; preds = %.lr.ph
  %6 = icmp ult i64 %4, %0
  %7 = add nuw nsw i64 %3, 1
  %8 = add nsw i64 %3, -1
  %.130 = select i1 %6, i64 %7, i64 %.02940
  %.127 = select i1 %6, i64 %.02641, i64 %8
  %.124 = select i1 %6, i64 %3, i64 %.02342
  %.not = icmp sgt i64 %.130, %.127
  br i1 %.not, label %.thread, label %.lr.ph

.thread:                                          ; preds = %5, %.lr.ph, %1
  %.3 = phi i64 [ %0, %1 ], [ %.124, %5 ], [ %3, %.lr.ph ]
  ret i64 %.3
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #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" }
attributes #1 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }

!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)"}


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