; LLVM IR from kenjin/Awesome-LC-C-Cracker :: leetcode-c-sol/0096_Unique-Binary-Search-Trees (0096_Unique-Binary-Search-Trees)

; 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 @numTrees(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = icmp sle i32 %8, 1
  br i1 %9, label %10, label %11

10:                                               ; preds = %1
  store i32 1, ptr %2, align 4
  br label %50

11:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  store i32 0, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %29, %11
  %13 = load i32, ptr %6, align 4, !tbaa !5
  %14 = load i32, ptr %3, align 4, !tbaa !5
  %15 = sdiv i32 %14, 2
  %16 = icmp slt i32 %13, %15
  br i1 %16, label %18, label %17

17:                                               ; preds = %12
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %32

18:                                               ; preds = %12
  %19 = load i32, ptr %6, align 4, !tbaa !5
  %20 = call i32 @numTrees(i32 noundef %19)
  %21 = load i32, ptr %3, align 4, !tbaa !5
  %22 = load i32, ptr %6, align 4, !tbaa !5
  %23 = sub nsw i32 %21, %22
  %24 = sub nsw i32 %23, 1
  %25 = call i32 @numTrees(i32 noundef %24)
  %26 = mul nsw i32 %20, %25
  %27 = load i32, ptr %4, align 4, !tbaa !5
  %28 = add nsw i32 %27, %26
  store i32 %28, ptr %4, align 4, !tbaa !5
  br label %29

29:                                               ; preds = %18
  %30 = load i32, ptr %6, align 4, !tbaa !5
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %6, align 4, !tbaa !5
  br label %12, !llvm.loop !9

32:                                               ; preds = %17
  %33 = load i32, ptr %3, align 4, !tbaa !5
  %34 = srem i32 %33, 2
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %36, label %45

36:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %37 = load i32, ptr %3, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  %39 = sdiv i32 %38, 2
  %40 = sub nsw i32 %39, 1
  %41 = call i32 @numTrees(i32 noundef %40)
  store i32 %41, ptr %7, align 4, !tbaa !5
  %42 = load i32, ptr %7, align 4, !tbaa !5
  %43 = load i32, ptr %7, align 4, !tbaa !5
  %44 = mul nsw i32 %42, %43
  store i32 %44, ptr %5, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %45

45:                                               ; preds = %36, %32
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = mul nsw i32 %46, 2
  %48 = load i32, ptr %5, align 4, !tbaa !5
  %49 = add nsw i32 %47, %48
  store i32 %49, ptr %2, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %50

50:                                               ; preds = %45, %10
  %51 = load i32, ptr %2, align 4
  ret i32 %51
}

; 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 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 = { 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 nosync nounwind memory(none) uwtable
define dso_local i32 @numTrees(i32 noundef %0) local_unnamed_addr #0 {
  %2 = icmp slt i32 %0, 2
  br i1 %2, label %21, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1
  %3 = lshr i32 %0, 1
  br label %.lr.ph

._crit_edge:                                      ; preds = %.lr.ph
  %4 = and i32 %0, 1
  %.not = icmp eq i32 %4, 0
  br i1 %.not, label %18, label %12

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.01520 = phi i32 [ %11, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %.01719 = phi i32 [ %10, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %5 = tail call i32 @numTrees(i32 noundef %.01520)
  %6 = xor i32 %.01520, -1
  %7 = add nsw i32 %6, %0
  %8 = tail call i32 @numTrees(i32 noundef %7)
  %9 = mul nsw i32 %8, %5
  %10 = add nsw i32 %9, %.01719
  %11 = add nuw nsw i32 %.01520, 1
  %exitcond.not = icmp eq i32 %11, %3
  br i1 %exitcond.not, label %._crit_edge, label %.lr.ph, !llvm.loop !5

12:                                               ; preds = %._crit_edge
  %13 = add nuw nsw i32 %0, 1
  %14 = lshr exact i32 %13, 1
  %15 = add nsw i32 %14, -1
  %16 = tail call i32 @numTrees(i32 noundef %15)
  %17 = mul nsw i32 %16, %16
  br label %18

18:                                               ; preds = %12, %._crit_edge
  %.016 = phi i32 [ %17, %12 ], [ 0, %._crit_edge ]
  %19 = shl nsw i32 %10, 1
  %20 = add nsw i32 %.016, %19
  br label %21

21:                                               ; preds = %1, %18
  %.0 = phi i32 [ %20, %18 ], [ 1, %1 ]
  ret i32 %.0
}

attributes #0 = { nofree 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 = distinct !{!5, !6}
!6 = !{!"llvm.loop.mustprogress"}


; Effective O3 passes: GVNPass,GlobalOptPass,IPSCCPPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass