; LLVM IR from TheAlgorithms/C-Plus-Plus :: probability/poisson_dist.ll (Z4factd)

; 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 double @_Z4factd(double noundef %0) #0 {
  %2 = alloca double, align 8
  %3 = alloca double, align 8
  %4 = alloca i32, align 4
  store double %0, ptr %2, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  %5 = load double, ptr %2, align 8, !tbaa !5
  store double %5, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %6 = load double, ptr %2, align 8, !tbaa !5
  %7 = fsub double %6, 1.000000e+00
  %8 = fptosi double %7 to i32
  store i32 %8, ptr %4, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %18, %1
  %10 = load i32, ptr %4, align 4, !tbaa !9
  %11 = icmp sgt i32 %10, 0
  br i1 %11, label %13, label %12

12:                                               ; preds = %9
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %21

13:                                               ; preds = %9
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = sitofp i32 %14 to double
  %16 = load double, ptr %3, align 8, !tbaa !5
  %17 = fmul double %16, %15
  store double %17, ptr %3, align 8, !tbaa !5
  br label %18

18:                                               ; preds = %13
  %19 = load i32, ptr %4, align 4, !tbaa !9
  %20 = add nsw i32 %19, -1
  store i32 %20, ptr %4, align 4, !tbaa !9
  br label %9, !llvm.loop !11

21:                                               ; preds = %12
  %22 = load double, ptr %3, align 8, !tbaa !5
  %23 = fcmp ole double %22, 0.000000e+00
  br i1 %23, label %24, label %25

24:                                               ; preds = %21
  store double 1.000000e+00, ptr %3, align 8, !tbaa !5
  br label %25

25:                                               ; preds = %24, %21
  %26 = load double, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  ret double %26
}

; 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 = !{!"double", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !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 double @_Z4factd(double noundef %0) local_unnamed_addr #0 {
  %2 = fadd double %0, -1.000000e+00
  %3 = fptosi double %2 to i32
  %4 = icmp sgt i32 %3, 0
  br i1 %4, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %xtraiter = and i32 %3, 7
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol

.lr.ph.prol:                                      ; preds = %.lr.ph.preheader, %.lr.ph.prol
  %.09.prol = phi i32 [ %7, %.lr.ph.prol ], [ %3, %.lr.ph.preheader ]
  %.078.prol = phi double [ %6, %.lr.ph.prol ], [ %0, %.lr.ph.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.lr.ph.prol ], [ 0, %.lr.ph.preheader ]
  %5 = uitofp nneg i32 %.09.prol to double
  %6 = fmul double %.078.prol, %5
  %7 = add nsw i32 %.09.prol, -1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.lr.ph.prol.loopexit, label %.lr.ph.prol, !llvm.loop !5

.lr.ph.prol.loopexit:                             ; preds = %.lr.ph.prol, %.lr.ph.preheader
  %.09.unr = phi i32 [ %3, %.lr.ph.preheader ], [ %7, %.lr.ph.prol ]
  %.078.unr = phi double [ %0, %.lr.ph.preheader ], [ %6, %.lr.ph.prol ]
  %.lcssa.unr = phi double [ poison, %.lr.ph.preheader ], [ %6, %.lr.ph.prol ]
  %8 = icmp ult i32 %3, 8
  br i1 %8, label %._crit_edge, label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph.prol.loopexit, %.lr.ph, %1
  %.07.lcssa = phi double [ %0, %1 ], [ %.lcssa.unr, %.lr.ph.prol.loopexit ], [ %32, %.lr.ph ]
  %9 = fcmp ugt double %.07.lcssa, 0.000000e+00
  %.1 = select i1 %9, double %.07.lcssa, double 1.000000e+00
  ret double %.1

.lr.ph:                                           ; preds = %.lr.ph.prol.loopexit, %.lr.ph
  %.09 = phi i32 [ %33, %.lr.ph ], [ %.09.unr, %.lr.ph.prol.loopexit ]
  %.078 = phi double [ %32, %.lr.ph ], [ %.078.unr, %.lr.ph.prol.loopexit ]
  %10 = uitofp nneg i32 %.09 to double
  %11 = fmul double %.078, %10
  %12 = add nsw i32 %.09, -1
  %13 = uitofp nneg i32 %12 to double
  %14 = fmul double %11, %13
  %15 = add nsw i32 %.09, -2
  %16 = uitofp nneg i32 %15 to double
  %17 = fmul double %14, %16
  %18 = add nsw i32 %.09, -3
  %19 = uitofp nneg i32 %18 to double
  %20 = fmul double %17, %19
  %21 = add nsw i32 %.09, -4
  %22 = uitofp nneg i32 %21 to double
  %23 = fmul double %20, %22
  %24 = add nsw i32 %.09, -5
  %25 = uitofp nneg i32 %24 to double
  %26 = fmul double %23, %25
  %27 = add nsw i32 %.09, -6
  %28 = uitofp nneg i32 %27 to double
  %29 = fmul double %26, %28
  %30 = add nsw i32 %.09, -7
  %31 = uitofp nneg i32 %30 to double
  %32 = fmul double %29, %31
  %33 = add nsw i32 %.09, -8
  %34 = icmp ugt i32 %30, 1
  br i1 %34, label %.lr.ph, label %._crit_edge, !llvm.loop !7
}

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.unroll.disable"}
!7 = distinct !{!7, !8}
!8 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass