; LLVM IR from Hunterdii/GeeksforGeeks-POTD :: July 2024 GFG SOLUTION/July-22.ll (Z16largestBSTHelperP4Node)

; 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.Info = type { i8, i32, i32, i32 }
%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 { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %0) #1 {
  %2 = alloca %struct.Info, align 4
  %3 = alloca ptr, align 8
  %4 = alloca %struct.Info, align 4
  %5 = alloca %struct.Info, 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 %13

8:                                                ; preds = %1
  %9 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 0
  store i8 1, ptr %9, align 4, !tbaa !9
  %10 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 1
  store i32 0, ptr %10, align 4, !tbaa !13
  %11 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 2
  store i32 2147483647, ptr %11, align 4, !tbaa !14
  %12 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 3
  store i32 -2147483648, ptr %12, align 4, !tbaa !15
  br label %82

13:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 16, ptr %4) #3
  %14 = load ptr, ptr %3, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.Node, ptr %14, i32 0, i32 1
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = call { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %16)
  %18 = getelementptr inbounds { i64, i64 }, ptr %4, i32 0, i32 0
  %19 = extractvalue { i64, i64 } %17, 0
  store i64 %19, ptr %18, align 4
  %20 = getelementptr inbounds { i64, i64 }, ptr %4, i32 0, i32 1
  %21 = extractvalue { i64, i64 } %17, 1
  store i64 %21, ptr %20, align 4
  call void @llvm.lifetime.start.p0(i64 16, ptr %5) #3
  %22 = load ptr, ptr %3, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.Node, ptr %22, i32 0, i32 2
  %24 = load ptr, ptr %23, align 8, !tbaa !18
  %25 = call { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %24)
  %26 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 0
  %27 = extractvalue { i64, i64 } %25, 0
  store i64 %27, ptr %26, align 4
  %28 = getelementptr inbounds { i64, i64 }, ptr %5, i32 0, i32 1
  %29 = extractvalue { i64, i64 } %25, 1
  store i64 %29, ptr %28, align 4
  %30 = getelementptr inbounds %struct.Info, ptr %4, i32 0, i32 0
  %31 = load i8, ptr %30, align 4, !tbaa !9, !range !19, !noundef !20
  %32 = trunc i8 %31 to i1
  br i1 %32, label %33, label %72

33:                                               ; preds = %13
  %34 = getelementptr inbounds %struct.Info, ptr %5, i32 0, i32 0
  %35 = load i8, ptr %34, align 4, !tbaa !9, !range !19, !noundef !20
  %36 = trunc i8 %35 to i1
  br i1 %36, label %37, label %72

37:                                               ; preds = %33
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.Node, ptr %38, i32 0, i32 0
  %40 = load i32, ptr %39, align 8, !tbaa !21
  %41 = getelementptr inbounds %struct.Info, ptr %4, i32 0, i32 3
  %42 = load i32, ptr %41, align 4, !tbaa !15
  %43 = icmp sgt i32 %40, %42
  br i1 %43, label %44, label %72

44:                                               ; preds = %37
  %45 = load ptr, ptr %3, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.Node, ptr %45, i32 0, i32 0
  %47 = load i32, ptr %46, align 8, !tbaa !21
  %48 = getelementptr inbounds %struct.Info, ptr %5, i32 0, i32 2
  %49 = load i32, ptr %48, align 4, !tbaa !14
  %50 = icmp slt i32 %47, %49
  br i1 %50, label %51, label %72

51:                                               ; preds = %44
  %52 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 0
  store i8 1, ptr %52, align 4, !tbaa !9
  %53 = getelementptr inbounds %struct.Info, ptr %4, i32 0, i32 1
  %54 = load i32, ptr %53, align 4, !tbaa !13
  %55 = add nsw i32 1, %54
  %56 = getelementptr inbounds %struct.Info, ptr %5, i32 0, i32 1
  %57 = load i32, ptr %56, align 4, !tbaa !13
  %58 = add nsw i32 %55, %57
  %59 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 1
  store i32 %58, ptr %59, align 4, !tbaa !13
  %60 = load ptr, ptr %3, align 8, !tbaa !5
  %61 = getelementptr inbounds %struct.Node, ptr %60, i32 0, i32 0
  %62 = getelementptr inbounds %struct.Info, ptr %4, i32 0, i32 2
  %63 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %61, ptr noundef nonnull align 4 dereferenceable(4) %62)
  %64 = load i32, ptr %63, align 4, !tbaa !22
  %65 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 2
  store i32 %64, ptr %65, align 4, !tbaa !14
  %66 = load ptr, ptr %3, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.Node, ptr %66, i32 0, i32 0
  %68 = getelementptr inbounds %struct.Info, ptr %5, i32 0, i32 3
  %69 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %67, ptr noundef nonnull align 4 dereferenceable(4) %68)
  %70 = load i32, ptr %69, align 4, !tbaa !22
  %71 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 3
  store i32 %70, ptr %71, align 4, !tbaa !15
  br label %81

72:                                               ; preds = %44, %37, %33, %13
  %73 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 0
  store i8 0, ptr %73, align 4, !tbaa !9
  %74 = getelementptr inbounds %struct.Info, ptr %4, i32 0, i32 1
  %75 = getelementptr inbounds %struct.Info, ptr %5, i32 0, i32 1
  %76 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %74, ptr noundef nonnull align 4 dereferenceable(4) %75)
  %77 = load i32, ptr %76, align 4, !tbaa !22
  %78 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 1
  store i32 %77, ptr %78, align 4, !tbaa !13
  %79 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 2
  store i32 2147483647, ptr %79, align 4, !tbaa !14
  %80 = getelementptr inbounds %struct.Info, ptr %2, i32 0, i32 3
  store i32 -2147483648, ptr %80, align 4, !tbaa !15
  br label %81

81:                                               ; preds = %72, %51
  call void @llvm.lifetime.end.p0(i64 16, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr %4) #3
  br label %82

82:                                               ; preds = %81, %8
  %83 = load { i64, i64 }, ptr %2, align 4
  ret { i64, i64 } %83
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) #2

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 = { 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, !11, i64 0}
!10 = !{!"_ZTS4Info", !11, i64 0, !12, i64 4, !12, i64 8, !12, i64 12}
!11 = !{!"bool", !7, i64 0}
!12 = !{!"int", !7, i64 0}
!13 = !{!10, !12, i64 4}
!14 = !{!10, !12, i64 8}
!15 = !{!10, !12, i64 12}
!16 = !{!17, !6, i64 8}
!17 = !{!"_ZTS4Node", !12, i64 0, !6, i64 8, !6, i64 16}
!18 = !{!17, !6, i64 16}
!19 = !{i8 0, i8 2}
!20 = !{}
!21 = !{!17, !12, i64 0}
!22 = !{!12, !12, 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"

%struct.Info = type { i8, i32, i32, i32 }

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

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

; Function Attrs: mustprogress nounwind uwtable
define dso_local { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %0) local_unnamed_addr #1 {
  %2 = alloca %struct.Info, align 8
  %3 = alloca %struct.Info, align 8
  %4 = icmp eq ptr %0, null
  br i1 %4, label %54, label %5

5:                                                ; preds = %1
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %2) #3
  %6 = getelementptr inbounds i8, ptr %0, i64 8
  %7 = load ptr, ptr %6, align 8, !tbaa !5
  %8 = tail call { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %7)
  %9 = extractvalue { i64, i64 } %8, 0
  store i64 %9, ptr %2, align 8
  %10 = getelementptr inbounds i8, ptr %2, i64 8
  %11 = extractvalue { i64, i64 } %8, 1
  store i64 %11, ptr %10, align 8
  call void @llvm.lifetime.start.p0(i64 16, ptr nonnull %3) #3
  %12 = getelementptr inbounds i8, ptr %0, i64 16
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = tail call { i64, i64 } @_Z16largestBSTHelperP4Node(ptr noundef %13)
  %15 = extractvalue { i64, i64 } %14, 0
  store i64 %15, ptr %3, align 8
  %16 = getelementptr inbounds i8, ptr %3, i64 8
  %17 = extractvalue { i64, i64 } %14, 1
  store i64 %17, ptr %16, align 8
  %18 = trunc i64 %9 to i1
  %19 = lshr i64 %11, 32
  %20 = trunc nuw i64 %19 to i32
  %21 = trunc i64 %17 to i32
  %22 = lshr i64 %9, 32
  %23 = trunc nuw i64 %22 to i32
  %24 = lshr i64 %15, 32
  %25 = trunc nuw i64 %24 to i32
  br i1 %18, label %26, label %44

