; LLVM IR from TheAlgorithms/C-Plus-Plus :: math/sqrt_double.ll (Z4Sqrtd)

; 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 uwtable
define dso_local noundef double @_Z4Sqrtd(double noundef %0) #0 {
  %2 = alloca double, align 8
  %3 = alloca double, align 8
  %4 = alloca double, align 8
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  store double %0, ptr %3, align 8, !tbaa !5
  %9 = load double, ptr %3, align 8, !tbaa !5
  %10 = fcmp ogt double %9, 0.000000e+00
  br i1 %10, label %11, label %19

11:                                               ; preds = %1
  %12 = load double, ptr %3, align 8, !tbaa !5
  %13 = fcmp olt double %12, 1.000000e+00
  br i1 %13, label %14, label %19

14:                                               ; preds = %11
  %15 = load double, ptr %3, align 8, !tbaa !5
  %16 = fdiv double 1.000000e+00, %15
  %17 = call noundef double @_Z4Sqrtd(double noundef %16)
  %18 = fdiv double 1.000000e+00, %17
  store double %18, ptr %2, align 8
  br label %55

19:                                               ; preds = %11, %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  store double 0.000000e+00, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %20 = load double, ptr %3, align 8, !tbaa !5
  store double %20, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store double 0x3D719799812DEA11, ptr %6, align 8, !tbaa !5
  br label %21

21:                                               ; preds = %52, %19
  %22 = load double, ptr %4, align 8, !tbaa !5
  %23 = load double, ptr %5, align 8, !tbaa !5
  %24 = fcmp ole double %22, %23
  br i1 %24, label %25, label %53

25:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %26 = load double, ptr %4, align 8, !tbaa !5
  %27 = load double, ptr %5, align 8, !tbaa !5
  %28 = fadd double %26, %27
  %29 = fdiv double %28, 2.000000e+00
  store double %29, ptr %7, align 8, !tbaa !5
  %30 = load double, ptr %7, align 8, !tbaa !5
  %31 = load double, ptr %7, align 8, !tbaa !5
  %32 = fmul double %30, %31
  %33 = load double, ptr %3, align 8, !tbaa !5
  %34 = fcmp ogt double %32, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %25
  %36 = load double, ptr %7, align 8, !tbaa !5
  store double %36, ptr %5, align 8, !tbaa !5
  br label %49

37:                                               ; preds = %25
  %38 = load double, ptr %3, align 8, !tbaa !5
  %39 = load double, ptr %7, align 8, !tbaa !5
  %40 = load double, ptr %7, align 8, !tbaa !5
  %41 = fneg double %39
  %42 = call double @llvm.fmuladd.f64(double %41, double %40, double %38)
  %43 = load double, ptr %6, align 8, !tbaa !5
  %44 = fcmp olt double %42, %43
  br i1 %44, label %45, label %47

45:                                               ; preds = %37
  %46 = load double, ptr %7, align 8, !tbaa !5
  store double %46, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %50

47:                                               ; preds = %37
  %48 = load double, ptr %7, align 8, !tbaa !5
  store double %48, ptr %4, align 8, !tbaa !5
  br label %49

49:                                               ; preds = %47, %35
  store i32 0, ptr %8, align 4
  br label %50

50:                                               ; preds = %49, %45
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  %51 = load i32, ptr %8, align 4
  switch i32 %51, label %54 [
    i32 0, label %52
  ]

52:                                               ; preds = %50
  br label %21, !llvm.loop !9

53:                                               ; preds = %21
  store double -1.000000e+00, ptr %2, align 8
  store i32 1, ptr %8, align 4
  br label %54

54:                                               ; preds = %53, %50
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  br label %55

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

; 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 speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #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 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
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 = !{!"double", !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 nosync nounwind willreturn memory(none) uwtable
define dso_local noundef double @_Z4Sqrtd(double noundef %0) local_unnamed_addr #0 {
  %2 = fcmp ogt double %0, 0.000000e+00
  %3 = fcmp olt double %0, 1.000000e+00
  %or.cond = and i1 %2, %3
  br i1 %or.cond, label %5, label %.preheader

.preheader:                                       ; preds = %1
  %4 = fcmp ult double %0, 0.000000e+00
  br i1 %4, label %common.ret37, label %.lr.ph

common.ret37:                                     ; preds = %.preheader, %17, %13, %5
  %common.ret37.op = phi double [ %8, %5 ], [ -1.000000e+00, %.preheader ], [ %10, %13 ], [ -1.000000e+00, %17 ]
  ret double %common.ret37.op

5:                                                ; preds = %1
  %6 = fdiv double 1.000000e+00, %0
  %7 = tail call noundef double @_Z4Sqrtd(double noundef %6)
  %8 = fdiv double 1.000000e+00, %7
  br label %common.ret37

.lr.ph:                                           ; preds = %.preheader, %17
  %.02134 = phi double [ %.223, %17 ], [ 0.000000e+00, %.preheader ]
  %.02433 = phi double [ %.226, %17 ], [ %0, %.preheader ]
  %9 = fadd double %.02433, %.02134
  %10 = fmul double %9, 5.000000e-01
  %11 = fmul double %10, %10
  %12 = fcmp ogt double %11, %0
  br i1 %12, label %17, label %13

13:                                               ; preds = %.lr.ph
  %14 = fneg double %10
  %15 = tail call double @llvm.fmuladd.f64(double %14, double %10, double %0)
  %16 = fcmp olt double %15, 0x3D719799812DEA11
  br i1 %16, label %common.ret37, label %17

17:                                               ; preds = %13, %.lr.ph
  %.226 = phi double [ %10, %.lr.ph ], [ %.02433, %13 ]
  %.223 = phi double [ %.02134, %.lr.ph ], [ %10, %13 ]
  %18 = fcmp ugt double %.223, %.226
  br i1 %18, label %common.ret37, label %.lr.ph, !llvm.loop !5
}

; Function Attrs: mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare double @llvm.fmuladd.f64(double, double, double) #1

attributes #0 = { mustprogress nofree 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 = { mustprogress nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!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: CorrelatedValuePropagationPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass