; LLVM IR from devparthgarg/Data-Structures-and-Algorithms :: 15. Binary Tree/23 Count Nodes in Complete Binary Tree/23b.ll (Z5countP4Node)

; 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 i32 @_Z5countP4Node(ptr noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca ptr, align 8
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  store i32 0, ptr %5, align 4, !tbaa !9
  br label %9

9:                                                ; preds = %12, %1
  %10 = load ptr, ptr %4, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %18

12:                                               ; preds = %9
  %13 = load ptr, ptr %4, align 8, !tbaa !5
  %14 = getelementptr inbounds %class.Node, ptr %13, i32 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = add nsw i32 %16, 1
  store i32 %17, ptr %5, align 4, !tbaa !9
  br label %9, !llvm.loop !13

18:                                               ; preds = %9
  %19 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %19, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  store i32 0, ptr %6, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %23, %18
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %23, label %29

23:                                               ; preds = %20
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %class.Node, ptr %24, i32 0, i32 2
  %26 = load ptr, ptr %25, align 8, !tbaa !15
  store ptr %26, ptr %4, align 8, !tbaa !5
  %27 = load i32, ptr %6, align 4, !tbaa !9
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr %6, align 4, !tbaa !9
  br label %20, !llvm.loop !16

29:                                               ; preds = %20
  %30 = load i32, ptr %5, align 4, !tbaa !9
  %31 = load i32, ptr %6, align 4, !tbaa !9
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %38

33:                                               ; preds = %29
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = call noundef double @_ZSt3powIiiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(i32 noundef 2, i32 noundef %34)
  %36 = fsub double %35, 1.000000e+00
  %37 = fptosi double %36 to i32
  store i32 %37, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %49

38:                                               ; preds = %29
  %39 = load ptr, ptr %3, align 8, !tbaa !5
  %40 = getelementptr inbounds %class.Node, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  %42 = call noundef i32 @_Z5countP4Node(ptr noundef %41)
  %43 = add nsw i32 1, %42
  %44 = load ptr, ptr %3, align 8, !tbaa !5
  %45 = getelementptr inbounds %class.Node, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  %47 = call noundef i32 @_Z5countP4Node(ptr noundef %46)
  %48 = add nsw i32 %43, %47
  store i32 %48, ptr %2, align 4
  store i32 1, ptr %7, align 4
  br label %49

49:                                               ; preds = %38, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #3
  %50 = load i32, ptr %2, align 4
  ret i32 %50
}

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIiiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(i32 noundef, i32 noundef) #2

; 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 = { inlinehint 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 #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 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !6, i64 8}
!12 = !{!"_ZTS4Node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !6, i64 16}
!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 nounwind uwtable
define dso_local noundef i32 @_Z5countP4Node(ptr noundef readonly %0) local_unnamed_addr #0 {
  %.not1735 = icmp eq ptr %0, null
  br i1 %.not1735, label %._crit_edge.thread, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %1, %._crit_edge.thread31
  %.not1738 = phi i1 [ %.not17, %._crit_edge.thread31 ], [ %.not1735, %1 ]
  %.tr37 = phi ptr [ %16, %._crit_edge.thread31 ], [ %0, %1 ]
  %accumulator.tr36 = phi i32 [ %18, %._crit_edge.thread31 ], [ 0, %1 ]
  br label %.lr.ph

.preheader:                                       ; preds = %.lr.ph
  br i1 %.not1738, label %._crit_edge.thread31, label %.lr.ph23

.lr.ph:                                           ; preds = %.lr.ph.preheader, %.lr.ph
  %.01319 = phi i32 [ %4, %.lr.ph ], [ 0, %.lr.ph.preheader ]
  %.01418 = phi ptr [ %3, %.lr.ph ], [ %.tr37, %.lr.ph.preheader ]
  %2 = getelementptr inbounds i8, ptr %.01418, i64 8
  %3 = load ptr, ptr %2, align 8, !tbaa !5
  %4 = add nuw nsw i32 %.01319, 1
  %.not = icmp eq ptr %3, null
  br i1 %.not, label %.preheader, label %.lr.ph, !llvm.loop !11

.lr.ph23:                                         ; preds = %.preheader, %.lr.ph23
  %.022 = phi i32 [ %7, %.lr.ph23 ], [ 0, %.preheader ]
  %.121 = phi ptr [ %6, %.lr.ph23 ], [ %.tr37, %.preheader ]
  %5 = getelementptr inbounds i8, ptr %.121, i64 16
  %6 = load ptr, ptr %5, align 8, !tbaa !13
  %7 = add nuw nsw i32 %.022, 1
  %.not16 = icmp eq ptr %6, null
  br i1 %.not16, label %._crit_edge, label %.lr.ph23, !llvm.loop !14

._crit_edge:                                      ; preds = %.lr.ph23
  %8 = icmp eq i32 %.01319, %.022
  br i1 %8, label %._crit_edge.thread, label %._crit_edge.thread31

._crit_edge.thread:                               ; preds = %._crit_edge, %._crit_edge.thread31, %1
  %accumulator.tr.lcssa = phi i32 [ 0, %1 ], [ %accumulator.tr36, %._crit_edge ], [ %18, %._crit_edge.thread31 ]
  %.013.lcssa2730 = phi i32 [ 0, %1 ], [ %4, %._crit_edge ], [ 0, %._crit_edge.thread31 ]
  %9 = tail call noundef double @_ZSt3powIiiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(i32 noundef 2, i32 noundef %.013.lcssa2730)
  %10 = fadd double %9, -1.000000e+00
  %11 = fptosi double %10 to i32
  %accumulator.ret.tr = add nsw i32 %accumulator.tr.lcssa, %11
  ret i32 %accumulator.ret.tr

._crit_edge.thread31:                             ; preds = %.preheader, %._crit_edge
  %12 = getelementptr inbounds i8, ptr %.tr37, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !5
  %14 = tail call noundef i32 @_Z5countP4Node(ptr noundef %13)
  %15 = getelementptr inbounds i8, ptr %.tr37, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  %17 = add i32 %accumulator.tr36, 1
  %18 = add i32 %17, %14
  %.not17 = icmp eq ptr %16, null
  br i1 %.not17, label %._crit_edge.thread, label %.lr.ph.preheader
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef double @_ZSt3powIiiEN9__gnu_cxx9__promoteIDTplcvNS1_IT_Xsr3std12__is_integerIS2_EE7__valueEE6__typeELi0EcvNS1_IT0_Xsr3std12__is_integerIS5_EE7__valueEE6__typeELi0EEXsr3std12__is_integerIS8_EE7__valueEE6__typeES2_S5_(i32 noundef, i32 noundef) local_unnamed_addr #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 = { inlinehint 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" }

!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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!6, !10, i64 16}
!14 = distinct !{!14, !12}


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