; LLVM IR from Algo-Phantoms/Algo-Tree :: Code/C++/binary_tree_from_preorder_and_inorder.ll (Z9BuildTreePcS_ii)

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

%class.TreeNode = type { i8, ptr, ptr }

@_ZZ9BuildTreePcS_iiE8preindex = external hidden global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6searchPciic(ptr noundef, i32 noundef, i32 noundef, i8 noundef signext) #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: mustprogress uwtable
define dso_local noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #2 {
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  store ptr %0, ptr %6, align 8, !tbaa !5
  store ptr %1, ptr %7, align 8, !tbaa !5
  store i32 %2, ptr %8, align 4, !tbaa !9
  store i32 %3, ptr %9, align 4, !tbaa !9
  %13 = load i32, ptr %8, align 4, !tbaa !9
  %14 = load i32, ptr %9, align 4, !tbaa !9
  %15 = icmp sgt i32 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %4
  store ptr null, ptr %5, align 8
  br label %63

17:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %18 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  call void @llvm.memset.p0.i64(ptr align 16 %18, i8 0, i64 24, i1 false)
  store ptr %18, ptr %10, align 8, !tbaa !5
  %19 = load ptr, ptr %7, align 8, !tbaa !5
  %20 = load i32, ptr @_ZZ9BuildTreePcS_iiE8preindex, align 4, !tbaa !9
  %21 = sext i32 %20 to i64
  %22 = getelementptr inbounds i8, ptr %19, i64 %21
  %23 = load i8, ptr %22, align 1, !tbaa !11
  %24 = load ptr, ptr %10, align 8, !tbaa !5
  %25 = getelementptr inbounds %class.TreeNode, ptr %24, i32 0, i32 0
  store i8 %23, ptr %25, align 8, !tbaa !12
  %26 = load ptr, ptr %10, align 8, !tbaa !5
  %27 = getelementptr inbounds %class.TreeNode, ptr %26, i32 0, i32 1
  store ptr null, ptr %27, align 8, !tbaa !14
  %28 = load ptr, ptr %10, align 8, !tbaa !5
  %29 = getelementptr inbounds %class.TreeNode, ptr %28, i32 0, i32 2
  store ptr null, ptr %29, align 8, !tbaa !15
  %30 = load i32, ptr @_ZZ9BuildTreePcS_iiE8preindex, align 4, !tbaa !9
  %31 = add nsw i32 %30, 1
  store i32 %31, ptr @_ZZ9BuildTreePcS_iiE8preindex, align 4, !tbaa !9
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = load i32, ptr %9, align 4, !tbaa !9
  %34 = icmp eq i32 %32, %33
  br i1 %34, label %35, label %37

35:                                               ; preds = %17
  %36 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %36, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %62

37:                                               ; preds = %17
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #5
  %38 = load ptr, ptr %6, align 8, !tbaa !5
  %39 = load i32, ptr %8, align 4, !tbaa !9
  %40 = load i32, ptr %9, align 4, !tbaa !9
  %41 = load ptr, ptr %10, align 8, !tbaa !5
  %42 = getelementptr inbounds %class.TreeNode, ptr %41, i32 0, i32 0
  %43 = load i8, ptr %42, align 8, !tbaa !12
  %44 = call noundef i32 @_Z6searchPciic(ptr noundef %38, i32 noundef %39, i32 noundef %40, i8 noundef signext %43)
  store i32 %44, ptr %12, align 4, !tbaa !9
  %45 = load ptr, ptr %6, align 8, !tbaa !5
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i32, ptr %8, align 4, !tbaa !9
  %48 = load i32, ptr %12, align 4, !tbaa !9
  %49 = sub nsw i32 %48, 1
  %50 = call noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %45, ptr noundef %46, i32 noundef %47, i32 noundef %49)
  %51 = load ptr, ptr %10, align 8, !tbaa !5
  %52 = getelementptr inbounds %class.TreeNode, ptr %51, i32 0, i32 1
  store ptr %50, ptr %52, align 8, !tbaa !14
  %53 = load ptr, ptr %6, align 8, !tbaa !5
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  %55 = load i32, ptr %12, align 4, !tbaa !9
  %56 = add nsw i32 %55, 1
  %57 = load i32, ptr %9, align 4, !tbaa !9
  %58 = call noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %53, ptr noundef %54, i32 noundef %56, i32 noundef %57)
  %59 = load ptr, ptr %10, align 8, !tbaa !5
  %60 = getelementptr inbounds %class.TreeNode, ptr %59, i32 0, i32 2
  store ptr %58, ptr %60, align 8, !tbaa !15
  %61 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %61, ptr %5, align 8
  store i32 1, ptr %11, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #5
  br label %62

