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

; 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 @_Z19N_bonacci_iterativeii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca ptr, align 8
  %7 = alloca i64, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %13 = load i32, ptr %4, align 4, !tbaa !5
  %14 = zext i32 %13 to i64
  %15 = call ptr @llvm.stacksave.p0()
  store ptr %15, ptr %6, align 8
  %16 = alloca i32, i64 %14, align 16
  store i64 %14, ptr %7, align 8
  %17 = mul nuw i64 4, %14
  call void @llvm.memset.p0.i64(ptr align 16 %16, i8 0, i64 %17, i1 false)
  %18 = load i32, ptr %4, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i32, ptr %16, i64 %20
  store i32 1, ptr %21, align 4, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = load i32, ptr %4, align 4, !tbaa !5
  %24 = icmp slt i32 %22, %23
  br i1 %24, label %25, label %30

25:                                               ; preds = %2
  %26 = load i32, ptr %5, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds i32, ptr %16, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %83

30:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #4
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #4
  %31 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %31, ptr %10, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %78, %30
  %33 = load i32, ptr %10, align 4, !tbaa !5
  %34 = load i32, ptr %5, align 4, !tbaa !5
  %35 = icmp sle i32 %33, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %32
  store i32 2, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #4
  br label %81

37:                                               ; preds = %32
  store i32 0, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %38

38:                                               ; preds = %50, %37
  %39 = load i32, ptr %11, align 4, !tbaa !5
  %40 = load i32, ptr %4, align 4, !tbaa !5
  %41 = icmp slt i32 %39, %40
  br i1 %41, label %43, label %42

42:                                               ; preds = %38
  store i32 5, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  br label %53

43:                                               ; preds = %38
  %44 = load i32, ptr %11, align 4, !tbaa !5
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i32, ptr %16, i64 %45
  %47 = load i32, ptr %46, align 4, !tbaa !5
  %48 = load i32, ptr %9, align 4, !tbaa !5
  %49 = add nsw i32 %48, %47
  store i32 %49, ptr %9, align 4, !tbaa !5
  br label %50

50:                                               ; preds = %43
  %51 = load i32, ptr %11, align 4, !tbaa !5
  %52 = add nsw i32 %51, 1
  store i32 %52, ptr %11, align 4, !tbaa !5
  br label %38, !llvm.loop !9

53:                                               ; preds = %42
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !5
  br label %54

54:                                               ; preds = %69, %53
  %55 = load i32, ptr %12, align 4, !tbaa !5
  %56 = load i32, ptr %4, align 4, !tbaa !5
  %57 = sub nsw i32 %56, 1
  %58 = icmp slt i32 %55, %57
  br i1 %58, label %60, label %59

59:                                               ; preds = %54
  store i32 8, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  br label %72

60:                                               ; preds = %54
  %61 = load i32, ptr %12, align 4, !tbaa !5
  %62 = add nsw i32 %61, 1
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds i32, ptr %16, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  %66 = load i32, ptr %12, align 4, !tbaa !5
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i32, ptr %16, i64 %67
  store i32 %65, ptr %68, align 4, !tbaa !5
  br label %69

69:                                               ; preds = %60
  %70 = load i32, ptr %12, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %12, align 4, !tbaa !5
  br label %54, !llvm.loop !11

72:                                               ; preds = %59
  %73 = load i32, ptr %9, align 4, !tbaa !5
  %74 = load i32, ptr %4, align 4, !tbaa !5
  %75 = sub nsw i32 %74, 1
  %76 = sext i32 %75 to i64
  %77 = getelementptr inbounds i32, ptr %16, i64 %76
  store i32 %73, ptr %77, align 4, !tbaa !5
  br label %78

78:                                               ; preds = %72
  %79 = load i32, ptr %10, align 4, !tbaa !5
  %80 = add nsw i32 %79, 1
  store i32 %80, ptr %10, align 4, !tbaa !5
  br label %32, !llvm.loop !12

81:                                               ; preds = %36
  %82 = load i32, ptr %9, align 4, !tbaa !5
  store i32 %82, ptr %3, align 4
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #4
  br label %83

83:                                               ; preds = %81, %25
  %84 = load ptr, ptr %6, align 8
  call void @llvm.stackrestore.p0(ptr %84)
  %85 = load i32, ptr %3, align 4
  ret i32 %85
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 = { nocallback nofree nosync nounwind willreturn }
attributes #3 = { nocallback nofree nounwind willreturn memory(argmem: write) }
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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


