; LLVM IR from sbshah97/DS-Algo-Handbook :: Data-Structures/Trees/Binary_Trees (BinaryTree)

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

@ar = external dso_local global [20 x i32], align 16
@count = external dso_local global i32, align 4
@.str.1 = external hidden unnamed_addr constant [210 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1

; Function Attrs: nounwind uwtable
declare dso_local void @inorder(i32 noundef) #0

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

declare i32 @printf(ptr 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 uwtable
define dso_local i32 @main() #0 {
  %1 = alloca i32, align 4
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr %2) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #4
  store i32 0, ptr @count, align 4, !tbaa !5
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %5

5:                                                ; preds = %12, %0
  %6 = load i32, ptr %2, align 4, !tbaa !5
  %7 = icmp slt i32 %6, 20
  br i1 %7, label %8, label %15

8:                                                ; preds = %5
  %9 = load i32, ptr %2, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %10
  store i32 0, ptr %11, align 4, !tbaa !5
  br label %12

12:                                               ; preds = %8
  %13 = load i32, ptr %2, align 4, !tbaa !5
  %14 = add nsw i32 %13, 1
  store i32 %14, ptr %2, align 4, !tbaa !5
  br label %5, !llvm.loop !9

15:                                               ; preds = %5
  br label %16

16:                                               ; preds = %95, %15
  %17 = call i32 (ptr, ...) @printf(ptr noundef @.str.1)
  %18 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %4)
  %19 = load i32, ptr %4, align 4, !tbaa !5
  switch i32 %19, label %94 [
    i32 1, label %20
    i32 2, label %29
    i32 4, label %61
    i32 3, label %90
    i32 5, label %92
  ]

20:                                               ; preds = %16
  %21 = call i32 (ptr, ...) @printf(ptr noundef @.str.3)
  %22 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %3)
  %23 = load i32, ptr %3, align 4, !tbaa !5
  %24 = load i32, ptr @count, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %25
  store i32 %23, ptr %26, align 4, !tbaa !5
  %27 = load i32, ptr @count, align 4, !tbaa !5
  %28 = add nsw i32 %27, 1
  store i32 %28, ptr @count, align 4, !tbaa !5
  br label %94

29:                                               ; preds = %16
  %30 = call i32 (ptr, ...) @printf(ptr noundef @.str.4)
  %31 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %3)
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %32

32:                                               ; preds = %57, %29
  %33 = load i32, ptr %2, align 4, !tbaa !5
  %34 = load i32, ptr @count, align 4, !tbaa !5
  %35 = icmp slt i32 %33, %34
  br i1 %35, label %36, label %60

36:                                               ; preds = %32
  %37 = load i32, ptr %2, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %38
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = icmp eq i32 %40, %41
  br i1 %42, label %43, label %56

43:                                               ; preds = %36
  %44 = load i32, ptr @count, align 4, !tbaa !5
  %45 = add nsw i32 %44, -1
  store i32 %45, ptr @count, align 4, !tbaa !5
  %46 = load i32, ptr @count, align 4, !tbaa !5
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = load i32, ptr %2, align 4, !tbaa !5
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %51
  store i32 %49, ptr %52, align 4, !tbaa !5
  %53 = load i32, ptr @count, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %54
  store i32 0, ptr %55, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %43, %36
  br label %57

57:                                               ; preds = %56
  %58 = load i32, ptr %2, align 4, !tbaa !5
  %59 = add nsw i32 %58, 1
  store i32 %59, ptr %2, align 4, !tbaa !5
  br label %32, !llvm.loop !11

60:                                               ; preds = %32
  br label %94

61:                                               ; preds = %16
  %62 = call i32 (ptr, ...) @printf(ptr noundef @.str.5)
  %63 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef @.str.2, ptr noundef %3)
  store i32 0, ptr %2, align 4, !tbaa !5
  br label %64

64:                                               ; preds = %80, %61
  %65 = load i32, ptr %2, align 4, !tbaa !5
  %66 = load i32, ptr @count, align 4, !tbaa !5
  %67 = icmp slt i32 %65, %66
  br i1 %67, label %68, label %83

68:                                               ; preds = %64
  %69 = load i32, ptr %2, align 4, !tbaa !5
  %70 = sext i32 %69 to i64
  %71 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %70
  %72 = load i32, ptr %71, align 4, !tbaa !5
  %73 = load i32, ptr %3, align 4, !tbaa !5
  %74 = icmp eq i32 %72, %73
  br i1 %74, label %75, label %79