62:                                               ; preds = %37, %35
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  br label %63

63:                                               ; preds = %62, %16
  %64 = load ptr, ptr %5, align 8
  ret ptr %64
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) #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 = { mustprogress 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 = { 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 #3 = { nobuiltin 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 #4 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }

!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 = !{!7, !7, i64 0}
!12 = !{!13, !7, i64 0}
!13 = !{!"_ZTS8TreeNode", !7, i64 0, !6, i64 8, !6, i64 16}
!14 = !{!13, !6, i64 8}
!15 = !{!13, !6, i64 16}


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

@_ZZ9BuildTreePcS_iiE8preindex = external hidden local_unnamed_addr global i32, align 4

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_Z6searchPciic(ptr noundef, i32 noundef, i32 noundef, i8 noundef signext) local_unnamed_addr #0

; Function Attrs: mustprogress uwtable
define dso_local noalias noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) local_unnamed_addr #1 {
  %5 = icmp sgt i32 %2, %3
  br i1 %5, label %common.ret25, label %6

6:                                                ; preds = %4
  %7 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #4
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 16 dereferenceable(24) %7, i8 0, i64 16, i1 false)
  %8 = load i32, ptr @_ZZ9BuildTreePcS_iiE8preindex, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds i8, ptr %1, i64 %9
  %11 = load i8, ptr %10, align 1, !tbaa !9
  store i8 %11, ptr %7, align 8, !tbaa !10
  %12 = getelementptr inbounds i8, ptr %7, i64 8
  %13 = add nsw i32 %8, 1
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %12, i8 0, i64 16, i1 false)
  store i32 %13, ptr @_ZZ9BuildTreePcS_iiE8preindex, align 4, !tbaa !5
  %14 = icmp eq i32 %2, %3
  br i1 %14, label %common.ret25, label %15

common.ret25:                                     ; preds = %6, %4, %15
  %common.ret25.op = phi ptr [ %7, %15 ], [ null, %4 ], [ %7, %6 ]
  ret ptr %common.ret25.op

15:                                               ; preds = %6
  %16 = getelementptr inbounds i8, ptr %7, i64 16
  %17 = tail call noundef i32 @_Z6searchPciic(ptr noundef %0, i32 noundef %2, i32 noundef %3, i8 noundef signext %11)
  %18 = add nsw i32 %17, -1
  %19 = tail call noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %2, i32 noundef %18)
  store ptr %19, ptr %12, align 8, !tbaa !13
  %20 = add nsw i32 %17, 1
  %21 = tail call noundef ptr @_Z9BuildTreePcS_ii(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %20, i32 noundef %3)
  store ptr %21, ptr %16, align 8, !tbaa !14
  br label %common.ret25
}

; Function Attrs: nobuiltin allocsize(0)
declare noundef nonnull ptr @_Znwm(i64 noundef) local_unnamed_addr #2

; Function Attrs: mustprogress nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #3

attributes #0 = { mustprogress 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 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 #2 = { nobuiltin 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 #3 = { mustprogress nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #4 = { builtin allocsize(0) }

!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++ TBAA"}
!9 = !{!7, !7, i64 0}
!10 = !{!11, !7, i64 0}
!11 = !{!"_ZTS8TreeNode", !7, i64 0, !12, i64 8, !12, i64 16}
!12 = !{!"any pointer", !7, i64 0}
!13 = !{!11, !12, i64 8}
!14 = !{!11, !12, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,DSEPass,EarlyCSEPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,MemCpyOptPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass