; LLVM IR from DimitrisJim/leetcode_solutions :: C/Algorithms (637)

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

@SIZE = external hidden global i64, align 8
@counts = external hidden global ptr, align 8
@values = external hidden global ptr, align 8

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

9:                                                ; preds = %2
  br label %71

10:                                               ; preds = %2
  %11 = load i32, ptr %4, align 4, !tbaa !9
  %12 = sext i32 %11 to i64
  %13 = load i64, ptr @SIZE, align 8, !tbaa !11
  %14 = icmp eq i64 %12, %13
  br i1 %14, label %15, label %34

15:                                               ; preds = %10
  %16 = load i64, ptr @SIZE, align 8, !tbaa !11
  %17 = add nsw i64 %16, 1
  store i64 %17, ptr @SIZE, align 8, !tbaa !11
  %18 = load ptr, ptr @counts, align 8, !tbaa !5
  %19 = load i64, ptr @SIZE, align 8, !tbaa !11
  %20 = mul i64 %19, 8
  %21 = call ptr @realloc(ptr noundef %18, i64 noundef %20) #3
  store ptr %21, ptr @counts, align 8, !tbaa !5
  %22 = load ptr, ptr @values, align 8, !tbaa !5
  %23 = load i64, ptr @SIZE, align 8, !tbaa !11
  %24 = mul i64 %23, 8
  %25 = call ptr @realloc(ptr noundef %22, i64 noundef %24) #3
  store ptr %25, ptr @values, align 8, !tbaa !5
  %26 = load ptr, ptr @counts, align 8, !tbaa !5
  %27 = load i64, ptr @SIZE, align 8, !tbaa !11
  %28 = sub nsw i64 %27, 1
  %29 = getelementptr inbounds i64, ptr %26, i64 %28
  store i64 0, ptr %29, align 8, !tbaa !11
  %30 = load ptr, ptr @values, align 8, !tbaa !5
  %31 = load i64, ptr @SIZE, align 8, !tbaa !11
  %32 = sub nsw i64 %31, 1
  %33 = getelementptr inbounds i64, ptr %30, i64 %32
  store i64 0, ptr %33, align 8, !tbaa !11
  br label %34

34:                                               ; preds = %15, %10
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #4
  %35 = load ptr, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds %struct.TreeNode, ptr %35, i32 0, i32 1
  %37 = load ptr, ptr %36, align 8, !tbaa !13
  store ptr %37, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #4
  %38 = load ptr, ptr %3, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.TreeNode, ptr %38, i32 0, i32 2
  %40 = load ptr, ptr %39, align 8, !tbaa !15
  store ptr %40, ptr %6, align 8, !tbaa !5
  %41 = load ptr, ptr %3, align 8, !tbaa !5
  %42 = getelementptr inbounds %struct.TreeNode, ptr %41, i32 0, i32 0
  %43 = load i32, ptr %42, align 8, !tbaa !16
  %44 = sext i32 %43 to i64
  %45 = load ptr, ptr @values, align 8, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i64, ptr %45, i64 %47
  %49 = load i64, ptr %48, align 8, !tbaa !11
  %50 = add nsw i64 %49, %44
  store i64 %50, ptr %48, align 8, !tbaa !11
  %51 = load ptr, ptr @counts, align 8, !tbaa !5
  %52 = load i32, ptr %4, align 4, !tbaa !9
  %53 = sext i32 %52 to i64
  %54 = getelementptr inbounds i64, ptr %51, i64 %53
  %55 = load i64, ptr %54, align 8, !tbaa !11
  %56 = add nsw i64 %55, 1
  store i64 %56, ptr %54, align 8, !tbaa !11
  %57 = load ptr, ptr %5, align 8, !tbaa !5
  %58 = icmp ne ptr %57, null
  br i1 %58, label %59, label %63

59:                                               ; preds = %34
  %60 = load ptr, ptr %5, align 8, !tbaa !5
  %61 = load i32, ptr %4, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  call void @breadth(ptr noundef %60, i32 noundef %62)
  br label %63

63:                                               ; preds = %59, %34
  %64 = load ptr, ptr %6, align 8, !tbaa !5
  %65 = icmp ne ptr %64, null
  br i1 %65, label %66, label %70

66:                                               ; preds = %63
  %67 = load ptr, ptr %6, align 8, !tbaa !5
  %68 = load i32, ptr %4, align 4, !tbaa !9
  %69 = add nsw i32 %68, 1
  call void @breadth(ptr noundef %67, i32 noundef %69)
  br label %70

70:                                               ; preds = %66, %63
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #4
  br label %71

71:                                               ; preds = %70, %9
  ret void
}

; Function Attrs: nounwind allocsize(1)
declare ptr @realloc(ptr noundef, i64 noundef) #1

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

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

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" }
attributes #1 = { nounwind allocsize(1) "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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #3 = { nounwind allocsize(1) }
attributes #4 = { 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/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !12, i64 0}
!12 = !{!"long", !7, i64 0}
!13 = !{!14, !6, i64 8}
!14 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 16}
!16 = !{!14, !10, 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"

@SIZE = external hidden local_unnamed_addr global i64, align 8
@counts = external hidden local_unnamed_addr global ptr, align 8
@values = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local void @breadth(ptr noundef readonly %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not18 = icmp eq ptr %0, null
  br i1 %.not18, label %._crit_edge, label %.lr.ph.preheader

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

.lr.ph:                                           ; preds = %tailrecurse, %.lr.ph.preheader
  %indvars.iv = phi i64 [ %3, %.lr.ph.preheader ], [ %indvars.iv.next, %tailrecurse ]
  %.tr19 = phi ptr [ %0, %.lr.ph.preheader ], [ %28, %tailrecurse ]
  %4 = load i64, ptr @SIZE, align 8, !tbaa !5
  %5 = icmp eq i64 %4, %indvars.iv
  br i1 %5, label %6, label %.lr.ph._crit_edge

.lr.ph._crit_edge:                                ; preds = %.lr.ph
  %.pre = load ptr, ptr @values, align 8, !tbaa !9
  %.pre22 = load ptr, ptr @counts, align 8, !tbaa !9
  br label %22

6:                                                ; preds = %.lr.ph
  %7 = add nsw i64 %indvars.iv, 1
  store i64 %7, ptr @SIZE, align 8, !tbaa !5
  %8 = load ptr, ptr @counts, align 8, !tbaa !9
  %9 = shl nsw i64 %7, 3
  %10 = tail call ptr @realloc(ptr noundef %8, i64 noundef %9) #2
  store ptr %10, ptr @counts, align 8, !tbaa !9
  %11 = load ptr, ptr @values, align 8, !tbaa !9
  %12 = load i64, ptr @SIZE, align 8, !tbaa !5
  %13 = shl i64 %12, 3
  %14 = tail call ptr @realloc(ptr noundef %11, i64 noundef %13) #2
  store ptr %14, ptr @values, align 8, !tbaa !9
  %15 = load ptr, ptr @counts, align 8, !tbaa !9
  %16 = load i64, ptr @SIZE, align 8, !tbaa !5
  %17 = getelementptr i64, ptr %15, i64 %16
  %18 = getelementptr i8, ptr %17, i64 -8
  store i64 0, ptr %18, align 8, !tbaa !5
  %19 = load i64, ptr @SIZE, align 8, !tbaa !5
  %20 = getelementptr i64, ptr %14, i64 %19
  %21 = getelementptr i8, ptr %20, i64 -8
  store i64 0, ptr %21, align 8, !tbaa !5
  br label %22

22:                                               ; preds = %.lr.ph._crit_edge, %6
  %23 = phi ptr [ %.pre22, %.lr.ph._crit_edge ], [ %15, %6 ]
  %24 = phi ptr [ %.pre, %.lr.ph._crit_edge ], [ %14, %6 ]
  %25 = getelementptr inbounds i8, ptr %.tr19, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !11
  %27 = getelementptr inbounds i8, ptr %.tr19, i64 16
  %28 = load ptr, ptr %27, align 8, !tbaa !14
  %29 = load i32, ptr %.tr19, align 8, !tbaa !15
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds i64, ptr %24, i64 %indvars.iv
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = add nsw i64 %32, %30
  store i64 %33, ptr %31, align 8, !tbaa !5
  %34 = getelementptr inbounds i64, ptr %23, i64 %indvars.iv
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add nsw i64 %35, 1
  store i64 %36, ptr %34, align 8, !tbaa !5
  %.not15 = icmp eq ptr %26, null
  br i1 %.not15, label %tailrecurse, label %37

37:                                               ; preds = %22
  %38 = trunc i64 %indvars.iv to i32
  %39 = add i32 %38, 1
  tail call void @breadth(ptr noundef nonnull %26, i32 noundef %39)
  br label %tailrecurse

tailrecurse:                                      ; preds = %37, %22
  %.not16 = icmp eq ptr %28, null
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br i1 %.not16, label %._crit_edge, label %.lr.ph

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

; Function Attrs: mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias noundef ptr @realloc(ptr allocptr nocapture noundef, i64 noundef) local_unnamed_addr #1

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" }
attributes #1 = { mustprogress nounwind willreturn allockind("realloc") allocsize(1) memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "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 allocsize(1) }

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


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