; LLVM IR from wuzhouhui/leetcode :: 103_bin_tree_zigzag_trav (zigzagLevelOrder)

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

@qhead = external hidden global ptr, align 8
@p = external hidden global i32, align 4
@maxcolp = external hidden global i32, align 4
@stack = external hidden global ptr, align 8
@col = external hidden global ptr, align 8
@res = external hidden global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @zigzagLevelOrder(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
  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
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %15, label %12

12:                                               ; preds = %3
  %13 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr null, ptr %13, align 8, !tbaa !5
  %14 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 0, ptr %14, align 4, !tbaa !9
  store ptr null, ptr %4, align 8
  br label %63

15:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  call void @do_init()
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  call void @enque(ptr noundef %16)
  store i32 1, ptr %9, align 4, !tbaa !9
  br label %17

17:                                               ; preds = %56, %15
  %18 = load ptr, ptr @qhead, align 8, !tbaa !5
  %19 = icmp ne ptr %18, null
  br i1 %19, label %20, label %57

20:                                               ; preds = %17
  br label %21

21:                                               ; preds = %42, %20
  %22 = load ptr, ptr @qhead, align 8, !tbaa !5
  %23 = icmp ne ptr %22, null
  br i1 %23, label %24, label %44

24:                                               ; preds = %21
  %25 = call ptr @deque()
  store ptr %25, ptr %8, align 8, !tbaa !5
  %26 = load i32, ptr %9, align 4, !tbaa !9
  %27 = icmp ne i32 %26, 0
  br i1 %27, label %28, label %35

28:                                               ; preds = %24
  %29 = load ptr, ptr %8, align 8, !tbaa !5
  %30 = getelementptr inbounds %struct.TreeNode, ptr %29, i32 0, i32 1
  %31 = load ptr, ptr %30, align 8, !tbaa !11
  call void @push(ptr noundef %31)
  %32 = load ptr, ptr %8, align 8, !tbaa !5
  %33 = getelementptr inbounds %struct.TreeNode, ptr %32, i32 0, i32 2
  %34 = load ptr, ptr %33, align 8, !tbaa !13
  call void @push(ptr noundef %34)
  br label %42

35:                                               ; preds = %24
  %36 = load ptr, ptr %8, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.TreeNode, ptr %36, i32 0, i32 2
  %38 = load ptr, ptr %37, align 8, !tbaa !13
  call void @push(ptr noundef %38)
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.TreeNode, ptr %39, i32 0, i32 1
  %41 = load ptr, ptr %40, align 8, !tbaa !11
  call void @push(ptr noundef %41)
  br label %42

42:                                               ; preds = %35, %28
  %43 = load ptr, ptr %8, align 8, !tbaa !5
  call void @add_ans(ptr noundef %43)
  br label %21, !llvm.loop !14

44:                                               ; preds = %21
  %45 = load i32, ptr @p, align 4, !tbaa !9
  %46 = add nsw i32 %45, 1
  store i32 %46, ptr @p, align 4, !tbaa !9
  store i32 0, ptr @maxcolp, align 4, !tbaa !9
  %47 = load i32, ptr %9, align 4, !tbaa !9
  %48 = icmp ne i32 %47, 0
  %49 = xor i1 %48, true
  %50 = zext i1 %49 to i32
  store i32 %50, ptr %9, align 4, !tbaa !9
  br label %51

51:                                               ; preds = %54, %44
  %52 = load ptr, ptr @stack, align 8, !tbaa !5
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %56

54:                                               ; preds = %51
  %55 = call ptr @pop()
  call void @enque(ptr noundef %55)
  br label %51, !llvm.loop !16

56:                                               ; preds = %51
  br label %17, !llvm.loop !17

57:                                               ; preds = %17
  %58 = load ptr, ptr @col, align 8, !tbaa !5
  %59 = load ptr, ptr %6, align 8, !tbaa !5
  store ptr %58, ptr %59, align 8, !tbaa !5
  %60 = load i32, ptr @p, align 4, !tbaa !9
  %61 = load ptr, ptr %7, align 8, !tbaa !5
  store i32 %60, ptr %61, align 4, !tbaa !9
  %62 = load ptr, ptr @res, align 8, !tbaa !5
  store ptr %62, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  br label %63

63:                                               ; preds = %57, %12
  %64 = load ptr, ptr %4, align 8
  ret ptr %64
}

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

; Function Attrs: nounwind uwtable
declare hidden void @do_init() #0

; Function Attrs: nounwind uwtable
declare hidden void @enque(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden ptr @deque() #0

; Function Attrs: nounwind uwtable
declare hidden void @push(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden void @add_ans(ptr noundef) #0

; Function Attrs: nounwind uwtable
declare hidden ptr @pop() #0

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


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

@qhead = external hidden local_unnamed_addr global ptr, align 8
@p = external hidden local_unnamed_addr global i32, align 4
@maxcolp = external hidden local_unnamed_addr global i32, align 4
@stack = external hidden local_unnamed_addr global ptr, align 8
@col = external hidden local_unnamed_addr global ptr, align 8
@res = external hidden local_unnamed_addr global ptr, align 8

; Function Attrs: nounwind uwtable
define dso_local ptr @zigzagLevelOrder(ptr noundef %0, ptr nocapture noundef writeonly %1, ptr nocapture noundef writeonly %2) local_unnamed_addr #0 {
  %.not = icmp eq ptr %0, null
  br i1 %.not, label %4, label %5

4:                                                ; preds = %3
  store ptr null, ptr %1, align 8, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !9
  br label %26

5:                                                ; preds = %3
  tail call void @do_init()
  tail call void @enque(ptr noundef nonnull %0)
  %6 = load ptr, ptr @qhead, align 8, !tbaa !5
  %.not1521 = icmp eq ptr %6, null
  br i1 %.not1521, label %._crit_edge, label %.preheader

.loopexit:                                        ; preds = %.lr.ph
  %.pre = load ptr, ptr @qhead, align 8, !tbaa !5
  %.not15 = icmp eq ptr %.pre, null
  br i1 %.not15, label %._crit_edge, label %.preheader, !llvm.loop !11

.preheader:                                       ; preds = %5, %.loopexit
  %.022 = phi i32 [ %19, %.loopexit ], [ 1, %5 ]
  %.not19 = icmp eq i32 %.022, 0
  br i1 %.not19, label %.preheader.split.us, label %.preheader.split

.preheader.split.us:                              ; preds = %.preheader, %.preheader.split.us
  %7 = tail call ptr @deque()
  %8 = getelementptr inbounds i8, ptr %7, i64 16
  %9 = load ptr, ptr %8, align 8, !tbaa !13
  tail call void @push(ptr noundef %9)
  %10 = getelementptr inbounds i8, ptr %7, i64 8
  %11 = load ptr, ptr %10, align 8, !tbaa !15
  tail call void @push(ptr noundef %11)
  tail call void @add_ans(ptr noundef nonnull %7)
  %.pr.us = load ptr, ptr @qhead, align 8, !tbaa !5
  %.not16.us = icmp eq ptr %.pr.us, null
  br i1 %.not16.us, label %.split.us, label %.preheader.split.us, !llvm.loop !16

.preheader.split:                                 ; preds = %.preheader, %.preheader.split
  %12 = tail call ptr @deque()
  %13 = getelementptr inbounds i8, ptr %12, i64 8
  %14 = load ptr, ptr %13, align 8, !tbaa !15
  tail call void @push(ptr noundef %14)
  %15 = getelementptr inbounds i8, ptr %12, i64 16
  %16 = load ptr, ptr %15, align 8, !tbaa !13
  tail call void @push(ptr noundef %16)
  tail call void @add_ans(ptr noundef nonnull %12)
  %.pr = load ptr, ptr @qhead, align 8, !tbaa !5
  %.not16 = icmp eq ptr %.pr, null
  br i1 %.not16, label %.split.us, label %.preheader.split, !llvm.loop !16

.split.us:                                        ; preds = %.preheader.split, %.preheader.split.us
  %17 = load i32, ptr @p, align 4, !tbaa !9
  %18 = add nsw i32 %17, 1
  store i32 %18, ptr @p, align 4, !tbaa !9
  store i32 0, ptr @maxcolp, align 4, !tbaa !9
  %19 = xor i32 %.022, 1
  %20 = load ptr, ptr @stack, align 8, !tbaa !5
  %.not1820 = icmp eq ptr %20, null
  br i1 %.not1820, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %.split.us, %.lr.ph
  %21 = tail call ptr @pop()
  tail call void @enque(ptr noundef %21)
  %22 = load ptr, ptr @stack, align 8, !tbaa !5
  %.not18 = icmp eq ptr %22, null
  br i1 %.not18, label %.loopexit, label %.lr.ph, !llvm.loop !17

._crit_edge:                                      ; preds = %.loopexit, %.split.us, %5
  %23 = load ptr, ptr @col, align 8, !tbaa !5
  store ptr %23, ptr %1, align 8, !tbaa !5
  %24 = load i32, ptr @p, align 4, !tbaa !9
  store i32 %24, ptr %2, align 4, !tbaa !9
  %25 = load ptr, ptr @res, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %._crit_edge, %4
  %.013 = phi ptr [ %25, %._crit_edge ], [ null, %4 ]
  ret ptr %.013
}

; Function Attrs: nounwind uwtable
declare hidden void @do_init() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @enque(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @deque() local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @push(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden void @add_ans(ptr noundef) local_unnamed_addr #0

; Function Attrs: nounwind uwtable
declare hidden ptr @pop() local_unnamed_addr #0

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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 16}
!14 = !{!"TreeNode", !10, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 8}
!16 = distinct !{!16, !12}
!17 = distinct !{!17, !12}


; Effective O3 passes: CorrelatedValuePropagationPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass