; LLVM IR from ImChong/Practice_LeetCode_C :: F02_Programming_Topics/F01_Data_Structure/06_Binary_Tree/08_nlevelOrder (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.Node = type { i32, i32, ptr }

; Function Attrs: nounwind uwtable
define dso_local ptr @levelOrder(ptr noundef %0, ptr noundef %1, ptr noundef %2) #0 {
  %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 i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca ptr, align 8
  %17 = 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
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #4
  %18 = call noalias ptr @malloc(i64 noundef 8000) #5
  store ptr %18, ptr %8, align 8, !tbaa !5
  %19 = call noalias ptr @malloc(i64 noundef 4000) #5
  %20 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %19, ptr %20, align 8, !tbaa !5
  %21 = load ptr, ptr %5, align 8, !tbaa !5
  %22 = icmp ne ptr %21, null
  br i1 %22, label %26, label %23

23:                                               ; preds = %3
  %24 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 0, ptr %24, align 4, !tbaa !9
  %25 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %25, ptr %4, align 8
  store i32 1, ptr %9, align 4
  br label %113

26:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #4
  %27 = call noalias ptr @malloc(i64 noundef 80000) #5
  store ptr %27, ptr %10, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #4
  store i32 0, ptr %11, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #4
  store i32 0, ptr %12, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %13) #4
  store i32 0, ptr %13, align 4, !tbaa !9
  %28 = load ptr, ptr %5, align 8, !tbaa !5
  %29 = load ptr, ptr %10, align 8, !tbaa !5
  %30 = load i32, ptr %12, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr %12, align 4, !tbaa !9
  %32 = sext i32 %30 to i64
  %33 = getelementptr inbounds ptr, ptr %29, i64 %32
  store ptr %28, ptr %33, align 8, !tbaa !5
  br label %34

34:                                               ; preds = %100, %26
  %35 = load i32, ptr %11, align 4, !tbaa !9
  %36 = load i32, ptr %12, align 4, !tbaa !9
  %37 = icmp ne i32 %35, %36
  br i1 %37, label %38, label %108

38:                                               ; preds = %34
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #4
  %39 = load i32, ptr %12, align 4, !tbaa !9
  %40 = load i32, ptr %11, align 4, !tbaa !9
  %41 = sub nsw i32 %39, %40
  store i32 %41, ptr %14, align 4, !tbaa !9
  %42 = load i32, ptr %14, align 4, !tbaa !9
  %43 = sext i32 %42 to i64
  %44 = mul i64 4, %43
  %45 = call noalias ptr @malloc(i64 noundef %44) #5
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load i32, ptr %13, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds ptr, ptr %46, i64 %48
  store ptr %45, ptr %49, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #4
  store i32 0, ptr %15, align 4, !tbaa !9
  br label %50

50:                                               ; preds = %97, %38
  %51 = load i32, ptr %15, align 4, !tbaa !9
  %52 = load i32, ptr %14, align 4, !tbaa !9
  %53 = icmp slt i32 %51, %52
  br i1 %53, label %55, label %54

54:                                               ; preds = %50
  store i32 4, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #4
  br label %100

55:                                               ; preds = %50
  call void @llvm.lifetime.start.p0(i64 8, ptr %16) #4
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = load i32, ptr %11, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %11, align 4, !tbaa !9
  %59 = sext i32 %57 to i64
  %60 = getelementptr inbounds ptr, ptr %56, i64 %59
  %61 = load ptr, ptr %60, align 8, !tbaa !5
  store ptr %61, ptr %16, align 8, !tbaa !5
  %62 = load ptr, ptr %16, align 8, !tbaa !5
  %63 = getelementptr inbounds %struct.Node, ptr %62, i32 0, i32 0
  %64 = load i32, ptr %63, align 8, !tbaa !11
  %65 = load ptr, ptr %8, align 8, !tbaa !5
  %66 = load i32, ptr %13, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds ptr, ptr %65, i64 %67
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  %70 = load i32, ptr %15, align 4, !tbaa !9
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds i32, ptr %69, i64 %71
  store i32 %64, ptr %72, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %17) #4
  store i32 0, ptr %17, align 4, !tbaa !9
  br label %73

73:                                               ; preds = %93, %55
  %74 = load i32, ptr %17, align 4, !tbaa !9
  %75 = load ptr, ptr %16, align 8, !tbaa !5
  %76 = getelementptr inbounds %struct.Node, ptr %75, i32 0, i32 1
  %77 = load i32, ptr %76, align 4, !tbaa !13
  %78 = icmp slt i32 %74, %77
  br i1 %78, label %80, label %79

79:                                               ; preds = %73
  store i32 7, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %17) #4
  br label %96

80:                                               ; preds = %73
  %81 = load ptr, ptr %16, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.Node, ptr %81, i32 0, i32 2
  %83 = load ptr, ptr %82, align 8, !tbaa !14
  %84 = load i32, ptr %17, align 4, !tbaa !9
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds ptr, ptr %83, i64 %85
  %87 = load ptr, ptr %86, align 8, !tbaa !5
  %88 = load ptr, ptr %10, align 8, !tbaa !5
  %89 = load i32, ptr %12, align 4, !tbaa !9
  %90 = add nsw i32 %89, 1
  store i32 %90, ptr %12, align 4, !tbaa !9
  %91 = sext i32 %89 to i64
  %92 = getelementptr inbounds ptr, ptr %88, i64 %91
  store ptr %87, ptr %92, align 8, !tbaa !5
  br label %93

93:                                               ; preds = %80
  %94 = load i32, ptr %17, align 4, !tbaa !9
  %95 = add nsw i32 %94, 1
  store i32 %95, ptr %17, align 4, !tbaa !9
  br label %73, !llvm.loop !15

96:                                               ; preds = %79
  call void @llvm.lifetime.end.p0(i64 8, ptr %16) #4
  br label %97

97:                                               ; preds = %96
  %98 = load i32, ptr %15, align 4, !tbaa !9
  %99 = add nsw i32 %98, 1
  store i32 %99, ptr %15, align 4, !tbaa !9
  br label %50, !llvm.loop !17

100:                                              ; preds = %54
  %101 = load i32, ptr %14, align 4, !tbaa !9
  %102 = load ptr, ptr %7, align 8, !tbaa !5
  %103 = load ptr, ptr %102, align 8, !tbaa !5
  %104 = load i32, ptr %13, align 4, !tbaa !9
  %105 = add nsw i32 %104, 1
  store i32 %105, ptr %13, align 4, !tbaa !9
  %106 = sext i32 %104 to i64
  %107 = getelementptr inbounds i32, ptr %103, i64 %106
  store i32 %101, ptr %107, align 4, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #4
  br label %34, !llvm.loop !18

108:                                              ; preds = %34
  %109 = load i32, ptr %13, align 4, !tbaa !9
  %110 = load ptr, ptr %6, align 8, !tbaa !5
  store i32 %109, ptr %110, align 4, !tbaa !9
  %111 = load ptr, ptr %10, align 8, !tbaa !5
  call void @free(ptr noundef %111) #4
  %112 = load ptr, ptr %8, align 8, !tbaa !5
  store ptr %112, ptr %4, align 8
  store i32 1, ptr %9, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %13) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #4
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #4
  br label %113

113:                                              ; preds = %108, %23
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #4
  %114 = load ptr, ptr %4, align 8
  ret ptr %114
}

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

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

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

; Function Attrs: nounwind
declare void @free(ptr noundef) #3

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 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { nounwind "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 #4 = { nounwind }
attributes #5 = { 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 = !{!"Node", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 8}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !16}
!18 = distinct !{!18, !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: nounwind uwtable
define dso_local noalias noundef ptr @levelOrder(ptr noundef %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %4 = tail call noalias dereferenceable_or_null(8000) ptr @malloc(i64 noundef 8000) #4
  %5 = tail call noalias dereferenceable_or_null(4000) ptr @malloc(i64 noundef 4000) #4
  store ptr %5, ptr %2, align 8, !tbaa !5
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %6, label %7

6:                                                ; preds = %3
  store i32 0, ptr %1, align 4, !tbaa !9
  br label %35

7:                                                ; preds = %3
  %8 = tail call noalias dereferenceable_or_null(80000) ptr @malloc(i64 noundef 80000) #4
  store ptr %0, ptr %8, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %7, %._crit_edge53
  %indvars.iv71 = phi i64 [ 0, %7 ], [ %indvars.iv.next72, %._crit_edge53 ]
  %.04057 = phi i32 [ 1, %7 ], [ %.1.lcssa, %._crit_edge53 ]
  %.04156 = phi i32 [ 0, %7 ], [ %.142.lcssa, %._crit_edge53 ]
  %10 = sub nsw i32 %.04057, %.04156
  %11 = sext i32 %10 to i64
  %12 = shl nsw i64 %11, 2
  %13 = tail call noalias ptr @malloc(i64 noundef %12) #4
  %14 = getelementptr inbounds ptr, ptr %4, i64 %indvars.iv71
  store ptr %13, ptr %14, align 8, !tbaa !5
  %15 = icmp sgt i32 %10, 0
  br i1 %15, label %.lr.ph52.preheader, label %._crit_edge53

.lr.ph52.preheader:                               ; preds = %9
  %16 = sext i32 %.04156 to i64
  br label %.lr.ph52

._crit_edge53.loopexit:                           ; preds = %._crit_edge
  %17 = trunc nsw i64 %indvars.iv.next67 to i32
  br label %._crit_edge53

._crit_edge53:                                    ; preds = %._crit_edge53.loopexit, %9
  %.142.lcssa = phi i32 [ %.04156, %9 ], [ %17, %._crit_edge53.loopexit ]
  %.1.lcssa = phi i32 [ %.04057, %9 ], [ %.2.lcssa, %._crit_edge53.loopexit ]
  %indvars.iv.next72 = add nuw nsw i64 %indvars.iv71, 1
  %18 = getelementptr inbounds i32, ptr %5, i64 %indvars.iv71
  store i32 %10, ptr %18, align 4, !tbaa !9
  %.not45 = icmp eq i32 %.142.lcssa, %.1.lcssa
  br i1 %.not45, label %33, label %9, !llvm.loop !11

.lr.ph52:                                         ; preds = %.lr.ph52.preheader, %._crit_edge
  %indvars.iv66 = phi i64 [ %16, %.lr.ph52.preheader ], [ %indvars.iv.next67, %._crit_edge ]
  %indvars.iv = phi i64 [ 0, %.lr.ph52.preheader ], [ %indvars.iv.next, %._crit_edge ]
  %.149 = phi i32 [ %.04057, %.lr.ph52.preheader ], [ %.2.lcssa, %._crit_edge ]
  %indvars.iv.next67 = add nsw i64 %indvars.iv66, 1
  %19 = getelementptr inbounds ptr, ptr %8, i64 %indvars.iv66
  %20 = load ptr, ptr %19, align 8, !tbaa !5
  %21 = load i32, ptr %20, align 8, !tbaa !13
  %22 = getelementptr inbounds i32, ptr %13, i64 %indvars.iv
  store i32 %21, ptr %22, align 4, !tbaa !9
  %23 = getelementptr inbounds i8, ptr %20, i64 4
  %24 = load i32, ptr %23, align 4, !tbaa !15
  %25 = icmp sgt i32 %24, 0
  br i1 %25, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %.lr.ph52
  %26 = getelementptr inbounds i8, ptr %20, i64 8
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = sext i32 %.149 to i64
  %29 = shl nsw i64 %28, 3
  %scevgep = getelementptr i8, ptr %8, i64 %29
  %30 = zext nneg i32 %24 to i64
  %31 = shl nuw nsw i64 %30, 3
  tail call void @llvm.memcpy.p0.p0.i64(ptr align 8 %scevgep, ptr align 8 %27, i64 %31, i1 false), !tbaa !5
  %32 = add i32 %.149, %24
  br label %._crit_edge

._crit_edge:                                      ; preds = %.lr.ph, %.lr.ph52
  %.2.lcssa = phi i32 [ %.149, %.lr.ph52 ], [ %32, %.lr.ph ]
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %11
  br i1 %exitcond.not, label %._crit_edge53.loopexit, label %.lr.ph52, !llvm.loop !17

33:                                               ; preds = %._crit_edge53
  %34 = trunc nuw i64 %indvars.iv.next72 to i32
  store i32 %34, ptr %1, align 4, !tbaa !9
  tail call void @free(ptr noundef %8) #5
  br label %35

35:                                               ; preds = %33, %6
  ret ptr %4
}

; 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 nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #2

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #3

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 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 = { mustprogress nounwind willreturn allockind("free") 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 #3 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { nounwind allocsize(0) }
attributes #5 = { 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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !10, i64 0}
!14 = !{!"Node", !10, i64 0, !10, i64 4, !6, i64 8}
!15 = !{!14, !10, i64 4}
!16 = !{!14, !6, i64 8}
!17 = distinct !{!17, !12}


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