; LLVM IR from UtkarshPathrabe/Competitive-Coding :: Model Algorithms/Binary Tree (Level Order Spiral Tree Traversal)

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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

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

9:                                                ; preds = %3
  br label %52

10:                                               ; preds = %3
  %11 = load i32, ptr %5, align 4, !tbaa !9
  %12 = icmp eq i32 %11, 1
  br i1 %12, label %13, label %18

13:                                               ; preds = %10
  %14 = load ptr, ptr %4, align 8, !tbaa !5
  %15 = getelementptr inbounds %struct.node, ptr %14, i32 0, i32 0
  %16 = load i32, ptr %15, align 8, !tbaa !11
  %17 = call i32 (ptr, ...) @printf(ptr noundef @.str, i32 noundef %16)
  br label %52

18:                                               ; preds = %10
  %19 = load i32, ptr %5, align 4, !tbaa !9
  %20 = icmp sgt i32 %19, 1
  br i1 %20, label %21, label %51

21:                                               ; preds = %18
  %22 = load i32, ptr %6, align 4, !tbaa !9
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %37

24:                                               ; preds = %21
  %25 = load ptr, ptr %4, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.node, ptr %25, i32 0, i32 2
  %27 = load ptr, ptr %26, align 8, !tbaa !13
  %28 = load i32, ptr %5, align 4, !tbaa !9
  %29 = sub nsw i32 %28, 1
  %30 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z15printGivenLevelP4nodeii(ptr noundef %27, i32 noundef %29, i32 noundef %30)
  %31 = load ptr, ptr %4, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.node, ptr %31, i32 0, i32 1
  %33 = load ptr, ptr %32, align 8, !tbaa !14
  %34 = load i32, ptr %5, align 4, !tbaa !9
  %35 = sub nsw i32 %34, 1
  %36 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z15printGivenLevelP4nodeii(ptr noundef %33, i32 noundef %35, i32 noundef %36)
  br label %50

37:                                               ; preds = %21
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = getelementptr inbounds %struct.node, ptr %38, i32 0, i32 1
  %40 = load ptr, ptr %39, align 8, !tbaa !14
  %41 = load i32, ptr %5, align 4, !tbaa !9
  %42 = sub nsw i32 %41, 1
  %43 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z15printGivenLevelP4nodeii(ptr noundef %40, i32 noundef %42, i32 noundef %43)
  %44 = load ptr, ptr %4, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.node, ptr %44, i32 0, i32 2
  %46 = load ptr, ptr %45, align 8, !tbaa !13
  %47 = load i32, ptr %5, align 4, !tbaa !9
  %48 = sub nsw i32 %47, 1
  %49 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z15printGivenLevelP4nodeii(ptr noundef %46, i32 noundef %48, i32 noundef %49)
  br label %50

50:                                               ; preds = %37, %24
  br label %51

51:                                               ; preds = %50, %18
  br label %52

52:                                               ; preds = %51, %13, %9
  ret void
}

declare i32 @printf(ptr noundef, ...) #1

attributes #0 = { mustprogress 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 = { "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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = !{!12, !6, i64 16}
!14 = !{!12, !6, i64 8}


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

@.str = external hidden unnamed_addr constant [4 x i8], align 1

; Function Attrs: mustprogress nofree nounwind uwtable
define dso_local void @_Z15printGivenLevelP4nodeii(ptr noundef readonly %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %.loopexit, label %.lr.ph.preheader

.lr.ph.preheader:                                 ; preds = %3
  %5 = icmp eq i32 %2, 0
  br i1 %5, label %.lr.ph.split.us.peel, label %.lr.ph.split

.lr.ph.split.us.peel:                             ; preds = %.lr.ph.preheader
  %6 = icmp eq i32 %1, 1
  br i1 %6, label %.split.us, label %7

7:                                                ; preds = %.lr.ph.split.us.peel
  %8 = icmp sgt i32 %1, 1
  br i1 %8, label %.split27.us.peel, label %.loopexit

.split27.us.peel:                                 ; preds = %7
  %9 = getelementptr inbounds i8, ptr %0, i64 16
  %10 = load ptr, ptr %9, align 8, !tbaa !5
  %11 = add nsw i32 %1, -1
  tail call void @_Z15printGivenLevelP4nodeii(ptr noundef %10, i32 noundef %11, i32 noundef 0)
  %12 = getelementptr inbounds i8, ptr %0, i64 8
  %13 = load ptr, ptr %12, align 8, !tbaa !11
  %14 = icmp eq ptr %13, null
  br i1 %14, label %.loopexit, label %.lr.ph.split.us

.lr.ph.split.us:                                  ; preds = %.split27.us.peel, %.split27.us
  %.tr18.ph40 = phi i32 [ %20, %.split27.us ], [ %11, %.split27.us.peel ]
  %.tr.ph39 = phi ptr [ %22, %.split27.us ], [ %13, %.split27.us.peel ]
  %15 = icmp eq i32 %.tr18.ph40, 1
  br i1 %15, label %.split.us, label %16

16:                                               ; preds = %.lr.ph.split.us
  %17 = icmp sgt i32 %.tr18.ph40, 1
  br i1 %17, label %.split27.us, label %.loopexit

.split27.us:                                      ; preds = %16
  %18 = getelementptr inbounds i8, ptr %.tr.ph39, i64 16
  %19 = load ptr, ptr %18, align 8, !tbaa !5
  %20 = add nsw i32 %.tr18.ph40, -1
  tail call void @_Z15printGivenLevelP4nodeii(ptr noundef %19, i32 noundef %20, i32 noundef 0)
  %21 = getelementptr inbounds i8, ptr %.tr.ph39, i64 8
  %22 = load ptr, ptr %21, align 8, !tbaa !11
  %23 = icmp eq ptr %22, null
  br i1 %23, label %.loopexit, label %.lr.ph.split.us, !llvm.loop !12

.lr.ph.split:                                     ; preds = %.lr.ph.preheader, %tailrecurse
  %.tr1825 = phi i32 [ %31, %tailrecurse ], [ %1, %.lr.ph.preheader ]
  %.tr24 = phi ptr [ %33, %tailrecurse ], [ %0, %.lr.ph.preheader ]
  %24 = icmp eq i32 %.tr1825, 1
  br i1 %24, label %.split.us, label %27

.split.us:                                        ; preds = %.lr.ph.split, %.lr.ph.split.us, %.lr.ph.split.us.peel
  %.us-phi = phi ptr [ %0, %.lr.ph.split.us.peel ], [ %.tr.ph39, %.lr.ph.split.us ], [ %.tr24, %.lr.ph.split ]
  %25 = load i32, ptr %.us-phi, align 8, !tbaa !14
  %26 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %25)
  br label %.loopexit

27:                                               ; preds = %.lr.ph.split
  %28 = icmp sgt i32 %.tr1825, 1
  br i1 %28, label %tailrecurse, label %.loopexit

tailrecurse:                                      ; preds = %27
  %29 = getelementptr inbounds i8, ptr %.tr24, i64 8
  %30 = load ptr, ptr %29, align 8, !tbaa !11
  %31 = add nsw i32 %.tr1825, -1
  tail call void @_Z15printGivenLevelP4nodeii(ptr noundef %30, i32 noundef %31, i32 noundef %2)
  %32 = getelementptr inbounds i8, ptr %.tr24, i64 16
  %33 = load ptr, ptr %32, align 8, !tbaa !5
  %34 = icmp eq ptr %33, null
  br i1 %34, label %.loopexit, label %.lr.ph.split

.loopexit:                                        ; preds = %tailrecurse, %27, %.split27.us, %16, %7, %.split27.us.peel, %3, %.split.us
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #1

attributes #0 = { mustprogress 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 #1 = { nofree 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" }

!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, !10, i64 16}
!6 = !{!"_ZTS4node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 8}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.peeled.count", i32 1}
!14 = !{!6, !7, i64 0}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,IPSCCPPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LICMPass,LoopFullUnrollPass,LoopInstSimplifyPass,LoopRotatePass,LoopSimplifyCFGPass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SCCPPass,SROAPass,SimplifyCFGPass,TailCallElimPass