; LLVM IR from Chasmiccoder/Algos-for-Amigos :: Mathematics/Series/p01_fibonacci_series.ll (Z31naive_get_fibonacci_partial_sumxx)

; 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 i64 @_Z31naive_get_fibonacci_partial_sumxx(i64 noundef %0, i64 noundef %1) #1 {
  %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 i64, align 8
  %9 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  store i64 1, ptr %7, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  store i64 0, ptr %8, align 8, !tbaa !5
  br label %10

10:                                               ; preds = %34, %2
  %11 = load i64, ptr %8, align 8, !tbaa !5
  %12 = load i64, ptr %4, align 8, !tbaa !5
  %13 = icmp sle i64 %11, %12
  br i1 %13, label %15, label %14

14:                                               ; preds = %10
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %37

15:                                               ; preds = %10
  %16 = load i64, ptr %8, align 8, !tbaa !5
  %17 = load i64, ptr %3, align 8, !tbaa !5
  %18 = icmp sge i64 %16, %17
  br i1 %18, label %19, label %24

19:                                               ; preds = %15
  %20 = load i64, ptr %6, align 8, !tbaa !5
  %21 = srem i64 %20, 10
  %22 = load i64, ptr %5, align 8, !tbaa !5
  %23 = add nsw i64 %22, %21
  store i64 %23, ptr %5, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %19, %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #2
  %25 = load i64, ptr %7, align 8, !tbaa !5
  %26 = srem i64 %25, 10
  store i64 %26, ptr %9, align 8, !tbaa !5
  %27 = load i64, ptr %7, align 8, !tbaa !5
  %28 = srem i64 %27, 10
  %29 = load i64, ptr %6, align 8, !tbaa !5
  %30 = srem i64 %29, 10
  %31 = add nsw i64 %28, %30
  store i64 %31, ptr %7, align 8, !tbaa !5
  %32 = load i64, ptr %9, align 8, !tbaa !5
  %33 = srem i64 %32, 10
  store i64 %33, ptr %6, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #2
  br label %34

34:                                               ; preds = %24
  %35 = load i64, ptr %8, align 8, !tbaa !5
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %8, align 8, !tbaa !5
  br label %10, !llvm.loop !9

37:                                               ; preds = %14
  %38 = load i64, ptr %5, align 8, !tbaa !5
  %39 = srem i64 %38, 10
  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 i64 %39
}

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 }

!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 range(i64 -9, 10) i64 @_Z31naive_get_fibonacci_partial_sumxx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %.not16 = icmp slt i64 %1, 0
  br i1 %.not16, label %._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %2
  %3 = icmp eq i64 %1, 0
  br i1 %3, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %4 = add nuw i64 %1, 1
  %unroll_iter = and i64 %4, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %spec.select.lcssa.ph = phi i64 [ poison, %.lr.ph.preheader ], [ %spec.select.1, %.lr.ph ]
  %.020.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %spec.select.1, %.lr.ph ]
  %.01219.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %15, %.lr.ph ]
  %.01417.unr = phi i64 [ 0, %.lr.ph.preheader ], [ %13, %.lr.ph ]
  %5 = and i64 %1, 1
  %lcmp.mod.not.not = icmp eq i64 %5, 0
  %.not15.epil = icmp slt i64 %.01219.unr, %0
  %6 = select i1 %.not15.epil, i64 0, i64 %.01417.unr
  %spec.select.epil = add nsw i64 %6, %.020.unr
  %spec.select.lcssa = select i1 %lcmp.mod.not.not, i64 %spec.select.epil, i64 %spec.select.lcssa.ph
  %7 = srem i64 %spec.select.lcssa, 10
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit.unr-lcssa, %2
  %.0.lcssa = phi i64 [ 0, %2 ], [ %7, %._crit_edge.loopexit.unr-lcssa ]
  ret i64 %.0.lcssa

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %.020 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %spec.select.1, %.lr.ph ]
  %.01219 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %15, %.lr.ph ]
  %.01318 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %14, %.lr.ph ]
  %.01417 = phi i64 [ 0, %.lr.ph.preheader.new ], [ %13, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %.not15 = icmp slt i64 %.01219, %0
  %8 = select i1 %.not15, i64 0, i64 %.01417
  %spec.select = add nsw i64 %8, %.020
  %9 = srem i64 %.01318, 10
  %10 = add nsw i64 %9, %.01417
  %11 = or disjoint i64 %.01219, 1
  %.not15.1 = icmp slt i64 %11, %0
  %12 = select i1 %.not15.1, i64 0, i64 %9
  %spec.select.1 = add nsw i64 %12, %spec.select
  %13 = srem i64 %10, 10
  %14 = add nsw i64 %13, %9
  %15 = add nuw i64 %.01219, 2
  %niter.next.1 = add i64 %niter, 2
  %niter.ncmp.1 = icmp eq i64 %niter.next.1, %unroll_iter
  br i1 %niter.ncmp.1, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !5
}

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.mustprogress"}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass