; LLVM IR from codemistic/Data-Structures-and-Algorithms :: CPP/Trees/Expression_tree (BST)

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

; Function Attrs: nounwind uwtable
declare dso_local ptr @createEXTNode(i8 noundef signext, ptr noundef, ptr 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 i32 @is_operator(i8 noundef signext) #0

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

; Function Attrs: nounwind uwtable
define dso_local void @initTree(ptr noundef %0, ptr noundef %1, i32 noundef %2) #0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  store ptr %0, ptr %4, align 8, !tbaa !5
  store ptr %1, ptr %5, align 8, !tbaa !5
  store i32 %2, ptr %6, align 4, !tbaa !9
  %12 = load ptr, ptr %5, align 8, !tbaa !5
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = call ptr @convertToPostFix(ptr noundef %12, i32 noundef %13)
  store ptr %14, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #3
  store i32 0, ptr %7, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #3
  call void @initStack(ptr noundef %8)
  br label %15

15:                                               ; preds = %53, %3
  %16 = load ptr, ptr %5, align 8, !tbaa !5
  %17 = load i32, ptr %7, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds i8, ptr %16, i64 %18
  %20 = load i8, ptr %19, align 1, !tbaa !11
  %21 = sext i8 %20 to i32
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %23, label %56

23:                                               ; preds = %15
  %24 = load ptr, ptr %5, align 8, !tbaa !5
  %25 = load i32, ptr %7, align 4, !tbaa !9
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds i8, ptr %24, i64 %26
  %28 = load i8, ptr %27, align 1, !tbaa !11
  %29 = call i32 @is_operator(i8 noundef signext %28)
  %30 = icmp ne i32 %29, 0
  br i1 %30, label %38, label %31

31:                                               ; preds = %23
  %32 = load ptr, ptr %5, align 8, !tbaa !5
  %33 = load i32, ptr %7, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds i8, ptr %32, i64 %34
  %36 = load i8, ptr %35, align 1, !tbaa !11
  %37 = call ptr @createEXTNode(i8 noundef signext %36, ptr noundef null, ptr noundef null)
  call void @push(ptr noundef %8, ptr noundef %37)
  br label %53

38:                                               ; preds = %23
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %39 = load ptr, ptr %8, align 8, !tbaa !5
  %40 = call ptr @peek(ptr noundef %39)
  store ptr %40, ptr %9, align 8, !tbaa !5
  %41 = call i32 @pop(ptr noundef %8)
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #3
  %42 = load ptr, ptr %8, align 8, !tbaa !5
  %43 = call ptr @peek(ptr noundef %42)
  store ptr %43, ptr %10, align 8, !tbaa !5
  %44 = call i32 @pop(ptr noundef %8)
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i32, ptr %7, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i8, ptr %45, i64 %47
  %49 = load i8, ptr %48, align 1, !tbaa !11
  %50 = load ptr, ptr %10, align 8, !tbaa !5
  %51 = load ptr, ptr %9, align 8, !tbaa !5
  %52 = call ptr @createEXTNode(i8 noundef signext %49, ptr noundef %50, ptr noundef %51)
  call void @push(ptr noundef %8, ptr noundef %52)
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  br label %53

53:                                               ; preds = %38, %31
  %54 = load i32, ptr %7, align 4, !tbaa !9
  %55 = add nsw i32 %54, 1
  store i32 %55, ptr %7, align 4, !tbaa !9
  br label %15, !llvm.loop !12

56:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 8, ptr %11) #3
  %57 = load ptr, ptr %8, align 8, !tbaa !5
  %58 = call ptr @peek(ptr noundef %57)
  store ptr %58, ptr %11, align 8, !tbaa !5
  %59 = call i32 @pop(ptr noundef %8)
  %60 = load ptr, ptr %11, align 8, !tbaa !5
  %61 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %60, ptr %61, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %11) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #3
  ret void
}

declare void @initStack(ptr noundef) #2

declare void @push(ptr noundef, ptr noundef) #2

declare ptr @peek(ptr noundef) #2

declare i32 @pop(ptr noundef) #2

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 = { 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 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"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"

; Function Attrs: nounwind uwtable
declare dso_local ptr @createEXTNode(i8 noundef signext, ptr noundef, ptr 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: mustprogress 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 i32 @is_operator(i8 noundef signext) local_unnamed_addr #0

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

; Function Attrs: nounwind uwtable
define dso_local void @initTree(ptr nocapture noundef writeonly %0, ptr noundef %1, i32 noundef %2) local_unnamed_addr #0 {
  %4 = alloca ptr, align 8
  %5 = tail call ptr @convertToPostFix(ptr noundef %1, i32 noundef %2)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  call void @initStack(ptr noundef nonnull %4) #3
  %6 = load i8, ptr %5, align 1, !tbaa !5
  %.not15 = icmp eq i8 %6, 0
  br i1 %.not15, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %3, %22
  %indvars.iv = phi i64 [ %indvars.iv.next, %22 ], [ 0, %3 ]
  %7 = phi i8 [ %24, %22 ], [ %6, %3 ]
  %8 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv
  %9 = call i32 @is_operator(i8 noundef signext %7)
  %.not14 = icmp eq i32 %9, 0
  br i1 %.not14, label %10, label %13

10:                                               ; preds = %.lr.ph
  %11 = load i8, ptr %8, align 1, !tbaa !5
  %12 = call ptr @createEXTNode(i8 noundef signext %11, ptr noundef null, ptr noundef null)
  br label %22

13:                                               ; preds = %.lr.ph
  %14 = load ptr, ptr %4, align 8, !tbaa !8
  %15 = call ptr @peek(ptr noundef %14) #3
  %16 = call i32 @pop(ptr noundef nonnull %4) #3
  %17 = load ptr, ptr %4, align 8, !tbaa !8
  %18 = call ptr @peek(ptr noundef %17) #3
  %19 = call i32 @pop(ptr noundef nonnull %4) #3
  %20 = load i8, ptr %8, align 1, !tbaa !5
  %21 = call ptr @createEXTNode(i8 noundef signext %20, ptr noundef %18, ptr noundef %15)
  br label %22

22:                                               ; preds = %13, %10
  %.sink = phi ptr [ %21, %13 ], [ %12, %10 ]
  call void @push(ptr noundef nonnull %4, ptr noundef %.sink) #3
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %23 = getelementptr inbounds i8, ptr %5, i64 %indvars.iv.next
  %24 = load i8, ptr %23, align 1, !tbaa !5
  %.not = icmp eq i8 %24, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !10

._crit_edge:                                      ; preds = %22, %3
  %25 = load ptr, ptr %4, align 8, !tbaa !8
  %26 = call ptr @peek(ptr noundef %25) #3
  %27 = call i32 @pop(ptr noundef nonnull %4) #3
  store ptr %26, ptr %0, align 8, !tbaa !8
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  ret void
}

declare void @initStack(ptr noundef) local_unnamed_addr #2

declare void @push(ptr noundef, ptr noundef) local_unnamed_addr #2

declare ptr @peek(ptr noundef) local_unnamed_addr #2

declare i32 @pop(ptr noundef) local_unnamed_addr #2

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 = { "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 }

!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 = !{!9, !9, i64 0}
!9 = !{!"any pointer", !6, i64 0}
!10 = distinct !{!10, !11}
!11 = !{!"llvm.loop.mustprogress"}


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