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

; 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
declare dso_local ptr @BTree_InitArrQueue(i32 noundef) #2

; Function Attrs: nounwind uwtable
declare dso_local void @BTree_ArrQueueFree(ptr noundef) #2

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

12:                                               ; preds = %2
  store ptr null, ptr %3, align 8
  br label %133

13:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  %14 = call noalias ptr @malloc(i64 noundef 24) #4
  store ptr %14, ptr %6, align 8, !tbaa !5
  %15 = load ptr, ptr %4, align 8, !tbaa !5
  %16 = getelementptr inbounds i32, ptr %15, i64 0
  %17 = load i32, ptr %16, align 4, !tbaa !9
  %18 = load ptr, ptr %6, align 8, !tbaa !5
  %19 = getelementptr inbounds %struct.TreeNode, ptr %18, i32 0, i32 0
  store i32 %17, ptr %19, align 8, !tbaa !11
  %20 = load ptr, ptr %6, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.TreeNode, ptr %20, i32 0, i32 1
  store ptr null, ptr %21, align 8, !tbaa !13
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.TreeNode, ptr %22, i32 0, i32 2
  store ptr null, ptr %23, align 8, !tbaa !14
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %24 = load i32, ptr %5, align 4, !tbaa !9
  %25 = call ptr @BTree_InitArrQueue(i32 noundef %24)
  store ptr %25, ptr %7, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load ptr, ptr %7, align 8, !tbaa !5
  %28 = getelementptr inbounds %struct.TreeArrQueue, ptr %27, i32 0, i32 0
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = load ptr, ptr %7, align 8, !tbaa !5
  %31 = getelementptr inbounds %struct.TreeArrQueue, ptr %30, i32 0, i32 2
  %32 = load i32, ptr %31, align 4, !tbaa !17
  %33 = add nsw i32 %32, 1
  store i32 %33, ptr %31, align 4, !tbaa !17
  %34 = sext i32 %32 to i64
  %35 = getelementptr inbounds ptr, ptr %29, i64 %34
  store ptr %26, ptr %35, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %36

36:                                               ; preds = %127, %13
  %37 = load i32, ptr %8, align 4, !tbaa !9
  %38 = load i32, ptr %5, align 4, !tbaa !9
  %39 = icmp slt i32 %37, %38
  br i1 %39, label %41, label %40

40:                                               ; preds = %36
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %130

41:                                               ; preds = %36
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %42 = load ptr, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds %struct.TreeArrQueue, ptr %42, i32 0, i32 0
  %44 = load ptr, ptr %43, align 8, !tbaa !15
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.TreeArrQueue, ptr %45, i32 0, i32 1
  %47 = load i32, ptr %46, align 8, !tbaa !18
  %48 = add nsw i32 %47, 1
  store i32 %48, ptr %46, align 8, !tbaa !18
  %49 = sext i32 %47 to i64
  %50 = getelementptr inbounds ptr, ptr %44, i64 %49
  %51 = load ptr, ptr %50, align 8, !tbaa !5
  store ptr %51, ptr %9, align 8, !tbaa !5
  %52 = load i32, ptr %8, align 4, !tbaa !9
  %53 = load i32, ptr %5, align 4, !tbaa !9
  %54 = icmp slt i32 %52, %53
  br i1 %54, label %55, label %88

55:                                               ; preds = %41
  %56 = call noalias ptr @malloc(i64 noundef 24) #4
  %57 = load ptr, ptr %9, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.TreeNode, ptr %57, i32 0, i32 1
  store ptr %56, ptr %58, align 8, !tbaa !13
  %59 = load ptr, ptr %4, align 8, !tbaa !5
  %60 = load i32, ptr %8, align 4, !tbaa !9
  %61 = sext i32 %60 to i64
  %62 = getelementptr inbounds i32, ptr %59, i64 %61
  %63 = load i32, ptr %62, align 4, !tbaa !9
  %64 = load ptr, ptr %9, align 8, !tbaa !5
  %65 = getelementptr inbounds %struct.TreeNode, ptr %64, i32 0, i32 1
  %66 = load ptr, ptr %65, align 8, !tbaa !13
  %67 = getelementptr inbounds %struct.TreeNode, ptr %66, i32 0, i32 0
  store i32 %63, ptr %67, align 8, !tbaa !11
  %68 = load ptr, ptr %9, 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 = getelementptr inbounds %struct.TreeNode, ptr %70, i32 0, i32 1
  store ptr null, ptr %71, align 8, !tbaa !13
  %72 = load ptr, ptr %9, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.TreeNode, ptr %72, i32 0, i32 1
  %74 = load ptr, ptr %73, align 8, !tbaa !13
  %75 = getelementptr inbounds %struct.TreeNode, ptr %74, i32 0, i32 2
  store ptr null, ptr %75, align 8, !tbaa !14
  %76 = load ptr, ptr %9, align 8, !tbaa !5
  %77 = getelementptr inbounds %struct.TreeNode, ptr %76, i32 0, i32 1
  %78 = load ptr, ptr %77, align 8, !tbaa !13
  %79 = load ptr, ptr %7, align 8, !tbaa !5
  %80 = getelementptr inbounds %struct.TreeArrQueue, ptr %79, i32 0, i32 0
  %81 = load ptr, ptr %80, align 8, !tbaa !15
  %82 = load ptr, ptr %7, align 8, !tbaa !5
  %83 = getelementptr inbounds %struct.TreeArrQueue, ptr %82, i32 0, i32 2
  %84 = load i32, ptr %83, align 4, !tbaa !17
  %85 = add nsw i32 %84, 1
  store i32 %85, ptr %83, align 4, !tbaa !17
  %86 = sext i32 %84 to i64
  %87 = getelementptr inbounds ptr, ptr %81, i64 %86
  store ptr %78, ptr %87, align 8, !tbaa !5
  br label %88

88:                                               ; preds = %55, %41
  %89 = load i32, ptr %8, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %8, align 4, !tbaa !9
  %91 = load i32, ptr %5, align 4, !tbaa !9
  %92 = icmp slt i32 %90, %91
  br i1 %92, label %93, label %126

93:                                               ; preds = %88
  %94 = call noalias ptr @malloc(i64 noundef 24) #4
  %95 = load ptr, ptr %9, align 8, !tbaa !5
  %96 = getelementptr inbounds %struct.TreeNode, ptr %95, i32 0, i32 2
  store ptr %94, ptr %96, align 8, !tbaa !14
  %97 = load ptr, ptr %4, align 8, !tbaa !5
  %98 = load i32, ptr %8, align 4, !tbaa !9
  %99 = sext i32 %98 to i64
  %100 = getelementptr inbounds i32, ptr %97, i64 %99
  %101 = load i32, ptr %100, align 4, !tbaa !9
  %102 = load ptr, ptr %9, align 8, !tbaa !5
  %103 = getelementptr inbounds %struct.TreeNode, ptr %102, i32 0, i32 2
  %104 = load ptr, ptr %103, align 8, !tbaa !14
  %105 = getelementptr inbounds %struct.TreeNode, ptr %104, i32 0, i32 0
  store i32 %101, ptr %105, align 8, !tbaa !11
  %106 = load ptr, ptr %9, align 8, !tbaa !5
  %107 = getelementptr inbounds %struct.TreeNode, ptr %106, i32 0, i32 2
  %108 = load ptr, ptr %107, align 8, !tbaa !14
  %109 = getelementptr inbounds %struct.TreeNode, ptr %108, i32 0, i32 1
  store ptr null, ptr %109, align 8, !tbaa !13
  %110 = load ptr, ptr %9, align 8, !tbaa !5
  %111 = getelementptr inbounds %struct.TreeNode, ptr %110, i32 0, i32 2
  %112 = load ptr, ptr %111, align 8, !tbaa !14
  %113 = getelementptr inbounds %struct.TreeNode, ptr %112, i32 0, i32 2
  store ptr null, ptr %113, align 8, !tbaa !14
  %114 = load ptr, ptr %9, align 8, !tbaa !5
  %115 = getelementptr inbounds %struct.TreeNode, ptr %114, i32 0, i32 2
  %116 = load ptr, ptr %115, align 8, !tbaa !14
  %117 = load ptr, ptr %7, align 8, !tbaa !5
  %118 = getelementptr inbounds %struct.TreeArrQueue, ptr %117, i32 0, i32 0
  %119 = load ptr, ptr %118, align 8, !tbaa !15
  %120 = load ptr, ptr %7, align 8, !tbaa !5
  %121 = getelementptr inbounds %struct.TreeArrQueue, ptr %120, i32 0, i32 2
  %122 = load i32, ptr %121, align 4, !tbaa !17
  %123 = add nsw i32 %122, 1
  store i32 %123, ptr %121, align 4, !tbaa !17
  %124 = sext i32 %122 to i64
  %125 = getelementptr inbounds ptr, ptr %119, i64 %124
  store ptr %116, ptr %125, align 8, !tbaa !5
  br label %126

126:                                              ; preds = %93, %88
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %127

127:                                              ; preds = %126
  %128 = load i32, ptr %8, align 4, !tbaa !9
  %129 = add nsw i32 %128, 1
  store i32 %129, ptr %8, align 4, !tbaa !9
  br label %36, !llvm.loop !19

130:                                              ; preds = %40
  %131 = load ptr, ptr %7, align 8, !tbaa !5
  call void @BTree_ArrQueueFree(ptr noundef %131)
  %132 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %132, ptr %3, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  br label %133

133:                                              ; preds = %130, %12
  %134 = load ptr, ptr %3, align 8
  ret ptr %134
}

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 = !{!16, !6, i64 0}
!16 = !{!"", !6, i64 0, !10, i64 8, !10, i64 12, !10, i64 16}
!17 = !{!16, !10, i64 12}
!18 = !{!16, !10, i64 8}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


; 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 nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare dso_local ptr @BTree_InitArrQueue(i32 noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
declare dso_local void @BTree_ArrQueueFree(ptr noundef) local_unnamed_addr #1

; Function Attrs: nounwind uwtable
define dso_local noundef ptr @BTree_ArrayInit(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #1 {
  %3 = icmp eq i32 %1, 0
  br i1 %3, label %51, label %4

4:                                                ; preds = %2
  %5 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %6 = load i32, ptr %0, align 4, !tbaa !5
  store i32 %6, ptr %5, align 8, !tbaa !9
  %7 = getelementptr inbounds i8, ptr %5, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %7, i8 0, i64 16, i1 false)
  %8 = tail call ptr @BTree_InitArrQueue(i32 noundef %1)
  %9 = load ptr, ptr %8, align 8, !tbaa !12
  %10 = getelementptr inbounds i8, ptr %8, i64 12
  %11 = load i32, ptr %10, align 4, !tbaa !14
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr %10, align 4, !tbaa !14
  %13 = sext i32 %11 to i64
  %14 = getelementptr inbounds ptr, ptr %9, i64 %13
  store ptr %5, ptr %14, align 8, !tbaa !15
  %15 = icmp sgt i32 %1, 1
  br i1 %15, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %4
  %16 = getelementptr inbounds i8, ptr %8, i64 8
  %.promoted = load i32, ptr %16, align 8, !tbaa !16
  %17 = sext i32 %.promoted to i64
  %18 = zext nneg i32 %1 to i64
  br label %19

._crit_edge:                                      ; preds = %48, %4
  tail call void @BTree_ArrQueueFree(ptr noundef nonnull %8)
  br label %51

19:                                               ; preds = %.lr.ph, %48
  %indvars.iv43 = phi i64 [ 1, %.lr.ph ], [ %indvars.iv.next44, %48 ]
  %indvars.iv = phi i64 [ %17, %.lr.ph ], [ %indvars.iv.next, %48 ]
  %20 = phi i32 [ %12, %.lr.ph ], [ %49, %48 ]
  %21 = load ptr, ptr %8, align 8, !tbaa !12
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %22 = trunc nsw i64 %indvars.iv.next to i32
  store i32 %22, ptr %16, align 8, !tbaa !16
  %23 = getelementptr inbounds ptr, ptr %21, i64 %indvars.iv
  %24 = load ptr, ptr %23, align 8, !tbaa !15
  %25 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %26 = getelementptr inbounds i8, ptr %24, i64 8
  store ptr %25, ptr %26, align 8, !tbaa !17
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv43
  %28 = load i32, ptr %27, align 4, !tbaa !5
  store i32 %28, ptr %25, align 8, !tbaa !9
  %29 = getelementptr inbounds i8, ptr %25, i64 8
  store ptr null, ptr %29, align 8, !tbaa !17
  %30 = load ptr, ptr %26, align 8, !tbaa !17
  %31 = getelementptr inbounds i8, ptr %30, i64 16
  store ptr null, ptr %31, align 8, !tbaa !18
  %32 = add nsw i32 %20, 1
  store i32 %32, ptr %10, align 4, !tbaa !14
  %33 = sext i32 %20 to i64
  %34 = getelementptr inbounds ptr, ptr %21, i64 %33
  store ptr %30, ptr %34, align 8, !tbaa !15
  %35 = add nuw nsw i64 %indvars.iv43, 1
  %36 = icmp ult i64 %35, %18
  br i1 %36, label %37, label %48

37:                                               ; preds = %19
  %38 = tail call noalias dereferenceable_or_null(24) ptr @malloc(i64 noundef 24) #3
  %39 = getelementptr inbounds i8, ptr %24, i64 16
  store ptr %38, ptr %39, align 8, !tbaa !18
  %40 = getelementptr inbounds i32, ptr %0, i64 %35
  %41 = load i32, ptr %40, align 4, !tbaa !5
  store i32 %41, ptr %38, align 8, !tbaa !9
  %42 = getelementptr inbounds i8, ptr %38, i64 8
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %42, i8 0, i64 16, i1 false)
  %43 = load ptr, ptr %39, align 8, !tbaa !18
  %44 = load ptr, ptr %8, align 8, !tbaa !12
  %45 = add nsw i32 %20, 2
  store i32 %45, ptr %10, align 4, !tbaa !14
  %46 = sext i32 %32 to i64
  %47 = getelementptr inbounds ptr, ptr %44, i64 %46
  store ptr %43, ptr %47, align 8, !tbaa !15
  br label %48

48:                                               ; preds = %37, %19
  %49 = phi i32 [ %45, %37 ], [ %32, %19 ]
  %indvars.iv.next44 = add nuw nsw i64 %indvars.iv43, 2
  %50 = icmp ult i64 %indvars.iv.next44, %18
  br i1 %50, label %19, label %._crit_edge, !llvm.loop !19

51:                                               ; preds = %2, %._crit_edge
  %.0 = phi ptr [ %5, %._crit_edge ], [ null, %2 ]
  ret ptr %.0
}

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #2

attributes #0 = { 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 #1 = { 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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 = !{!"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, !11, i64 0}
!13 = !{!"", !11, i64 0, !6, i64 8, !6, i64 12, !6, i64 16}
!14 = !{!13, !6, i64 12}
!15 = !{!11, !11, i64 0}
!16 = !{!13, !6, i64 8}
!17 = !{!10, !11, i64 8}
!18 = !{!10, !11, i64 16}
!19 = distinct !{!19, !20}
!20 = !{!"llvm.loop.mustprogress"}


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