75:                                               ; preds = %68
  %76 = load i32, ptr @count, align 4, !tbaa !5
  %77 = sub nsw i32 %76, 1
  %78 = call i32 (ptr, ...) @printf(ptr noundef @.str.6, i32 noundef %77)
  br label %83

79:                                               ; preds = %68
  br label %80

80:                                               ; preds = %79
  %81 = load i32, ptr %2, align 4, !tbaa !5
  %82 = add nsw i32 %81, 1
  store i32 %82, ptr %2, align 4, !tbaa !5
  br label %64, !llvm.loop !12

83:                                               ; preds = %75, %64
  %84 = load i32, ptr %2, align 4, !tbaa !5
  %85 = load i32, ptr @count, align 4, !tbaa !5
  %86 = icmp eq i32 %84, %85
  br i1 %86, label %87, label %89

87:                                               ; preds = %83
  %88 = call i32 (ptr, ...) @printf(ptr noundef @.str.7)
  br label %89

89:                                               ; preds = %87, %83
  br label %94

90:                                               ; preds = %16
  call void @inorder(i32 noundef 0)
  %91 = call i32 (ptr, ...) @printf(ptr noundef @.str.8)
  br label %94

92:                                               ; preds = %16
  %93 = call i32 (ptr, ...) @printf(ptr noundef @.str.9)
  call void @exit(i32 noundef 0) #5
  unreachable

94:                                               ; preds = %90, %89, %60, %20, %16
  br label %95

95:                                               ; preds = %94
  %96 = load i32, ptr %4, align 4, !tbaa !5
  %97 = icmp ne i32 %96, 5
  br i1 %97, label %16, label %98, !llvm.loop !13

98:                                               ; preds = %95
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #4
  call void @llvm.lifetime.end.p0(i64 4, ptr %2) #4
  ret i32 0
}

declare i32 @__isoc99_scanf(ptr noundef, ...) #2

; Function Attrs: noreturn nounwind
declare void @exit(i32 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 = { "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 = { noreturn 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 = { noreturn 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}
!13 = distinct !{!13, !10}


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

@ar = external dso_local local_unnamed_addr global [20 x i32], align 16
@count = external dso_local local_unnamed_addr global i32, align 4
@.str.1 = external hidden unnamed_addr constant [210 x i8], align 1
@.str.2 = external hidden unnamed_addr constant [3 x i8], align 1
@.str.3 = external hidden unnamed_addr constant [35 x i8], align 1
@.str.4 = external hidden unnamed_addr constant [34 x i8], align 1
@.str.5 = external hidden unnamed_addr constant [32 x i8], align 1
@.str.6 = external hidden unnamed_addr constant [31 x i8], align 1
@.str.7 = external hidden unnamed_addr constant [25 x i8], align 1
@.str.8 = external hidden unnamed_addr constant [2 x i8], align 1
@.str.9 = external hidden unnamed_addr constant [11 x i8], align 1

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

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

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

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

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #0 {
.preheader.preheader:
  %0 = alloca i32, align 4
  %1 = alloca i32, align 4
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %0) #5
  call void @llvm.lifetime.start.p0(i64 4, ptr nonnull %1) #5
  store i32 0, ptr @count, align 4, !tbaa !5
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(80) @ar, i8 0, i64 80, i1 false), !tbaa !5
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %53
  %2 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.1)
  %3 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %1)
  %4 = load i32, ptr %1, align 4, !tbaa !5
  switch i32 %4, label %53 [
    i32 1, label %5
    i32 2, label %13
    i32 4, label %31
    i32 3, label %49
    i32 5, label %51
  ]

5:                                                ; preds = %.preheader
  %6 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.3)
  %7 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %0)
  %8 = load i32, ptr %0, align 4, !tbaa !5
  %9 = load i32, ptr @count, align 4, !tbaa !5
  %10 = sext i32 %9 to i64
  %11 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %10
  store i32 %8, ptr %11, align 4, !tbaa !5
  %12 = add nsw i32 %9, 1
  store i32 %12, ptr @count, align 4, !tbaa !5
  br label %thread-pre-split

13:                                               ; preds = %.preheader
  %14 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.4)
  %15 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %0)
  %count.promoted = load i32, ptr @count, align 4, !tbaa !5
  %16 = icmp sgt i32 %count.promoted, 0
  br i1 %16, label %.lr.ph19, label %thread-pre-split

.lr.ph19:                                         ; preds = %13
  %17 = load i32, ptr %0, align 4, !tbaa !5
  br label %18

18:                                               ; preds = %.lr.ph19, %28
  %indvars.iv23 = phi i64 [ 0, %.lr.ph19 ], [ %indvars.iv.next24, %28 ]
  %19 = phi i32 [ %count.promoted, %.lr.ph19 ], [ %29, %28 ]
  %20 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %indvars.iv23
  %21 = load i32, ptr %20, align 4, !tbaa !5
  %22 = icmp eq i32 %21, %17
  br i1 %22, label %23, label %._crit_edge

._crit_edge:                                      ; preds = %18
  %.pre26 = sext i32 %19 to i64
  br label %28

23:                                               ; preds = %18
  %24 = add nsw i32 %19, -1
  store i32 %24, ptr @count, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  store i32 %27, ptr %20, align 4, !tbaa !5
  store i32 0, ptr %26, align 4, !tbaa !5
  br label %28

28:                                               ; preds = %._crit_edge, %23
  %.pre-phi = phi i64 [ %.pre26, %._crit_edge ], [ %25, %23 ]
  %29 = phi i32 [ %19, %._crit_edge ], [ %24, %23 ]
  %indvars.iv.next24 = add nuw nsw i64 %indvars.iv23, 1
  %30 = icmp slt i64 %indvars.iv.next24, %.pre-phi
  br i1 %30, label %18, label %thread-pre-split, !llvm.loop !9

31:                                               ; preds = %.preheader
  %32 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.5)
  %33 = call i32 (ptr, ...) @__isoc99_scanf(ptr noundef nonnull @.str.2, ptr noundef nonnull %0)
  %34 = load i32, ptr @count, align 4, !tbaa !5
  %35 = icmp sgt i32 %34, 0
  br i1 %35, label %.lr.ph, label %.loopexit

.lr.ph:                                           ; preds = %31
  %36 = load i32, ptr %0, align 4, !tbaa !5
  %37 = add nsw i32 %34, -1
  %wide.trip.count = zext nneg i32 %34 to i64
  br label %38

38:                                               ; preds = %.lr.ph, %45
  %indvars.iv = phi i64 [ 0, %.lr.ph ], [ %indvars.iv.next, %45 ]
  %39 = getelementptr inbounds [20 x i32], ptr @ar, i64 0, i64 %indvars.iv
  %40 = load i32, ptr %39, align 4, !tbaa !5
  %41 = icmp eq i32 %40, %36
  br i1 %41, label %42, label %45

42:                                               ; preds = %38
  %43 = trunc nuw nsw i64 %indvars.iv to i32
  %44 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.6, i32 noundef %37)
  %.pre = load i32, ptr @count, align 4, !tbaa !5
  br label %.loopexit

45:                                               ; preds = %38
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %.loopexit.thread, label %38, !llvm.loop !11

.loopexit:                                        ; preds = %31, %42
  %46 = phi i32 [ %.pre, %42 ], [ %34, %31 ]
  %.215 = phi i32 [ %43, %42 ], [ 0, %31 ]
  %47 = icmp eq i32 %.215, %46
  br i1 %47, label %.loopexit.thread, label %thread-pre-split

.loopexit.thread:                                 ; preds = %45, %.loopexit
  %48 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.7)
  br label %thread-pre-split

49:                                               ; preds = %.preheader
  call void @inorder(i32 noundef 0)
  %50 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.8)
  br label %thread-pre-split

51:                                               ; preds = %.preheader
  %52 = call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str.9)
  call void @exit(i32 noundef 0) #6
  unreachable

thread-pre-split:                                 ; preds = %28, %13, %.loopexit, %.loopexit.thread, %49, %5
  %.pr = load i32, ptr %1, align 4, !tbaa !5
  br label %53

53:                                               ; preds = %thread-pre-split, %.preheader
  %54 = phi i32 [ %.pr, %thread-pre-split ], [ %4, %.preheader ]
  %.not = icmp eq i32 %54, 5
  br i1 %.not, label %55, label %.preheader, !llvm.loop !12

55:                                               ; preds = %53
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %1) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr nonnull %0) #5
  ret i32 0
}

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

; Function Attrs: noreturn nounwind
declare void @exit(i32 noundef) local_unnamed_addr #3

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { 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 #3 = { noreturn 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 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { noreturn 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = distinct !{!11, !10}
!12 = distinct !{!12, !10}


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