; LLVM IR from EdoPro98/SimSiPM :: src/SiPMRandom.ll (ZN4sipm10SiPMRandom11randPoissonEd)

; 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: mustprogress nounwind uwtable
define dso_local noundef i32 @_ZN4sipm10SiPMRandom11randPoissonEd(ptr noundef nonnull align 64 dereferenceable(524416) %0, double noundef %1) #1 align 2 {
  %3 = alloca i32, align 4
  %4 = alloca ptr, align 8
  %5 = alloca double, align 8
  %6 = alloca double, align 8
  %7 = alloca double, align 8
  %8 = alloca i32, align 4
  %9 = alloca double, align 8
  %10 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store double %1, ptr %5, align 8, !tbaa !9
  %11 = load ptr, ptr %4, align 8
  %12 = load double, ptr %5, align 8, !tbaa !9
  %13 = fcmp ole double %12, 0.000000e+00
  br i1 %13, label %14, label %15

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

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %16 = load double, ptr %5, align 8, !tbaa !9
  %17 = fneg double %16
  %18 = call double @exp(double noundef %17) #4
  store double %18, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #4
  store double 1.000000e+00, ptr %7, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #4
  store i32 0, ptr %8, align 4, !tbaa !11
  br label %19

19:                                               ; preds = %36, %15
  br label %20

20:                                               ; preds = %19
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #4
  %21 = call noundef double @_ZN4sipm10SiPMRandom4RandIdEET_v(ptr noundef nonnull align 64 dereferenceable(524416) %11) #4
  store double %21, ptr %9, align 8, !tbaa !9
  %22 = load double, ptr %9, align 8, !tbaa !9
  %23 = load double, ptr %7, align 8, !tbaa !9
  %24 = fmul double %23, %22
  store double %24, ptr %7, align 8, !tbaa !9
  %25 = load double, ptr %7, align 8, !tbaa !9
  %26 = load double, ptr %6, align 8, !tbaa !9
  %27 = fcmp ogt double %25, %26
  br i1 %27, label %28, label %31

28:                                               ; preds = %20
  %29 = load i32, ptr %8, align 4, !tbaa !11
  %30 = add i32 %29, 1
  store i32 %30, ptr %8, align 4, !tbaa !11
  br label %33

31:                                               ; preds = %20
  %32 = load i32, ptr %8, align 4, !tbaa !11
  store i32 %32, ptr %3, align 4
  store i32 1, ptr %10, align 4
  br label %34

33:                                               ; preds = %28
  store i32 0, ptr %10, align 4
  br label %34

34:                                               ; preds = %33, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #4
  %35 = load i32, ptr %10, align 4
  switch i32 %35, label %37 [
    i32 0, label %36
  ]

36:                                               ; preds = %34
  br label %19, !llvm.loop !13

37:                                               ; preds = %34
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  br label %38

38:                                               ; preds = %37, %14
  %39 = load i32, ptr %3, align 4
  ret i32 %39
}

; Function Attrs: nounwind
declare double @exp(double noundef) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZN4sipm10SiPMRandom4RandIdEET_v(ptr noundef nonnull align 64 dereferenceable(524416)) #3 align 2

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #2 = { nounwind "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 #3 = { inlinehint 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 #4 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"double", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"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 nounwind uwtable
define dso_local noundef i32 @_ZN4sipm10SiPMRandom11randPoissonEd(ptr noundef nonnull align 64 dereferenceable(524416) %0, double noundef %1) local_unnamed_addr #0 align 2 {
  %3 = fcmp ugt double %1, 0.000000e+00
  br i1 %3, label %4, label %.loopexit

4:                                                ; preds = %2
  %5 = fneg double %1
  %6 = tail call double @exp(double noundef %5) #3
  br label %7

7:                                                ; preds = %7, %4
  %.013 = phi double [ 1.000000e+00, %4 ], [ %9, %7 ]
  %.011 = phi i32 [ 0, %4 ], [ %11, %7 ]
  %8 = tail call noundef double @_ZN4sipm10SiPMRandom4RandIdEET_v(ptr noundef nonnull align 64 dereferenceable(524416) %0) #3
  %9 = fmul double %.013, %8
  %10 = fcmp ogt double %9, %6
  %11 = add i32 %.011, 1
  br i1 %10, label %7, label %.loopexit, !llvm.loop !5

.loopexit:                                        ; preds = %7, %2
  %.2 = phi i32 [ 0, %2 ], [ %.011, %7 ]
  ret i32 %.2
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(write)
declare double @exp(double noundef) local_unnamed_addr #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZN4sipm10SiPMRandom4RandIdEET_v(ptr noundef nonnull align 64 dereferenceable(524416)) local_unnamed_addr #2 align 2

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


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