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

; Function Attrs: nounwind uwtable
declare dso_local ptr @newNode(i32 noundef) #0

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createQueue() #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @buildTree(ptr noundef %0) #0 {
  %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 ptr, align 8
  %8 = alloca i32, align 4
  store ptr %0, ptr %3, align 8, !tbaa !5
  %9 = load ptr, ptr %3, align 8, !tbaa !5
  %10 = call i64 @strlen(ptr noundef %9) #5
  %11 = icmp eq i64 %10, 0
  br i1 %11, label %18, label %12

12:                                               ; preds = %1
  %13 = load ptr, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 0
  %15 = load i8, ptr %14, align 1, !tbaa !9
  %16 = sext i8 %15 to i32
  %17 = icmp eq i32 %16, 78
  br i1 %17, label %18, label %19

18:                                               ; preds = %12, %1
  store ptr null, ptr %2, align 8
  br label %80

19:                                               ; preds = %12
  call void @llvm.lifetime.start.p0(i64 8, ptr %4) #6
  %20 = load ptr, ptr %3, align 8, !tbaa !5
  %21 = call ptr @strtok(ptr noundef %20, ptr noundef @.str) #6
  store ptr %21, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #6
  %22 = load ptr, ptr %4, align 8, !tbaa !5
  %23 = call i32 @atoi(ptr noundef %22) #5
  %24 = call ptr @newNode(i32 noundef %23)
  store ptr %24, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #6
  %25 = call ptr @createQueue()
  store ptr %25, ptr %6, align 8, !tbaa !5
  %26 = load ptr, ptr %6, align 8, !tbaa !5
  %27 = load ptr, ptr %5, align 8, !tbaa !5
  call void @enqueue(ptr noundef %26, ptr noundef %27)
  br label %28

28:                                               ; preds = %76, %19
  %29 = load ptr, ptr %6, align 8, !tbaa !5
  %30 = call i32 @isQueueEmpty(ptr noundef %29)
  %31 = icmp ne i32 %30, 0
  %32 = xor i1 %31, true
  br i1 %32, label %33, label %77

33:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #6
  %34 = load ptr, ptr %6, align 8, !tbaa !5
  %35 = call ptr @dequeue(ptr noundef %34)
  store ptr %35, ptr %7, align 8, !tbaa !5
  %36 = call ptr @strtok(ptr noundef null, ptr noundef @.str) #6
  store ptr %36, ptr %4, align 8, !tbaa !5
  %37 = load ptr, ptr %4, align 8, !tbaa !5
  %38 = icmp eq ptr %37, null
  br i1 %38, label %39, label %40

39:                                               ; preds = %33
  store i32 3, ptr %8, align 4
  br label %74

40:                                               ; preds = %33
  %41 = load ptr, ptr %4, align 8, !tbaa !5
  %42 = call i32 @strcmp(ptr noundef %41, ptr noundef @.str.1) #5
  %43 = icmp ne i32 %42, 0
  br i1 %43, label %44, label %54

44:                                               ; preds = %40
  %45 = load ptr, ptr %4, align 8, !tbaa !5
  %46 = call i32 @atoi(ptr noundef %45) #5
  %47 = call ptr @newNode(i32 noundef %46)
  %48 = load ptr, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds %struct.Node, ptr %48, i32 0, i32 1
  store ptr %47, ptr %49, align 8, !tbaa !10
  %50 = load ptr, ptr %6, align 8, !tbaa !5
  %51 = load ptr, ptr %7, align 8, !tbaa !5
  %52 = getelementptr inbounds %struct.Node, ptr %51, i32 0, i32 1
  %53 = load ptr, ptr %52, align 8, !tbaa !10
  call void @enqueue(ptr noundef %50, ptr noundef %53)
  br label %54

54:                                               ; preds = %44, %40
  %55 = call ptr @strtok(ptr noundef null, ptr noundef @.str) #6
  store ptr %55, ptr %4, align 8, !tbaa !5
  %56 = load ptr, ptr %4, align 8, !tbaa !5
  %57 = icmp eq ptr %56, null
  br i1 %57, label %58, label %59

58:                                               ; preds = %54
  store i32 3, ptr %8, align 4
  br label %74

59:                                               ; preds = %54
  %60 = load ptr, ptr %4, align 8, !tbaa !5
  %61 = call i32 @strcmp(ptr noundef %60, ptr noundef @.str.1) #5
  %62 = icmp ne i32 %61, 0
  br i1 %62, label %63, label %73

63:                                               ; preds = %59
  %64 = load ptr, ptr %4, align 8, !tbaa !5
  %65 = call i32 @atoi(ptr noundef %64) #5
  %66 = call ptr @newNode(i32 noundef %65)
  %67 = load ptr, ptr %7, align 8, !tbaa !5
  %68 = getelementptr inbounds %struct.Node, ptr %67, i32 0, i32 2
  store ptr %66, ptr %68, align 8, !tbaa !13
  %69 = load ptr, ptr %6, align 8, !tbaa !5
  %70 = load ptr, ptr %7, align 8, !tbaa !5
  %71 = getelementptr inbounds %struct.Node, ptr %70, i32 0, i32 2
  %72 = load ptr, ptr %71, align 8, !tbaa !13
  call void @enqueue(ptr noundef %69, ptr noundef %72)
  br label %73

73:                                               ; preds = %63, %59
  store i32 0, ptr %8, align 4
  br label %74

74:                                               ; preds = %73, %58, %39
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #6
  %75 = load i32, ptr %8, align 4
  switch i32 %75, label %82 [
    i32 0, label %76
    i32 3, label %77
  ]

76:                                               ; preds = %74
  br label %28, !llvm.loop !14

77:                                               ; preds = %74, %28
  %78 = load ptr, ptr %6, align 8, !tbaa !5
  call void @free(ptr noundef %78) #6
  %79 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %79, ptr %2, align 8
  store i32 1, ptr %8, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #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 %80

80:                                               ; preds = %77, %18
  %81 = load ptr, ptr %2, align 8
  ret ptr %81

82:                                               ; preds = %74
  unreachable
}

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

; Function Attrs: nounwind
declare ptr @strtok(ptr noundef, ptr noundef) #2

; Function Attrs: inlinehint nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr noundef nonnull) #4

; Function Attrs: nounwind willreturn memory(read)
declare i32 @strcmp(ptr noundef, 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 "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 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 #4 = { inlinehint nounwind willreturn memory(read) 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 #5 = { nounwind willreturn memory(read) }
attributes #6 = { 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 = !{!7, !7, i64 0}
!10 = !{!11, !6, i64 8}
!11 = !{!"Node", !12, i64 0, !6, i64 8, !6, i64 16}
!12 = !{!"int", !7, i64 0}
!13 = !{!11, !6, i64 16}
!14 = distinct !{!14, !15}
!15 = !{!"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"

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

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createQueue() local_unnamed_addr #0

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local ptr @buildTree(ptr noundef %0) local_unnamed_addr #0 {
  %char0 = load i8, ptr %0, align 1
  switch i8 %char0, label %2 [
    i8 0, label %28
    i8 78, label %28
  ]

2:                                                ; preds = %1
  %3 = tail call ptr @strtok(ptr noundef nonnull %0, ptr noundef nonnull @.str) #5
  %4 = tail call i32 @atoi(ptr nocapture noundef %3) #6
  %5 = tail call ptr @newNode(i32 noundef %4)
  %6 = tail call ptr @createQueue()
  tail call void @enqueue(ptr noundef %6, ptr noundef %5)
  %7 = tail call i32 @isQueueEmpty(ptr noundef %6)
  %.not30 = icmp eq i32 %7, 0
  br i1 %.not30, label %.lr.ph, label %.thread

.lr.ph:                                           ; preds = %2, %26
  %8 = tail call ptr @dequeue(ptr noundef %6)
  %9 = tail call ptr @strtok(ptr noundef null, ptr noundef nonnull @.str) #5
  %10 = icmp eq ptr %9, null
  br i1 %10, label %.thread, label %11

11:                                               ; preds = %.lr.ph
  %12 = tail call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %9, ptr noundef nonnull dereferenceable(1) @.str.1) #6
  %.not26 = icmp eq i32 %12, 0
  br i1 %.not26, label %17, label %13

13:                                               ; preds = %11
  %14 = tail call i32 @atoi(ptr nocapture noundef %9) #6
  %15 = tail call ptr @newNode(i32 noundef %14)
  %16 = getelementptr inbounds i8, ptr %8, i64 8
  store ptr %15, ptr %16, align 8, !tbaa !5
  tail call void @enqueue(ptr noundef %6, ptr noundef %15)
  br label %17

17:                                               ; preds = %13, %11
  %18 = tail call ptr @strtok(ptr noundef null, ptr noundef nonnull @.str) #5
  %19 = icmp eq ptr %18, null
  br i1 %19, label %.thread, label %20

20:                                               ; preds = %17
  %21 = tail call i32 @strcmp(ptr noundef nonnull dereferenceable(1) %18, ptr noundef nonnull dereferenceable(1) @.str.1) #6
  %.not27 = icmp eq i32 %21, 0
  br i1 %.not27, label %26, label %22

22:                                               ; preds = %20
  %23 = tail call i32 @atoi(ptr nocapture noundef %18) #6
  %24 = tail call ptr @newNode(i32 noundef %23)
  %25 = getelementptr inbounds i8, ptr %8, i64 16
  store ptr %24, ptr %25, align 8, !tbaa !11
  tail call void @enqueue(ptr noundef %6, ptr noundef %24)
  br label %26

26:                                               ; preds = %20, %22
  %27 = tail call i32 @isQueueEmpty(ptr noundef %6)
  %.not = icmp eq i32 %27, 0
  br i1 %.not, label %.lr.ph, label %.thread

.thread:                                          ; preds = %26, %.lr.ph, %17, %2
  tail call void @free(ptr noundef %6) #5
  br label %28

28:                                               ; preds = %1, %1, %.thread
  %.023 = phi ptr [ %5, %.thread ], [ null, %1 ], [ null, %1 ]
  ret ptr %.023
}

; Function Attrs: mustprogress nofree nounwind willreturn
declare ptr @strtok(ptr noundef, ptr nocapture noundef readonly) local_unnamed_addr #2

; Function Attrs: inlinehint mustprogress nofree nounwind willreturn memory(read) uwtable
declare i32 @atoi(ptr nocapture noundef nonnull) local_unnamed_addr #3

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

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 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 #2 = { mustprogress nofree nounwind willreturn "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 = { inlinehint mustprogress nofree nounwind willreturn memory(read) 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 #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 = { nounwind }
attributes #6 = { nounwind willreturn memory(read) }

!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 8}
!6 = !{!"Node", !7, i64 0, !10, i64 8, !10, i64 16}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !10, i64 16}


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