; LLVM IR from JanaSabuj/cppdump :: catalan.ll (Z4initx)

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

@catalan = 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 void @_Z4initx(i64 noundef %0) #1 {
  %2 = alloca i64, align 8
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  store i64 %0, ptr %2, align 8, !tbaa !5
  store i64 1, ptr getelementptr inbounds ([100 x i64], ptr @catalan, i64 0, i64 1), align 8, !tbaa !5
  store i64 1, ptr @catalan, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %3) #2
  store i64 2, ptr %3, align 8, !tbaa !5
  br label %6

6:                                                ; preds = %49, %1
  %7 = load i64, ptr %3, align 8, !tbaa !5
  %8 = load i64, ptr %2, align 8, !tbaa !5
  %9 = icmp slt i64 %7, %8
  br i1 %9, label %11, label %10

10:                                               ; preds = %6
  store i32 2, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %3) #2
  br label %52

11:                                               ; preds = %6
  %12 = load i64, ptr %3, align 8, !tbaa !5
  %13 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %12
  store i64 0, ptr %13, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  store i64 0, ptr %5, align 8, !tbaa !5
  br label %14

14:                                               ; preds = %45, %11
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = load i64, ptr %3, align 8, !tbaa !5
  %17 = icmp slt i64 %15, %16
  br i1 %17, label %19, label %18

18:                                               ; preds = %14
  store i32 5, ptr %4, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %48

19:                                               ; preds = %14
  %20 = load i64, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %20
  %22 = load i64, ptr %21, align 8, !tbaa !5
  %23 = load i64, ptr %3, align 8, !tbaa !5
  %24 = load i64, ptr %5, align 8, !tbaa !5
  %25 = sub nsw i64 %23, %24
  %26 = sub nsw i64 %25, 1
  %27 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %26
  %28 = load i64, ptr %27, align 8, !tbaa !5
  %29 = mul nsw i64 %22, %28
  %30 = srem i64 %29, 1000000007
  %31 = load i64, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %31
  %33 = load i64, ptr %32, align 8, !tbaa !5
  %34 = add nsw i64 %33, %30
  store i64 %34, ptr %32, align 8, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = icmp sge i64 %37, 1000000007
  br i1 %38, label %39, label %44

39:                                               ; preds = %19
  %40 = load i64, ptr %3, align 8, !tbaa !5
  %41 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %40
  %42 = load i64, ptr %41, align 8, !tbaa !5
  %43 = sub nsw i64 %42, 1000000007
  store i64 %43, ptr %41, align 8, !tbaa !5
  br label %44

44:                                               ; preds = %39, %19
  br label %45

45:                                               ; preds = %44
  %46 = load i64, ptr %5, align 8, !tbaa !5
  %47 = add nsw i64 %46, 1
  store i64 %47, ptr %5, align 8, !tbaa !5
  br label %14, !llvm.loop !9

48:                                               ; preds = %18
  br label %49

49:                                               ; preds = %48
  %50 = load i64, ptr %3, align 8, !tbaa !5
  %51 = add nsw i64 %50, 1
  store i64 %51, ptr %3, align 8, !tbaa !5
  br label %6, !llvm.loop !11

52:                                               ; preds = %10
  ret void
}

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 = !{!"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, !10}


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

@catalan = 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 void @_Z4initx(i64 noundef %0) local_unnamed_addr #0 {
  store i64 1, ptr getelementptr inbounds (i8, ptr @catalan, i64 8), align 8, !tbaa !5
  store i64 1, ptr @catalan, align 16, !tbaa !5
  %2 = icmp sgt i64 %0, 2
  br i1 %2, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %4, %1
  ret void

.lr.ph:                                           ; preds = %1, %4
  %.01318 = phi i64 [ %5, %4 ], [ 2, %1 ]
  %3 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %.01318
  store i64 0, ptr %3, align 8, !tbaa !5
  br label %6

4:                                                ; preds = %6
  %5 = add nuw nsw i64 %.01318, 1
  %exitcond19.not = icmp eq i64 %5, %0
  br i1 %exitcond19.not, label %._crit_edge, label %.lr.ph, !llvm.loop !9

6:                                                ; preds = %.lr.ph, %6
  %.017 = phi i64 [ 0, %.lr.ph ], [ %18, %6 ]
  %spec.select1516 = phi i64 [ 0, %.lr.ph ], [ %spec.select, %6 ]
  %7 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %.017
  %8 = load i64, ptr %7, align 8, !tbaa !5
  %9 = xor i64 %.017, -1
  %10 = add nsw i64 %.01318, %9
  %11 = getelementptr inbounds [100 x i64], ptr @catalan, i64 0, i64 %10
  %12 = load i64, ptr %11, align 8, !tbaa !5
  %13 = mul nsw i64 %12, %8
  %14 = srem i64 %13, 1000000007
  %15 = add nsw i64 %14, %spec.select1516
  %16 = icmp sgt i64 %15, 1000000006
  %17 = add nsw i64 %15, -1000000007
  %spec.select = select i1 %16, i64 %17, i64 %15
  store i64 %spec.select, ptr %3, align 8, !tbaa !5
  %18 = add nuw nsw i64 %.017, 1
  %exitcond.not = icmp eq i64 %18, %.01318
  br i1 %exitcond.not, label %4, label %6, !llvm.loop !11
}

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, !10}


; Effective O3 passes: EarlyCSEPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass