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

; 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
@dp1 = external dso_local global [100010 x i64], align 16
@dp2 = external dso_local global [100010 x i64], align 16
@tr = external dso_local global %class.Tree, align 8

; 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: mustprogress uwtable
define dso_local void @_Z5dfs_2xx(i64 noundef %0, i64 noundef %1) #1 {
  %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
  %8 = alloca i64, align 8
  store i64 %0, ptr %3, align 8, !tbaa !5
  store i64 %1, ptr %4, align 8, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #2
  %9 = load i64, ptr %3, align 8, !tbaa !5
  %10 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (%class.Tree, ptr @tr, i32 0, i32 1), i64 0, i64 %9
  %11 = load i64, ptr %10, align 8, !tbaa !5
  store i64 %11, ptr %5, align 8, !tbaa !5
  br label %12

12:                                               ; preds = %62, %2
  %13 = load i64, ptr %5, align 8, !tbaa !5
  %14 = icmp ne i64 %13, 0
  br i1 %14, label %16, label %15

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

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

24:                                               ; preds = %16
  store i32 4, ptr %6, align 4
  br label %59

25:                                               ; preds = %16
  call void @llvm.lifetime.start.p0(i64 8, ptr %8) #2
  %26 = load i64, ptr %5, align 8, !tbaa !5
  %27 = getelementptr inbounds [200020 x %"struct.Tree<long long>::edge"], ptr @tr, i64 0, i64 %26
  %28 = getelementptr inbounds %"struct.Tree<long long>::edge", ptr %27, i32 0, i32 2
  %29 = load i64, ptr %28, align 8, !tbaa !11
  store i64 %29, ptr %8, align 8, !tbaa !5
  %30 = load i64, ptr %3, align 8, !tbaa !5
  %31 = getelementptr inbounds [100010 x i64], ptr @dp2, i64 0, i64 %30
  %32 = load i64, ptr %31, align 8, !tbaa !5
  %33 = load i64, ptr %3, align 8, !tbaa !5
  %34 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %33
  %35 = load i64, ptr %34, align 8, !tbaa !5
  %36 = add nsw i64 %32, %35
  %37 = load i64, ptr %7, align 8, !tbaa !5
  %38 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %37
  %39 = load i64, ptr %38, align 8, !tbaa !5
  %40 = sub nsw i64 %36, %39
  %41 = load i64, ptr %7, align 8, !tbaa !5
  %42 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %41
  %43 = load i64, ptr %42, align 8, !tbaa !5
  %44 = load i64, ptr %8, align 8, !tbaa !5
  %45 = mul nsw i64 %43, %44
  %46 = sub nsw i64 %40, %45
  %47 = load i64, ptr getelementptr inbounds ([100010 x i64], ptr @sz, i64 0, i64 1), align 8, !tbaa !5
  %48 = load i64, ptr %7, align 8, !tbaa !5
  %49 = getelementptr inbounds [100010 x i64], ptr @sz, i64 0, i64 %48
  %50 = load i64, ptr %49, align 8, !tbaa !5
  %51 = sub nsw i64 %47, %50
  %52 = load i64, ptr %8, align 8, !tbaa !5
  %53 = mul nsw i64 %51, %52
  %54 = add nsw i64 %46, %53
  %55 = load i64, ptr %7, align 8, !tbaa !5
  %56 = getelementptr inbounds [100010 x i64], ptr @dp2, i64 0, i64 %55
  store i64 %54, ptr %56, align 8, !tbaa !5
  %57 = load i64, ptr %7, align 8, !tbaa !5
  %58 = load i64, ptr %3, align 8, !tbaa !5
  call void @_Z5dfs_2xx(i64 noundef %57, i64 noundef %58)
  call void @llvm.lifetime.end.p0(i64 8, ptr %8) #2
  store i32 0, ptr %6, align 4
  br label %59

59:                                               ; preds = %25, %24
  call void @llvm.lifetime.end.p0(i64 8, ptr %7) #2
  %60 = load i32, ptr %6, align 4
  switch i32 %60, label %68 [
    i32 0, label %61
    i32 4, label %62
  ]

61:                                               ; preds = %59
  br label %62

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

67:                                               ; preds = %15
  ret void

68:                                               ; preds = %59
  unreachable
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
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 = { 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
@dp1 = external dso_local local_unnamed_addr global [100010 x i64], align 16
@dp2 = 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_2xx(i64 noundef %0, i64 noundef %1) local_unnamed_addr #0 {
  %3 = getelementptr inbounds [100010 x i64], ptr getelementptr inbounds (i8, ptr @tr, i64 4800480), i64 0, i64 %0
  %.021 = load i64, ptr %3, align 8, !tbaa !5
  %.not22 = icmp eq i64 %.021, 0
  br i1 %.not22, label %._crit_edge, label %.lr.ph

.lr.ph:                                           ; preds = %2
  %4 = getelementptr inbounds [100010 x i64], ptr @dp2, i64 0, i64 %0
  %5 = getelementptr inbounds [100010 x i64], ptr @dp1, i64 0, i64 %0
  br label %6

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

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

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

25:                                               ; preds = %6, %10
  %26 = getelementptr inbounds i8, ptr %7, i64 8
  %.0 = load i64, ptr %26, align 8, !tbaa !5
  %.not = icmp eq i64 %.0, 0
  br i1 %.not, label %._crit_edge, label %6, !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