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

; 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.treenode = type { i32, i32, i32, ptr, ptr }

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z9insertionP8treenodei(ptr noundef %0, i32 noundef %1) #0 {
  %3 = alloca ptr, align 8
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca ptr, align 8
  %7 = alloca ptr, align 8
  store ptr %0, ptr %3, align 8, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  %8 = load ptr, ptr %3, align 8, !tbaa !5
  store ptr %8, ptr %5, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #3
  store ptr null, ptr %6, align 8, !tbaa !5
  br label %9

9:                                                ; preds = %41, %2
  %10 = load ptr, ptr %5, align 8, !tbaa !5
  %11 = icmp ne ptr %10, null
  br i1 %11, label %12, label %42

12:                                               ; preds = %9
  %13 = load ptr, ptr %5, align 8, !tbaa !5
  store ptr %13, ptr %6, align 8, !tbaa !5
  %14 = load i32, ptr %4, align 4, !tbaa !9
  %15 = load ptr, ptr %5, align 8, !tbaa !5
  %16 = getelementptr inbounds %struct.treenode, ptr %15, i32 0, i32 0
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = icmp slt i32 %14, %17
  br i1 %18, label %19, label %30

19:                                               ; preds = %12
  %20 = load ptr, ptr %5, align 8, !tbaa !5
  %21 = getelementptr inbounds %struct.treenode, ptr %20, i32 0, i32 1
  %22 = load i32, ptr %21, align 4, !tbaa !13
  %23 = icmp eq i32 %22, 0
  br i1 %23, label %24, label %28

24:                                               ; preds = %19
  %25 = load ptr, ptr %5, align 8, !tbaa !5
  %26 = getelementptr inbounds %struct.treenode, ptr %25, i32 0, i32 3
  %27 = load ptr, ptr %26, align 8, !tbaa !14
  store ptr %27, ptr %5, align 8, !tbaa !5
  br label %29

28:                                               ; preds = %19
  br label %42

29:                                               ; preds = %24
  br label %41

30:                                               ; preds = %12
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = getelementptr inbounds %struct.treenode, ptr %31, i32 0, i32 2
  %33 = load i32, ptr %32, align 8, !tbaa !15
  %34 = icmp eq i32 %33, 0
  br i1 %34, label %35, label %39

35:                                               ; preds = %30
  %36 = load ptr, ptr %5, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.treenode, ptr %36, i32 0, i32 4
  %38 = load ptr, ptr %37, align 8, !tbaa !16
  store ptr %38, ptr %5, align 8, !tbaa !5
  br label %40

39:                                               ; preds = %30
  br label %42

40:                                               ; preds = %35
  br label %41

41:                                               ; preds = %40, %29
  br label %9, !llvm.loop !17

42:                                               ; preds = %39, %28, %9
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #3
  %43 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 32) #4
  store ptr %43, ptr %7, align 8, !tbaa !5
  %44 = load i32, ptr %4, align 4, !tbaa !9
  %45 = load ptr, ptr %7, align 8, !tbaa !5
  %46 = getelementptr inbounds %struct.treenode, ptr %45, i32 0, i32 0
  store i32 %44, ptr %46, align 8, !tbaa !11
  %47 = load ptr, ptr %7, align 8, !tbaa !5
  %48 = getelementptr inbounds %struct.treenode, ptr %47, i32 0, i32 1
  store i32 1, ptr %48, align 4, !tbaa !13
  %49 = load ptr, ptr %7, align 8, !tbaa !5
  %50 = getelementptr inbounds %struct.treenode, ptr %49, i32 0, i32 2
  store i32 1, ptr %50, align 8, !tbaa !15
  %51 = load ptr, ptr %6, align 8, !tbaa !5
  %52 = icmp eq ptr %51, null
  br i1 %52, label %53, label %59

53:                                               ; preds = %42
  %54 = load ptr, ptr %7, align 8, !tbaa !5
  store ptr %54, ptr %3, align 8, !tbaa !5
  %55 = load ptr, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds %struct.treenode, ptr %55, i32 0, i32 3
  store ptr null, ptr %56, align 8, !tbaa !14
  %57 = load ptr, ptr %7, align 8, !tbaa !5
  %58 = getelementptr inbounds %struct.treenode, ptr %57, i32 0, i32 4
  store ptr null, ptr %58, align 8, !tbaa !16
  br label %94

59:                                               ; preds = %42
  %60 = load i32, ptr %4, align 4, !tbaa !9
  %61 = load ptr, ptr %6, align 8, !tbaa !5
  %62 = getelementptr inbounds %struct.treenode, ptr %61, i32 0, i32 0
  %63 = load i32, ptr %62, align 8, !tbaa !11
  %64 = icmp slt i32 %60, %63
  br i1 %64, label %65, label %79

65:                                               ; preds = %59
  %66 = load ptr, ptr %6, align 8, !tbaa !5
  %67 = getelementptr inbounds %struct.treenode, ptr %66, i32 0, i32 3
  %68 = load ptr, ptr %67, align 8, !tbaa !14
  %69 = load ptr, ptr %7, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.treenode, ptr %69, i32 0, i32 3
  store ptr %68, ptr %70, align 8, !tbaa !14
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = getelementptr inbounds %struct.treenode, ptr %72, i32 0, i32 4
  store ptr %71, ptr %73, align 8, !tbaa !16
  %74 = load ptr, ptr %6, align 8, !tbaa !5
  %75 = getelementptr inbounds %struct.treenode, ptr %74, i32 0, i32 1
  store i32 0, ptr %75, align 4, !tbaa !13
  %76 = load ptr, ptr %7, align 8, !tbaa !5
  %77 = load ptr, ptr %6, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.treenode, ptr %77, i32 0, i32 3
  store ptr %76, ptr %78, align 8, !tbaa !14
  br label %93

79:                                               ; preds = %59
  %80 = load ptr, ptr %6, align 8, !tbaa !5
  %81 = load ptr, ptr %7, align 8, !tbaa !5
  %82 = getelementptr inbounds %struct.treenode, ptr %81, i32 0, i32 3
  store ptr %80, ptr %82, align 8, !tbaa !14
  %83 = load ptr, ptr %6, align 8, !tbaa !5
  %84 = getelementptr inbounds %struct.treenode, ptr %83, i32 0, i32 4
  %85 = load ptr, ptr %84, align 8, !tbaa !16
  %86 = load ptr, ptr %7, align 8, !tbaa !5
  %87 = getelementptr inbounds %struct.treenode, ptr %86, i32 0, i32 4
  store ptr %85, ptr %87, align 8, !tbaa !16
  %88 = load ptr, ptr %6, align 8, !tbaa !5
  %89 = getelementptr inbounds %struct.treenode, ptr %88, i32 0, i32 2
  store i32 0, ptr %89, align 8, !tbaa !15
  %90 = load ptr, ptr %7, align 8, !tbaa !5
  %91 = load ptr, ptr %6, align 8, !tbaa !5
  %92 = getelementptr inbounds %struct.treenode, ptr %91, i32 0, i32 4
  store ptr %90, ptr %92, align 8, !tbaa !16
  br label %93

93:                                               ; preds = %79, %65
  br label %94

94:                                               ; preds = %93, %53
  %95 = load ptr, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #3
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret ptr %95
}

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

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

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

attributes #0 = { 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 #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { nounwind }
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 = !{!"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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS8treenode", !10, i64 0, !10, i64 4, !10, i64 8, !6, i64 16, !6, i64 24}
!13 = !{!12, !10, i64 4}
!14 = !{!12, !6, i64 16}
!15 = !{!12, !10, i64 8}
!16 = !{!12, !6, i64 24}
!17 = distinct !{!17, !18}
!18 = !{!"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: mustprogress uwtable
define dso_local noundef ptr @_Z9insertionP8treenodei(ptr noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %.not36 = icmp eq ptr %0, null
  br i1 %.not36, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2, %13
  %.03437 = phi ptr [ %.135, %13 ], [ %0, %2 ]
  %3 = load i32, ptr %.03437, align 8, !tbaa !5
  %4 = icmp sgt i32 %3, %1
  br i1 %4, label %5, label %9

5:                                                ; preds = %.lr.ph
  %6 = getelementptr inbounds i8, ptr %.03437, i64 4
  %7 = load i32, ptr %6, align 4, !tbaa !11
  %8 = icmp eq i32 %7, 0
  br i1 %8, label %13, label %._crit_edge

9:                                                ; preds = %.lr.ph
  %10 = getelementptr inbounds i8, ptr %.03437, i64 8
  %11 = load i32, ptr %10, align 8, !tbaa !12
  %12 = icmp eq i32 %11, 0
  br i1 %12, label %13, label %._crit_edge

13:                                               ; preds = %9, %5
  %.sink = phi i64 [ 16, %5 ], [ 24, %9 ]
  %14 = getelementptr inbounds i8, ptr %.03437, i64 %.sink
  %.135 = load ptr, ptr %14, align 8, !tbaa !13
  %.not = icmp eq ptr %.135, null
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %13, %5, %9, %2
  %.1 = phi ptr [ null, %2 ], [ %.03437, %9 ], [ %.03437, %5 ], [ %.03437, %13 ]
  %15 = tail call noalias noundef nonnull dereferenceable(32) ptr @_Znwm(i64 noundef 32) #3
  store i32 %1, ptr %15, align 8, !tbaa !5
  %16 = getelementptr inbounds i8, ptr %15, i64 4
  store i32 1, ptr %16, align 4, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %15, i64 8
  store i32 1, ptr %17, align 8, !tbaa !12
  %18 = icmp eq ptr %.1, null
  br i1 %18, label %19, label %21

19:                                               ; preds = %._crit_edge
  %20 = getelementptr inbounds i8, ptr %15, i64 16
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %20, i8 0, i64 16, i1 false)
  br label %35

21:                                               ; preds = %._crit_edge
  %22 = load i32, ptr %.1, align 8, !tbaa !5
  %23 = icmp sgt i32 %22, %1
  %24 = getelementptr inbounds i8, ptr %15, i64 24
  br i1 %23, label %25, label %30

25:                                               ; preds = %21
  %26 = getelementptr inbounds i8, ptr %.1, i64 16
  %27 = load ptr, ptr %26, align 8, !tbaa !16
  %28 = getelementptr inbounds i8, ptr %15, i64 16
  store ptr %27, ptr %28, align 8, !tbaa !16
  store ptr %.1, ptr %24, align 8, !tbaa !17
  %29 = getelementptr inbounds i8, ptr %.1, i64 4
  store i32 0, ptr %29, align 4, !tbaa !11
  store ptr %15, ptr %26, align 8, !tbaa !16
  br label %35

30:                                               ; preds = %21
  %31 = getelementptr inbounds i8, ptr %15, i64 16
  store ptr %.1, ptr %31, align 8, !tbaa !16
  %32 = getelementptr inbounds i8, ptr %.1, i64 24
  %33 = load ptr, ptr %32, align 8, !tbaa !17
  store ptr %33, ptr %24, align 8, !tbaa !17
  %34 = getelementptr inbounds i8, ptr %.1, i64 8
  store i32 0, ptr %34, align 8, !tbaa !12
  store ptr %15, ptr %32, align 8, !tbaa !17
  br label %35

35:                                               ; preds = %25, %30, %19
  %.0 = phi ptr [ %15, %19 ], [ %0, %25 ], [ %0, %30 ]
  ret ptr %.0
}

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

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

attributes #0 = { 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 #1 = { 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 #2 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #3 = { 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, !7, i64 0}
!6 = !{!"_ZTS8treenode", !7, i64 0, !7, i64 4, !7, i64 8, !10, i64 16, !10, i64 24}
!7 = !{!"int", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"any pointer", !8, i64 0}
!11 = !{!6, !7, i64 4}
!12 = !{!6, !7, i64 8}
!13 = !{!10, !10, i64 0}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = !{!6, !10, i64 16}
!17 = !{!6, !10, i64 24}


; Effective O3 passes: EarlyCSEPass,GVNPass,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,JumpThreadingPass,LCSSAPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,MemCpyOptPass,SROAPass,SimplifyCFGPass,TailCallElimPass