; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p2986.ll (Z5dfs_1xx)

; 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.Tree = type { [200020 x %"struct.Tree<long long>::edge"], [100010 x i64], i64 }
%"struct.Tree<long long>::edge" = type { i64, i64, i64 }

@sz = external dso_local global [100010 x i64], align 16
@weight = external dso_local global [100010 x i64], align 16
@dp1 = external dso_local global [100010 x i64], align 16
@tr = external dso_local global %class.Tree, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z5dfs_1xx(i64 noundef %0, i64 noundef %1) #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  %8 = load i64, ptr %3, align 8, !tbaa !5
  %9 = getelementptr inbounds [100010 x i64], ptr @weight, i64 0, i64 %8
  %10 = load i64, ptr %9, align 8, !tbaa !5
  %11 = load i64, ptr %3, align 8, !tbaa !5
  %12 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %11
  store i64 %10, ptr %12, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %13 = load i64, ptr %3, align 8, !tbaa !5
  %14 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (%class.Tree, ptr @tr, i32 0, i32 1), i64 0, i64 %13
  %15 = load i64, ptr %14, align 8, !tbaa !5
  store i64 %15, ptr %5, align 8, !tbaa !5
  br label %16

16:                                               ; preds = %58, %2
  %17 = load i64, ptr %5, align 8, !tbaa !5
  %18 = icmp ne i64 %17, 0
  br i1 %18, label %20, label %19

19:                                               ; preds = %16
  store i32 2, ptr %6, align 4
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #2
  br label %63

20:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %7) #2
  %21 = load i64, ptr %5, align 8, !tbaa !5
  %22 = getelementptr inbounds [200020 x %"struct.Tree<long long>::edge"], ptr @tr, i64 0, i64 %21
  %23 = getelementptr inbounds %"struct.Tree<long long>::edge", ptr %22, i32 0, i32 0
  %24 = load i64, ptr %23, align 8, !tbaa !9
  store i64 %24, ptr %7, align 8, !tbaa !5
  %25 = load i64, ptr %7, align 8, !tbaa !5
  %26 = load i64, ptr %4, align 8, !tbaa !5
  %27 = icmp eq i64 %25, %26
  br i1 %27, label %28, label %29

28:                                               ; preds = %20
  store i32 4, ptr %6, align 4
  br label %55

29:                                               ; preds = %20
  %30 = load i64, ptr %7, align 8, !tbaa !5
  %31 = load i64, ptr %3, align 8, !tbaa !5
  call void @_Z5dfs_1xx(i64 noundef %30, i64 noundef %31)
  %32 = load i64, ptr %7, align 8, !tbaa !5
  %33 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %32
  %34 = load i64, ptr %33, align 8, !tbaa !5
  %35 = load i64, ptr %3, align 8, !tbaa !5
  %36 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %35
  %37 = load i64, ptr %36, align 8, !tbaa !5
  %38 = add nsw i64 %37, %34
  store i64 %38, ptr %36, align 8, !tbaa !5
  %39 = load i64, ptr %7, align 8, !tbaa !5
  %40 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %39
  %41 = load i64, ptr %40, align 8, !tbaa !5
  %42 = load i64, ptr %7, align 8, !tbaa !5
  %43 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %42
  %44 = load i64, ptr %43, align 8, !tbaa !5
  %45 = load i64, ptr %5, align 8, !tbaa !5
  %46 = getelementptr inbounds [200020 x %"struct.Tree<long long>::edge"], ptr @tr, i64 0, i64 %45
  %47 = getelementptr inbounds %"struct.Tree<long long>::edge", ptr %46, i32 0, i32 2
  %48 = load i64, ptr %47, align 8, !tbaa !11
  %49 = mul nsw i64 %44, %48
  %50 = add nsw i64 %41, %49
  %51 = load i64, ptr %3, align 8, !tbaa !5
  %52 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !5
  %54 = add nsw i64 %53, %50
  store i64 %54, ptr %52, align 8, !tbaa !5
  store i32 0, ptr %6, align 4
  br label %55

55:                                               ; preds = %29, %28
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %56 = load i32, ptr %6, align 4
  switch i32 %56, label %64 [
    i32 0, label %57
    i32 4, label %58
  ]

57:                                               ; preds = %55
  br label %58

58:                                               ; preds = %57, %55
  %59 = load i64, ptr %5, align 8, !tbaa !5
  %60 = getelementptr inbounds [200020 x %"struct.Tree<long long>::edge"], ptr @tr, i64 0, i64 %59
  %61 = getelementptr inbounds %"struct.Tree<long long>::edge", ptr %60, i32 0, i32 1
  %62 = load i64, ptr %61, align 8, !tbaa !12
  store i64 %62, ptr %5, align 8, !tbaa !5
  br label %16, !llvm.loop !13

63:                                               ; preds = %19
  ret void

64:                                               ; preds = %55
  unreachable
}

; 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

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 = { 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 = !{!"long long", !7, i64 0}
!7 = !{!"omnipotent char", !8, i64 0}
!8 = !{!"Simple C++ TBAA"}
!9 = !{!10, !6, i64 0}
!10 = !{!"_ZTSN4TreeIxE4edgeE", !6, i64 0, !6, i64 8, !6, i64 16}
!11 = !{!10, !6, i64 16}
!12 = !{!10, !6, i64 8}
!13 = distinct !{!13, !14}
!14 = !{!"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"

%class.Tree = type { [200020 x %"struct.Tree<long long>::edge"], [100010 x i64], i64 }
%"struct.Tree<long long>::edge" = type { i64, i64, i64 }

@sz = external dso_local local_unnamed_addr global [100010 x i64], align 16
@weight = external dso_local local_unnamed_addr global [100010 x i64], align 16
@dp1 = external dso_local local_unnamed_addr global [100010 x i64], align 16
@tr = external dso_local local_unnamed_addr global %class.Tree, align 8

; Function Attrs: mustprogress nofree nosync nounwind memory(readwrite, argmem: none, inaccessiblemem: none) uwtable
define dso_local void @_Z5dfs_1xx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds [100010 x i64], ptr @weight, i64 0, i64 %0
  %4 = load i64, ptr %3, align 8, !tbaa !5
  %5 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %0
  store i64 %4, ptr %5, align 8, !tbaa !5
  %6 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (i8, ptr @tr, i64 4800480), i64 0, i64 %0
  %.017 = load i64, ptr %6, align 8, !tbaa !5
  %.not18 = icmp eq i64 %.017, 0
  br i1 %.not18, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %7 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %0
  br label %8

._crit_edge:                                      ; preds = %26, %2
  ret void

8:                                                ; preds = %.lr.ph, %26
  %.019 = phi i64 [ %.017, %.lr.ph ], [ %.0, %26 ]
  %9 = getelementptr inbounds [200020 x %"struct.Tree<long long>::edge"], ptr @tr, i64 0, i64 %.019
  %10 = load i64, ptr %9, align 8, !tbaa !9
  %11 = icmp eq i64 %10, %1
  br i1 %11, label %26, label %12

12:                                               ; preds = %8
  tail call void @_Z5dfs_1xx(i64 noundef %10, i64 noundef %0)
  %13 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %10
  %14 = load i64, ptr %13, align 8, !tbaa !5
  %15 = load i64, ptr %5, align 8, !tbaa !5
  %16 = add nsw i64 %15, %14
  store i64 %16, ptr %5, align 8, !tbaa !5
  %17 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %10
  %18 = load i64, ptr %17, align 8, !tbaa !5
  %19 = load i64, ptr %13, align 8, !tbaa !5
  %20 = getelementptr inbounds i8, ptr %9, i64 16
  %21 = load i64, ptr %20, align 8, !tbaa !11
  %22 = mul nsw i64 %21, %19
  %23 = add nsw i64 %22, %18
  %24 = load i64, ptr %7, align 8, !tbaa !5
  %25 = add nsw i64 %23, %24
  store i64 %25, ptr %7, align 8, !tbaa !5
  br label %26

26:                                               ; preds = %8, %12
  %27 = getelementptr inbounds i8, ptr %9, i64 8
  %.0 = load i64, ptr %27, align 8, !tbaa !5
  %.not = icmp eq i64 %.0, 0
  br i1 %.not, label %._crit_edge, label %8, !llvm.loop !12
}

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


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