; LLVM IR from lolosssss/leetcode :: c (70_climbing_stairs)

; 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 i32 @climbStairs(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = zext i32 %8 to i64
  %10 = call ptr @llvm.stacksave.p0()
  store ptr %10, ptr %5, align 8
  %11 = alloca i32, i64 %9, align 16
  store i64 %9, ptr %6, align 8
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = icmp slt i32 %12, 4
  br i1 %13, label %14, label %16

14:                                               ; preds = %1
  %15 = load i32, ptr %3, align 4, !tbaa !5
  store i32 %15, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %46

16:                                               ; preds = %1
  %17 = getelementptr inbounds i32, ptr %11, i64 0
  store i32 1, ptr %17, align 16, !tbaa !5
  %18 = getelementptr inbounds i32, ptr %11, i64 1
  store i32 2, ptr %18, align 4, !tbaa !5
  store i32 2, ptr %4, align 4, !tbaa !5
  br label %19

19:                                               ; preds = %23, %16
  %20 = load i32, ptr %4, align 4, !tbaa !5
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = icmp slt i32 %20, %21
  br i1 %22, label %23, label %40

23:                                               ; preds = %19
  %24 = load i32, ptr %4, align 4, !tbaa !5
  %25 = sub nsw i32 %24, 1
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i32, ptr %11, i64 %26
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = load i32, ptr %4, align 4, !tbaa !5
  %30 = sub nsw i32 %29, 2
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds i32, ptr %11, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  %34 = add nsw i32 %28, %33
  %35 = load i32, ptr %4, align 4, !tbaa !5
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %11, i64 %36
  store i32 %34, ptr %37, align 4, !tbaa !5
  %38 = load i32, ptr %4, align 4, !tbaa !5
  %39 = add nsw i32 %38, 1
  store i32 %39, ptr %4, align 4, !tbaa !5
  br label %19, !llvm.loop !9

40:                                               ; preds = %19
  %41 = load i32, ptr %4, align 4, !tbaa !5
  %42 = sub nsw i32 %41, 1
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds i32, ptr %11, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  store i32 %45, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %46

46:                                               ; preds = %40, %14
  %47 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %47)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  %48 = load i32, ptr %2, align 4
  ret i32 %48
}

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

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

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

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #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 memory(argmem: readwrite) }
attributes #2 = { nocallback nofree nosync nounwind willreturn }
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 = 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: nofree norecurse nosync nounwind memory(none) uwtable
define dso_local i32 @climbStairs(i32 noundef %0) local_unnamed_addr #0 {
  %2 = zext i32 %0 to i64
  %3 = alloca i32, i64 %2, align 16
  %4 = icmp slt i32 %0, 4
  br i1 %4, label %35, label %5

5:                                                ; preds = %1
  store i32 1, ptr %3, align 16, !tbaa !5
  %6 = getelementptr inbounds i8, ptr %3, i64 4
  store i32 2, ptr %6, align 4, !tbaa !5
  %7 = add nsw i64 %2, -2
  %8 = add nsw i64 %2, -3
  %xtraiter = and i64 %7, 3
  %9 = icmp ult i64 %8, 3
  br i1 %9, label %.unr-lcssa, label %.new

.new:                                             ; preds = %5
  %unroll_iter = and i64 %7, -4
  br label %10

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

.unr-lcssa:                                       ; preds = %10, %5
  %store_forwarded.unr = phi i32 [ 2, %5 ], [ %27, %10 ]
  %indvars.iv.unr = phi i64 [ 2, %5 ], [ %indvars.iv.next.3, %10 ]
  %lcmp.mod.not = icmp eq i64 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.epilog-lcssa, label %.epil.preheader

.epil.preheader:                                  ; preds = %.unr-lcssa, %.epil.preheader
  %store_forwarded.epil = phi i32 [ %31, %.epil.preheader ], [ %store_forwarded.unr, %.unr-lcssa ]
  %indvars.iv.epil = phi i64 [ %indvars.iv.next.epil, %.epil.preheader ], [ %indvars.iv.unr, %.unr-lcssa ]
  %epil.iter = phi i64 [ %epil.iter.next, %.epil.preheader ], [ 0, %.unr-lcssa ]
  %28 = getelementptr i32, ptr %3, i64 %indvars.iv.epil
  %29 = getelementptr i8, ptr %28, i64 -8
  %30 = load i32, ptr %29, align 4, !tbaa !5
  %31 = add nsw i32 %30, %store_forwarded.epil
  store i32 %31, ptr %28, align 4, !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 %.epilog-lcssa, label %.epil.preheader, !llvm.loop !11

.epilog-lcssa:                                    ; preds = %.epil.preheader, %.unr-lcssa
  %32 = getelementptr i32, ptr %3, i64 %2
  %33 = getelementptr i8, ptr %32, i64 -4
  %34 = load i32, ptr %33, align 4, !tbaa !5
  br label %35

35:                                               ; preds = %1, %.epilog-lcssa
  %.0 = phi i32 [ %34, %.epilog-lcssa ], [ %0, %1 ]
  ret i32 %.0
}

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

!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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.unroll.disable"}


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