; LLVM IR from ritikbanger/Hacktoberfest2022-DSA :: 15. Binary Search Tree/isDeadEnd.ll (Z14getCountOfNodeP4Nodeii)

; 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.Node = type { i32, ptr, ptr }

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

10:                                               ; preds = %3
  store i32 0, ptr %4, align 4
  br label %71

11:                                               ; preds = %3
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = getelementptr inbounds %struct.Node, ptr %12, i32 0, i32 0
  %14 = load i32, ptr %13, align 8, !tbaa !11
  %15 = load i32, ptr %7, align 4, !tbaa !9
  %16 = icmp eq i32 %14, %15
  br i1 %16, label %17, label %24

17:                                               ; preds = %11
  %18 = load ptr, ptr %5, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.Node, ptr %18, i32 0, i32 0
  %20 = load i32, ptr %19, align 8, !tbaa !11
  %21 = load i32, ptr %6, align 4, !tbaa !9
  %22 = icmp eq i32 %20, %21
  br i1 %22, label %23, label %24

23:                                               ; preds = %17
  store i32 1, ptr %4, align 4
  br label %71

24:                                               ; preds = %17, %11
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.Node, ptr %25, i32 0, i32 0
  %27 = load i32, ptr %26, align 8, !tbaa !11
  %28 = load i32, ptr %7, align 4, !tbaa !9
  %29 = icmp sle i32 %27, %28
  br i1 %29, label %30, label %51

30:                                               ; preds = %24
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.Node, ptr %31, i32 0, i32 0
  %33 = load i32, ptr %32, align 8, !tbaa !11
  %34 = load i32, ptr %6, align 4, !tbaa !9
  %35 = icmp sge i32 %33, %34
  br i1 %35, label %36, label %51

36:                                               ; preds = %30
  %37 = load ptr, ptr %5, align 8, !tbaa !5
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 1
  %39 = load ptr, ptr %38, align 8, !tbaa !13
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = load i32, ptr %7, align 4, !tbaa !9
  %42 = call noundef i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef %39, i32 noundef %40, i32 noundef %41)
  %43 = add nsw i32 1, %42
  %44 = load ptr, ptr %5, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !14
  %47 = load i32, ptr %6, align 4, !tbaa !9
  %48 = load i32, ptr %7, align 4, !tbaa !9
  %49 = call noundef i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef %46, i32 noundef %47, i32 noundef %48)
  %50 = add nsw i32 %43, %49
  store i32 %50, ptr %4, align 4
  br label %71

51:                                               ; preds = %30, %24
  %52 = load ptr, ptr %5, align 8, !tbaa !5
  %53 = getelementptr inbounds %struct.Node, ptr %52, i32 0, i32 0
  %54 = load i32, ptr %53, align 8, !tbaa !11
  %55 = load i32, ptr %6, align 4, !tbaa !9
  %56 = icmp slt i32 %54, %55
  br i1 %56, label %57, label %64

57:                                               ; preds = %51
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.Node, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !14
  %61 = load i32, ptr %6, align 4, !tbaa !9
  %62 = load i32, ptr %7, align 4, !tbaa !9
  %63 = call noundef i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef %60, i32 noundef %61, i32 noundef %62)
  store i32 %63, ptr %4, align 4
  br label %71

64:                                               ; preds = %51
  %65 = load ptr, ptr %5, align 8, !tbaa !5
  %66 = getelementptr inbounds %struct.Node, ptr %65, i32 0, i32 1
  %67 = load ptr, ptr %66, align 8, !tbaa !13
  %68 = load i32, ptr %6, align 4, !tbaa !9
  %69 = load i32, ptr %7, align 4, !tbaa !9
  %70 = call noundef i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef %67, i32 noundef %68, i32 noundef %69)
  store i32 %70, ptr %4, align 4
  br label %71

71:                                               ; preds = %64, %57, %36, %23, %10
  %72 = load i32, ptr %4, align 4
  ret i32 %72
}

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

!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, !10, i64 0}
!12 = !{!"_ZTS4Node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}


; 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 i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %.not3441 = icmp eq ptr %0, null
  br i1 %.not3441, label %tailrecurse.outer._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %tailrecurse.outer
  %.tr.ph43 = phi ptr [ %13, %tailrecurse.outer ], [ %0, %3 ]
  %accumulator.tr.ph42 = phi i32 [ %15, %tailrecurse.outer ], [ 0, %3 ]
  br label %4

4:                                                ; preds = %.lr.ph, %tailrecurse.backedge
  %.tr35 = phi ptr [ %.tr.ph43, %.lr.ph ], [ %.tr.be, %tailrecurse.backedge ]
  %5 = load i32, ptr %.tr35, align 8, !tbaa !5
  %6 = icmp eq i32 %5, %2
  %7 = icmp eq i32 %5, %1
  %or.cond = and i1 %6, %7
  br i1 %or.cond, label %tailrecurse.outer._crit_edge, label %8

8:                                                ; preds = %4
  %.not28 = icmp sgt i32 %5, %2
  %.not29 = icmp slt i32 %5, %1
  %or.cond30 = or i1 %.not28, %.not29
  br i1 %or.cond30, label %tailrecurse.backedge, label %tailrecurse.outer

tailrecurse.outer:                                ; preds = %8
  %9 = getelementptr inbounds i8, ptr %.tr35, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = tail call noundef i32 @_Z14getCountOfNodeP4Nodeii(ptr noundef %10, i32 noundef %1, i32 noundef %2)
  %12 = getelementptr inbounds i8, ptr %.tr35, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !12
  %14 = add i32 %accumulator.tr.ph42, 1
  %15 = add i32 %14, %11
  %.not34 = icmp eq ptr %13, null
  br i1 %.not34, label %tailrecurse.outer._crit_edge, label %.lr.ph

tailrecurse.backedge:                             ; preds = %8
  %spec.select = select i1 %.not29, i64 16, i64 8
  %16 = getelementptr inbounds i8, ptr %.tr35, i64 %spec.select
  %.tr.be = load ptr, ptr %16, align 8, !tbaa !13
  %.not = icmp eq ptr %.tr.be, null
  br i1 %.not, label %tailrecurse.outer._crit_edge, label %4

tailrecurse.outer._crit_edge:                     ; preds = %tailrecurse.outer, %tailrecurse.backedge, %4, %3
  %accumulator.tr.ph.lcssa = phi i32 [ 0, %3 ], [ %accumulator.tr.ph42, %4 ], [ %accumulator.tr.ph42, %tailrecurse.backedge ], [ %15, %tailrecurse.outer ]
  %.0 = phi i32 [ 0, %3 ], [ 0, %tailrecurse.backedge ], [ 1, %4 ], [ 0, %tailrecurse.outer ]
  %accumulator.ret.tr = add nsw i32 %.0, %accumulator.tr.ph.lcssa
  ret i32 %accumulator.ret.tr
}

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, !7, i64 0}
!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 8}
!12 = !{!6, !10, i64 16}
!13 = !{!10, !10, i64 0}


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