; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p1131.ll (Z3dfsii)

; 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 { [1000020 x %"struct.tree<long long>::edge"], [500010 x i32], i32 }
%"struct.tree<long long>::edge" = type { i32, i32, i64 }

@dp = external dso_local global [500010 x [2 x i64]], align 16
@tr = external dso_local global %class.tree, align 8

; Function Attrs: mustprogress uwtable
define dso_local void @_Z3dfsii(i32 noundef %0, i32 noundef %1) #0 {
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i64, align 8
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i64, align 8
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  store i32 %0, ptr %3, align 4, !tbaa !5
  store i32 %1, ptr %4, align 4, !tbaa !5
  %12 = load i32, ptr %3, align 4, !tbaa !5
  %13 = sext i32 %12 to i64
  %14 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %13
  %15 = getelementptr inbounds [2 x i64], ptr %14, i64 0, i64 0
  store i64 0, ptr %15, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 8, ptr %5) #3
  store i64 0, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %16 = load i32, ptr %3, align 4, !tbaa !5
  %17 = sext i32 %16 to i64
  %18 = getelementptr inbounds [500010 x i32], ptr getelementptr inbounds (%class.tree, ptr @tr, i32 0, i32 1), i64 0, i64 %17
  %19 = load i32, ptr %18, align 4, !tbaa !5
  store i32 %19, ptr %6, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %53, %2
  %21 = load i32, ptr %6, align 4, !tbaa !5
  %22 = icmp ne i32 %21, 0
  br i1 %22, label %24, label %23

23:                                               ; preds = %20
  store i32 2, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %6) #3
  br label %59

24:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %25 = load i32, ptr %6, align 4, !tbaa !5
  %26 = sext i32 %25 to i64
  %27 = getelementptr inbounds [1000020 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 0
  %29 = load i32, ptr %28, align 8, !tbaa !11
  store i32 %29, ptr %8, align 4, !tbaa !5
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = load i32, ptr %4, align 4, !tbaa !5
  %32 = icmp eq i32 %30, %31
  br i1 %32, label %33, label %34

33:                                               ; preds = %24
  store i32 4, ptr %7, align 4
  br label %50

34:                                               ; preds = %24
  %35 = load i32, ptr %8, align 4, !tbaa !5
  %36 = load i32, ptr %3, align 4, !tbaa !5
  call void @_Z3dfsii(i32 noundef %35, i32 noundef %36)
  call void @llvm.lifetime.start.p0(i64 8, ptr %9) #3
  %37 = load i32, ptr %6, align 4, !tbaa !5
  %38 = sext i32 %37 to i64
  %39 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %38
  %40 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %39, i32 0, i32 2
  %41 = load i64, ptr %40, align 8, !tbaa !13
  %42 = load i32, ptr %8, align 4, !tbaa !5
  %43 = sext i32 %42 to i64
  %44 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %43
  %45 = getelementptr inbounds [2 x i64], ptr %44, i64 0, i64 0
  %46 = load i64, ptr %45, align 16, !tbaa !9
  %47 = add nsw i64 %41, %46
  store i64 %47, ptr %9, align 8, !tbaa !9
  %48 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %5, ptr noundef nonnull align 8 dereferenceable(8) %9)
  %49 = load i64, ptr %48, align 8, !tbaa !9
  store i64 %49, ptr %5, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %9) #3
  store i32 0, ptr %7, align 4
  br label %50

50:                                               ; preds = %34, %33
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  %51 = load i32, ptr %7, align 4
  switch i32 %51, label %117 [
    i32 0, label %52
    i32 4, label %53
  ]

52:                                               ; preds = %50
  br label %53

53:                                               ; preds = %52, %50
  %54 = load i32, ptr %6, align 4, !tbaa !5
  %55 = sext i32 %54 to i64
  %56 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %55
  %57 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %56, i32 0, i32 1
  %58 = load i32, ptr %57, align 4, !tbaa !14
  store i32 %58, ptr %6, align 4, !tbaa !5
  br label %20, !llvm.loop !15

59:                                               ; preds = %23
  %60 = load i64, ptr %5, align 8, !tbaa !9
  %61 = load i32, ptr %3, align 4, !tbaa !5
  %62 = sext i32 %61 to i64
  %63 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %62
  %64 = getelementptr inbounds [2 x i64], ptr %63, i64 0, i64 0
  store i64 %60, ptr %64, align 16, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %65 = load i32, ptr %3, align 4, !tbaa !5
  %66 = sext i32 %65 to i64
  %67 = getelementptr inbounds [500010 x i32], ptr getelementptr inbounds (%class.tree, ptr @tr, i32 0, i32 1), i64 0, i64 %66
  %68 = load i32, ptr %67, align 4, !tbaa !5
  store i32 %68, ptr %10, align 4, !tbaa !5
  br label %69

