; LLVM IR from HarshCasper/NeoAlgo :: C-Plus-Plus/graphs/right_view_of_tree.ll (ZN8bin_tree18right_view_of_treeEv)

; 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.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }
%class.bin_tree = type <{ ptr, i32, [4 x i8] }>
%struct.node = type { i32, ptr, ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8bin_tree18right_view_of_treeEv(ptr noundef nonnull align 8 dereferenceable(12) %0) #0 align 2 {
  %2 = alloca ptr, align 8
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i64, align 8
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  store ptr %0, ptr %2, align 8, !tbaa !5
  %9 = load ptr, ptr %2, align 8
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #6
  store i32 0, ptr %3, align 4, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #6
  store i32 0, ptr %4, align 4, !tbaa !9
  %10 = call noundef i32 @_ZN8bin_tree4sizeEv(ptr noundef nonnull align 8 dereferenceable(12) %9)
  %11 = zext i32 %10 to i64
  %12 = call ptr @llvm.stacksave.p0()
  store ptr %12, ptr %5, align 8
  %13 = alloca ptr, i64 %11, align 16
  store i64 %11, ptr %6, align 8
  %14 = getelementptr inbounds %class.bin_tree, ptr %9, i32 0, i32 0
  %15 = load ptr, ptr %14, align 8, !tbaa !11
  %16 = icmp ne ptr %15, null
  br i1 %16, label %17, label %24

17:                                               ; preds = %1
  %18 = getelementptr inbounds %class.bin_tree, ptr %9, i32 0, i32 0
  %19 = load ptr, ptr %18, align 8, !tbaa !11
  %20 = load i32, ptr %3, align 4, !tbaa !9
  %21 = add nsw i32 %20, 1
  store i32 %21, ptr %3, align 4, !tbaa !9
  %22 = sext i32 %20 to i64
  %23 = getelementptr inbounds ptr, ptr %13, i64 %22
  store ptr %19, ptr %23, align 8, !tbaa !5
  br label %24

24:                                               ; preds = %17, %1
  br label %25

25:                                               ; preds = %90, %24
  %26 = load i32, ptr %3, align 4, !tbaa !9
  %27 = load i32, ptr %4, align 4, !tbaa !9
  %28 = icmp ne i32 %26, %27
  br i1 %28, label %29, label %91

29:                                               ; preds = %25
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #6
  %30 = load i32, ptr %3, align 4, !tbaa !9
  %31 = load i32, ptr %4, align 4, !tbaa !9
  %32 = sub nsw i32 %30, %31
  store i32 %32, ptr %7, align 4, !tbaa !9
  %33 = load i32, ptr %4, align 4, !tbaa !9
  %34 = sext i32 %33 to i64
  %35 = getelementptr inbounds ptr, ptr %13, i64 %34
  %36 = load ptr, ptr %35, align 8, !tbaa !5
  %37 = getelementptr inbounds %struct.node, ptr %36, i32 0, i32 0
  %38 = load i32, ptr %37, align 8, !tbaa !13
  %39 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %38)
  %40 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %39, ptr noundef @.str)
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #6
  store i32 1, ptr %8, align 4, !tbaa !9
  br label %41

41:                                               ; preds = %87, %29
  %42 = load i32, ptr %8, align 4, !tbaa !9
  %43 = load i32, ptr %7, align 4, !tbaa !9
  %44 = icmp sle i32 %42, %43
  br i1 %44, label %46, label %45

45:                                               ; preds = %41
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #6
  br label %90

46:                                               ; preds = %41
  %47 = load i32, ptr %4, align 4, !tbaa !9
  %48 = sext i32 %47 to i64
  %49 = getelementptr inbounds ptr, ptr %13, i64 %48
  %50 = load ptr, ptr %49, align 8, !tbaa !5
  %51 = getelementptr inbounds %struct.node, ptr %50, i32 0, i32 2
  %52 = load ptr, ptr %51, align 8, !tbaa !15
  %53 = icmp ne ptr %52, null
  br i1 %53, label %54, label %65

54:                                               ; preds = %46
  %55 = load i32, ptr %4, align 4, !tbaa !9
  %56 = sext i32 %55 to i64
  %57 = getelementptr inbounds ptr, ptr %13, i64 %56
  %58 = load ptr, ptr %57, align 8, !tbaa !5
  %59 = getelementptr inbounds %struct.node, ptr %58, i32 0, i32 2
  %60 = load ptr, ptr %59, align 8, !tbaa !15
  %61 = load i32, ptr %3, align 4, !tbaa !9
  %62 = add nsw i32 %61, 1
  store i32 %62, ptr %3, align 4, !tbaa !9
  %63 = sext i32 %61 to i64
  %64 = getelementptr inbounds ptr, ptr %13, i64 %63
  store ptr %60, ptr %64, align 8, !tbaa !5
  br label %65

