; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/dp/Catalan_DP.ll (Z9n_catalanl)

; 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 @_Z9n_catalanl(i64 noundef %0) #0 {
  %2 = alloca i64, align 8
  %3 = alloca ptr, align 8
  %4 = alloca i64, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store i64 %0, ptr %2, align 8, !tbaa !5
  %9 = load i64, ptr %2, align 8, !tbaa !5
  %10 = add nsw i64 %9, 1
  %11 = call ptr @llvm.stacksave.p0()
  store ptr %11, ptr %3, align 8
  %12 = alloca i64, i64 %10, align 16
  store i64 %10, ptr %4, align 8
  %13 = getelementptr inbounds i64, ptr %12, i64 0
  store i64 1, ptr %13, align 16, !tbaa !9
  %14 = getelementptr inbounds i64, ptr %12, i64 1
  store i64 1, ptr %14, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 2, ptr %5, align 4, !tbaa !11
  br label %15

15:                                               ; preds = %26, %1
  %16 = load i32, ptr %5, align 4, !tbaa !11
  %17 = sext i32 %16 to i64
  %18 = load i64, ptr %2, align 8, !tbaa !5
  %19 = add nsw i64 %18, 1
  %20 = icmp slt i64 %17, %19
  br i1 %20, label %22, label %21

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

22:                                               ; preds = %15
  %23 = load i32, ptr %5, align 4, !tbaa !11
  %24 = sext i32 %23 to i64
  %25 = getelementptr inbounds i64, ptr %12, i64 %24
  store i64 0, ptr %25, align 8, !tbaa !9
  br label %26

26:                                               ; preds = %22
  %27 = load i32, ptr %5, align 4, !tbaa !11
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %5, align 4, !tbaa !11
  br label %15, !llvm.loop !13

29:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 2, ptr %6, align 4, !tbaa !11
  br label %30

30:                                               ; preds = %65, %29
  %31 = load i32, ptr %6, align 4, !tbaa !11
  %32 = sext i32 %31 to i64
  %33 = load i64, ptr %2, align 8, !tbaa !5
  %34 = add nsw i64 %33, 1
  %35 = icmp slt i64 %32, %34
  br i1 %35, label %37, label %36

36:                                               ; preds = %30
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %68

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

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

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

43:                                               ; preds = %38
  %44 = load i32, ptr %8, align 4, !tbaa !11
  %45 = sext i32 %44 to i64
  %46 = getelementptr inbounds i64, ptr %12, i64 %45
  %47 = load i64, ptr %46, align 8, !tbaa !9
  %48 = load i32, ptr %6, align 4, !tbaa !11
  %49 = load i32, ptr %8, align 4, !tbaa !11
  %50 = sub nsw i32 %48, %49
  %51 = sub nsw i32 %50, 1
  %52 = sext i32 %51 to i64
  %53 = getelementptr inbounds i64, ptr %12, i64 %52
  %54 = load i64, ptr %53, align 8, !tbaa !9
  %55 = mul nsw i64 %47, %54
  %56 = load i32, ptr %6, align 4, !tbaa !11
  %57 = sext i32 %56 to i64
  %58 = getelementptr inbounds i64, ptr %12, i64 %57
  %59 = load i64, ptr %58, align 8, !tbaa !9
  %60 = add nsw i64 %59, %55
  store i64 %60, ptr %58, align 8, !tbaa !9
  br label %61

61:                                               ; preds = %43
  %62 = load i32, ptr %8, align 4, !tbaa !11
  %63 = add nsw i32 %62, 1
  store i32 %63, ptr %8, align 4, !tbaa !11
  br label %38, !llvm.loop !15

64:                                               ; preds = %42
  br label %65

65:                                               ; preds = %64
  %66 = load i32, ptr %6, align 4, !tbaa !11
  %67 = add nsw i32 %66, 1
  store i32 %67, ptr %6, align 4, !tbaa !11
  br label %30, !llvm.loop !16

68:                                               ; preds = %36
  %69 = load i64, ptr %2, align 8, !tbaa !5
  %70 = getelementptr inbounds i64, ptr %12, i64 %69
  %71 = load i64, ptr %70, align 8, !tbaa !9
  store i32 1, ptr %7, align 4
  %72 = load ptr, ptr %3, align 8
  call void @llvm.stackrestore.p0(ptr %72)
  ret i64 %71
}

; 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 = { 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 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 = !{!"long", !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 = !{!12, !12, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = distinct !{!15, !14}
!16 = distinct !{!16, !14}


; 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 @_Z9n_catalanl(i64 noundef %0) local_unnamed_addr #0 {
  %2 = add i64 %0, 1
  %3 = alloca i64, i64 %2, align 16
  store i64 1, ptr %3, align 16, !tbaa !5
  %4 = getelementptr inbounds i8, ptr %3, i64 8
  store i64 1, ptr %4, align 8, !tbaa !5
  %.not23 = icmp slt i64 %0, 2
  br i1 %.not23, label %._crit_edge, label %.preheader.preheader

.preheader.preheader:                             ; preds = %1
  %scevgep = getelementptr i8, ptr %3, i64 16
  %5 = shl i64 %0, 3
  %6 = add i64 %5, -8
  call void @llvm.memset.p0.i64(ptr align 16 %scevgep, i8 0, i64 %6, i1 false), !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %.loopexit
  %indvars.iv30 = phi i64 [ 2, %.preheader.preheader ], [ %indvars.iv.next31, %.loopexit ]
  %7 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv30
  %.promoted = load i64, ptr %7, align 8, !tbaa !5
  %8 = getelementptr i64, ptr %3, i64 %indvars.iv30
  %min.iters.check = icmp ult i64 %indvars.iv30, 4
  br i1 %min.iters.check, label %scalar.ph.preheader, label %vector.ph

scalar.ph.preheader:                              ; preds = %middle.block, %.preheader
  %indvars.iv.ph = phi i64 [ 0, %.preheader ], [ %n.vec, %middle.block ]
  %.ph = phi i64 [ %.promoted, %.preheader ], [ %21, %middle.block ]
  br label %scalar.ph

vector.ph:                                        ; preds = %.preheader
  %n.vec = and i64 %indvars.iv30, 9223372036854775804
  %9 = insertelement <2 x i64> <i64 poison, i64 0>, i64 %.promoted, i64 0
  br label %vector.body

vector.body:                                      ; preds = %vector.body, %vector.ph
  %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
  %vec.phi = phi <2 x i64> [ %9, %vector.ph ], [ %18, %vector.body ]
  %vec.phi34 = phi <2 x i64> [ zeroinitializer, %vector.ph ], [ %19, %vector.body ]
  %10 = getelementptr inbounds i64, ptr %3, i64 %index
  %11 = getelementptr inbounds i8, ptr %10, i64 16
  %wide.load = load <2 x i64>, ptr %10, align 16, !tbaa !5
  %wide.load35 = load <2 x i64>, ptr %11, align 16, !tbaa !5
  %12 = xor i64 %index, -1
  %13 = getelementptr i64, ptr %8, i64 %12
  %14 = getelementptr i8, ptr %13, i64 -8
  %15 = getelementptr i8, ptr %13, i64 -24
  %wide.load36 = load <2 x i64>, ptr %14, align 8, !tbaa !5
  %reverse = shufflevector <2 x i64> %wide.load36, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %wide.load37 = load <2 x i64>, ptr %15, align 8, !tbaa !5
  %reverse38 = shufflevector <2 x i64> %wide.load37, <2 x i64> poison, <2 x i32> <i32 1, i32 0>
  %16 = mul nsw <2 x i64> %reverse, %wide.load
  %17 = mul nsw <2 x i64> %reverse38, %wide.load35
  %18 = add <2 x i64> %vec.phi, %16
  %19 = add <2 x i64> %vec.phi34, %17
  %index.next = add nuw i64 %index, 4
  %20 = icmp eq i64 %index.next, %n.vec
  br i1 %20, label %middle.block, label %vector.body, !llvm.loop !9

middle.block:                                     ; preds = %vector.body
  %bin.rdx = add <2 x i64> %19, %18
  %21 = tail call i64 @llvm.vector.reduce.add.v2i64(<2 x i64> %bin.rdx)
  store i64 %21, ptr %7, align 8, !tbaa !5
  %cmp.n = icmp eq i64 %indvars.iv30, %n.vec
  br i1 %cmp.n, label %.loopexit, label %scalar.ph.preheader

._crit_edge:                                      ; preds = %.loopexit, %1
  %22 = getelementptr inbounds i64, ptr %3, i64 %0
  %23 = load i64, ptr %22, align 8, !tbaa !5
  ret i64 %23

.loopexit:                                        ; preds = %scalar.ph, %middle.block
  %indvars.iv.next31 = add nuw nsw i64 %indvars.iv30, 1
  %exitcond33 = icmp eq i64 %indvars.iv30, %0
  br i1 %exitcond33, label %._crit_edge, label %.preheader, !llvm.loop !13

scalar.ph:                                        ; preds = %scalar.ph.preheader, %scalar.ph
  %indvars.iv = phi i64 [ %indvars.iv.next, %scalar.ph ], [ %indvars.iv.ph, %scalar.ph.preheader ]
  %24 = phi i64 [ %31, %scalar.ph ], [ %.ph, %scalar.ph.preheader ]
  %25 = getelementptr inbounds i64, ptr %3, i64 %indvars.iv
  %26 = load i64, ptr %25, align 8, !tbaa !5
  %27 = xor i64 %indvars.iv, -1
  %28 = getelementptr i64, ptr %8, i64 %27
  %29 = load i64, ptr %28, align 8, !tbaa !5
  %30 = mul nsw i64 %29, %26
  %31 = add nsw i64 %24, %30
  store i64 %31, ptr %7, align 8, !tbaa !5
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %indvars.iv30
  br i1 %exitcond.not, label %.loopexit, label %scalar.ph, !llvm.loop !14
}

; Function Attrs: 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 nosync nounwind speculatable willreturn memory(none)
declare i64 @llvm.vector.reduce.add.v2i64(<2 x i64>) #2

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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #2 = { 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 = !{!"long long", !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}
!14 = distinct !{!14, !10, !12, !11}


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