69:                                               ; preds = %110, %59
  %70 = load i32, ptr %10, align 4, !tbaa !5
  %71 = icmp ne i32 %70, 0
  br i1 %71, label %73, label %72

72:                                               ; preds = %69
  store i32 5, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %10) #3
  br label %116

73:                                               ; preds = %69
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  %74 = load i32, ptr %10, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %75
  %77 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %76, i32 0, i32 0
  %78 = load i32, ptr %77, align 8, !tbaa !11
  store i32 %78, ptr %11, align 4, !tbaa !5
  %79 = load i32, ptr %11, align 4, !tbaa !5
  %80 = load i32, ptr %4, align 4, !tbaa !5
  %81 = icmp eq i32 %79, %80
  br i1 %81, label %82, label %83

82:                                               ; preds = %73
  store i32 7, ptr %7, align 4
  br label %107

83:                                               ; preds = %73
  %84 = load i32, ptr %3, align 4, !tbaa !5
  %85 = sext i32 %84 to i64
  %86 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %85
  %87 = getelementptr inbounds [2 x i64], ptr %86, i64 0, i64 0
  %88 = load i64, ptr %87, align 16, !tbaa !9
  %89 = load i32, ptr %10, align 4, !tbaa !5
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %90
  %92 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %91, i32 0, i32 2
  %93 = load i64, ptr %92, align 8, !tbaa !13
  %94 = load i32, ptr %11, align 4, !tbaa !5
  %95 = sext i32 %94 to i64
  %96 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %95
  %97 = getelementptr inbounds [2 x i64], ptr %96, i64 0, i64 0
  %98 = load i64, ptr %97, align 16, !tbaa !9
  %99 = add nsw i64 %93, %98
  %100 = sub nsw i64 %88, %99
  %101 = load i32, ptr %3, align 4, !tbaa !5
  %102 = sext i32 %101 to i64
  %103 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %102
  %104 = getelementptr inbounds [2 x i64], ptr %103, i64 0, i64 1
  %105 = load i64, ptr %104, align 8, !tbaa !9
  %106 = add nsw i64 %105, %100
  store i64 %106, ptr %104, align 8, !tbaa !9
  store i32 0, ptr %7, align 4
  br label %107

107:                                              ; preds = %83, %82
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  %108 = load i32, ptr %7, align 4
  switch i32 %108, label %117 [
    i32 0, label %109
    i32 7, label %110
  ]

109:                                              ; preds = %107
  br label %110

110:                                              ; preds = %109, %107
  %111 = load i32, ptr %10, align 4, !tbaa !5
  %112 = sext i32 %111 to i64
  %113 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %112
  %114 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %113, i32 0, i32 1
  %115 = load i32, ptr %114, align 4, !tbaa !14
  store i32 %115, ptr %10, align 4, !tbaa !5
  br label %69, !llvm.loop !17

116:                                              ; preds = %72
  call void @llvm.lifetime.end.p0(i64 8, ptr %5) #3
  ret void

117:                                              ; preds = %107, %50
  unreachable
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) #2

