; LLVM IR from sbu-fsl/kernel-ml :: src/math (math)

; 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: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

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

; Function Attrs: nounwind uwtable
declare dso_local double @fast_sqrt_d(double noundef) #1

; Function Attrs: nounwind uwtable
define dso_local double @agm_d(double noundef %0, double noundef %1) #1 {
  %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 double, align 8
  %9 = alloca double, align 8
  %10 = alloca double, align 8
  %11 = alloca i64, align 8
  store double %0, ptr %3, align 8, !tbaa !5
  store double %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store double 1.000000e-09, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store double 1.000000e+00, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %12 = load double, ptr %3, align 8, !tbaa !5
  store double %12, ptr %9, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #2
  %13 = load double, ptr %4, align 8, !tbaa !5
  store double %13, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #2
  store i64 4294967286, ptr %11, align 8, !tbaa !9
  br label %14

14:                                               ; preds = %47, %2
  %15 = load double, ptr %6, align 8, !tbaa !5
  %16 = load double, ptr %5, align 8, !tbaa !5
  %17 = fcmp ogt double %15, %16
  br i1 %17, label %18, label %21

18:                                               ; preds = %14
  %19 = load i64, ptr %11, align 8, !tbaa !9
  %20 = icmp ne i64 %19, 0
  br label %21

21:                                               ; preds = %18, %14
  %22 = phi i1 [ false, %14 ], [ %20, %18 ]
  br i1 %22, label %23, label %51

23:                                               ; preds = %21
  %24 = load double, ptr %9, align 8, !tbaa !5
  %25 = load double, ptr %10, align 8, !tbaa !5
  %26 = fadd double %24, %25
  store double %26, ptr %7, align 8, !tbaa !5
  %27 = load double, ptr %9, align 8, !tbaa !5
  %28 = load double, ptr %10, align 8, !tbaa !5
  %29 = fmul double %27, %28
  store double %29, ptr %8, align 8, !tbaa !5
  %30 = load double, ptr %7, align 8, !tbaa !5
  %31 = fdiv double %30, 2.000000e+00
  store double %31, ptr %9, align 8, !tbaa !5
  %32 = load double, ptr %8, align 8, !tbaa !5
  %33 = call double @fast_sqrt_d(double noundef %32)
  store double %33, ptr %10, align 8, !tbaa !5
  %34 = load double, ptr %9, align 8, !tbaa !5
  %35 = load double, ptr %10, align 8, !tbaa !5
  %36 = fsub double %34, %35
  %37 = fcmp oge double %36, 0.000000e+00
  br i1 %37, label %38, label %42

38:                                               ; preds = %23
  %39 = load double, ptr %9, align 8, !tbaa !5
  %40 = load double, ptr %10, align 8, !tbaa !5
  %41 = fsub double %39, %40
  br label %47

42:                                               ; preds = %23
  %43 = load double, ptr %9, align 8, !tbaa !5
  %44 = fneg double %43
  %45 = load double, ptr %10, align 8, !tbaa !5
  %46 = fsub double %44, %45
  br label %47

47:                                               ; preds = %42, %38
  %48 = phi double [ %41, %38 ], [ %46, %42 ]
  store double %48, ptr %6, align 8, !tbaa !5
  %49 = load i64, ptr %11, align 8, !tbaa !9
  %50 = add i64 %49, -1
  store i64 %50, ptr %11, align 8, !tbaa !9
  br label %14, !llvm.loop !11

51:                                               ; preds = %21
  %52 = load double, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  ret double %52
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/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: nounwind uwtable
declare dso_local double @fast_sqrt_d(double noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
define dso_local double @agm_d(double noundef %0, double noundef %1) local_unnamed_addr #0 {
  br label %3

3:                                                ; preds = %2, %3
  %.024 = phi i64 [ 4294967286, %2 ], [ %13, %3 ]
  %.01823 = phi double [ %1, %2 ], [ %7, %3 ]
  %.01922 = phi double [ %0, %2 ], [ %6, %3 ]
  %4 = fadd double %.01922, %.01823
  %5 = fmul double %.01922, %.01823
  %6 = fmul double %4, 5.000000e-01
  %7 = tail call double @fast_sqrt_d(double noundef %5)
  %8 = fsub double %6, %7
  %9 = fcmp ult double %8, 0.000000e+00
  %10 = fneg double %6
  %11 = fsub double %10, %7
  %12 = select i1 %9, double %11, double %8
  %13 = add nsw i64 %.024, -1
  %14 = fcmp ogt double %12, 1.000000e-09
  %15 = icmp ne i64 %13, 0
  %16 = select i1 %14, i1 %15, i1 false
  br i1 %16, label %3, label %17, !llvm.loop !5

17:                                               ; preds = %3
  ret double %7
}

attributes #0 = { 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" }

!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,GVNPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass