; LLVM IR from Dev-XYS/Algorithms :: Persistent-Array.ll (Z5buildRP4nodeii)

; 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.node = type <{ ptr, ptr, i32, [4 x i8] }>

@d = external dso_local global [100000 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN4nodenwEm(i64 noundef) #0 align 2

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5buildRP4nodeii(ptr noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1, i32 noundef %2) #1 personality ptr @__gxx_personality_v0 {
  %4 = alloca ptr, align 8
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca ptr, align 8
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  store ptr %0, ptr %4, align 8, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !9
  store i32 %2, ptr %6, align 4, !tbaa !9
  %10 = call noundef ptr @_ZN4nodenwEm(i64 noundef 24)
  invoke void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(20) %10, i32 noundef 0)
          to label %11 unwind label %24

11:                                               ; preds = %3
  %12 = load ptr, ptr %4, align 8, !tbaa !5
  store ptr %10, ptr %12, align 8, !tbaa !5
  %13 = load i32, ptr %5, align 4, !tbaa !9
  %14 = load i32, ptr %6, align 4, !tbaa !9
  %15 = icmp eq i32 %13, %14
  br i1 %15, label %16, label %28

16:                                               ; preds = %11
  %17 = load i32, ptr %5, align 4, !tbaa !9
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %18
  %20 = load i32, ptr %19, align 4, !tbaa !9
  %21 = load ptr, ptr %4, align 8, !tbaa !5
  %22 = load ptr, ptr %21, align 8, !tbaa !5
  %23 = getelementptr inbounds %struct.node, ptr %22, i32 0, i32 2
  store i32 %20, ptr %23, align 8, !tbaa !11
  br label %44

24:                                               ; preds = %3
  %25 = landingpad { ptr, i32 }
          cleanup
  %26 = extractvalue { ptr, i32 } %25, 0
  store ptr %26, ptr %7, align 8
  %27 = extractvalue { ptr, i32 } %25, 1
  store i32 %27, ptr %8, align 4
  call void @_ZdlPvm(ptr noundef %10, i64 noundef 24) #4
  br label %45

28:                                               ; preds = %11
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #5
  %29 = load i32, ptr %5, align 4, !tbaa !9
  %30 = load i32, ptr %6, align 4, !tbaa !9
  %31 = add nsw i32 %29, %30
  %32 = ashr i32 %31, 1
  store i32 %32, ptr %9, align 4, !tbaa !9
  %33 = load ptr, ptr %4, align 8, !tbaa !5
  %34 = load ptr, ptr %33, align 8, !tbaa !5
  %35 = getelementptr inbounds %struct.node, ptr %34, i32 0, i32 0
  %36 = load i32, ptr %5, align 4, !tbaa !9
  %37 = load i32, ptr %9, align 4, !tbaa !9
  call void @_Z5buildRP4nodeii(ptr noundef nonnull align 8 dereferenceable(8) %35, i32 noundef %36, i32 noundef %37)
  %38 = load ptr, ptr %4, align 8, !tbaa !5
  %39 = load ptr, ptr %38, align 8, !tbaa !5
  %40 = getelementptr inbounds %struct.node, ptr %39, i32 0, i32 1
  %41 = load i32, ptr %9, align 4, !tbaa !9
  %42 = add nsw i32 %41, 1
  %43 = load i32, ptr %6, align 4, !tbaa !9
  call void @_Z5buildRP4nodeii(ptr noundef nonnull align 8 dereferenceable(8) %40, i32 noundef %42, i32 noundef %43)
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #5
  br label %44

44:                                               ; preds = %28, %16
  ret void

45:                                               ; preds = %24
  %46 = load ptr, ptr %7, align 8
  %47 = load i32, ptr %8, align 4
  %48 = insertvalue { ptr, i32 } poison, ptr %46, 0
  %49 = insertvalue { ptr, i32 } %48, i32 %47, 1
  resume { ptr, i32 } %49
}

declare i32 @__gxx_personality_v0(...)

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

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

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

attributes #0 = { 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 #1 = { 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 #2 = { 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 #3 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #4 = { builtin nounwind }
attributes #5 = { 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 16}
!12 = !{!"_ZTS4node", !6, i64 0, !6, i64 8, !10, 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"

@d = external dso_local local_unnamed_addr global [100000 x i32], align 16

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

; Function Attrs: mustprogress nounwind uwtable
declare dso_local noundef ptr @_ZN4nodenwEm(i64 noundef) local_unnamed_addr #0 align 2

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z5buildRP4nodeii(ptr nocapture noundef nonnull align 8 dereferenceable(8) %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #0 personality ptr @__gxx_personality_v0 {
  %4 = tail call noundef ptr @_ZN4nodenwEm(i64 noundef 24)
  tail call void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(20) %4, i32 noundef 0)
  store ptr %4, ptr %0, align 8, !tbaa !5
  %5 = icmp eq i32 %1, %2
  br i1 %5, label %tailrecurse._crit_edge, label %tailrecurse

tailrecurse._crit_edge:                           ; preds = %tailrecurse, %3
  %.tr13.lcssa = phi i32 [ %1, %3 ], [ %2, %tailrecurse ]
  %.lcssa = phi ptr [ %4, %3 ], [ %16, %tailrecurse ]
  %6 = sext i32 %.tr13.lcssa to i64
  %7 = getelementptr inbounds [100000 x i32], ptr @d, i64 0, i64 %6
  %8 = load i32, ptr %7, align 4, !tbaa !9
  %9 = getelementptr inbounds i8, ptr %.lcssa, i64 16
  store i32 %8, ptr %9, align 8, !tbaa !11
  ret void

tailrecurse:                                      ; preds = %3, %tailrecurse
  %10 = phi ptr [ %16, %tailrecurse ], [ %4, %3 ]
  %.tr1316 = phi i32 [ %15, %tailrecurse ], [ %1, %3 ]
  %.tr15 = phi ptr [ %14, %tailrecurse ], [ %0, %3 ]
  %11 = add nsw i32 %.tr1316, %2
  %12 = ashr i32 %11, 1
  tail call void @_Z5buildRP4nodeii(ptr noundef nonnull align 8 dereferenceable(8) %10, i32 noundef %.tr1316, i32 noundef %12)
  %13 = load ptr, ptr %.tr15, align 8, !tbaa !5
  %14 = getelementptr inbounds i8, ptr %13, i64 8
  %15 = add nsw i32 %12, 1
  %16 = tail call noundef ptr @_ZN4nodenwEm(i64 noundef 24)
  tail call void @_ZN4nodeC2Ei(ptr noundef nonnull align 8 dereferenceable(20) %16, i32 noundef 0)
  store ptr %16, ptr %14, align 8, !tbaa !5
  %17 = icmp eq i32 %15, %2
  br i1 %17, label %tailrecurse._crit_edge, label %tailrecurse
}

declare i32 @__gxx_personality_v0(...)

attributes #0 = { 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" }

!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 16}
!12 = !{!"_ZTS4node", !6, i64 0, !6, i64 8, !10, i64 16}


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