; LLVM IR from ImChong/Practice_LeetCode_C :: F01_Common_Functions/src (common_binary_tree)

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

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

; Function Attrs: nounwind allocsize(0)
declare noalias ptr @malloc(i64 noundef) #1

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

; Function Attrs: nounwind uwtable
define dso_local ptr @BTree_LevelOrder(ptr noundef %0, ptr noundef %1, ptr noundef %2) #2 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca [1000 x ptr], align 16
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store ptr %1, ptr %6, align 8, !tbaa !5
  store ptr %2, ptr %7, align 8, !tbaa !5
  %15 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %15, align 4, !tbaa !9
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %19

18:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  br label %107

19:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  %20 = call noalias ptr @malloc(i64 noundef 8000) #4
  store ptr %20, ptr %8, align 8, !tbaa !5
  %21 = call noalias ptr @malloc(i64 noundef 4000) #4
  %22 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %21, ptr %22, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8000, ptr %9) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  store i32 0, ptr %10, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %23 = load ptr, ptr %5, align 8, !tbaa !5
  %24 = load i32, ptr %11, align 4, !tbaa !9
  %25 = add nsw i32 %24, 1
  store i32 %25, ptr %11, align 4, !tbaa !9
  %26 = sext i32 %24 to i64
  %27 = getelementptr inbounds [1000 x ptr], ptr %9, i64 0, i64 %26
  store ptr %23, ptr %27, align 8, !tbaa !5
  br label %28

28:                                               ; preds = %94, %19
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = icmp ne i32 %29, %30
  br i1 %31, label %32, label %105

32:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #3
  store i32 0, ptr %13, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #3
  %33 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %33, ptr %14, align 4, !tbaa !9
  %34 = load i32, ptr %14, align 4, !tbaa !9
  %35 = load i32, ptr %10, align 4, !tbaa !9
  %36 = sub nsw i32 %34, %35
  %37 = sext i32 %36 to i64
  %38 = mul i64 4, %37
  %39 = call noalias ptr @malloc(i64 noundef %38) #4
  %40 = load ptr, ptr %8, align 8, !tbaa !5
  %41 = load ptr, ptr %6, align 8, !tbaa !5
  %42 = load i32, ptr %41, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds ptr, ptr %40, i64 %43
  store ptr %39, ptr %44, align 8, !tbaa !5
  br label %45

45:                                               ; preds = %93, %32
  %46 = load i32, ptr %10, align 4, !tbaa !9
  %47 = load i32, ptr %14, align 4, !tbaa !9
  %48 = icmp slt i32 %46, %47
  br i1 %48, label %49, label %94

49:                                               ; preds = %45
  %50 = load i32, ptr %10, align 4, !tbaa !9
  %51 = add nsw i32 %50, 1
  store i32 %51, ptr %10, align 4, !tbaa !9
  %52 = sext i32 %50 to i64
  %53 = getelementptr inbounds [1000 x ptr], ptr %9, i64 0, i64 %52
  %54 = load ptr, ptr %53, align 8, !tbaa !5
  store ptr %54, ptr %12, align 8, !tbaa !5
  %55 = load ptr, ptr %12, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.TreeNode, ptr %55, i32 0, i32 0
  %57 = load i32, ptr %56, align 8, !tbaa !11
  %58 = load ptr, ptr %8, align 8, !tbaa !5
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  %60 = load i32, ptr %59, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds ptr, ptr %58, i64 %61
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = load i32, ptr %13, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %13, align 4, !tbaa !9
  %66 = sext i32 %64 to i64
  %67 = getelementptr inbounds i32, ptr %63, i64 %66
  store i32 %57, ptr %67, align 4, !tbaa !9
  %68 = load ptr, ptr %12, align 8, !tbaa !5
  %69 = getelementptr inbounds %struct.TreeNode, ptr %68, i32 0, i32 1
  %70 = load ptr, ptr %69, align 8, !tbaa !13
  %71 = icmp ne ptr %70, null
  br i1 %71, label %72, label %80

72:                                               ; preds = %49
  %73 = load ptr, ptr %12, align 8, !tbaa !5
  %74 = getelementptr inbounds %struct.TreeNode, ptr %73, i32 0, i32 1
  %75 = load ptr, ptr %74, align 8, !tbaa !13
  %76 = load i32, ptr %11, align 4, !tbaa !9
  %77 = add nsw i32 %76, 1
  store i32 %77, ptr %11, align 4, !tbaa !9
  %78 = sext i32 %76 to i64
  %79 = getelementptr inbounds [1000 x ptr], ptr %9, i64 0, i64 %78
  store ptr %75, ptr %79, align 8, !tbaa !5
  br label %80

80:                                               ; preds = %72, %49
  %81 = load ptr, ptr %12, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.TreeNode, ptr %81, i32 0, i32 2
  %83 = load ptr, ptr %82, align 8, !tbaa !14
  %84 = icmp ne ptr %83, null
  br i1 %84, label %85, label %93

85:                                               ; preds = %80
  %86 = load ptr, ptr %12, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.TreeNode, ptr %86, i32 0, i32 2
  %88 = load ptr, ptr %87, align 8, !tbaa !14
  %89 = load i32, ptr %11, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %11, align 4, !tbaa !9
  %91 = sext i32 %89 to i64
  %92 = getelementptr inbounds [1000 x ptr], ptr %9, i64 0, i64 %91
  store ptr %88, ptr %92, align 8, !tbaa !5
  br label %93

93:                                               ; preds = %85, %80
  br label %45, !llvm.loop !15

94:                                               ; preds = %45
  %95 = load i32, ptr %13, align 4, !tbaa !9
  %96 = load ptr, ptr %7, align 8, !tbaa !5
  %97 = load ptr, ptr %96, align 8, !tbaa !5
  %98 = load ptr, ptr %6, align 8, !tbaa !5
  %99 = load i32, ptr %98, align 4, !tbaa !9
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds i32, ptr %97, i64 %100
  store i32 %95, ptr %101, align 4, !tbaa !9
  %102 = load ptr, ptr %6, align 8, !tbaa !5
  %103 = load i32, ptr %102, align 4, !tbaa !9
  %104 = add nsw i32 %103, 1
  store i32 %104, ptr %102, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #3
  br label %28, !llvm.loop !17

105:                                              ; preds = %28
  %106 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %106, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8000, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  br label %107

107:                                              ; preds = %105, %18
  %108 = load ptr, ptr %4, align 8
  ret ptr %108
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { nounwind allocsize(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 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 }
attributes #4 = { nounwind allocsize(0) }

!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 = !{!12, !6, i64 8}
!14 = !{!12, !6, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #0

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

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

; Function Attrs: nofree nounwind uwtable
define dso_local noalias noundef ptr @BTree_LevelOrder(ptr noundef %0, ptr nocapture noundef %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #2 {
  %4 = alloca [1000 x ptr], align 16
  store i32 0, ptr %1, align 4, !tbaa !5
  %5 = icmp eq ptr %0, null
  br i1 %5, label %46, label %6

6:                                                ; preds = %3
  %7 = tail call noalias dereferenceable_or_null(8000) ptr @malloc(i64 noundef 8000) #3
  %8 = tail call noalias dereferenceable_or_null(4000) ptr @malloc(i64 noundef 4000) #3
  store ptr %8, ptr %2, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8000, ptr nonnull %4) #4
  store ptr %0, ptr %4, align 16, !tbaa !9
  br label %9

9:                                                ; preds = %6, %._crit_edge
  %10 = phi i32 [ 0, %6 ], [ %44, %._crit_edge ]
  %.03145 = phi i32 [ 0, %6 ], [ %.1.lcssa, %._crit_edge ]
  %.03244 = phi i32 [ 1, %6 ], [ %.133.lcssa, %._crit_edge ]
  %11 = sub i32 %.03244, %.03145
  %12 = sext i32 %11 to i64
  %13 = shl nsw i64 %12, 2
  %14 = tail call noalias ptr @malloc(i64 noundef %13) #3
  %15 = sext i32 %10 to i64
  %16 = getelementptr inbounds ptr, ptr %7, i64 %15
  store ptr %14, ptr %16, align 8, !tbaa !9
  %17 = icmp slt i32 %.03145, %.03244
  br i1 %17, label %.lr.ph.preheader, label %._crit_edge

.lr.ph.preheader:                                 ; preds = %9
  %18 = sext i32 %.03145 to i64
  %wide.trip.count = zext i32 %11 to i64
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %40
  %indvars.iv47 = phi i64 [ %18, %.lr.ph.preheader ], [ %indvars.iv.next48, %40 ]
  %indvars.iv = phi i64 [ 0, %.lr.ph.preheader ], [ %indvars.iv.next, %40 ]
  %.13339 = phi i32 [ %.03244, %.lr.ph.preheader ], [ %.3, %40 ]
  %indvars.iv.next48 = add nsw i64 %indvars.iv47, 1
  %19 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %indvars.iv47
  %20 = load ptr, ptr %19, align 8, !tbaa !9
  %21 = load i32, ptr %20, align 8, !tbaa !11
  %22 = load i32, ptr %1, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds ptr, ptr %7, i64 %23
  %25 = load ptr, ptr %24, align 8, !tbaa !9
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %26 = getelementptr inbounds i32, ptr %25, i64 %indvars.iv
  store i32 %21, ptr %26, align 4, !tbaa !5
  %27 = getelementptr inbounds i8, ptr %20, i64 8
  %28 = load ptr, ptr %27, align 8, !tbaa !13
  %.not37 = icmp eq ptr %28, null
  br i1 %.not37, label %33, label %29

29:                                               ; preds = %.lr.ph
  %30 = add nsw i32 %.13339, 1
  %31 = sext i32 %.13339 to i64
  %32 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %31
  store ptr %28, ptr %32, align 8, !tbaa !9
  br label %33

33:                                               ; preds = %29, %.lr.ph
  %.2 = phi i32 [ %30, %29 ], [ %.13339, %.lr.ph ]
  %34 = getelementptr inbounds i8, ptr %20, i64 16
  %35 = load ptr, ptr %34, align 8, !tbaa !14
  %.not38 = icmp eq ptr %35, null
  br i1 %.not38, label %40, label %36

36:                                               ; preds = %33
  %37 = add nsw i32 %.2, 1
  %38 = sext i32 %.2 to i64
  %39 = getelementptr inbounds [1000 x ptr], ptr %4, i64 0, i64 %38
  store ptr %35, ptr %39, align 8, !tbaa !9
  br label %40

40:                                               ; preds = %36, %33
  %.3 = phi i32 [ %37, %36 ], [ %.2, %33 ]
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge.loopexit, label %.lr.ph, !llvm.loop !15

._crit_edge.loopexit:                             ; preds = %40
  %41 = trunc nsw i64 %indvars.iv.next48 to i32
  %.pre = load i32, ptr %1, align 4, !tbaa !5
  %.pre52 = sext i32 %.pre to i64
  br label %._crit_edge

._crit_edge:                                      ; preds = %._crit_edge.loopexit, %9
  %.pre-phi = phi i64 [ %.pre52, %._crit_edge.loopexit ], [ %15, %9 ]
  %42 = phi i32 [ %.pre, %._crit_edge.loopexit ], [ %10, %9 ]
  %.133.lcssa = phi i32 [ %.3, %._crit_edge.loopexit ], [ %.03244, %9 ]
  %.1.lcssa = phi i32 [ %41, %._crit_edge.loopexit ], [ %.03145, %9 ]
  %.030.lcssa = phi i32 [ %11, %._crit_edge.loopexit ], [ 0, %9 ]
  %43 = getelementptr inbounds i32, ptr %8, i64 %.pre-phi
  store i32 %.030.lcssa, ptr %43, align 4, !tbaa !5
  %44 = add nsw i32 %42, 1
  store i32 %44, ptr %1, align 4, !tbaa !5
  %.not = icmp eq i32 %.1.lcssa, %.133.lcssa
  br i1 %.not, label %45, label %9, !llvm.loop !17

45:                                               ; preds = %._crit_edge
  call void @llvm.lifetime.end.p0(i64 8000, ptr nonnull %4) #4
  br label %46

46:                                               ; preds = %3, %45
  %.0 = phi ptr [ %7, %45 ], [ null, %3 ]
  ret ptr %.0
}

attributes #0 = { mustprogress nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(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 = { nofree 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 allocsize(0) }
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 = !{!"int", !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, !6, i64 0}
!12 = !{!"TreeNode", !6, i64 0, !10, i64 8, !10, i64 16}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !10, i64 16}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}


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