65:                                               ; preds = %54, %46
  %66 = load i32, ptr %4, align 4, !tbaa !9
  %67 = sext i32 %66 to i64
  %68 = getelementptr inbounds ptr, ptr %13, i64 %67
  %69 = load ptr, ptr %68, align 8, !tbaa !5
  %70 = getelementptr inbounds %struct.node, ptr %69, i32 0, i32 1
  %71 = load ptr, ptr %70, align 8, !tbaa !16
  %72 = icmp ne ptr %71, null
  br i1 %72, label %73, label %84

73:                                               ; preds = %65
  %74 = load i32, ptr %4, align 4, !tbaa !9
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds ptr, ptr %13, i64 %75
  %77 = load ptr, ptr %76, align 8, !tbaa !5
  %78 = getelementptr inbounds %struct.node, ptr %77, i32 0, i32 1
  %79 = load ptr, ptr %78, align 8, !tbaa !16
  %80 = load i32, ptr %3, align 4, !tbaa !9
  %81 = add nsw i32 %80, 1
  store i32 %81, ptr %3, align 4, !tbaa !9
  %82 = sext i32 %80 to i64
  %83 = getelementptr inbounds ptr, ptr %13, i64 %82
  store ptr %79, ptr %83, align 8, !tbaa !5
  br label %84

84:                                               ; preds = %73, %65
  %85 = load i32, ptr %4, align 4, !tbaa !9
  %86 = add nsw i32 %85, 1
  store i32 %86, ptr %4, align 4, !tbaa !9
  br label %87

87:                                               ; preds = %84
  %88 = load i32, ptr %8, align 4, !tbaa !9
  %89 = add nsw i32 %88, 1
  store i32 %89, ptr %8, align 4, !tbaa !9
  br label %41, !llvm.loop !17

90:                                               ; preds = %45
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #6
  br label %25, !llvm.loop !19

91:                                               ; preds = %25
  %92 = load ptr, ptr %5, align 8
  call void @llvm.stackrestore.p0(ptr %92)
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #6
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #6
  ret void
}

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN8bin_tree4sizeEv(ptr noundef nonnull align 8 dereferenceable(12)) #2 align 2

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave.p0() #3

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) #4

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) #5

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

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare void @llvm.stackrestore.p0(ptr) #3

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 = { 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 = { nocallback nofree nosync nounwind willreturn }
attributes #4 = { inlinehint 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 = { "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 #6 = { 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, !6, i64 0}
!12 = !{!"_ZTS8bin_tree", !6, i64 0, !10, i64 8}
!13 = !{!14, !10, i64 0}
!14 = !{!"_ZTS4node", !10, i64 0, !6, i64 8, !6, i64 16}
!15 = !{!14, !6, i64 16}
!16 = !{!14, !6, i64 8}
!17 = distinct !{!17, !18}
!18 = !{!"llvm.loop.mustprogress"}
!19 = distinct !{!19, !18}


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

%"class.std::basic_ostream" = type { ptr, %"class.std::basic_ios" }
%"class.std::basic_ios" = type { %"class.std::ios_base", ptr, i8, i8, ptr, ptr, ptr, ptr }
%"class.std::ios_base" = type { ptr, i64, i64, i32, i32, i32, ptr, %"struct.std::ios_base::_Words", [8 x %"struct.std::ios_base::_Words"], i32, ptr, %"class.std::locale" }
%"struct.std::ios_base::_Words" = type { ptr, i64 }
%"class.std::locale" = type { ptr }

@_ZSt4cout = external global %"class.std::basic_ostream", align 8
@.str = external hidden unnamed_addr constant [2 x i8], align 1

; Function Attrs: mustprogress uwtable
define dso_local void @_ZN8bin_tree18right_view_of_treeEv(ptr noundef nonnull align 8 dereferenceable(12) %0) local_unnamed_addr #0 align 2 {
  %2 = tail call noundef i32 @_ZN8bin_tree4sizeEv(ptr noundef nonnull align 8 dereferenceable(12) %0)
  %3 = zext i32 %2 to i64
  %4 = alloca ptr, i64 %3, align 16
  %5 = load ptr, ptr %0, align 8, !tbaa !5
  %.not = icmp eq ptr %5, null
  br i1 %.not, label %._crit_edge, label %.lr.ph35.preheader

.lr.ph35.preheader:                               ; preds = %1
  store ptr %5, ptr %4, align 16, !tbaa !11
  br label %.lr.ph35

.loopexit.loopexit:                               ; preds = %31
  %6 = trunc nsw i64 %indvars.iv.next to i32
  br label %.loopexit

.loopexit:                                        ; preds = %.loopexit.loopexit, %.lr.ph35
  %.119.lcssa = phi i32 [ %.01833, %.lr.ph35 ], [ %6, %.loopexit.loopexit ]
  %.2.lcssa = phi i32 [ %.134, %.lr.ph35 ], [ %.4, %.loopexit.loopexit ]
  %.not23 = icmp eq i32 %.2.lcssa, %.119.lcssa
  br i1 %.not23, label %._crit_edge, label %.lr.ph35, !llvm.loop !12

.lr.ph35:                                         ; preds = %.lr.ph35.preheader, %.loopexit
  %.134 = phi i32 [ %.2.lcssa, %.loopexit ], [ 1, %.lr.ph35.preheader ]
  %.01833 = phi i32 [ %.119.lcssa, %.loopexit ], [ 0, %.lr.ph35.preheader ]
  %7 = sext i32 %.01833 to i64
  %8 = getelementptr inbounds ptr, ptr %4, i64 %7
  %9 = load ptr, ptr %8, align 8, !tbaa !11
  %10 = load i32, ptr %9, align 8, !tbaa !14
  %11 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8) @_ZSt4cout, i32 noundef %10)
  %12 = tail call noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8) %11, ptr noundef nonnull @.str)
  %.not2427.not = icmp sgt i32 %.134, %.01833
  br i1 %.not2427.not, label %.lr.ph.preheader, label %.loopexit

.lr.ph.preheader:                                 ; preds = %.lr.ph35
  %13 = add i32 %.134, 1
  %14 = sub i32 %13, %.01833
  br label %.lr.ph

.lr.ph:                                           ; preds = %.lr.ph.preheader, %31
  %indvars.iv = phi i64 [ %7, %.lr.ph.preheader ], [ %indvars.iv.next, %31 ]
  %.030 = phi i32 [ 1, %.lr.ph.preheader ], [ %32, %31 ]
  %.229 = phi i32 [ %.134, %.lr.ph.preheader ], [ %.4, %31 ]
  %15 = getelementptr inbounds ptr, ptr %4, i64 %indvars.iv
  %16 = load ptr, ptr %15, align 8, !tbaa !11
  %17 = getelementptr inbounds i8, ptr %16, i64 16
  %18 = load ptr, ptr %17, align 8, !tbaa !16
  %.not25 = icmp eq ptr %18, null
  br i1 %.not25, label %23, label %19

19:                                               ; preds = %.lr.ph
  %20 = add nsw i32 %.229, 1
  %21 = sext i32 %.229 to i64
  %22 = getelementptr inbounds ptr, ptr %4, i64 %21
  store ptr %18, ptr %22, align 8, !tbaa !11
  %.pre = load ptr, ptr %15, align 8, !tbaa !11
  br label %23

23:                                               ; preds = %19, %.lr.ph
  %24 = phi ptr [ %.pre, %19 ], [ %16, %.lr.ph ]
  %.3 = phi i32 [ %20, %19 ], [ %.229, %.lr.ph ]
  %25 = getelementptr inbounds i8, ptr %24, i64 8
  %26 = load ptr, ptr %25, align 8, !tbaa !17
  %.not26 = icmp eq ptr %26, null
  br i1 %.not26, label %31, label %27

27:                                               ; preds = %23
  %28 = add nsw i32 %.3, 1
  %29 = sext i32 %.3 to i64
  %30 = getelementptr inbounds ptr, ptr %4, i64 %29
  store ptr %26, ptr %30, align 8, !tbaa !11
  br label %31

31:                                               ; preds = %27, %23
  %.4 = phi i32 [ %28, %27 ], [ %.3, %23 ]
  %indvars.iv.next = add nsw i64 %indvars.iv, 1
  %32 = add nuw i32 %.030, 1
  %exitcond.not = icmp eq i32 %32, %14
  br i1 %exitcond.not, label %.loopexit.loopexit, label %.lr.ph, !llvm.loop !18

._crit_edge:                                      ; preds = %.loopexit, %1
  ret void
}

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef i32 @_ZN8bin_tree4sizeEv(ptr noundef nonnull align 8 dereferenceable(12)) local_unnamed_addr #1 align 2

; Function Attrs: inlinehint mustprogress uwtable
declare noundef nonnull align 8 dereferenceable(8) ptr @_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef) local_unnamed_addr #2

declare noundef nonnull align 8 dereferenceable(8) ptr @_ZNSolsEi(ptr noundef nonnull align 8 dereferenceable(8), i32 noundef) local_unnamed_addr #3

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 = { 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 = { inlinehint 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 = { "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" }

!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 = !{!"_ZTS8bin_tree", !7, i64 0, !10, i64 8}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C++ TBAA"}
!10 = !{!"int", !8, i64 0}
!11 = !{!7, !7, i64 0}
!12 = distinct !{!12, !13}
!13 = !{!"llvm.loop.mustprogress"}
!14 = !{!15, !10, i64 0}
!15 = !{!"_ZTS4node", !10, i64 0, !7, i64 8, !7, i64 16}
!16 = !{!15, !7, i64 16}
!17 = !{!15, !7, i64 8}
!18 = distinct !{!18, !13}


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