; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 15. Binary Tree/14 Children Sum Property/14.ll (Z11childrenSumP4Node)

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

%class.Node = type { i32, ptr, ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef zeroext i1 @_Z11childrenSumP4Node(ptr noundef %0) #0 {
  %2 = alloca i1, align 1
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %5 = load ptr, ptr %3, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %8

7:                                                ; preds = %1
  store i1 true, ptr %2, align 1
  br label %63

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

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

18:                                               ; preds = %13
  store i1 true, ptr %2, align 1
  br label %63

19:                                               ; preds = %13, %8
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !13
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = getelementptr inbounds %class.Node, ptr %20, i32 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !9
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %32

24:                                               ; preds = %19
  %25 = load ptr, ptr %3, align 8, !tbaa !5
  %26 = getelementptr inbounds %class.Node, ptr %25, i32 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !9
  %28 = getelementptr inbounds %class.Node, ptr %27, i32 0, i32 0
  %29 = load i32, ptr %28, align 8, !tbaa !14
  %30 = load i32, ptr %4, align 4, !tbaa !13
  %31 = add nsw i32 %30, %29
  store i32 %31, ptr %4, align 4, !tbaa !13
  br label %32

32:                                               ; preds = %24, %19
  %33 = load ptr, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds %class.Node, ptr %33, i32 0, i32 2
  %35 = load ptr, ptr %34, align 8, !tbaa !12
  %36 = icmp ne ptr %35, null
  br i1 %36, label %37, label %45

37:                                               ; preds = %32
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %class.Node, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !12
  %41 = getelementptr inbounds %class.Node, ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !14
  %43 = load i32, ptr %4, align 4, !tbaa !13
  %44 = add nsw i32 %43, %42
  store i32 %44, ptr %4, align 4, !tbaa !13
  br label %45

45:                                               ; preds = %37, %32
  %46 = load ptr, ptr %3, align 8, !tbaa !5
  %47 = getelementptr inbounds %class.Node, ptr %46, i32 0, i32 0
  %48 = load i32, ptr %47, align 8, !tbaa !14
  %49 = load i32, ptr %4, align 4, !tbaa !13
  %50 = icmp eq i32 %48, %49
  br i1 %50, label %51, label %61

51:                                               ; preds = %45
  %52 = load ptr, ptr %3, align 8, !tbaa !5
  %53 = getelementptr inbounds %class.Node, ptr %52, i32 0, i32 1
  %54 = load ptr, ptr %53, align 8, !tbaa !9
  %55 = call noundef zeroext i1 @_Z11childrenSumP4Node(ptr noundef %54)
  br i1 %55, label %56, label %61

56:                                               ; preds = %51
  %57 = load ptr, ptr %3, align 8, !tbaa !5
  %58 = getelementptr inbounds %class.Node, ptr %57, i32 0, i32 2
  %59 = load ptr, ptr %58, align 8, !tbaa !12
  %60 = call noundef zeroext i1 @_Z11childrenSumP4Node(ptr noundef %59)
  br label %61

61:                                               ; preds = %56, %51, %45
  %62 = phi i1 [ false, %51 ], [ false, %45 ], [ %60, %56 ]
  store i1 %62, ptr %2, align 1
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  br label %63

63:                                               ; preds = %61, %18, %7
  %64 = load i1, ptr %2, align 1
  ret i1 %64
}

; 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 = { mustprogress 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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 8}
!10 = !{!"_ZTS4Node", !11, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!"int", !7, i64 0}
!12 = !{!10, !6, i64 16}
!13 = !{!11, !11, i64 0}
!14 = !{!10, !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: mustprogress nofree nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local noundef zeroext i1 @_Z11childrenSumP4Node(ptr noundef readonly %0) local_unnamed_addr #0 {
  br label %tailrecurse

tailrecurse:                                      ; preds = %19, %1
  %.tr = phi ptr [ %0, %1 ], [ %16, %19 ]
  %2 = icmp eq ptr %.tr, null
  br i1 %2, label %21, label %3

3:                                                ; preds = %tailrecurse
  %4 = getelementptr inbounds i8, ptr %.tr, i64 8
  %5 = load ptr, ptr %4, align 8, !tbaa !5
  %6 = icmp eq ptr %5, null
  br i1 %6, label %7, label %.thread

7:                                                ; preds = %3
  %8 = getelementptr inbounds i8, ptr %.tr, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = icmp eq ptr %9, null
  br i1 %10, label %21, label %.thread.thread

.thread:                                          ; preds = %3
  %11 = load i32, ptr %5, align 8, !tbaa !12
  %.phi.trans.insert = getelementptr inbounds i8, ptr %.tr, i64 16
  %.pre = load ptr, ptr %.phi.trans.insert, align 8, !tbaa !11
  %.not17 = icmp eq ptr %.pre, null
  br i1 %.not17, label %15, label %.thread.thread

.thread.thread:                                   ; preds = %7, %.thread
  %.020 = phi i32 [ %11, %.thread ], [ 0, %7 ]
  %12 = phi ptr [ %.pre, %.thread ], [ %9, %7 ]
  %13 = load i32, ptr %12, align 8, !tbaa !12
  %14 = add nsw i32 %13, %.020
  br label %15

15:                                               ; preds = %.thread.thread, %.thread
  %16 = phi ptr [ %12, %.thread.thread ], [ null, %.thread ]
  %.1 = phi i32 [ %14, %.thread.thread ], [ %11, %.thread ]
  %17 = load i32, ptr %.tr, align 8, !tbaa !12
  %18 = icmp eq i32 %17, %.1
  br i1 %18, label %19, label %21

19:                                               ; preds = %15
  %20 = tail call noundef zeroext i1 @_Z11childrenSumP4Node(ptr noundef %5)
  br i1 %20, label %tailrecurse, label %21

21:                                               ; preds = %15, %19, %7, %tailrecurse
  %.013 = phi i1 [ true, %tailrecurse ], [ true, %7 ], [ false, %19 ], [ false, %15 ]
  ret i1 %.013
}

attributes #0 = { mustprogress nofree nosync nounwind willreturn 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" }

!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 = !{!"_ZTS4Node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}
!12 = !{!6, !7, i64 0}


; Effective O3 passes: BDCEPass,CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,PostOrderFunctionAttrsPass,ReassociatePass,SROAPass,SimplifyCFGPass,TailCallElimPass