; 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
@parent = external dso_local global [100005 x i32], align 16
@heavy = external dso_local global [100005 x i32], align 16
@head = external dso_local global [100005 x i32], align 16
@currentPos = external dso_local global i32, align 4
@pos = 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 void @decompose(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca ptr, align 8
  %6 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %7 = load i32, ptr %4, align 4, !tbaa !5
  %8 = load i32, ptr %3, align 4, !tbaa !5
  %9 = sext i32 %8 to i64
  %10 = getelementptr inbounds [100005 x i32], ptr @head, i64 0, i64 %9
  store i32 %7, ptr %10, align 4, !tbaa !5
  %11 = load i32, ptr @currentPos, align 4, !tbaa !5
  %12 = add nsw i32 %11, 1
  store i32 %12, ptr @currentPos, align 4, !tbaa !5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = sext i32 %13 to i64
  %15 = getelementptr inbounds [100005 x i32], ptr @pos, i64 0, i64 %14
  store i32 %11, ptr %15, align 4, !tbaa !5
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  %20 = icmp ne i32 %19, -1
  br i1 %20, label %21, label %27

21:                                               ; preds = %2
  %22 = load i32, ptr %3, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %23
  %25 = load i32, ptr %24, align 4, !tbaa !5
  %26 = load i32, ptr %4, align 4, !tbaa !5
  call void @decompose(i32 noundef %25, i32 noundef %26)
  br label %27

27:                                               ; preds = %21, %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %28 = load i32, ptr %3, align 4, !tbaa !5
  %29 = sext i32 %28 to i64
  %30 = getelementptr inbounds [100005 x ptr], ptr @tree, i64 0, i64 %29
  %31 = load ptr, ptr %30, align 8, !tbaa !9
  store ptr %31, ptr %5, align 8, !tbaa !9
  br label %32

32:                                               ; preds = %57, %27
  %33 = load ptr, ptr %5, align 8, !tbaa !9
  %34 = icmp ne ptr %33, null
  br i1 %34, label %36, label %35

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

36:                                               ; preds = %32
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #2
  %37 = load ptr, ptr %5, align 8, !tbaa !9
  %38 = getelementptr inbounds %struct.Node, ptr %37, i32 0, i32 0
  %39 = load i32, ptr %38, align 8, !tbaa !11
  store i32 %39, ptr %6, align 4, !tbaa !5
  %40 = load i32, ptr %6, align 4, !tbaa !5
  %41 = load i32, ptr %3, align 4, !tbaa !5
  %42 = sext i32 %41 to i64
  %43 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %42
  %44 = load i32, ptr %43, align 4, !tbaa !5
  %45 = icmp ne i32 %40, %44
  br i1 %45, label %46, label %56

46:                                               ; preds = %36
  %47 = load i32, ptr %6, align 4, !tbaa !5
  %48 = load i32, ptr %3, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %49
  %51 = load i32, ptr %50, align 4, !tbaa !5
  %52 = icmp ne i32 %47, %51
  br i1 %52, label %53, label %56

53:                                               ; preds = %46
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = load i32, ptr %6, align 4, !tbaa !5
  call void @decompose(i32 noundef %54, i32 noundef %55)
  br label %56

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

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

61:                                               ; preds = %35
  ret void
}

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
@parent = 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
@head = external dso_local local_unnamed_addr global [100005 x i32], align 16
@currentPos = external dso_local local_unnamed_addr global i32, align 4
@pos = 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 void @decompose(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = sext i32 %0 to i64
  %4 = getelementptr inbounds [100005 x i32], ptr @head, i64 0, i64 %3
  store i32 %1, ptr %4, align 4, !tbaa !5
  %5 = load i32, ptr @currentPos, align 4, !tbaa !5
  %6 = add nsw i32 %5, 1
  store i32 %6, ptr @currentPos, align 4, !tbaa !5
  %7 = getelementptr inbounds [100005 x i32], ptr @pos, i64 0, i64 %3
  store i32 %5, ptr %7, align 4, !tbaa !5
  %8 = getelementptr inbounds [100005 x i32], ptr @heavy, i64 0, i64 %3
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %.not = icmp eq i32 %9, -1
  br i1 %.not, label %11, label %10

10:                                               ; preds = %2
  tail call void @decompose(i32 noundef %9, i32 noundef %1)
  br label %11

11:                                               ; preds = %10, %2
  %12 = getelementptr inbounds [100005 x ptr], ptr @tree, i64 0, i64 %3
  %.020 = load ptr, ptr %12, align 8, !tbaa !9
  %.not1721 = icmp eq ptr %.020, null
  br i1 %.not1721, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %11
  %13 = getelementptr inbounds [100005 x i32], ptr @parent, i64 0, i64 %3
  br label %14

._crit_edge:                                      ; preds = %20, %11
  ret void

14:                                               ; preds = %.lr.ph, %20
  %.022 = phi ptr [ %.020, %.lr.ph ], [ %.0, %20 ]
  %15 = load i32, ptr %.022, align 8, !tbaa !11
  %16 = load i32, ptr %13, align 4, !tbaa !5
  %.not18 = icmp eq i32 %15, %16
  br i1 %.not18, label %20, label %17

17:                                               ; preds = %14
  %18 = load i32, ptr %8, align 4, !tbaa !5
  %.not19 = icmp eq i32 %15, %18
  br i1 %.not19, label %20, label %19

19:                                               ; preds = %17
  tail call void @decompose(i32 noundef %15, i32 noundef %15)
  br label %20

20:                                               ; preds = %19, %17, %14
  %21 = getelementptr inbounds i8, ptr %.022, i64 8
  %.0 = load ptr, ptr %21, align 8, !tbaa !9
  %.not17 = icmp eq ptr %.0, null
  br i1 %.not17, label %._crit_edge, label %14, !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,GlobalOptPass,InferFunctionAttrsPass,InstCombinePass,JumpThreadingPass,LICMPass,LoopRotatePass,LoopSimplifyPass,LoopUnrollPass,PostOrderFunctionAttrsPass,SROAPass,SimplifyCFGPass,TailCallElimPass