; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/math/tribonacci.ll (Z15find_tribonaccii)

; 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 i64 @_Z15find_tribonaccii(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = zext i32 %6 to i64
  %8 = call ptr @llvm.stacksave.p0()
  store ptr %8, ptr %3, align 8
  %9 = alloca i64, i64 %7, align 16
  store i64 %7, ptr %4, align 8
  %10 = mul nuw i64 8, %7
  call void @llvm.memset.p0.i64(ptr align 16 %9, i8 0, i64 %10, i1 false)
  %11 = getelementptr inbounds i64, ptr %9, i64 2
  store i64 1, ptr %11, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #4
  store i32 3, ptr %5, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %38, %1
  %13 = load i32, ptr %5, align 4, !tbaa !5
  %14 = load i32, ptr %2, align 4, !tbaa !5
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %17, label %16

16:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #4
  br label %41

17:                                               ; preds = %12
  %18 = load i32, ptr %5, align 4, !tbaa !5
  %19 = sub nsw i32 %18, 1
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds i64, ptr %9, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !9
  %23 = load i32, ptr %5, align 4, !tbaa !5
  %24 = sub nsw i32 %23, 2
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds i64, ptr %9, i64 %25
  %27 = load i64, ptr %26, align 8, !tbaa !9
  %28 = add nsw i64 %22, %27
  %29 = load i32, ptr %5, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 3
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i64, ptr %9, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !9
  %34 = add nsw i64 %28, %33
  %35 = load i32, ptr %5, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i64, ptr %9, i64 %36
  store i64 %34, ptr %37, align 8, !tbaa !9
  br label %38

38:                                               ; preds = %17
  %39 = load i32, ptr %5, align 4, !tbaa !5
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr %5, align 4, !tbaa !5
  br label %12, !llvm.loop !11

41:                                               ; preds = %16
  %42 = load i32, ptr %2, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 1
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i64, ptr %9, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !9
  %47 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %47)
  ret i64 %46
}

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

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #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 }
attributes #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!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: mustprogress nofree norecurse nosync nounwind willreturn memory(none) uwtable
define dso_local noundef i64 @_Z15find_tribonaccii(i32 noundef %0) local_unnamed_addr #0 {
  %2 = zext i32 %0 to i64
  %3 = alloca i64, i64 %2, align 16
  %4 = shl nuw nsw i64 %2, 3
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %3, i8 0, i64 %4, i1 false)
  %5 = getelementptr inbounds i8, ptr %3, i64 16
  store i64 1, ptr %5, align 16, !tbaa !5
  %6 = icmp sgt i32 %0, 3
  br i1 %6, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %1
  %scevgep = getelementptr i8, ptr %3, i64 16
  %load_initial = load i64, ptr %scevgep, align 16
  %7 = add nsw i64 %2, -3
  %xtraiter = and i64 %7, 1
  %8 = icmp eq i32 %0, 4
  br i1 %8, label %._crit_edge.loopexit.unr-lcssa, label %.lr.ph.preheader.new

.lr.ph.preheader.new:                             ; preds = %.lr.ph.preheader
  %unroll_iter = and i64 %7, -2
  br label %.lr.ph

._crit_edge.loopexit.unr-lcssa:                   ; preds = %.lr.ph, %.lr.ph.preheader
  %store_forwarded.unr = phi i64 [ %load_initial, %.lr.ph.preheader ], [ %32, %.lr.ph ]
  %indvars.iv.unr = phi i64 [ 3, %.lr.ph.preheader ], [ %indvars.iv.next.1, %.lr.ph ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %._crit_edge, label %.lr.ph.epil

.lr.ph.epil:                                      ; preds = %._crit_edge.loopexit.unr-lcssa
  %9 = getelementptr i64, ptr %3, i64 %indvars.iv.unr
  %10 = getelementptr i8, ptr %9, i64 -16
  %11 = load i64, ptr %10, align 8, !tbaa !5
  %12 = add nsw i64 %11, %store_forwarded.unr
  %13 = getelementptr i8, ptr %9, i64 -24
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = add nsw i64 %12, %14
  store i64 %15, ptr %9, align 8, !tbaa !5
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph.epil, %._crit_edge.loopexit.unr-lcssa, %1
  %16 = sext i32 %0 to i64
  %17 = getelementptr i64, ptr %3, i64 %16
  %18 = getelementptr i8, ptr %17, i64 -8
  %19 = load i64, ptr %18, align 8, !tbaa !5
  ret i64 %19

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %store_forwarded = phi i64 [ %load_initial, %.lr.ph.preheader.new ], [ %32, %.lr.ph ]
  %indvars.iv = phi i64 [ 3, %.lr.ph.preheader.new ], [ %indvars.iv.next.1, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.1, %.lr.ph ]
  %20 = getelementptr i64, ptr %3, i64 %indvars.iv
  %21 = getelementptr i8, ptr %20, i64 -16
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = add nsw i64 %22, %store_forwarded
  %24 = getelementptr i8, ptr %20, i64 -24
  %25 = load i64, ptr %24, align 8, !tbaa !5
  %26 = add nsw i64 %23, %25
  store i64 %26, ptr %20, align 8, !tbaa !5
  %27 = getelementptr i64, ptr %3, i64 %indvars.iv
  %28 = getelementptr i8, ptr %27, i64 8
  %29 = getelementptr i8, ptr %27, i64 -8
  %30 = load i64, ptr %29, align 8, !tbaa !5
  %31 = add nsw i64 %30, %26
  %32 = add nsw i64 %31, %22
  store i64 %32, ptr %28, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 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 !9
}

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

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) }

!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", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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