; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/cp/largest_bst_in_btree.ll (Z5buildPiS_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.node = type { i32, ptr, ptr }

@_ZZ5buildPiS_iiE1i = external hidden global i32, align 4

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z5buildPiS_ii(ptr noundef %0, ptr noundef %1, i32 noundef %2, i32 noundef %3) #0 personality ptr @__gxx_personality_v0 {
  %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 i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca ptr, align 8
  %13 = alloca ptr, align 8
  %14 = alloca i32, align 4
  %15 = 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
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #5
  store i32 -1, ptr %10, align 4, !tbaa !9
  %16 = load i32, ptr %8, align 4, !tbaa !9
  %17 = load i32, ptr %9, align 4, !tbaa !9
  %18 = icmp sgt i32 %16, %17
  br i1 %18, label %19, label %20

19:                                               ; preds = %4
  store ptr null, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %81

20:                                               ; preds = %4
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #5
  %21 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  %22 = load ptr, ptr %6, align 8, !tbaa !5
  %23 = load i32, ptr @_ZZ5buildPiS_iiE1i, align 4, !tbaa !9
  %24 = add nsw i32 %23, 1
  store i32 %24, ptr @_ZZ5buildPiS_iiE1i, align 4, !tbaa !9
  %25 = sext i32 %23 to i64
  %26 = getelementptr inbounds i32, ptr %22, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !9
  invoke void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %21, i32 noundef %27)
          to label %28 unwind label %34

28:                                               ; preds = %20
  store ptr %21, ptr %12, align 8, !tbaa !5
  %29 = load i32, ptr %8, align 4, !tbaa !9
  %30 = load i32, ptr %9, align 4, !tbaa !9
  %31 = icmp eq i32 %29, %30
  br i1 %31, label %32, label %38

32:                                               ; preds = %28
  %33 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %33, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %80

34:                                               ; preds = %20
  %35 = landingpad { ptr, i32 }
          cleanup
  %36 = extractvalue { ptr, i32 } %35, 0
  store ptr %36, ptr %13, align 8
  %37 = extractvalue { ptr, i32 } %35, 1
  store i32 %37, ptr %14, align 4
  call void @_ZdlPvm(ptr noundef %21, i64 noundef 24) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  br label %83

38:                                               ; preds = %28
  call void @llvm.lifetime.start.p0(i64 4, ptr %15) #5
  %39 = load i32, ptr %8, align 4, !tbaa !9
  store i32 %39, ptr %15, align 4, !tbaa !9
  br label %40

40:                                               ; preds = %58, %38
  %41 = load i32, ptr %8, align 4, !tbaa !9
  %42 = load i32, ptr %9, align 4, !tbaa !9
  %43 = icmp sle i32 %41, %42
  br i1 %43, label %45, label %44

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

45:                                               ; preds = %40
  %46 = load ptr, ptr %7, align 8, !tbaa !5
  %47 = load i32, ptr %15, 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 ptr, ptr %12, align 8, !tbaa !5
  %52 = getelementptr inbounds %class.node, ptr %51, i32 0, i32 0
  %53 = load i32, ptr %52, align 8, !tbaa !11
  %54 = icmp eq i32 %50, %53
  br i1 %54, label %55, label %57

55:                                               ; preds = %45
  %56 = load i32, ptr %15, align 4, !tbaa !9
  store i32 %56, ptr %10, align 4, !tbaa !9
  store i32 2, ptr %11, align 4
  br label %61

57:                                               ; preds = %45
  br label %58

58:                                               ; preds = %57
  %59 = load i32, ptr %15, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  store i32 %60, ptr %15, align 4, !tbaa !9
  br label %40, !llvm.loop !13

61:                                               ; preds = %55, %44
  call void @llvm.lifetime.end.p0(i64 4, ptr %15) #5
  br label %62

62:                                               ; preds = %61
  %63 = load ptr, ptr %6, align 8, !tbaa !5
  %64 = load ptr, ptr %7, align 8, !tbaa !5
  %65 = load i32, ptr %8, align 4, !tbaa !9
  %66 = load i32, ptr %10, align 4, !tbaa !9
  %67 = sub nsw i32 %66, 1
  %68 = call noundef ptr @_Z5buildPiS_ii(ptr noundef %63, ptr noundef %64, i32 noundef %65, i32 noundef %67)
  %69 = load ptr, ptr %12, align 8, !tbaa !5
  %70 = getelementptr inbounds %class.node, ptr %69, i32 0, i32 1
  store ptr %68, ptr %70, align 8, !tbaa !15
  %71 = load ptr, ptr %6, align 8, !tbaa !5
  %72 = load ptr, ptr %7, align 8, !tbaa !5
  %73 = load i32, ptr %10, align 4, !tbaa !9
  %74 = add nsw i32 %73, 1
  %75 = load i32, ptr %9, align 4, !tbaa !9
  %76 = call noundef ptr @_Z5buildPiS_ii(ptr noundef %71, ptr noundef %72, i32 noundef %74, i32 noundef %75)
  %77 = load ptr, ptr %12, align 8, !tbaa !5
  %78 = getelementptr inbounds %class.node, ptr %77, i32 0, i32 2
  store ptr %76, ptr %78, align 8, !tbaa !16
  %79 = load ptr, ptr %12, align 8, !tbaa !5
  store ptr %79, ptr %5, align 8
  store i32 1, ptr %11, align 4
  br label %80

80:                                               ; preds = %62, %32
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #5
  br label %81

81:                                               ; preds = %80, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #5
  %82 = load ptr, ptr %5, align 8
  ret ptr %82

83:                                               ; preds = %34
  %84 = load ptr, ptr %13, align 8
  %85 = load i32, ptr %14, align 4
  %86 = insertvalue { ptr, i32 } poison, ptr %84, 0
  %87 = insertvalue { ptr, i32 } %86, i32 %85, 1
  resume { ptr, i32 } %87
}

; 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: mustprogress nounwind uwtable
declare dso_local void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24), i32 noundef) unnamed_addr #3 align 2

declare i32 @__gxx_personality_v0(...)

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

; 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 = { 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 #4 = { 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 #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 = !{!12, !10, i64 0}
!12 = !{!"_ZTS4node", !10, i64 0, !6, i64 8, !6, i64 16}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}
!15 = !{!12, !6, i64 8}
!16 = !{!12, !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"

@_ZZ5buildPiS_iiE1i = external hidden local_unnamed_addr global i32, align 4

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

6:                                                ; preds = %4
  %7 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  %8 = load i32, ptr @_ZZ5buildPiS_iiE1i, align 4, !tbaa !5
  %9 = add nsw i32 %8, 1
  store i32 %9, ptr @_ZZ5buildPiS_iiE1i, align 4, !tbaa !5
  %10 = sext i32 %8 to i64
  %11 = getelementptr inbounds i32, ptr %0, i64 %10
  %12 = load i32, ptr %11, align 4, !tbaa !5
  tail call void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %7, i32 noundef %12)
  %13 = icmp eq i32 %2, %3
  br i1 %13, label %common.ret30, label %.preheader

.preheader:                                       ; preds = %6
  %14 = load i32, ptr %7, align 8, !tbaa !9
  %15 = sext i32 %2 to i64
  br label %16

16:                                               ; preds = %16, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next, %16 ], [ %15, %.preheader ]
  %17 = getelementptr inbounds i32, ptr %1, i64 %indvars.iv
  %18 = load i32, ptr %17, align 4, !tbaa !5
  %19 = icmp eq i32 %18, %14
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  br i1 %19, label %20, label %16, !llvm.loop !12

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

20:                                               ; preds = %16
  %21 = trunc nsw i64 %indvars.iv to i32
  %22 = add nsw i32 %21, -1
  %23 = tail call noundef ptr @_Z5buildPiS_ii(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %2, i32 noundef %22)
  %24 = getelementptr inbounds i8, ptr %7, i64 8
  store ptr %23, ptr %24, align 8, !tbaa !14
  %25 = add nsw i32 %21, 1
  %26 = tail call noundef ptr @_Z5buildPiS_ii(ptr noundef %0, ptr noundef nonnull %1, i32 noundef %25, i32 noundef %3)
  %27 = getelementptr inbounds i8, ptr %7, i64 16
  store ptr %26, ptr %27, align 8, !tbaa !15
  br label %common.ret30
}

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

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

declare i32 @__gxx_personality_v0(...)

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


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