; 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 i32 @_Z19N_bonacci_iterativeii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = zext i32 %0 to i64
  %4 = alloca i32, i64 %3, align 16
  %5 = shl nuw nsw i64 %3, 2
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %4, i8 0, i64 %5, i1 false)
  %6 = add nsw i32 %0, -1
  %7 = sext i32 %6 to i64
  %8 = getelementptr inbounds i32, ptr %4, i64 %7
  store i32 1, ptr %8, align 4, !tbaa !5
  %9 = icmp slt i32 %1, %0
  br i1 %9, label %30, label %.preheader31

.preheader31:                                     ; preds = %2
  %10 = icmp sgt i32 %0, 1
  br i1 %10, label %.preheader30.us.us.preheader, label %.preheader31.split

.preheader30.us.us.preheader:                     ; preds = %.preheader31
  %scevgep65 = getelementptr i8, ptr %4, i64 4
  %11 = zext nneg i32 %6 to i64
  %12 = shl nuw nsw i64 %11, 2
  %min.iters.check = icmp ult i32 %0, 8
  %n.vec = and i64 %3, 2147483640
  %cmp.n = icmp eq i64 %n.vec, %3
  br label %.preheader30.us.us

.preheader30.us.us:                               ; preds = %.preheader30.us.us.preheader, %..preheader_crit_edge.us.us
  %.02436.us.us = phi i32 [ %22, %..preheader_crit_edge.us.us ], [ %0, %.preheader30.us.us.preheader ]
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.body

vector.body:                                      ; preds = %.preheader30.us.us, %vector.body
  %index = phi i64 [ %index.next, %vector.body ], [ 0, %.preheader30.us.us ]
  %vec.phi = phi <4 x i32> [ %15, %vector.body ], [ zeroinitializer, %.preheader30.us.us ]
  %vec.phi74 = phi <4 x i32> [ %16, %vector.body ], [ zeroinitializer, %.preheader30.us.us ]
  %13 = getelementptr inbounds i32, ptr %4, i64 %index
  %14 = getelementptr inbounds i8, ptr %13, i64 16
  %wide.load = load <4 x i32>, ptr %13, align 16, !tbaa !5
  %wide.load75 = load <4 x i32>, ptr %14, align 16, !tbaa !5
  %15 = add <4 x i32> %wide.load, %vec.phi
  %16 = add <4 x i32> %wide.load75, %vec.phi74
  %index.next = add nuw i64 %index, 8
  %17 = icmp eq i64 %index.next, %n.vec
  br i1 %17, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <4 x i32> %16, %15
  %18 = tail call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> %bin.rdx)
  br i1 %cmp.n, label %..preheader_crit_edge.us.us, label %scalar.ph.preheader

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader30.us.us
  %indvars.iv60.ph = phi i64 [ 0, %.preheader30.us.us ], [ %n.vec, %middle.block ]
  %.132.us.us.ph = phi i32 [ 0, %.preheader30.us.us ], [ %18, %middle.block ]
  br label %scalar.ph

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv60 = phi i64 [ %indvars.iv.next61, %scalar.ph ], [ %indvars.iv60.ph, %scalar.ph.preheader ]
  %.132.us.us = phi i32 [ %21, %scalar.ph ], [ %.132.us.us.ph, %scalar.ph.preheader ]
  %19 = getelementptr inbounds i32, ptr %4, i64 %indvars.iv60
  %20 = load i32, ptr %19, align 4, !tbaa !5
  %21 = add nsw i32 %20, %.132.us.us
  %indvars.iv.next61 = add nuw nsw i64 %indvars.iv60, 1
  %exitcond64.not = icmp eq i64 %indvars.iv.next61, %3
  br i1 %exitcond64.not, label %..preheader_crit_edge.us.us, label %scalar.ph, !llvm.loop !13

..preheader_crit_edge.us.us:                      ; preds = %scalar.ph, %middle.block
  %.lcssa = phi i32 [ %18, %middle.block ], [ %21, %scalar.ph ]
  call void @llvm.memmove.p0.p0.i64(ptr nonnull align 16 %4, ptr align 4 %scevgep65, i64 %12, i1 false), !tbaa !5
  store i32 %.lcssa, ptr %8, align 4, !tbaa !5
  %22 = add i32 %.02436.us.us, 1
  %exitcond69.not = icmp eq i32 %.02436.us.us, %1
  br i1 %exitcond69.not, label %.loopexit, label %.preheader30.us.us, !llvm.loop !14

.preheader31.split:                               ; preds = %.preheader31
  %23 = icmp eq i32 %0, 1
  br i1 %23, label %..preheader_crit_edge.us45.preheader, label %.loopexit

..preheader_crit_edge.us45.preheader:             ; preds = %.preheader31.split
  %24 = add i32 %1, -1
  %xtraiter = and i32 %1, 3
  %25 = icmp ult i32 %24, 3
  br i1 %25, label %.loopexit.loopexit79.unr-lcssa, label %..preheader_crit_edge.us45.preheader.new

..preheader_crit_edge.us45.preheader.new:         ; preds = %..preheader_crit_edge.us45.preheader
  %unroll_iter = and i32 %1, -4
  br label %..preheader_crit_edge.us45

..preheader_crit_edge.us45:                       ; preds = %..preheader_crit_edge.us45, %..preheader_crit_edge.us45.preheader.new
  %niter = phi i32 [ 0, %..preheader_crit_edge.us45.preheader.new ], [ %niter.next.3, %..preheader_crit_edge.us45 ]
  %26 = load i32, ptr %4, align 16, !tbaa !5
  store i32 %26, ptr %8, align 4, !tbaa !5
  %27 = load i32, ptr %4, align 16, !tbaa !5
  store i32 %27, ptr %8, align 4, !tbaa !5
  %28 = load i32, ptr %4, align 16, !tbaa !5
  store i32 %28, ptr %8, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 16, !tbaa !5
  store i32 %29, ptr %8, align 4, !tbaa !5
  %niter.next.3 = add i32 %niter, 4
  %niter.ncmp.3 = icmp eq i32 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.loopexit.loopexit79.unr-lcssa, label %..preheader_crit_edge.us45, !llvm.loop !14

30:                                               ; preds = %2
  %31 = sext i32 %1 to i64
  %32 = getelementptr inbounds i32, ptr %4, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  br label %.loopexit

.loopexit.loopexit79.unr-lcssa:                   ; preds = %..preheader_crit_edge.us45, %..preheader_crit_edge.us45.preheader
  %.lcssa80.ph = phi i32 [ poison, %..preheader_crit_edge.us45.preheader ], [ %29, %..preheader_crit_edge.us45 ]
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.loopexit, label %..preheader_crit_edge.us45.epil

..preheader_crit_edge.us45.epil:                  ; preds = %.loopexit.loopexit79.unr-lcssa, %..preheader_crit_edge.us45.epil
  %epil.iter = phi i32 [ %epil.iter.next, %..preheader_crit_edge.us45.epil ], [ 0, %.loopexit.loopexit79.unr-lcssa ]
  %34 = load i32, ptr %4, align 16, !tbaa !5
  store i32 %34, ptr %8, align 4, !tbaa !5
  %epil.iter.next = add i32 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i32 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %..preheader_crit_edge.us45.epil, !llvm.loop !15

.loopexit:                                        ; preds = %.loopexit.loopexit79.unr-lcssa, %..preheader_crit_edge.us45.epil, %..preheader_crit_edge.us.us, %.preheader31.split, %30
  %.026 = phi i32 [ %33, %30 ], [ 0, %.preheader31.split ], [ %.lcssa, %..preheader_crit_edge.us.us ], [ %.lcssa80.ph, %.loopexit.loopexit79.unr-lcssa ], [ %34, %..preheader_crit_edge.us45.epil ]
  ret i32 %.026
}

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #1

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memmove.p0.p0.i64(ptr nocapture writeonly, ptr nocapture readonly, i64, i1 immarg) #2

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.vector.reduce.add.v4i32(<4 x i32>) #3

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" }
attributes #1 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { 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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10, !11, !12}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!"llvm.loop.isvectorized", i32 1}
!12 = !{!"llvm.loop.unroll.runtime.disable"}
!13 = distinct !{!13, !10, !12, !11}
!14 = distinct !{!14, !10}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.unroll.disable"}


; Effective O3 passes: ADCEPass,CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopIdiomRecognizePass,LoopInstSimplifyPass,LoopLoadEliminationPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,LoopVectorizePass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass