; LLVM IR from kaynewbie/algorithm :: algorithm/LeetCode/cmmon (binary-tree)

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

%struct.TreeNode = type { i32, ptr, ptr }

; 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: nounwind uwtable
define dso_local i32 @maxDepth(ptr noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %6 = load ptr, ptr %3, align 8, !tbaa !5
  %7 = icmp eq ptr %6, null
  br i1 %7, label %8, label %9

8:                                                ; preds = %1
  store i32 0, ptr %2, align 4
  br label %66

9:                                                ; preds = %1
  %10 = load ptr, ptr %3, align 8, !tbaa !5
  %11 = getelementptr inbounds %struct.TreeNode, ptr %10, i32 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !9
  %13 = icmp eq ptr %12, null
  br i1 %13, label %14, label %20

14:                                               ; preds = %9
  %15 = load ptr, ptr %3, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.TreeNode, ptr %15, i32 0, i32 2
  %17 = load ptr, ptr %16, align 8, !tbaa !12
  %18 = icmp eq ptr %17, null
  br i1 %18, label %19, label %20

19:                                               ; preds = %14
  store i32 1, ptr %2, align 4
  br label %66

20:                                               ; preds = %14, %9
  %21 = load ptr, ptr %3, align 8, !tbaa !5
  %22 = getelementptr inbounds %struct.TreeNode, ptr %21, i32 0, i32 1
  %23 = load ptr, ptr %22, align 8, !tbaa !9
  %24 = icmp ne ptr %23, null
  br i1 %24, label %25, label %49

25:                                               ; preds = %20
  %26 = load ptr, ptr %3, align 8, !tbaa !5
  %27 = getelementptr inbounds %struct.TreeNode, ptr %26, i32 0, i32 2
  %28 = load ptr, ptr %27, align 8, !tbaa !12
  %29 = icmp ne ptr %28, null
  br i1 %29, label %30, label %49

30:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  %31 = load ptr, ptr %3, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.TreeNode, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !9
  %34 = call i32 @maxDepth(ptr noundef %33)
  store i32 %34, ptr %4, align 4, !tbaa !13
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #2
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.TreeNode, ptr %35, i32 0, i32 2
  %37 = load ptr, ptr %36, align 8, !tbaa !12
  %38 = call i32 @maxDepth(ptr noundef %37)
  store i32 %38, ptr %5, align 4, !tbaa !13
  %39 = load i32, ptr %4, align 4, !tbaa !13
  %40 = load i32, ptr %5, align 4, !tbaa !13
  %41 = icmp sgt i32 %39, %40
  br i1 %41, label %42, label %44

42:                                               ; preds = %30
  %43 = load i32, ptr %4, align 4, !tbaa !13
  br label %46

44:                                               ; preds = %30
  %45 = load i32, ptr %5, align 4, !tbaa !13
  br label %46

46:                                               ; preds = %44, %42
  %47 = phi i32 [ %43, %42 ], [ %45, %44 ]
  %48 = add nsw i32 %47, 1
  store i32 %48, 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 %66

49:                                               ; preds = %25, %20
  %50 = load ptr, ptr %3, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.TreeNode, ptr %50, i32 0, i32 1
  %52 = load ptr, ptr %51, align 8, !tbaa !9
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %60

54:                                               ; preds = %49
  %55 = load ptr, ptr %3, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.TreeNode, ptr %55, i32 0, i32 1
  %57 = load ptr, ptr %56, align 8, !tbaa !9
  %58 = call i32 @maxDepth(ptr noundef %57)
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %2, align 4
  br label %66

60:                                               ; preds = %49
  %61 = load ptr, ptr %3, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.TreeNode, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !12
  %64 = call i32 @maxDepth(ptr noundef %63)
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %2, align 4
  br label %66

66:                                               ; preds = %60, %54, %46, %19, %8
  %67 = load i32, ptr %2, align 4
  ret i32 %67
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"TreeNode", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!11, !11, i64 0}


; 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(read, inaccessiblemem: none) uwtable
define dso_local i32 @maxDepth(ptr noundef readonly %0) local_unnamed_addr #0 {
  br label %tailrecurse61

tailrecurse61:                                    ; preds = %21, %1
  %accumulator.tr = phi i32 [ 0, %1 ], [ %accumulator.ret.tr20, %21 ]
  %.tr = phi ptr [ %0, %1 ], [ %7, %21 ]
  %2 = icmp eq ptr %.tr, null
  br i1 %2, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %tailrecurse61
  %3 = getelementptr inbounds i8, ptr %.tr, i64 8
  %4 = load ptr, ptr %3, align 8, !tbaa !5
  %5 = icmp eq ptr %4, null
  br i1 %5, label %.lr.ph._crit_edge, label %.lr.ph49

.lr.ph._crit_edge:                                ; preds = %tailrecurse, %.lr.ph.preheader
  %.tr30.lcssa = phi ptr [ %.tr, %.lr.ph.preheader ], [ %9, %tailrecurse ]
  %accumulator.tr29.lcssa = phi i32 [ 0, %.lr.ph.preheader ], [ %17, %tailrecurse ]
  %6 = getelementptr inbounds i8, ptr %.tr30.lcssa, i64 16
  %7 = load ptr, ptr %6, align 8, !tbaa !11
  %8 = icmp eq ptr %7, null
  br i1 %8, label %.loopexit, label %21

.lr.ph49:                                         ; preds = %.lr.ph.preheader, %tailrecurse
  %9 = phi ptr [ %19, %tailrecurse ], [ %4, %.lr.ph.preheader ]
  %accumulator.tr2948 = phi i32 [ %17, %tailrecurse ], [ 0, %.lr.ph.preheader ]
  %.tr3047 = phi ptr [ %9, %tailrecurse ], [ %.tr, %.lr.ph.preheader ]
  %10 = getelementptr inbounds i8, ptr %.tr3047, i64 16
  %11 = load ptr, ptr %10, align 8, !tbaa !11
  %.not17 = icmp eq ptr %11, null
  br i1 %.not17, label %tailrecurse, label %12

common.ret64:                                     ; preds = %12, %.loopexit
  %common.ret64.op = phi i32 [ %accumulator.ret.tr62, %.loopexit ], [ %accumulator.ret.tr63, %12 ]
  ret i32 %common.ret64.op

12:                                               ; preds = %.lr.ph49
  %13 = tail call i32 @maxDepth(ptr noundef nonnull %9)
  %14 = tail call i32 @maxDepth(ptr noundef nonnull %11)
  %15 = tail call i32 @llvm.smax.i32(i32 %13, i32 %14)
  %16 = add nuw i32 %accumulator.tr2948, 1
  %accumulator.ret.tr = add i32 %16, %15
  %accumulator.ret.tr63 = add i32 %accumulator.ret.tr, %accumulator.tr
  br label %common.ret64

tailrecurse:                                      ; preds = %.lr.ph49
  %17 = add nuw nsw i32 %accumulator.tr2948, 1
  %18 = getelementptr inbounds i8, ptr %9, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = icmp eq ptr %19, null
  br i1 %20, label %.lr.ph._crit_edge, label %.lr.ph49

21:                                               ; preds = %.lr.ph._crit_edge
  %22 = add nuw i32 %accumulator.tr29.lcssa, 1
  %accumulator.ret.tr20 = add i32 %22, %accumulator.tr
  br label %tailrecurse61

.loopexit:                                        ; preds = %tailrecurse61, %.lr.ph._crit_edge
  %accumulator.tr26 = phi i32 [ %accumulator.tr29.lcssa, %.lr.ph._crit_edge ], [ 0, %tailrecurse61 ]
  %.0 = phi i32 [ 1, %.lr.ph._crit_edge ], [ 0, %tailrecurse61 ]
  %accumulator.ret.tr21 = add nuw nsw i32 %.0, %accumulator.tr26
  %accumulator.ret.tr62 = add i32 %accumulator.ret.tr21, %accumulator.tr
  br label %common.ret64
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #1

attributes #0 = { nofree nosync nounwind memory(read, 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" }
attributes #1 = { 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, !10, i64 8}
!6 = !{!"TreeNode", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}


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