; LLVM IR from AlgoGenesis/C :: Miscellaneous Algorithms/Heavy-light decomposition (Program)

; 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 { i32, ptr }

@tree = external dso_local global [100005 x ptr], align 16
@subtreeSize = external dso_local global [100005 x i32], align 16
@parent = external dso_local global [100005 x i32], align 16
@depth = external dso_local global [100005 x i32], align 16
@heavy = external dso_local global [100005 x i32], align 16

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

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

; Function Attrs: nounwind uwtable
define dso_local i32 @dfs(i32 noundef %0) #1 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  store i32 %0, ptr %2, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %3) #2
  store i32 1, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #2
  store i32 0, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %2, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [100005 x i32], ptr @subtreeSize, i64 0, i64 %9
  store i32 1, ptr %10, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %11 = load i32, ptr %2, align 4, !tbaa !5
  %12 = sext i32 %11 to i64
  %13 = getelementptr inbounds [100005 x ptr], ptr @tree, i64 0, i64 %12
  %14 = load ptr, ptr %13, align 8, !tbaa !9
  store ptr %14, ptr %5, align 8, !tbaa !9
  br label %15

15:                                               ; preds = %58, %1
  %16 = load ptr, ptr %5, align 8, !tbaa !9
  %17 = icmp ne ptr %16, null
  br i1 %17, label %19, label %18

18:                                               ; preds = %15
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %62

19:                                               ; preds = %15
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %20 = load ptr, ptr %5, align 8, !tbaa !9
  %21 = getelementptr inbounds %struct.Node, ptr %20, i32 0, i32 0
  %22 = load i32, ptr %21, align 8, !tbaa !11
  store i32 %22, ptr %6, align 4, !tbaa !5
  %23 = load i32, ptr %6, align 4, !tbaa !5
  %24 = load i32, ptr %2, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %25
  %27 = load i32, ptr %26, align 4, !tbaa !5
  %28 = icmp ne i32 %23, %27
  br i1 %28, label %29, label %57

29:                                               ; preds = %19
  %30 = load i32, ptr %2, align 4, !tbaa !5
  %31 = load i32, ptr %6, align 4, !tbaa !5
  %32 = sext i32 %31 to i64
  %33 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %32
  store i32 %30, ptr %33, align 4, !tbaa !5
  %34 = load i32, ptr %2, align 4, !tbaa !5
  %35 = sext i32 %34 to i64
  %36 = getelementptr inbounds [100005 x i32], ptr @depth, i64 0, i64 %35
  %37 = load i32, ptr %36, align 4, !tbaa !5
  %38 = add nsw i32 %37, 1
  %39 = load i32, ptr %6, align 4, !tbaa !5
  %40 = sext i32 %39 to i64
  %41 = getelementptr inbounds [100005 x i32], ptr @depth, i64 0, i64 %40
  store i32 %38, ptr %41, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %42 = load i32, ptr %6, align 4, !tbaa !5
  %43 = call i32 @dfs(i32 noundef %42)
  store i32 %43, ptr %7, align 4, !tbaa !5
  %44 = load i32, ptr %7, align 4, !tbaa !5
  %45 = load i32, ptr %3, align 4, !tbaa !5
  %46 = add nsw i32 %45, %44
  store i32 %46, ptr %3, align 4, !tbaa !5
  %47 = load i32, ptr %7, align 4, !tbaa !5
  %48 = load i32, ptr %4, align 4, !tbaa !5
  %49 = icmp sgt i32 %47, %48
  br i1 %49, label %50, label %56

50:                                               ; preds = %29
  %51 = load i32, ptr %7, align 4, !tbaa !5
  store i32 %51, ptr %4, align 4, !tbaa !5
  %52 = load i32, ptr %6, align 4, !tbaa !5
  %53 = load i32, ptr %2, align 4, !tbaa !5
  %54 = sext i32 %53 to i64
  %55 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %54
  store i32 %52, ptr %55, align 4, !tbaa !5
  br label %56

56:                                               ; preds = %50, %29
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  br label %57

57:                                               ; preds = %56, %19
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #2
  br label %58

58:                                               ; preds = %57
  %59 = load ptr, ptr %5, align 8, !tbaa !9
  %60 = getelementptr inbounds %struct.Node, ptr %59, i32 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !13
  store ptr %61, ptr %5, align 8, !tbaa !9
  br label %15, !llvm.loop !14

62:                                               ; preds = %18
  %63 = load i32, ptr %3, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %3) #2
  ret i32 %63
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 = { 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}


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

@tree = external dso_local local_unnamed_addr global [100005 x ptr], align 16
@subtreeSize = external dso_local local_unnamed_addr global [100005 x i32], align 16
@parent = external dso_local local_unnamed_addr global [100005 x i32], align 16
@depth = external dso_local local_unnamed_addr global [100005 x i32], align 16
@heavy = external dso_local local_unnamed_addr global [100005 x i32], align 16

; Function Attrs: nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) uwtable
define dso_local i32 @dfs(i32 noundef %0) local_unnamed_addr #0 {
  %2 = sext i32 %0 to i64
  %3 = getelementptr inbounds [100005 x i32], ptr @subtreeSize, i64 0, i64 %2
  store i32 1, ptr %3, align 4, !tbaa !5
  %4 = getelementptr inbounds [100005 x ptr], ptr @tree, i64 0, i64 %2
  %.02125 = load ptr, ptr %4, align 8, !tbaa !9
  %.not26 = icmp eq ptr %.02125, null
  br i1 %.not26, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %1
  %5 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %2
  %6 = getelementptr inbounds [100005 x i32], ptr @depth, i64 0, i64 %2
  %7 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %2
  br label %8

._crit_edge:                                      ; preds = %21, %1
  %.0.lcssa = phi i32 [ 1, %1 ], [ %.1, %21 ]
  ret i32 %.0.lcssa

8:                                                ; preds = %.lr.ph, %21
  %.02129 = phi ptr [ %.02125, %.lr.ph ], [ %.021, %21 ]
  %.028 = phi i32 [ 1, %.lr.ph ], [ %.1, %21 ]
  %.01927 = phi i32 [ 0, %.lr.ph ], [ %.2, %21 ]
  %9 = load i32, ptr %.02129, align 8, !tbaa !11
  %10 = load i32, ptr %5, align 4, !tbaa !5
  %.not24 = icmp eq i32 %9, %10
  br i1 %.not24, label %21, label %11

11:                                               ; preds = %8
  %12 = sext i32 %9 to i64
  %13 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %12
  store i32 %0, ptr %13, align 4, !tbaa !5
  %14 = load i32, ptr %6, align 4, !tbaa !5
  %15 = add nsw i32 %14, 1
  %16 = getelementptr inbounds [100005 x i32], ptr @depth, i64 0, i64 %12
  store i32 %15, ptr %16, align 4, !tbaa !5
  %17 = tail call i32 @dfs(i32 noundef %9)
  %18 = add nsw i32 %17, %.028
  %19 = icmp sgt i32 %17, %.01927
  br i1 %19, label %20, label %21

20:                                               ; preds = %11
  store i32 %9, ptr %7, align 4, !tbaa !5
  br label %21

21:                                               ; preds = %11, %20, %8
  %.2 = phi i32 [ %.01927, %8 ], [ %17, %20 ], [ %.01927, %11 ]
  %.1 = phi i32 [ %.028, %8 ], [ %18, %20 ], [ %18, %11 ]
  %22 = getelementptr inbounds i8, ptr %.02129, i64 8
  %.021 = load ptr, ptr %22, align 8, !tbaa !9
  %.not = icmp eq ptr %.021, null
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !13
}

attributes #0 = { nofree nosync nounwind memory(readwrite, argmem: read, inaccessiblemem: none) 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 = !{!"int", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C/C++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"any pointer", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"", !6, i64 0, !10, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"llvm.loop.mustprogress"}


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