; 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 = { inlinehint 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 #3 = { 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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = !{!12, !6, i64 0}
!12 = !{!"_ZTSN4treeIxE4edgeE", !6, i64 0, !6, i64 4, !10, i64 8}
!13 = !{!12, !10, i64 8}
!14 = !{!12, !6, i64 4}
!15 = distinct !{!15, !16}
!16 = !{!"llvm.loop.mustprogress"}
!17 = distinct !{!17, !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"

%class.tree = type { [1000020 x %"struct.tree<long long>::edge"], [500010 x i32], i32 }
%"struct.tree<long long>::edge" = type { i32, i32, i64 }

@dp = external dso_local local_unnamed_addr global [500010 x [2 x i64]], align 16
@tr = external dso_local local_unnamed_addr global %class.tree, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local void @_Z3dfsii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = alloca i64, align 8
  %4 = alloca i64, align 8
  %5 = sext i32 %0 to i64
  %6 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %5
  store i64 0, ptr %6, align 16, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %3) #3
  store i64 0, ptr %3, align 8, !tbaa !5
  %7 = getelementptr inbounds [500010 x i32], ptr getelementptr inbounds (i8, ptr @tr, i64 16000320), i64 0, i64 %5
  %.028 = load i32, ptr %7, align 4, !tbaa !9
  %.not29 = icmp eq i32 %.028, 0
  br i1 %.not29, label %._crit_edge.thread, label %.lr.ph

._crit_edge.thread:                               ; preds = %2
  store i64 0, ptr %6, align 16, !tbaa !5
  br label %._crit_edge36

._crit_edge:                                      ; preds = %23
  %.02231.pre = load i32, ptr %7, align 4, !tbaa !9
  store i64 %24, ptr %6, align 16, !tbaa !5
  %.not2532 = icmp eq i32 %.02231.pre, 0
  br i1 %.not2532, label %._crit_edge36, label %.lr.ph35

.lr.ph35:                                         ; preds = %._crit_edge
  %8 = getelementptr inbounds i8, ptr %6, i64 8
  br label %26

.lr.ph:                                           ; preds = %2, %23
  %9 = phi i64 [ %24, %23 ], [ 0, %2 ]
  %.030 = phi i32 [ %.0, %23 ], [ %.028, %2 ]
  %10 = sext i32 %.030 to i64
  %11 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %10
  %12 = load i32, ptr %11, align 8, !tbaa !11
  %13 = icmp eq i32 %12, %1
  br i1 %13, label %23, label %14

14:                                               ; preds = %.lr.ph
  call void @_Z3dfsii(i32 noundef %12, i32 noundef %0)
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %4) #3
  %15 = getelementptr inbounds i8, ptr %11, i64 8
  %16 = load i64, ptr %15, align 8, !tbaa !13
  %17 = sext i32 %12 to i64
  %18 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %17
  %19 = load i64, ptr %18, align 16, !tbaa !5
  %20 = add nsw i64 %19, %16
  store i64 %20, ptr %4, align 8, !tbaa !5
  %21 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %3, ptr noundef nonnull align 8 dereferenceable(8) %4)
  %22 = load i64, ptr %21, align 8, !tbaa !5
  store i64 %22, ptr %3, align 8, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %4) #3
  br label %23

23:                                               ; preds = %.lr.ph, %14
  %24 = phi i64 [ %9, %.lr.ph ], [ %22, %14 ]
  %25 = getelementptr inbounds i8, ptr %11, i64 4
  %.0 = load i32, ptr %25, align 4, !tbaa !9
  %.not = icmp eq i32 %.0, 0
  br i1 %.not, label %._crit_edge, label %.lr.ph, !llvm.loop !14

._crit_edge36:                                    ; preds = %41, %._crit_edge.thread, %._crit_edge
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %3) #3
  ret void

26:                                               ; preds = %.lr.ph35, %41
  %.02233 = phi i32 [ %.02231.pre, %.lr.ph35 ], [ %.022, %41 ]
  %27 = sext i32 %.02233 to i64
  %28 = getelementptr inbounds [1000020 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %27
  %29 = load i32, ptr %28, align 8, !tbaa !11
  %30 = icmp eq i32 %29, %1
  br i1 %30, label %41, label %31

31:                                               ; preds = %26
  %32 = getelementptr inbounds i8, ptr %28, i64 8
  %33 = load i64, ptr %32, align 8, !tbaa !13
  %34 = sext i32 %29 to i64
  %35 = getelementptr inbounds [500010 x [2 x i64]], ptr @dp, i64 0, i64 %34
  %36 = load i64, ptr %35, align 16, !tbaa !5
  %37 = load i64, ptr %8, align 8, !tbaa !5
  %38 = add i64 %33, %36
  %39 = sub i64 %24, %38
  %40 = add nsw i64 %39, %37
  store i64 %40, ptr %8, align 8, !tbaa !5
  br label %41

41:                                               ; preds = %26, %31
  %42 = getelementptr inbounds i8, ptr %28, i64 4
  %.022 = load i32, ptr %42, align 4, !tbaa !9
  %.not25 = icmp eq i32 %.022, 0
  br i1 %.not25, label %._crit_edge36, label %26, !llvm.loop !16
}

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

; Function Attrs: inlinehint mustprogress nounwind uwtable
declare dso_local noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8), ptr noundef nonnull align 8 dereferenceable(8)) local_unnamed_addr #2

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

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 nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #2 = { inlinehint 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 #3 = { 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, !10, i64 0}
!10 = !{!"int", !7, i64 0}
!11 = !{!12, !10, i64 0}
!12 = !{!"_ZTSN4treeIxE4edgeE", !10, i64 0, !10, i64 4, !6, i64 8}
!13 = !{!12, !6, i64 8}
!14 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}


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