; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Binary Tree/Path Having a given sum of Nodes.ll (Z10hasPathSumP4nodei)

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

11:                                               ; preds = %2
  %12 = load i32, ptr %5, align 4, !tbaa !9
  %13 = icmp eq i32 %12, 0
  %14 = zext i1 %13 to i32
  store i32 %14, ptr %3, align 4
  br label %73

15:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  store i32 0, ptr %6, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %16 = load i32, ptr %5, align 4, !tbaa !9
  %17 = load ptr, ptr %4, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.node, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = sub nsw i32 %16, %19
  store i32 %20, ptr %7, align 4, !tbaa !9
  %21 = load i32, ptr %7, align 4, !tbaa !9
  %22 = icmp eq i32 %21, 0
  br i1 %22, label %23, label %34

23:                                               ; preds = %15
  %24 = load ptr, ptr %4, align 8, !tbaa !5
  %25 = getelementptr inbounds %struct.node, ptr %24, i32 0, i32 1
  %26 = load ptr, ptr %25, align 8, !tbaa !13
  %27 = icmp eq ptr %26, null
  br i1 %27, label %28, label %34

28:                                               ; preds = %23
  %29 = load ptr, ptr %4, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.node, ptr %29, i32 0, i32 2
  %31 = load ptr, ptr %30, align 8, !tbaa !14
  %32 = icmp eq ptr %31, null
  br i1 %32, label %33, label %34

33:                                               ; preds = %28
  store i32 1, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %72

34:                                               ; preds = %28, %23, %15
  %35 = load ptr, ptr %4, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.node, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  %38 = icmp ne ptr %37, null
  br i1 %38, label %39, label %52

39:                                               ; preds = %34
  %40 = load i32, ptr %6, align 4, !tbaa !9
  %41 = icmp ne i32 %40, 0
  br i1 %41, label %49, label %42

42:                                               ; preds = %39
  %43 = load ptr, ptr %4, align 8, !tbaa !5
  %44 = getelementptr inbounds %struct.node, ptr %43, i32 0, i32 1
  %45 = load ptr, ptr %44, align 8, !tbaa !13
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = call noundef i32 @_Z10hasPathSumP4nodei(ptr noundef %45, i32 noundef %46)
  %48 = icmp ne i32 %47, 0
  br label %49

49:                                               ; preds = %42, %39
  %50 = phi i1 [ true, %39 ], [ %48, %42 ]
  %51 = zext i1 %50 to i32
  store i32 %51, ptr %6, align 4, !tbaa !9
  br label %52

52:                                               ; preds = %49, %34
  %53 = load ptr, ptr %4, align 8, !tbaa !5
  %54 = getelementptr inbounds %struct.node, ptr %53, i32 0, i32 2
  %55 = load ptr, ptr %54, align 8, !tbaa !14
  %56 = icmp ne ptr %55, null
  br i1 %56, label %57, label %70

57:                                               ; preds = %52
  %58 = load i32, ptr %6, align 4, !tbaa !9
  %59 = icmp ne i32 %58, 0
  br i1 %59, label %67, label %60

60:                                               ; preds = %57
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.node, ptr %61, i32 0, i32 2
  %63 = load ptr, ptr %62, align 8, !tbaa !14
  %64 = load i32, ptr %7, align 4, !tbaa !9
  %65 = call noundef i32 @_Z10hasPathSumP4nodei(ptr noundef %63, i32 noundef %64)
  %66 = icmp ne i32 %65, 0
  br label %67

67:                                               ; preds = %60, %57
  %68 = phi i1 [ true, %57 ], [ %66, %60 ]
  %69 = zext i1 %68 to i32
  store i32 %69, ptr %6, align 4, !tbaa !9
  br label %70

70:                                               ; preds = %67, %52
  %71 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %71, ptr %3, align 4
  store i32 1, ptr %8, align 4
  br label %72

72:                                               ; preds = %70, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %73

73:                                               ; preds = %72, %11
  %74 = load i32, ptr %3, align 4
  ret i32 %74
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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 range(i32 0, 2) i32 @_Z10hasPathSumP4nodei(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %2
  %4 = icmp eq i32 %1, 0
  %5 = zext i1 %4 to i32
  br label %.loopexit

.lr.ph:                                           ; preds = %2, %.lr.ph.backedge
  %.tr2932 = phi i32 [ %7, %.lr.ph.backedge ], [ %1, %2 ]
  %.tr31 = phi ptr [ %.tr31.be, %.lr.ph.backedge ], [ %0, %2 ]
  %6 = load i32, ptr %.tr31, align 8, !tbaa !5
  %7 = sub nsw i32 %.tr2932, %6
  %8 = icmp eq i32 %6, %.tr2932
  %9 = getelementptr inbounds i8, ptr %.tr31, i64 8
  %10 = load ptr, ptr %9, align 8, !tbaa !11
  %11 = icmp eq ptr %10, null
  br i1 %8, label %12, label %17

12:                                               ; preds = %.lr.ph
  br i1 %11, label %13, label %.thread35

13:                                               ; preds = %12
  %14 = getelementptr inbounds i8, ptr %.tr31, i64 16
  %15 = load ptr, ptr %14, align 8, !tbaa !12
  %16 = icmp eq ptr %15, null
  br i1 %16, label %.loopexit, label %.thread

17:                                               ; preds = %.lr.ph
  br i1 %11, label %.thread, label %.thread35

.thread35:                                        ; preds = %12, %17
  %18 = tail call noundef i32 @_Z10hasPathSumP4nodei(ptr noundef nonnull %10, i32 noundef %7)
  %19 = getelementptr inbounds i8, ptr %.tr31, i64 16
  %20 = load ptr, ptr %19, align 8, !tbaa !12
  %.not21 = icmp eq ptr %20, null
  br i1 %.not21, label %.loopexit, label %23

.thread:                                          ; preds = %13, %17
  %21 = getelementptr inbounds i8, ptr %.tr31, i64 16
  %22 = load ptr, ptr %21, align 8, !tbaa !12
  %.not2124 = icmp eq ptr %22, null
  br i1 %.not2124, label %.loopexit, label %.lr.ph.backedge

.lr.ph.backedge:                                  ; preds = %.thread, %23
  %.tr31.be = phi ptr [ %22, %.thread ], [ %20, %23 ]
  br label %.lr.ph

23:                                               ; preds = %.thread35
  %.not22 = icmp eq i32 %18, 0
  br i1 %.not22, label %.lr.ph.backedge, label %.loopexit

.loopexit:                                        ; preds = %.thread, %13, %23, %.thread35, %tailrecurse._crit_edge
  %.1 = phi i32 [ %5, %tailrecurse._crit_edge ], [ 0, %.thread ], [ 1, %23 ], [ %18, %.thread35 ], [ 1, %13 ]
  ret i32 %.1
}

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}


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