; LLVM IR from next-step-for-interview-prep/Coding-Ninjas-DSA-course_code_and_notes_cpp :: 1/0000000000000359/BinaryTreeUse.ll (Z15buildTreeHelperPiS_iiii)

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

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

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN14BinaryTreeNodeIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #2 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: nobuiltin nounwind
declare void @_ZdlPvm(ptr noundef, i64 noundef) #3

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) #4 personality ptr @__gxx_personality_v0 {
  %7 = alloca ptr, align 8
  %8 = alloca ptr, align 8
  %9 = alloca ptr, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i32, align 4
  %13 = alloca i32, align 4
  %14 = alloca i32, align 4
  %15 = alloca i32, align 4
  %16 = alloca i32, align 4
  %17 = alloca i32, align 4
  %18 = alloca i32, align 4
  %19 = alloca i32, align 4
  %20 = alloca i32, align 4
  %21 = alloca i32, align 4
  %22 = alloca i32, align 4
  %23 = alloca i32, align 4
  %24 = alloca i32, align 4
  %25 = alloca i32, align 4
  %26 = alloca ptr, align 8
  %27 = alloca ptr, align 8
  %28 = alloca i32, align 4
  store ptr %0, ptr %8, align 8, !tbaa !5
  store ptr %1, ptr %9, align 8, !tbaa !5
  store i32 %2, ptr %10, align 4, !tbaa !9
  store i32 %3, ptr %11, align 4, !tbaa !9
  store i32 %4, ptr %12, align 4, !tbaa !9
  store i32 %5, ptr %13, align 4, !tbaa !9
  %29 = load i32, ptr %10, align 4, !tbaa !9
  %30 = load i32, ptr %11, align 4, !tbaa !9
  %31 = icmp sgt i32 %29, %30
  br i1 %31, label %32, label %33

32:                                               ; preds = %6
  store ptr null, ptr %7, align 8
  br label %103

33:                                               ; preds = %6
  call void @llvm.lifetime.start.p0(i64 4, ptr %14) #5
  %34 = load ptr, ptr %9, align 8, !tbaa !5
  %35 = load i32, ptr %12, align 4, !tbaa !9
  %36 = sext i32 %35 to i64
  %37 = getelementptr inbounds i32, ptr %34, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !9
  store i32 %38, ptr %14, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  store i32 -1, ptr %15, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %16) #5
  %39 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %39, ptr %16, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %56, %33
  %41 = load i32, ptr %16, align 4, !tbaa !9
  %42 = load i32, ptr %11, align 4, !tbaa !9
  %43 = icmp sle i32 %41, %42
  br i1 %43, label %45, label %44

44:                                               ; preds = %40
  store i32 2, ptr %17, align 4
  br label %59

45:                                               ; preds = %40
  %46 = load ptr, ptr %8, align 8, !tbaa !5
  %47 = load i32, ptr %16, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds i32, ptr %46, i64 %48
  %50 = load i32, ptr %49, align 4, !tbaa !9
  %51 = load i32, ptr %14, align 4, !tbaa !9
  %52 = icmp eq i32 %50, %51
  br i1 %52, label %53, label %55

53:                                               ; preds = %45
  %54 = load i32, ptr %16, align 4, !tbaa !9
  store i32 %54, ptr %15, align 4, !tbaa !9
  store i32 2, ptr %17, align 4
  br label %59

55:                                               ; preds = %45
  br label %56

56:                                               ; preds = %55
  %57 = load i32, ptr %16, align 4, !tbaa !9
  %58 = add nsw i32 %57, 1
  store i32 %58, ptr %16, align 4, !tbaa !9
  br label %40, !llvm.loop !11

59:                                               ; preds = %53, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %16) #5
  br label %60

