; LLVM IR from Hunterdii/GeeksforGeeks-POTD :: February 2025 GFG SOLUTION (February-05)

; 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.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [4 x i8], align 1

; 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 @createQueue() #2

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

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

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

; Function Attrs: nounwind uwtable
declare dso_local i32 @isQueueEmpty(ptr noundef) #2

; Function Attrs: nounwind willreturn memory(read)
declare i64 @strlen(ptr noundef) #4

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

12:                                               ; preds = %1
  %13 = call noalias ptr @strdup(ptr noundef @.str.2) #6
  store ptr %13, ptr %2, align 8
  br label %89

14:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %15 = call noalias ptr @malloc(i64 noundef 2000000) #7
  store ptr %15, ptr %4, align 8, !tbaa !5
  %16 = load ptr, ptr %4, align 8, !tbaa !5
  %17 = getelementptr inbounds i8, ptr %16, i64 0
  store i8 0, ptr %17, align 1, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %18 = call ptr @createQueue()
  store ptr %18, ptr %5, align 8, !tbaa !5
  %19 = load ptr, ptr %5, align 8, !tbaa !5
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  call void @enqueue(ptr noundef %19, ptr noundef %20)
  br label %21

21:                                               ; preds = %53, %51, %14
  %22 = load ptr, ptr %5, align 8, !tbaa !5
  %23 = call i32 @isQueueEmpty(ptr noundef %22)
  %24 = icmp ne i32 %23, 0
  %25 = xor i1 %24, true
  br i1 %25, label %26, label %54

26:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  %28 = call ptr @dequeue(ptr noundef %27)
  store ptr %28, ptr %6, align 8, !tbaa !5
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = icmp eq ptr %29, null
  br i1 %30, label %31, label %34

31:                                               ; preds = %26
  %32 = load ptr, ptr %4, align 8, !tbaa !5
  %33 = call ptr @strcat(ptr noundef %32, ptr noundef @.str.3) #6
  store i32 2, ptr %7, align 4
  br label %51, !llvm.loop !10

34:                                               ; preds = %26
  call void @llvm.lifetime.start.p0(i64 12, ptr %8) #6
  %35 = getelementptr inbounds [12 x i8], ptr %8, i64 0, i64 0
  %36 = load ptr, ptr %6, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.Node, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !12
  %39 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef %35, ptr noundef @.str.4, i32 noundef %38) #6
  %40 = load ptr, ptr %4, align 8, !tbaa !5
  %41 = getelementptr inbounds [12 x i8], ptr %8, i64 0, i64 0
  %42 = call ptr @strcat(ptr noundef %40, ptr noundef %41) #6
  %43 = load ptr, ptr %5, align 8, !tbaa !5
  %44 = load ptr, ptr %6, align 8, !tbaa !5
  %45 = getelementptr inbounds %struct.Node, ptr %44, i32 0, i32 1
  %46 = load ptr, ptr %45, align 8, !tbaa !15
  call void @enqueue(ptr noundef %43, ptr noundef %46)
  %47 = load ptr, ptr %5, align 8, !tbaa !5
  %48 = load ptr, ptr %6, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 2
  %50 = load ptr, ptr %49, align 8, !tbaa !16
  call void @enqueue(ptr noundef %47, ptr noundef %50)
  call void @llvm.lifetime.end.p0(i64 12, ptr %8) #6
  store i32 0, ptr %7, align 4
  br label %51

51:                                               ; preds = %34, %31
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #6
  %52 = load i32, ptr %7, align 4
  switch i32 %52, label %91 [
    i32 0, label %53
    i32 2, label %21
  ]

53:                                               ; preds = %51
  br label %21, !llvm.loop !10

54:                                               ; preds = %21
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #6
  %55 = load ptr, ptr %4, align 8, !tbaa !5
  %56 = call i64 @strlen(ptr noundef %55) #8
  %57 = trunc i64 %56 to i32
  store i32 %57, ptr %9, align 4, !tbaa !17
  br label %58

58:                                               ; preds = %80, %54
  %59 = load i32, ptr %9, align 4, !tbaa !17
  %60 = icmp sgt i32 %59, 0
  br i1 %60, label %61, label %78

61:                                               ; preds = %58
  %62 = call ptr @__ctype_b_loc() #9
  %63 = load ptr, ptr %62, align 8, !tbaa !5
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = load i32, ptr %9, align 4, !tbaa !17
  %66 = sub nsw i32 %65, 1
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds i8, ptr %64, i64 %67
  %69 = load i8, ptr %68, align 1, !tbaa !9
  %70 = sext i8 %69 to i32
  %71 = sext i32 %70 to i64
  %72 = getelementptr inbounds i16, ptr %63, i64 %71
  %73 = load i16, ptr %72, align 2, !tbaa !18
  %74 = zext i16 %73 to i32
  %75 = and i32 %74, 2048
  %76 = icmp ne i32 %75, 0
  %77 = xor i1 %76, true
  br label %78

78:                                               ; preds = %61, %58
  %79 = phi i1 [ false, %58 ], [ %77, %61 ]
  br i1 %79, label %80, label %86

80:                                               ; preds = %78
  %81 = load ptr, ptr %4, align 8, !tbaa !5
  %82 = load i32, ptr %9, align 4, !tbaa !17
  %83 = add nsw i32 %82, -1
  store i32 %83, ptr %9, align 4, !tbaa !17
  %84 = sext i32 %83 to i64
  %85 = getelementptr inbounds i8, ptr %81, i64 %84
  store i8 0, ptr %85, align 1, !tbaa !9
  br label %58, !llvm.loop !20

86:                                               ; preds = %78
  %87 = load ptr, ptr %5, align 8, !tbaa !5
  call void @free(ptr noundef %87) #6
  %88 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %88, ptr %2, align 8
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #6
  call void @llvm.lifetime.end.p0(i64 8, ptr %4) #6
  br label %89

89:                                               ; preds = %86, %12
  %90 = load ptr, ptr %2, align 8
  ret ptr %90

91:                                               ; preds = %51
  unreachable
}

; Function Attrs: nounwind
declare noalias ptr @strdup(ptr noundef) #3

; Function Attrs: nounwind
declare ptr @strcat(ptr noundef, ptr noundef) #3

; Function Attrs: nounwind
declare i32 @sprintf(ptr noundef, ptr noundef, ...) #3

; Function Attrs: nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() #5

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 "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 willreturn memory(read) "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 #5 = { nounwind willreturn memory(none) "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 #6 = { nounwind }
attributes #7 = { nounwind allocsize(0) }
attributes #8 = { nounwind willreturn memory(read) }
attributes #9 = { nounwind willreturn memory(none) }

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


; 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.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [4 x i8], align 1

; 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: nounwind uwtable
declare dso_local ptr @createQueue() local_unnamed_addr #2

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @dequeue(ptr noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #3

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

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: read)
declare i64 @strlen(ptr nocapture noundef) local_unnamed_addr #4

; Function Attrs: nounwind uwtable
define dso_local ptr @levelOrder(ptr noundef %0) local_unnamed_addr #2 {
  %2 = alloca [12 x i8], align 1
  %3 = icmp eq ptr %0, null
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call noalias ptr @strdup(ptr noundef nonnull @.str.2) #9
  br label %41

6:                                                ; preds = %1
  %7 = tail call noalias dereferenceable_or_null(2000000) ptr @malloc(i64 noundef 2000000) #10
  store i8 0, ptr %7, align 1, !tbaa !5
  %8 = tail call ptr @createQueue()
  tail call void @enqueue(ptr noundef %8, ptr noundef nonnull %0)
  %9 = tail call i32 @isQueueEmpty(ptr noundef %8)
  %.not26 = icmp eq i32 %9, 0
  br i1 %.not26, label %.lr.ph, label %._crit_edge

.lr.ph:                                           ; preds = %6, %22
  %10 = tail call ptr @dequeue(ptr noundef %8)
  %11 = icmp eq ptr %10, null
  br i1 %11, label %12, label %14

12:                                               ; preds = %.lr.ph
  %13 = tail call ptr @strcat(ptr noundef nonnull dereferenceable(1) %7, ptr noundef nonnull dereferenceable(1) @.str.3) #9
  br label %22, !llvm.loop !8

14:                                               ; preds = %.lr.ph
  call void @llvm.lifetime.start.p0(i64 12, ptr nonnull %2) #9
  %15 = load i32, ptr %10, align 8, !tbaa !10
  %16 = call i32 (ptr, ptr, ...) @sprintf(ptr noundef nonnull dereferenceable(1) %2, ptr noundef nonnull dereferenceable(1) @.str.4, i32 noundef %15) #9
  %17 = call ptr @strcat(ptr noundef nonnull dereferenceable(1) %7, ptr noundef nonnull dereferenceable(1) %2) #9
  %18 = getelementptr inbounds i8, ptr %10, i64 8
  %19 = load ptr, ptr %18, align 8, !tbaa !14
  tail call void @enqueue(ptr noundef %8, ptr noundef %19)
  %20 = getelementptr inbounds i8, ptr %10, i64 16
  %21 = load ptr, ptr %20, align 8, !tbaa !15
  tail call void @enqueue(ptr noundef %8, ptr noundef %21)
  call void @llvm.lifetime.end.p0(i64 12, ptr nonnull %2) #9
  br label %22

22:                                               ; preds = %14, %12
  %23 = tail call i32 @isQueueEmpty(ptr noundef %8)
  %.not = icmp eq i32 %23, 0
  br i1 %.not, label %.lr.ph, label %._crit_edge

._crit_edge:                                      ; preds = %22, %6
  %24 = tail call i64 @strlen(ptr noundef nonnull dereferenceable(1) %7) #11
  %25 = trunc i64 %24 to i32
  %invariant.gep = getelementptr i8, ptr %7, i64 -1
  %26 = icmp sgt i32 %25, 0
  br i1 %26, label %.lr.ph29, label %.critedge

.lr.ph29:                                         ; preds = %._crit_edge
  %27 = tail call ptr @__ctype_b_loc() #12
  br label %28

28:                                               ; preds = %.lr.ph29, %36
  %.027 = phi i32 [ %25, %.lr.ph29 ], [ %37, %36 ]
  %29 = load ptr, ptr %27, align 8, !tbaa !16
  %30 = zext nneg i32 %.027 to i64
  %gep = getelementptr i8, ptr %invariant.gep, i64 %30
  %31 = load i8, ptr %gep, align 1, !tbaa !5
  %32 = sext i8 %31 to i64
  %33 = getelementptr inbounds i16, ptr %29, i64 %32
  %34 = load i16, ptr %33, align 2, !tbaa !17
  %35 = and i16 %34, 2048
  %.not25 = icmp eq i16 %35, 0
  br i1 %.not25, label %36, label %.critedge

36:                                               ; preds = %28
  %37 = add nsw i32 %.027, -1
  %38 = zext nneg i32 %37 to i64
  %39 = getelementptr inbounds i8, ptr %7, i64 %38
  store i8 0, ptr %39, align 1, !tbaa !5
  %40 = icmp sgt i32 %.027, 1
  br i1 %40, label %28, label %.critedge, !llvm.loop !19

.critedge:                                        ; preds = %28, %36, %._crit_edge
  tail call void @free(ptr noundef %8) #9
  br label %41

41:                                               ; preds = %.critedge, %4
  %.022 = phi ptr [ %5, %4 ], [ %7, %.critedge ]
  ret ptr %.022
}

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite, inaccessiblemem: readwrite)
declare noalias ptr @strdup(ptr nocapture noundef readonly) local_unnamed_addr #5

; Function Attrs: mustprogress nofree nounwind willreturn memory(argmem: readwrite)
declare ptr @strcat(ptr noalias noundef returned, ptr noalias nocapture noundef readonly) local_unnamed_addr #6

; Function Attrs: nofree nounwind
declare noundef i32 @sprintf(ptr noalias nocapture noundef writeonly, ptr nocapture noundef readonly, ...) local_unnamed_addr #7

; Function Attrs: mustprogress nofree nosync nounwind willreturn memory(none)
declare ptr @__ctype_b_loc() local_unnamed_addr #8

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 = { 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 = { 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 #4 = { mustprogress nofree nounwind willreturn memory(argmem: read) "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 #5 = { mustprogress nofree nounwind willreturn 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 #6 = { mustprogress nofree nounwind willreturn memory(argmem: readwrite) "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 #7 = { 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" }
attributes #8 = { mustprogress nofree nosync nounwind willreturn memory(none) "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 #9 = { nounwind }
attributes #10 = { nounwind allocsize(0) }
attributes #11 = { nounwind willreturn memory(read) }
attributes #12 = { nounwind willreturn memory(none) }

!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 = !{!"omnipotent char", !7, i64 0}
!7 = !{!"Simple C/C++ TBAA"}
!8 = distinct !{!8, !9}
!9 = !{!"llvm.loop.mustprogress"}
!10 = !{!11, !12, i64 0}
!11 = !{!"Node", !12, i64 0, !13, i64 8, !13, i64 16}
!12 = !{!"int", !6, i64 0}
!13 = !{!"any pointer", !6, i64 0}
!14 = !{!11, !13, i64 8}
!15 = !{!11, !13, i64 16}
!16 = !{!13, !13, i64 0}
!17 = !{!18, !18, i64 0}
!18 = !{!"short", !6, i64 0}
!19 = distinct !{!19, !9}


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