; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/cp/construct_special_tree.ll (Z11specialTreePiii)

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

; Function Attrs: mustprogress uwtable
define dso_local noundef ptr @_Z11specialTreePiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) #0 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca ptr, align 8
  %11 = alloca ptr, align 8
  %12 = alloca i32, align 4
  store ptr %0, ptr %5, align 8, !tbaa !5
  store i32 %1, ptr %6, align 4, !tbaa !9
  store i32 %2, ptr %7, align 4, !tbaa !9
  %13 = load i32, ptr %6, align 4, !tbaa !9
  %14 = load i32, ptr %7, align 4, !tbaa !9
  %15 = icmp sgt i32 %13, %14
  br i1 %15, label %16, label %17

16:                                               ; preds = %3
  store ptr null, ptr %4, align 8
  br label %70

17:                                               ; preds = %3
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #5
  %18 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %18, ptr %8, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %19 = load i32, ptr %6, align 4, !tbaa !9
  store i32 %19, ptr %9, align 4, !tbaa !9
  br label %20

20:                                               ; preds = %40, %17
  %21 = load i32, ptr %9, align 4, !tbaa !9
  %22 = load i32, ptr %7, align 4, !tbaa !9
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %43

25:                                               ; preds = %20
  %26 = load ptr, ptr %5, align 8, !tbaa !5
  %27 = load i32, ptr %9, align 4, !tbaa !9
  %28 = sext i32 %27 to i64
  %29 = getelementptr inbounds i32, ptr %26, i64 %28
  %30 = load i32, ptr %29, align 4, !tbaa !9
  %31 = load ptr, ptr %5, align 8, !tbaa !5
  %32 = load i32, ptr %8, align 4, !tbaa !9
  %33 = sext i32 %32 to i64
  %34 = getelementptr inbounds i32, ptr %31, i64 %33
  %35 = load i32, ptr %34, align 4, !tbaa !9
  %36 = icmp sgt i32 %30, %35
  br i1 %36, label %37, label %39

37:                                               ; preds = %25
  %38 = load i32, ptr %9, align 4, !tbaa !9
  store i32 %38, ptr %8, align 4, !tbaa !9
  br label %39

39:                                               ; preds = %37, %25
  br label %40

40:                                               ; preds = %39
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  store i32 %42, ptr %9, align 4, !tbaa !9
  br label %20, !llvm.loop !11

43:                                               ; preds = %24
  call void @llvm.lifetime.start.p0(i64 8, ptr %10) #5
  %44 = call noalias noundef nonnull ptr @_Znwm(i64 noundef 24) #6
  %45 = load ptr, ptr %5, align 8, !tbaa !5
  %46 = load i32, ptr %8, align 4, !tbaa !9
  %47 = sext i32 %46 to i64
  %48 = getelementptr inbounds i32, ptr %45, i64 %47
  %49 = load i32, ptr %48, align 4, !tbaa !9
  invoke void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %44, i32 noundef %49)
          to label %50 unwind label %66

50:                                               ; preds = %43
  store ptr %44, ptr %10, align 8, !tbaa !5
  %51 = load ptr, ptr %5, align 8, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !9
  %53 = load i32, ptr %8, align 4, !tbaa !9
  %54 = sub nsw i32 %53, 1
  %55 = call noundef ptr @_Z11specialTreePiii(ptr noundef %51, i32 noundef %52, i32 noundef %54)
  %56 = load ptr, ptr %10, align 8, !tbaa !5
  %57 = getelementptr inbounds %class.Node, ptr %56, i32 0, i32 1
  store ptr %55, ptr %57, align 8, !tbaa !13
  %58 = load ptr, ptr %5, align 8, !tbaa !5
  %59 = load i32, ptr %8, align 4, !tbaa !9
  %60 = add nsw i32 %59, 1
  %61 = load i32, ptr %7, align 4, !tbaa !9
  %62 = call noundef ptr @_Z11specialTreePiii(ptr noundef %58, i32 noundef %60, i32 noundef %61)
  %63 = load ptr, ptr %10, align 8, !tbaa !5
  %64 = getelementptr inbounds %class.Node, ptr %63, i32 0, i32 2
  store ptr %62, ptr %64, align 8, !tbaa !15
  %65 = load ptr, ptr %10, align 8, !tbaa !5
  store ptr %65, ptr %4, align 8
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %70

66:                                               ; preds = %43
  %67 = landingpad { ptr, i32 }
          cleanup
  %68 = extractvalue { ptr, i32 } %67, 0
  store ptr %68, ptr %11, align 8
  %69 = extractvalue { ptr, i32 } %67, 1
  store i32 %69, ptr %12, align 4
  call void @_ZdlPvm(ptr noundef %44, i64 noundef 24) #7
  call void @llvm.lifetime.end.p0(i64 8, ptr %10) #5
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #5
  br label %72

70:                                               ; preds = %50, %16
  %71 = load ptr, ptr %4, align 8
  ret ptr %71

72:                                               ; preds = %66
  %73 = load ptr, ptr %11, align 8
  %74 = load i32, ptr %12, align 4
  %75 = insertvalue { ptr, i32 } poison, ptr %73, 0
  %76 = insertvalue { ptr, i32 } %75, i32 %74, 1
  resume { ptr, i32 } %76
}

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

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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}
!13 = !{!14, !6, i64 8}
!14 = !{!"_ZTS4Node", !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: mustprogress uwtable
define dso_local noundef ptr @_Z11specialTreePiii(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %4 = icmp sgt i32 %1, %2
  br i1 %4, label %common.ret30, label %.preheader.preheader

.preheader.preheader:                             ; preds = %3
  %5 = sext i32 %1 to i64
  %6 = add i32 %2, 1
  %7 = sub i32 %6, %1
  %8 = sub i32 %2, %1
  %xtraiter = and i32 %7, 3
  %lcmp.mod.not = icmp eq i32 %xtraiter, 0
  br i1 %lcmp.mod.not, label %.preheader.prol.loopexit, label %.preheader.prol

.preheader.prol:                                  ; preds = %.preheader.preheader, %.preheader.prol
  %indvars.iv.prol = phi i64 [ %indvars.iv.next.prol, %.preheader.prol ], [ %5, %.preheader.preheader ]
  %.02427.prol = phi i32 [ %spec.select.prol, %.preheader.prol ], [ %1, %.preheader.preheader ]
  %prol.iter = phi i32 [ %prol.iter.next, %.preheader.prol ], [ 0, %.preheader.preheader ]
  %9 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.prol
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %.02427.prol to i64
  %12 = getelementptr inbounds i32, ptr %0, i64 %11
  %13 = load i32, ptr %12, align 4, !tbaa !5
  %14 = icmp sgt i32 %10, %13
  %15 = trunc nsw i64 %indvars.iv.prol to i32
  %spec.select.prol = select i1 %14, i32 %15, i32 %.02427.prol
  %indvars.iv.next.prol = add nsw i64 %indvars.iv.prol, 1
  %prol.iter.next = add i32 %prol.iter, 1
  %prol.iter.cmp.not = icmp eq i32 %prol.iter.next, %xtraiter
  br i1 %prol.iter.cmp.not, label %.preheader.prol.loopexit, label %.preheader.prol, !llvm.loop !9

.preheader.prol.loopexit:                         ; preds = %.preheader.prol, %.preheader.preheader
  %spec.select.lcssa.unr = phi i32 [ poison, %.preheader.preheader ], [ %spec.select.prol, %.preheader.prol ]
  %indvars.iv.unr = phi i64 [ %5, %.preheader.preheader ], [ %indvars.iv.next.prol, %.preheader.prol ]
  %.02427.unr = phi i32 [ %1, %.preheader.preheader ], [ %spec.select.prol, %.preheader.prol ]
  %16 = icmp ult i32 %8, 3
  br i1 %16, label %.unr-lcssa, label %.preheader

common.ret30:                                     ; preds = %3, %.unr-lcssa
  %common.ret30.op = phi ptr [ %17, %.unr-lcssa ], [ null, %3 ]
  ret ptr %common.ret30.op

.unr-lcssa:                                       ; preds = %.preheader, %.preheader.prol.loopexit
  %spec.select.lcssa = phi i32 [ %spec.select.lcssa.unr, %.preheader.prol.loopexit ], [ %spec.select.3, %.preheader ]
  %17 = tail call noalias noundef nonnull dereferenceable(24) ptr @_Znwm(i64 noundef 24) #3
  %18 = sext i32 %spec.select.lcssa to i64
  %19 = getelementptr inbounds i32, ptr %0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !5
  tail call void @_ZN4NodeC2Ei(ptr noundef nonnull align 8 dereferenceable(24) %17, i32 noundef %20)
  %21 = add nsw i32 %spec.select.lcssa, -1
  %22 = tail call noundef ptr @_Z11specialTreePiii(ptr noundef nonnull %0, i32 noundef %1, i32 noundef %21)
  %23 = getelementptr inbounds i8, ptr %17, i64 8
  store ptr %22, ptr %23, align 8, !tbaa !11
  %24 = add nsw i32 %spec.select.lcssa, 1
  %25 = tail call noundef ptr @_Z11specialTreePiii(ptr noundef nonnull %0, i32 noundef %24, i32 noundef %2)
  %26 = getelementptr inbounds i8, ptr %17, i64 16
  store ptr %25, ptr %26, align 8, !tbaa !14
  br label %common.ret30

.preheader:                                       ; preds = %.preheader.prol.loopexit, %.preheader
  %indvars.iv = phi i64 [ %indvars.iv.next.3, %.preheader ], [ %indvars.iv.unr, %.preheader.prol.loopexit ]
  %.02427 = phi i32 [ %spec.select.3, %.preheader ], [ %.02427.unr, %.preheader.prol.loopexit ]
  %27 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv
  %28 = load i32, ptr %27, align 4, !tbaa !5
  %29 = sext i32 %.02427 to i64
  %30 = getelementptr inbounds i32, ptr %0, i64 %29
  %31 = load i32, ptr %30, align 4, !tbaa !5
  %32 = icmp sgt i32 %28, %31
  %33 = trunc nsw i64 %indvars.iv to i32
  %spec.select = select i1 %32, i32 %33, i32 %.02427
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %34 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next
  %35 = load i32, ptr %34, align 4, !tbaa !5
  %36 = sext i32 %spec.select to i64
  %37 = getelementptr inbounds i32, ptr %0, i64 %36
  %38 = load i32, ptr %37, align 4, !tbaa !5
  %39 = icmp sgt i32 %35, %38
  %40 = trunc nsw i64 %indvars.iv.next to i32
  %spec.select.1 = select i1 %39, i32 %40, i32 %spec.select
  %indvars.iv.next.1 = add nsw i64 %indvars.iv, 2
  %41 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.1
  %42 = load i32, ptr %41, align 4, !tbaa !5
  %43 = sext i32 %spec.select.1 to i64
  %44 = getelementptr inbounds i32, ptr %0, i64 %43
  %45 = load i32, ptr %44, align 4, !tbaa !5
  %46 = icmp sgt i32 %42, %45
  %47 = trunc nsw i64 %indvars.iv.next.1 to i32
  %spec.select.2 = select i1 %46, i32 %47, i32 %spec.select.1
  %indvars.iv.next.2 = add nsw i64 %indvars.iv, 3
  %48 = getelementptr inbounds i32, ptr %0, i64 %indvars.iv.next.2
  %49 = load i32, ptr %48, align 4, !tbaa !5
  %50 = sext i32 %spec.select.2 to i64
  %51 = getelementptr inbounds i32, ptr %0, i64 %50
  %52 = load i32, ptr %51, align 4, !tbaa !5
  %53 = icmp sgt i32 %49, %52
  %54 = trunc nsw i64 %indvars.iv.next.2 to i32
  %spec.select.3 = select i1 %53, i32 %54, i32 %spec.select.2
  %indvars.iv.next.3 = add nsw i64 %indvars.iv, 4
  %lftr.wideiv.3 = trunc i64 %indvars.iv.next.3 to i32
  %exitcond.not.3 = icmp eq i32 %6, %lftr.wideiv.3
  br i1 %exitcond.not.3, label %.unr-lcssa, label %.preheader, !llvm.loop !15
}

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


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