60:                                               ; preds = %59
  call void @llvm.lifetime.start.p0(i64 4, ptr %18) #5
  %61 = load i32, ptr %10, align 4, !tbaa !9
  store i32 %61, ptr %18, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %19) #5
  %62 = load i32, ptr %15, align 4, !tbaa !9
  %63 = sub nsw i32 %62, 1
  store i32 %63, ptr %19, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %20) #5
  %64 = load i32, ptr %12, align 4, !tbaa !9
  %65 = add nsw i32 %64, 1
  store i32 %65, ptr %20, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %21) #5
  %66 = load i32, ptr %19, align 4, !tbaa !9
  %67 = load i32, ptr %18, align 4, !tbaa !9
  %68 = sub nsw i32 %66, %67
  %69 = load i32, ptr %20, align 4, !tbaa !9
  %70 = add nsw i32 %68, %69
  store i32 %70, ptr %21, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %22) #5
  %71 = load i32, ptr %21, align 4, !tbaa !9
  %72 = add nsw i32 %71, 1
  store i32 %72, ptr %22, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %23) #5
  %73 = load i32, ptr %13, align 4, !tbaa !9
  store i32 %73, ptr %23, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %24) #5
  %74 = load i32, ptr %15, align 4, !tbaa !9
  %75 = add nsw i32 %74, 1
  store i32 %75, ptr %24, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %25) #5
  %76 = load i32, ptr %11, align 4, !tbaa !9
  store i32 %76, ptr %25, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %26) #5
  %77 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  %78 = load i32, ptr %14, align 4, !tbaa !9
  invoke void @_ZN14BinaryTreeNodeIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %77, i32 noundef %78)
          to label %79 unwind label %99

79:                                               ; preds = %60
  store ptr %77, ptr %26, align 8, !tbaa !5
  %80 = load ptr, ptr %8, align 8, !tbaa !5
  %81 = load ptr, ptr %9, align 8, !tbaa !5
  %82 = load i32, ptr %18, align 4, !tbaa !9
  %83 = load i32, ptr %19, align 4, !tbaa !9
  %84 = load i32, ptr %20, align 4, !tbaa !9
  %85 = load i32, ptr %21, align 4, !tbaa !9
  %86 = call noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef %80, ptr noundef %81, i32 noundef %82, i32 noundef %83, i32 noundef %84, i32 noundef %85)
  %87 = load ptr, ptr %26, align 8, !tbaa !5
  %88 = getelementptr inbounds %class.BinaryTreeNode, ptr %87, i32 0, i32 1
  store ptr %86, ptr %88, align 8, !tbaa !13
  %89 = load ptr, ptr %8, align 8, !tbaa !5
  %90 = load ptr, ptr %9, align 8, !tbaa !5
  %91 = load i32, ptr %24, align 4, !tbaa !9
  %92 = load i32, ptr %25, align 4, !tbaa !9
  %93 = load i32, ptr %22, align 4, !tbaa !9
  %94 = load i32, ptr %23, align 4, !tbaa !9
  %95 = call noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef %89, ptr noundef %90, i32 noundef %91, i32 noundef %92, i32 noundef %93, i32 noundef %94)
  %96 = load ptr, ptr %26, align 8, !tbaa !5
  %97 = getelementptr inbounds %class.BinaryTreeNode, ptr %96, i32 0, i32 2
  store ptr %95, ptr %97, align 8, !tbaa !15
  %98 = load ptr, ptr %26, align 8, !tbaa !5
  store ptr %98, ptr %7, align 8
  store i32 1, ptr %17, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %26) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %23) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %22) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %103

99:                                               ; preds = %60
  %100 = landingpad { ptr, i32 }
          cleanup
  %101 = extractvalue { ptr, i32 } %100, 0
  store ptr %101, ptr %27, align 8
  %102 = extractvalue { ptr, i32 } %100, 1
  store i32 %102, ptr %28, align 4
  call void @_ZdlPvm(ptr noundef %77, i64 noundef 24) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %26) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %25) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %24) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %23) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %22) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %21) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %20) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %19) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %18) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %14) #5
  br label %105

103:                                              ; preds = %79, %32
  %104 = load ptr, ptr %7, align 8
  ret ptr %104

105:                                              ; preds = %99
  %106 = load ptr, ptr %27, align 8
  %107 = load i32, ptr %28, align 4
  %108 = insertvalue { ptr, i32 } poison, ptr %106, 0
  %109 = insertvalue { ptr, i32 } %108, i32 %107, 1
  resume { ptr, i32 } %109
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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 #3 = { nobuiltin 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 = { 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 #5 = { nounwind }
attributes #6 = { builtin allocsize(0) }
attributes #7 = { builtin 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTS14BinaryTreeNodeIiE", !10, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !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"

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local void @_ZN14BinaryTreeNodeIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #1 align 2

declare i32 @__gxx_personality_v0(...)

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3, i32 noundef %4, i32 noundef %5) local_unnamed_addr #2 personality ptr @__gxx_personality_v0 {
  %7 = icmp sgt i32 %2, %3
  br i1 %7, label %common.ret49, label %8

8:                                                ; preds = %6
  %9 = sext i32 %4 to i64
  %10 = getelementptr inbounds i32, ptr %1, i64 %9
  %11 = load i32, ptr %10, align 4, !tbaa !5
  %12 = sext i32 %2 to i64
  %13 = add i32 %3, 1
  br label %14

14:                                               ; preds = %8, %18
  %indvars.iv = phi i64 [ %12, %8 ], [ %indvars.iv.next, %18 ]
  %15 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %16 = load i32, ptr %15, align 4, !tbaa !5
  %17 = icmp eq i32 %16, %11
  br i1 %17, label %.split.loop.exit, label %18

18:                                               ; preds = %14
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
  %exitcond.not = icmp eq i32 %13, %lftr.wideiv
  br i1 %exitcond.not, label %.split.loop.exit44, label %14, !llvm.loop !9

common.ret49:                                     ; preds = %6, %.split.loop.exit44
  %common.ret49.op = phi ptr [ %26, %.split.loop.exit44 ], [ null, %6 ]
  ret ptr %common.ret49.op

.split.loop.exit:                                 ; preds = %14
  %19 = trunc nsw i64 %indvars.iv to i32
  br label %.split.loop.exit44

.split.loop.exit44:                               ; preds = %18, %.split.loop.exit
  %.038 = phi i32 [ %19, %.split.loop.exit ], [ -1, %18 ]
  %20 = add nsw i32 %.038, -1
  %21 = add nsw i32 %4, 1
  %22 = sub nsw i32 %20, %2
  %23 = add nsw i32 %22, %21
  %24 = add nsw i32 %23, 1
  %25 = add nsw i32 %.038, 1
  %26 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  tail call void @_ZN14BinaryTreeNodeIiEC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %26, i32 noundef %11)
  %27 = tail call noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %2, i32 noundef %20, i32 noundef %21, i32 noundef %23)
  %28 = getelementptr inbounds i8, ptr %26, i64 8
  store ptr %27, ptr %28, align 8, !tbaa !11
  %29 = tail call noundef ptr @_Z15buildTreeHelperPiS_iiii(ptr noundef nonnull %0, ptr noundef %1, i32 noundef %25, i32 noundef %3, i32 noundef %24, i32 noundef %5)
  %30 = getelementptr inbounds i8, ptr %26, i64 16
  store ptr %29, ptr %30, align 8, !tbaa !14
  br label %common.ret49
}

attributes #0 = { 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 #1 = { 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 #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 = { 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 = distinct !{!9, !10}
!10 = !{!"llvm.loop.mustprogress"}
!11 = !{!12, !13, i64 8}
!12 = !{!"_ZTS14BinaryTreeNodeIiE", !6, i64 0, !13, i64 8, !13, i64 16}
!13 = !{!"any pointer", !7, i64 0}
!14 = !{!12, !13, i64 16}


; Effective O3 passes: CorrelatedValuePropagationPass,GlobalOptPass,IndVarSimplifyPass,InferFunctionAttrsPass,InstCombinePass,InstSimplifyPass,LCSSAPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,SROAPass,SimplifyCFGPass,TailCallElimPass