26:                                               ; preds = %5
  %27 = trunc i64 %15 to i1
  br i1 %27, label %28, label %44

28:                                               ; preds = %26
  %29 = load i32, ptr %0, align 8, !tbaa !12
  %30 = icmp sgt i32 %29, %20
  %31 = icmp slt i32 %29, %21
  %or.cond = select i1 %30, i1 %31, i1 false
  br i1 %or.cond, label %32, label %44

32:                                               ; preds = %28
  %33 = add nsw i32 %23, 1
  %34 = add nsw i32 %33, %25
  %35 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %0, ptr noundef nonnull align 4 dereferenceable(4) %10)
  %36 = load i32, ptr %35, align 4, !tbaa !13
  %37 = getelementptr inbounds i8, ptr %3, i64 12
  %38 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %0, ptr noundef nonnull align 4 dereferenceable(4) %37)
  %39 = load i32, ptr %38, align 4, !tbaa !13
  %40 = zext i32 %39 to i64
  %41 = shl nuw i64 %40, 32
  %42 = zext i32 %36 to i64
  %43 = or disjoint i64 %41, %42
  br label %49

44:                                               ; preds = %28, %26, %5
  %45 = getelementptr inbounds i8, ptr %2, i64 4
  %46 = getelementptr inbounds i8, ptr %3, i64 4
  %47 = call noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4) %45, ptr noundef nonnull align 4 dereferenceable(4) %46)
  %48 = load i32, ptr %47, align 4, !tbaa !13
  br label %49

49:                                               ; preds = %44, %32
  %.sroa.47.0 = phi i32 [ %34, %32 ], [ %48, %44 ]
  %.sroa.0.0 = phi i64 [ 1, %32 ], [ 0, %44 ]
  %50 = phi i64 [ %43, %32 ], [ -9223372034707292161, %44 ]
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %3) #3
  call void @llvm.lifetime.end.p0(i64 16, ptr nonnull %2) #3
  %51 = zext i32 %.sroa.47.0 to i64
  %52 = shl nuw i64 %51, 32
  %53 = or disjoint i64 %52, %.sroa.0.0
  br label %54

54:                                               ; preds = %1, %49
  %.sroa.0.0.insert.insert = phi i64 [ %53, %49 ], [ 1, %1 ]
  %.sroa.7.8.insert.insert = phi i64 [ %50, %49 ], [ -9223372034707292161, %1 ]
  %.fca.0.insert = insertvalue { i64, i64 } poison, i64 %.sroa.0.0.insert.insert, 0
  %.fca.1.insert = insertvalue { i64, i64 } %.fca.0.insert, i64 %.sroa.7.8.insert.insert, 1
  ret { i64, i64 } %.fca.1.insert
}

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3minIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 4 dereferenceable(4) ptr @_ZSt3maxIiERKT_S2_S2_(ptr noundef nonnull align 4 dereferenceable(4), ptr noundef nonnull align 4 dereferenceable(4)) local_unnamed_addr #2

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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, !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}
!13 = !{!7, !7, i64 0}


; Effective O3 passes: BDCEPass,EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferAlignmentPass,InferFunctionAttrsPass,InstCombinePass,PostOrderFunctionAttrsPass,ReassociatePass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass