; LLVM IR from HarshCasper/NeoAlgo :: C/math (tribonacci)

; 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: nounwind uwtable
define dso_local i64 @find_tribonacci(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %7 = load i32, ptr %2, align 4, !tbaa !5
  %8 = zext i32 %7 to i64
  %9 = call ptr @llvm.stacksave.p0()
  store ptr %9, ptr %3, align 8
  %10 = alloca i64, i64 %8, align 16
  store i64 %8, ptr %4, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !5
  br label %11

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

15:                                               ; preds = %11
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  br label %23

16:                                               ; preds = %11
  %17 = load i32, ptr %5, align 4, !tbaa !5
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i64, ptr %10, i64 %18
  store i64 0, ptr %19, align 8, !tbaa !9
  br label %20

20:                                               ; preds = %16
  %21 = load i32, ptr %5, align 4, !tbaa !5
  %22 = add nsw i32 %21, 1
  store i32 %22, ptr %5, align 4, !tbaa !5
  br label %11, !llvm.loop !11

23:                                               ; preds = %15
  %24 = getelementptr inbounds i64, ptr %10, i64 2
  store i64 1, ptr %24, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 3, ptr %6, align 4, !tbaa !5
  br label %25

25:                                               ; preds = %51, %23
  %26 = load i32, ptr %6, align 4, !tbaa !5
  %27 = load i32, ptr %2, align 4, !tbaa !5
  %28 = icmp slt i32 %26, %27
  br i1 %28, label %30, label %29

29:                                               ; preds = %25
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %54

30:                                               ; preds = %25
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sub nsw i32 %31, 1
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i64, ptr %10, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = load i32, ptr %6, align 4, !tbaa !5
  %37 = sub nsw i32 %36, 2
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds i64, ptr %10, i64 %38
  %40 = load i64, ptr %39, align 8, !tbaa !9
  %41 = add nsw i64 %35, %40
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = sub nsw i32 %42, 3
  %44 = sext i32 %43 to i64
  %45 = getelementptr inbounds i64, ptr %10, i64 %44
  %46 = load i64, ptr %45, align 8, !tbaa !9
  %47 = add nsw i64 %41, %46
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds i64, ptr %10, i64 %49
  store i64 %47, ptr %50, align 8, !tbaa !9
  br label %51

51:                                               ; preds = %30
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = add nsw i32 %52, 1
  store i32 %53, ptr %6, align 4, !tbaa !5
  br label %25, !llvm.loop !13

54:                                               ; preds = %29
  %55 = load i32, ptr %2, align 4, !tbaa !5
  %56 = sub nsw i32 %55, 1
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i64, ptr %10, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  %60 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %60)
  ret i64 %59
}

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

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

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

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

attributes #0 = { 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 nosync nounwind willreturn memory(argmem: readwrite) }
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 = !{!6, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = distinct !{!13, !12}


; 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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i64 @find_tribonacci(i32 noundef %0) local_unnamed_addr #0 {
  %2 = zext i32 %0 to i64
  %3 = alloca i64, i64 %2, align 16
  %4 = icmp sgt i32 %0, 0
  br i1 %4, label %._crit_edge, label %._crit_edge.thread

._crit_edge.thread:                               ; preds = %1
  %5 = getelementptr inbounds i8, ptr %3, i64 16
  store i64 1, ptr %5, align 16, !tbaa !5
  br label %._crit_edge19

._crit_edge:                                      ; preds = %1
  %6 = shl nuw nsw i64 %2, 3
  call void @llvm.memset.p0.i64(ptr nonnull align 16 %3, i8 0, i64 %6, i1 false), !tbaa !5
  %7 = getelementptr inbounds i8, ptr %3, i64 16
  store i64 1, ptr %7, align 16, !tbaa !5
  %8 = icmp ugt i32 %0, 3
  br i1 %8, label %.lr.ph18.preheader, label %._crit_edge19

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

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

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

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

._crit_edge19:                                    ; preds = %.lr.ph18.epil, %._crit_edge19.loopexit.unr-lcssa, %._crit_edge.thread, %._crit_edge
  %18 = sext i32 %0 to i64
  %19 = getelementptr i64, ptr %3, i64 %18
  %20 = getelementptr i8, ptr %19, i64 -8
  %21 = load i64, ptr %20, align 8, !tbaa !5
  ret i64 %21

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

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

attributes #0 = { nofree norecurse nosync nounwind 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 = { 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/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}


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