; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Dynamic Programming/Fibonacci Numbers/Fibonacci Using Iteration.l_47467f84 (Itei)

; 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"

@fib = external dso_local global [100 x i64], align 16

; 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 @_Z13Fibonacci_Itei(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  %4 = load i32, ptr %2, align 4, !tbaa !5
  %5 = sext i32 %4 to i64
  %6 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %5
  %7 = load i64, ptr %6, align 8, !tbaa !9
  %8 = icmp slt i64 %7, 0
  br i1 %8, label %9, label %34

9:                                                ; preds = %1
  store i64 0, ptr @fib, align 16, !tbaa !9
  store i64 1, ptr getelementptr inbounds ([100 x i64], ptr @fib, i64 0, i64 1), align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 2, ptr %3, align 4, !tbaa !5
  br label %10

10:                                               ; preds = %30, %9
  %11 = load i32, ptr %3, align 4, !tbaa !5
  %12 = load i32, ptr %2, align 4, !tbaa !5
  %13 = icmp sle i32 %11, %12
  br i1 %13, label %15, label %14

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

15:                                               ; preds = %10
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sub nsw i32 %16, 1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = sub nsw i32 %21, 2
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %23
  %25 = load i64, ptr %24, align 8, !tbaa !9
  %26 = add nsw i64 %20, %25
  %27 = load i32, ptr %3, align 4, !tbaa !5
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %28
  store i64 %26, ptr %29, align 8, !tbaa !9
  br label %30

30:                                               ; preds = %15
  %31 = load i32, ptr %3, align 4, !tbaa !5
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %3, align 4, !tbaa !5
  br label %10, !llvm.loop !11

33:                                               ; preds = %14
  br label %34

34:                                               ; preds = %33, %1
  %35 = load i32, ptr %2, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %36
  %38 = load i64, ptr %37, align 8, !tbaa !9
  ret i64 %38
}

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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long 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"

@fib = external dso_local local_unnamed_addr global [100 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z13Fibonacci_Itei(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %2
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = icmp slt i64 %4, 0
  br i1 %5, label %6, label %.loopexit

6:                                                ; preds = %1
  store i64 0, ptr @fib, align 16, !tbaa !5
  store i64 1, ptr getelementptr inbounds (i8, ptr @fib, i64 8), align 8, !tbaa !5
  %.not8 = icmp slt i32 %0, 2
  br i1 %.not8, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %6
  %7 = add nuw i32 %0, 1
  %wide.trip.count = zext i32 %7 to i64
  %8 = add nsw i64 %wide.trip.count, -2
  %9 = add nsw i64 %wide.trip.count, -3
  %xtraiter = and i64 %8, 3
  %10 = icmp ult i64 %9, 3
  br i1 %10, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph.preheader.new

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

.lr.ph:                                           ; preds = %.lr.ph, %.lr.ph.preheader.new
  %11 = phi i64 [ 1, %.lr.ph.preheader.new ], [ %29, %.lr.ph ]
  %indvars.iv = phi i64 [ 2, %.lr.ph.preheader.new ], [ %indvars.iv.next.3, %.lr.ph ]
  %niter = phi i64 [ 0, %.lr.ph.preheader.new ], [ %niter.next.3, %.lr.ph ]
  %12 = add nsw i64 %indvars.iv, -2
  %13 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %12
  %14 = load i64, ptr %13, align 16, !tbaa !5
  %15 = add nsw i64 %14, %11
  %16 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv
  store i64 %15, ptr %16, align 16, !tbaa !5
  %indvars.iv.next = or disjoint i64 %indvars.iv, 1
  %17 = add nsw i64 %indvars.iv, -1
  %18 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %17
  %19 = load i64, ptr %18, align 8, !tbaa !5
  %20 = add nsw i64 %19, %15
  %21 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv.next
  store i64 %20, ptr %21, align 8, !tbaa !5
  %indvars.iv.next.1 = add nuw nsw i64 %indvars.iv, 2
  %22 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv
  %23 = load i64, ptr %22, align 16, !tbaa !5
  %24 = add nsw i64 %23, %20
  %25 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv.next.1
  store i64 %24, ptr %25, align 16, !tbaa !5
  %indvars.iv.next.2 = add nuw nsw i64 %indvars.iv, 3
  %26 = or disjoint i64 %indvars.iv, 1
  %27 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = add nsw i64 %28, %24
  %30 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv.next.2
  store i64 %29, ptr %30, align 8, !tbaa !5
  %indvars.iv.next.3 = add nuw nsw i64 %indvars.iv, 4
  %niter.next.3 = add nuw i64 %niter, 4
  %niter.ncmp.3 = icmp eq i64 %niter.next.3, %unroll_iter
  br i1 %niter.ncmp.3, label %.loopexit.loopexit.unr-lcssa, label %.lr.ph, !llvm.loop !9

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

.lr.ph.epil:                                      ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil
  %31 = phi i64 [ %35, %.lr.ph.epil ], [ %.unr, %.loopexit.loopexit.unr-lcssa ]
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.lr.ph.epil ], [ %indvars.iv.unr, %.loopexit.loopexit.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.lr.ph.epil ], [ 0, %.loopexit.loopexit.unr-lcssa ]
  %32 = add nsw i64 %indvars.iv.epil, -2
  %33 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = add nsw i64 %34, %31
  %36 = getelementptr inbounds [100 x i64], ptr @fib, i64 0, i64 %indvars.iv.epil
  store i64 %35, ptr %36, align 8, !tbaa !5
  %indvars.iv.next.epil = add nuw nsw i64 %indvars.iv.epil, 1
  %epil.iter.next = add i64 %epil.iter, 1
  %epil.iter.cmp.not = icmp eq i64 %epil.iter.next, %xtraiter
  br i1 %epil.iter.cmp.not, label %.loopexit, label %.lr.ph.epil, !llvm.loop !11

.loopexit:                                        ; preds = %.loopexit.loopexit.unr-lcssa, %.lr.ph.epil, %6, %1
  %37 = load i64, ptr %3, align 8, !tbaa !5
  ret i64 %37
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: 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 = !{!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"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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