; LLVM IR from Tunghohin/Competitive_Coding :: Luogu/p1273.ll (Z3dfsi)

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

@n = external dso_local global i32, align 4
@m = external dso_local global i32, align 4
@val = external dso_local global [3010 x i32], align 16
@dp = external dso_local global [3010 x [3010 x i64]], align 16
@tr = external dso_local global %class.tree, align 8

; Function Attrs: mustprogress uwtable
define dso_local noundef i32 @_Z3dfsi(i32 noundef %0) #0 {
  %2 = alloca i32, align 4
  %3 = alloca i32, align 4
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i32, align 4
  %7 = alloca i32, align 4
  %8 = alloca i32, align 4
  %9 = alloca i32, align 4
  %10 = alloca i32, align 4
  %11 = alloca i32, align 4
  %12 = alloca i64, align 8
  store i32 %0, ptr %3, align 4, !tbaa !5
  %13 = load i32, ptr %3, align 4, !tbaa !5
  %14 = load i32, ptr @n, align 4, !tbaa !5
  %15 = load i32, ptr @m, align 4, !tbaa !5
  %16 = sub nsw i32 %14, %15
  %17 = icmp sgt i32 %13, %16
  br i1 %17, label %18, label %28

18:                                               ; preds = %1
  %19 = load i32, ptr %3, align 4, !tbaa !5
  %20 = sext i32 %19 to i64
  %21 = getelementptr inbounds [3010 x i32], ptr @val, i64 0, i64 %20
  %22 = load i32, ptr %21, align 4, !tbaa !5
  %23 = sext i32 %22 to i64
  %24 = load i32, ptr %3, align 4, !tbaa !5
  %25 = sext i32 %24 to i64
  %26 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %25
  %27 = getelementptr inbounds [3010 x i64], ptr %26, i64 0, i64 1
  store i64 %23, ptr %27, align 8, !tbaa !9
  store i32 1, ptr %2, align 4
  br label %122

28:                                               ; preds = %1
  call void @llvm.lifetime.start.p0(i64 4, ptr %4) #3
  store i32 0, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.start.p0(i64 4, ptr %6) #3
  %29 = load i32, ptr %3, align 4, !tbaa !5
  %30 = sext i32 %29 to i64
  %31 = getelementptr inbounds [3010 x i32], ptr getelementptr inbounds (%class.tree, ptr @tr, i32 0, i32 1), i64 0, i64 %30
  %32 = load i32, ptr %31, align 4, !tbaa !5
  store i32 %32, ptr %6, align 4, !tbaa !5
  br label %33

33:                                               ; preds = %114, %28
  %34 = load i32, ptr %6, align 4, !tbaa !5
  %35 = icmp ne i32 %34, 0
  br i1 %35, label %37, label %36

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

37:                                               ; preds = %33
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #3
  %38 = load i32, ptr %6, align 4, !tbaa !5
  %39 = sext i32 %38 to i64
  %40 = getelementptr inbounds [3010 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %39
  %41 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %40, i32 0, i32 0
  %42 = load i32, ptr %41, align 8, !tbaa !11
  store i32 %42, ptr %8, align 4, !tbaa !5
  %43 = load i32, ptr %8, align 4, !tbaa !5
  %44 = call noundef i32 @_Z3dfsi(i32 noundef %43)
  store i32 %44, ptr %5, align 4, !tbaa !5
  %45 = load i32, ptr %5, align 4, !tbaa !5
  %46 = load i32, ptr %4, align 4, !tbaa !5
  %47 = add nsw i32 %46, %45
  store i32 %47, ptr %4, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #3
  %48 = load i32, ptr %6, align 4, !tbaa !5
  %49 = sext i32 %48 to i64
  %50 = getelementptr inbounds [3010 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %49
  %51 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %50, i32 0, i32 2
  %52 = load i64, ptr %51, align 8, !tbaa !13
  %53 = trunc i64 %52 to i32
  store i32 %53, ptr %9, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %10) #3
  %54 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %54, ptr %10, align 4, !tbaa !5
  br label %55

55:                                               ; preds = %110, %37
  %56 = load i32, ptr %10, align 4, !tbaa !5
  %57 = icmp sgt i32 %56, 0
  br i1 %57, label %59, label %58

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

59:                                               ; preds = %55
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #3
  store i32 1, ptr %11, align 4, !tbaa !5
  br label %60

60:                                               ; preds = %106, %59
  %61 = load i32, ptr %11, align 4, !tbaa !5
  %62 = load i32, ptr %5, align 4, !tbaa !5
  %63 = icmp sle i32 %61, %62
  br i1 %63, label %65, label %64

64:                                               ; preds = %60
  store i32 8, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #3
  br label %109

65:                                               ; preds = %60
  %66 = load i32, ptr %10, align 4, !tbaa !5
  %67 = load i32, ptr %11, align 4, !tbaa !5
  %68 = sub nsw i32 %66, %67
  %69 = icmp sge i32 %68, 0
  br i1 %69, label %70, label %105

70:                                               ; preds = %65
  %71 = load i32, ptr %3, align 4, !tbaa !5
  %72 = sext i32 %71 to i64
  %73 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %72
  %74 = load i32, ptr %10, align 4, !tbaa !5
  %75 = sext i32 %74 to i64
  %76 = getelementptr inbounds [3010 x i64], ptr %73, i64 0, i64 %75
  call void @llvm.lifetime.start.p0(i64 8, ptr %12) #3
  %77 = load i32, ptr %3, align 4, !tbaa !5
  %78 = sext i32 %77 to i64
  %79 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %78
  %80 = load i32, ptr %10, align 4, !tbaa !5
  %81 = load i32, ptr %11, align 4, !tbaa !5
  %82 = sub nsw i32 %80, %81
  %83 = sext i32 %82 to i64
  %84 = getelementptr inbounds [3010 x i64], ptr %79, i64 0, i64 %83
  %85 = load i64, ptr %84, align 8, !tbaa !9
  %86 = load i32, ptr %8, align 4, !tbaa !5
  %87 = sext i32 %86 to i64
  %88 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %87
  %89 = load i32, ptr %11, align 4, !tbaa !5
  %90 = sext i32 %89 to i64
  %91 = getelementptr inbounds [3010 x i64], ptr %88, i64 0, i64 %90
  %92 = load i64, ptr %91, align 8, !tbaa !9
  %93 = add nsw i64 %85, %92
  %94 = load i32, ptr %9, align 4, !tbaa !5
  %95 = sext i32 %94 to i64
  %96 = sub nsw i64 %93, %95
  store i64 %96, ptr %12, align 8, !tbaa !9
  %97 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %76, ptr noundef nonnull align 8 dereferenceable(8) %12)
  %98 = load i64, ptr %97, align 8, !tbaa !9
  %99 = load i32, ptr %3, align 4, !tbaa !5
  %100 = sext i32 %99 to i64
  %101 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %100
  %102 = load i32, ptr %10, align 4, !tbaa !5
  %103 = sext i32 %102 to i64
  %104 = getelementptr inbounds [3010 x i64], ptr %101, i64 0, i64 %103
  store i64 %98, ptr %104, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr %12) #3
  br label %105

105:                                              ; preds = %70, %65
  br label %106

106:                                              ; preds = %105
  %107 = load i32, ptr %11, align 4, !tbaa !5
  %108 = add nsw i32 %107, 1
  store i32 %108, ptr %11, align 4, !tbaa !5
  br label %60, !llvm.loop !14

109:                                              ; preds = %64
  br label %110

110:                                              ; preds = %109
  %111 = load i32, ptr %10, align 4, !tbaa !5
  %112 = add nsw i32 %111, -1
  store i32 %112, ptr %10, align 4, !tbaa !5
  br label %55, !llvm.loop !16

113:                                              ; preds = %58
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #3
  br label %114

114:                                              ; preds = %113
  %115 = load i32, ptr %6, align 4, !tbaa !5
  %116 = sext i32 %115 to i64
  %117 = getelementptr inbounds [3010 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %116
  %118 = getelementptr inbounds %"struct.tree<long long>::edge", ptr %117, i32 0, i32 1
  %119 = load i32, ptr %118, align 4, !tbaa !17
  store i32 %119, ptr %6, align 4, !tbaa !5
  br label %33, !llvm.loop !18

120:                                              ; preds = %36
  %121 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %121, ptr %2, align 4
  store i32 1, ptr %7, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %5) #3
  call void @llvm.lifetime.end.p0(i64 4, ptr %4) #3
  br label %122

122:                                              ; preds = %120, %18
  %123 = load i32, ptr %2, align 4
  ret i32 %123
}

; 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 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = !{!12, !6, i64 4}
!18 = distinct !{!18, !15}


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

@n = external dso_local local_unnamed_addr global i32, align 4
@m = external dso_local local_unnamed_addr global i32, align 4
@val = external dso_local local_unnamed_addr global [3010 x i32], align 16
@dp = external dso_local global [3010 x [3010 x i64]], align 16
@tr = external dso_local local_unnamed_addr global %class.tree, align 8

; Function Attrs: mustprogress nounwind uwtable
define dso_local noundef i32 @_Z3dfsi(i32 noundef %0) local_unnamed_addr #0 {
  %2 = alloca i64, align 8
  %3 = load i32, ptr @n, align 4, !tbaa !5
  %4 = load i32, ptr @m, align 4, !tbaa !5
  %5 = sub nsw i32 %3, %4
  %6 = icmp slt i32 %5, %0
  %7 = sext i32 %0 to i64
  br i1 %6, label %8, label %13

8:                                                ; preds = %1
  %9 = getelementptr inbounds [3010 x i32], ptr @val, i64 0, i64 %7
  %10 = load i32, ptr %9, align 4, !tbaa !5
  %11 = sext i32 %10 to i64
  %12 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %7, i64 1
  store i64 %11, ptr %12, align 8, !tbaa !9
  br label %.loopexit

13:                                               ; preds = %1
  %14 = getelementptr inbounds [3010 x i32], ptr getelementptr inbounds (i8, ptr @tr, i64 48160), i64 0, i64 %7
  %.03442 = load i32, ptr %14, align 4, !tbaa !5
  %.not43 = icmp eq i32 %.03442, 0
  br i1 %.not43, label %.loopexit, label %.lr.ph

.lr.ph:                                           ; preds = %13, %._crit_edge41
  %.03445 = phi i32 [ %.034, %._crit_edge41 ], [ %.03442, %13 ]
  %.03344 = phi i32 [ %19, %._crit_edge41 ], [ 0, %13 ]
  %15 = sext i32 %.03445 to i64
  %16 = getelementptr inbounds [3010 x %"struct.tree<long long>::edge"], ptr @tr, i64 0, i64 %15
  %17 = load i32, ptr %16, align 8, !tbaa !11
  %18 = call noundef i32 @_Z3dfsi(i32 noundef %17)
  %19 = add nsw i32 %18, %.03344
  %20 = icmp sgt i32 %19, 0
  br i1 %20, label %.preheader.lr.ph, label %._crit_edge41

.preheader.lr.ph:                                 ; preds = %.lr.ph
  %21 = getelementptr inbounds i8, ptr %16, i64 8
  %22 = load i64, ptr %21, align 8, !tbaa !13
  %.not3738 = icmp slt i32 %18, 1
  %23 = sext i32 %17 to i64
  %sext = shl i64 %22, 32
  %24 = ashr exact i64 %sext, 32
  br i1 %.not3738, label %._crit_edge41, label %.preheader.preheader

.preheader.preheader:                             ; preds = %.preheader.lr.ph
  %25 = add nuw i32 %18, 1
  %26 = zext nneg i32 %19 to i64
  %wide.trip.count = zext i32 %25 to i64
  br label %.preheader

.preheader:                                       ; preds = %.preheader.preheader, %._crit_edge
  %indvars.iv49 = phi i64 [ %26, %.preheader.preheader ], [ %indvars.iv.next50, %._crit_edge ]
  %27 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %7, i64 %indvars.iv49
  br label %30

._crit_edge41:                                    ; preds = %._crit_edge, %.preheader.lr.ph, %.lr.ph
  %28 = getelementptr inbounds i8, ptr %16, i64 4
  %.034 = load i32, ptr %28, align 4, !tbaa !5
  %.not = icmp eq i32 %.034, 0
  br i1 %.not, label %.loopexit, label %.lr.ph, !llvm.loop !14

._crit_edge:                                      ; preds = %42
  %indvars.iv.next50 = add nsw i64 %indvars.iv49, -1
  %29 = icmp sgt i64 %indvars.iv49, 1
  br i1 %29, label %.preheader, label %._crit_edge41, !llvm.loop !16

30:                                               ; preds = %.preheader, %42
  %indvars.iv = phi i64 [ 1, %.preheader ], [ %indvars.iv.next, %42 ]
  %31 = sub nsw i64 %indvars.iv49, %indvars.iv
  %32 = icmp sgt i64 %31, -1
  br i1 %32, label %33, label %42

33:                                               ; preds = %30
  call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #3
  %34 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %7, i64 %31
  %35 = load i64, ptr %34, align 8, !tbaa !9
  %36 = getelementptr inbounds [3010 x [3010 x i64]], ptr @dp, i64 0, i64 %23, i64 %indvars.iv
  %37 = load i64, ptr %36, align 8, !tbaa !9
  %38 = sub i64 %35, %24
  %39 = add i64 %38, %37
  store i64 %39, ptr %2, align 8, !tbaa !9
  %40 = call noundef nonnull align 8 dereferenceable(8) ptr @_ZSt3maxIxERKT_S2_S2_(ptr noundef nonnull align 8 dereferenceable(8) %27, ptr noundef nonnull align 8 dereferenceable(8) %2)
  %41 = load i64, ptr %40, align 8, !tbaa !9
  store i64 %41, ptr %27, align 8, !tbaa !9
  call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #3
  br label %42

42:                                               ; preds = %30, %33
  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
  %exitcond.not = icmp eq i64 %indvars.iv.next, %wide.trip.count
  br i1 %exitcond.not, label %._crit_edge, label %30, !llvm.loop !17

.loopexit:                                        ; preds = %._crit_edge41, %13, %8
  %.032 = phi i32 [ 1, %8 ], [ 0, %13 ], [ %19, %._crit_edge41 ]
  ret i32 %.032
}

; 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 = !{!"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 = distinct !{!14, !15}
!15 = !{!"llvm.loop.mustprogress"}
!16 = distinct !{!16, !15}
!17 = distinct !{!17, !15}


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