; LLVM IR from ImChong/Practice_LeetCode_C :: F02_Programming_Topics/F01_Data_Structure/06_Binary_Tree/07_averageOfLevels (main)

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

@sumsSize = external dso_local global i32, align 4
@countsSize = external dso_local global i32, align 4

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

11:                                               ; preds = %4
  br label %63

12:                                               ; preds = %4
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr @sumsSize, align 4, !tbaa !9
  %15 = icmp slt i32 %13, %14
  br i1 %15, label %16, label %33

16:                                               ; preds = %12
  %17 = load ptr, ptr %5, align 8, !tbaa !5
  %18 = getelementptr inbounds %struct.TreeNode, ptr %17, i32 0, i32 0
  %19 = load i32, ptr %18, align 8, !tbaa !11
  %20 = sitofp i32 %19 to double
  %21 = load ptr, ptr %8, align 8, !tbaa !5
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds double, ptr %21, i64 %23
  %25 = load double, ptr %24, align 8, !tbaa !13
  %26 = fadd double %25, %20
  store double %26, ptr %24, align 8, !tbaa !13
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = load i32, ptr %6, align 4, !tbaa !9
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds i32, ptr %27, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !9
  %32 = add nsw i32 %31, 1
  store i32 %32, ptr %30, align 4, !tbaa !9
  br label %48

33:                                               ; preds = %12
  %34 = load ptr, ptr %5, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.TreeNode, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %35, align 8, !tbaa !11
  %37 = sitofp i32 %36 to double
  %38 = load ptr, ptr %8, align 8, !tbaa !5
  %39 = load i32, ptr @sumsSize, align 4, !tbaa !9
  %40 = add nsw i32 %39, 1
  store i32 %40, ptr @sumsSize, align 4, !tbaa !9
  %41 = sext i32 %39 to i64
  %42 = getelementptr inbounds double, ptr %38, i64 %41
  store double %37, ptr %42, align 8, !tbaa !13
  %43 = load ptr, ptr %7, align 8, !tbaa !5
  %44 = load i32, ptr @countsSize, align 4, !tbaa !9
  %45 = add nsw i32 %44, 1
  store i32 %45, ptr @countsSize, align 4, !tbaa !9
  %46 = sext i32 %44 to i64
  %47 = getelementptr inbounds i32, ptr %43, i64 %46
  store i32 1, ptr %47, align 4, !tbaa !9
  br label %48

48:                                               ; preds = %33, %16
  %49 = load ptr, ptr %5, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.TreeNode, ptr %49, i32 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = add nsw i32 %52, 1
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load ptr, ptr %8, align 8, !tbaa !5
  call void @depthFirstSearch(ptr noundef %51, i32 noundef %53, ptr noundef %54, ptr noundef %55)
  %56 = load ptr, ptr %5, align 8, !tbaa !5
  %57 = getelementptr inbounds %struct.TreeNode, ptr %56, i32 0, i32 2
  %58 = load ptr, ptr %57, align 8, !tbaa !16
  %59 = load i32, ptr %6, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  %62 = load ptr, ptr %8, align 8, !tbaa !5
  call void @depthFirstSearch(ptr noundef %58, i32 noundef %60, ptr noundef %61, ptr noundef %62)
  br label %63

63:                                               ; preds = %48, %11
  ret void
}

attributes #0 = { 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, !6, i64 0}
!6 = !{!"any pointer", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!14, !14, i64 0}
!14 = !{!"double", !7, i64 0}
!15 = !{!12, !6, i64 8}
!16 = !{!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"

@sumsSize = external dso_local local_unnamed_addr global i32, align 4
@countsSize = external dso_local local_unnamed_addr global i32, align 4

; Function Attrs: nofree nosync nounwind memory(readwrite, inaccessiblemem: none) uwtable
define dso_local void @depthFirstSearch(ptr noundef readonly %0, i32 noundef %1, ptr nocapture noundef %2, ptr nocapture noundef %3) local_unnamed_addr #0 {
  %5 = icmp eq ptr %0, null
  br i1 %5, label %tailrecurse._crit_edge, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %4
  %6 = sext i32 %1 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %tailrecurse
  %indvars.iv = phi i64 [ %6, %.lr.ph.preheader ], [ %indvars.iv.next, %tailrecurse ]
  %.tr22 = phi ptr [ %0, %.lr.ph.preheader ], [ %30, %tailrecurse ]
  %7 = load i32, ptr @sumsSize, align 4, !tbaa !5
  %8 = sext i32 %7 to i64
  %9 = icmp slt i64 %indvars.iv, %8
  %10 = load i32, ptr %.tr22, align 8, !tbaa !9
  %11 = sitofp i32 %10 to double
  br i1 %9, label %12, label %19

12:                                               ; preds = %.lr.ph
  %13 = getelementptr inbounds double, ptr %3, i64 %indvars.iv
  %14 = load double, ptr %13, align 8, !tbaa !12
  %15 = fadd double %14, %11
  store double %15, ptr %13, align 8, !tbaa !12
  %16 = getelementptr inbounds i32, ptr %2, i64 %indvars.iv
  %17 = load i32, ptr %16, align 4, !tbaa !5
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr %16, align 4, !tbaa !5
  br label %tailrecurse

19:                                               ; preds = %.lr.ph
  %20 = add nsw i32 %7, 1
  store i32 %20, ptr @sumsSize, align 4, !tbaa !5
  %21 = getelementptr inbounds double, ptr %3, i64 %8
  store double %11, ptr %21, align 8, !tbaa !12
  %22 = load i32, ptr @countsSize, align 4, !tbaa !5
  %23 = add nsw i32 %22, 1
  store i32 %23, ptr @countsSize, align 4, !tbaa !5
  %24 = sext i32 %22 to i64
  %25 = getelementptr inbounds i32, ptr %2, i64 %24
  store i32 1, ptr %25, align 4, !tbaa !5
  br label %tailrecurse

tailrecurse:                                      ; preds = %19, %12
  %26 = getelementptr inbounds i8, ptr %.tr22, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %28 = trunc nsw i64 %indvars.iv.next to i32
  tail call void @depthFirstSearch(ptr noundef %27, i32 noundef %28, ptr noundef nonnull %2, ptr noundef nonnull %3)
  %29 = getelementptr inbounds i8, ptr %.tr22, i64 16
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = icmp eq ptr %30, null
  br i1 %31, label %tailrecurse._crit_edge, label %.lr.ph

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %4
  ret void
}

attributes #0 = { nofree nosync nounwind memory(readwrite, 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, !6, i64 0}
!6 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"TreeNode", !6, i64 0, !11, i64 8, !11, i64 16}
!11 = !{!"any pointer", !7, i64 0}
!12 = !{!13, !13, i64 0}
!13 = !{!"double", !7, i64 0}
!14 = !{!10, !11, i64 8}
!15 = !{!10, !11, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,EarlyCSEPass,GVNPass,GlobalOptPass,IndVarSimplifyPass,InstCombinePass,JumpThreadingPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass