; LLVM IR from sunkafei/xcpc-algorithm-templates :: 算法/树算法/限定距离的子树问题.ll (Z6travelii)

; 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"

@id = external dso_local global [1010000 x i32], align 16
@l = external dso_local global [1010000 x i32], align 16
@r = external dso_local global [1010000 x i32], align 16
@sum = external dso_local global [1010000 x i64], 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: mustprogress nounwind uwtable
define dso_local noundef i64 @_Z6travelii(i32 noundef %0, i32 noundef %1) #1 {
  %3 = alloca i64, align 8
  %4 = alloca i32, align 4
  %5 = alloca i32, align 4
  %6 = alloca i64, align 8
  %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 i32, align 4
  %13 = alloca i64, align 8
  store i32 %0, ptr %4, align 4, !tbaa !5
  store i32 %1, ptr %5, align 4, !tbaa !5
  %14 = load i32, ptr %5, align 4, !tbaa !5
  %15 = icmp slt i32 %14, 0
  br i1 %15, label %16, label %17

16:                                               ; preds = %2
  store i64 0, ptr %3, align 8
  br label %75

17:                                               ; preds = %2
  call void @llvm.lifetime.start.p0(i64 8, ptr %6) #2
  store i64 0, ptr %6, align 8, !tbaa !9
  call void @llvm.lifetime.start.p0(i64 4, ptr %7) #2
  %18 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %18, ptr %7, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %8) #2
  %19 = load i32, ptr %4, align 4, !tbaa !5
  store i32 %19, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %9) #2
  store i32 0, ptr %9, align 4, !tbaa !5
  br label %20

20:                                               ; preds = %69, %17
  %21 = load i32, ptr %9, align 4, !tbaa !5
  %22 = load i32, ptr %5, align 4, !tbaa !5
  %23 = icmp sle i32 %21, %22
  br i1 %23, label %25, label %24

24:                                               ; preds = %20
  store i32 2, ptr %10, align 4
  br label %72

25:                                               ; preds = %20
  call void @llvm.lifetime.start.p0(i64 4, ptr %11) #2
  %26 = load i32, ptr %7, align 4, !tbaa !5
  %27 = sext i32 %26 to i64
  %28 = getelementptr inbounds [1010000 x i32], ptr @id, i64 0, i64 %27
  %29 = load i32, ptr %28, align 4, !tbaa !5
  store i32 %29, ptr %11, align 4, !tbaa !5
  call void @llvm.lifetime.start.p0(i64 4, ptr %12) #2
  %30 = load i32, ptr %8, align 4, !tbaa !5
  %31 = sext i32 %30 to i64
  %32 = getelementptr inbounds [1010000 x i32], ptr @id, i64 0, i64 %31
  %33 = load i32, ptr %32, align 4, !tbaa !5
  store i32 %33, ptr %12, align 4, !tbaa !5
  %34 = load i32, ptr %7, align 4, !tbaa !5
  %35 = icmp eq i32 %34, 0
  br i1 %35, label %43, label %36

36:                                               ; preds = %25
  %37 = load i32, ptr %8, align 4, !tbaa !5
  %38 = icmp eq i32 %37, 0
  br i1 %38, label %43, label %39

39:                                               ; preds = %36
  %40 = load i32, ptr %11, align 4, !tbaa !5
  %41 = load i32, ptr %12, align 4, !tbaa !5
  %42 = icmp sgt i32 %40, %41
  br i1 %42, label %43, label %44

43:                                               ; preds = %39, %36, %25
  store i32 2, ptr %10, align 4
  br label %66

44:                                               ; preds = %39
  call void @llvm.lifetime.start.p0(i64 8, ptr %13) #2
  %45 = load i32, ptr %12, align 4, !tbaa !5
  %46 = sext i32 %45 to i64
  %47 = getelementptr inbounds [1010000 x i64], ptr @sum, i64 0, i64 %46
  %48 = load i64, ptr %47, align 8, !tbaa !9
  %49 = load i32, ptr %11, align 4, !tbaa !5
  %50 = sub nsw i32 %49, 1
  %51 = sext i32 %50 to i64
  %52 = getelementptr inbounds [1010000 x i64], ptr @sum, i64 0, i64 %51
  %53 = load i64, ptr %52, align 8, !tbaa !9
  %54 = sub nsw i64 %48, %53
  store i64 %54, ptr %13, align 8, !tbaa !9
  %55 = load i64, ptr %13, align 8, !tbaa !9
  %56 = load i64, ptr %6, align 8, !tbaa !9
  %57 = add nsw i64 %56, %55
  store i64 %57, ptr %6, align 8, !tbaa !9
  %58 = load i32, ptr %7, align 4, !tbaa !5
  %59 = sext i32 %58 to i64
  %60 = getelementptr inbounds [1010000 x i32], ptr @l, i64 0, i64 %59
  %61 = load i32, ptr %60, align 4, !tbaa !5
  store i32 %61, ptr %7, align 4, !tbaa !5
  %62 = load i32, ptr %8, align 4, !tbaa !5
  %63 = sext i32 %62 to i64
  %64 = getelementptr inbounds [1010000 x i32], ptr @r, i64 0, i64 %63
  %65 = load i32, ptr %64, align 4, !tbaa !5
  store i32 %65, ptr %8, align 4, !tbaa !5
  call void @llvm.lifetime.end.p0(i64 8, ptr %13) #2
  store i32 0, ptr %10, align 4
  br label %66

66:                                               ; preds = %44, %43
  call void @llvm.lifetime.end.p0(i64 4, ptr %12) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %11) #2
  %67 = load i32, ptr %10, align 4
  switch i32 %67, label %72 [
    i32 0, label %68
  ]

68:                                               ; preds = %66
  br label %69

69:                                               ; preds = %68
  %70 = load i32, ptr %9, align 4, !tbaa !5
  %71 = add nsw i32 %70, 1
  store i32 %71, ptr %9, align 4, !tbaa !5
  br label %20, !llvm.loop !11

72:                                               ; preds = %66, %24
  call void @llvm.lifetime.end.p0(i64 4, ptr %9) #2
  br label %73

73:                                               ; preds = %72
  %74 = load i64, ptr %6, align 8, !tbaa !9
  store i64 %74, ptr %3, align 8
  store i32 1, ptr %10, align 4
  call void @llvm.lifetime.end.p0(i64 4, ptr %8) #2
  call void @llvm.lifetime.end.p0(i64 4, ptr %7) #2
  call void @llvm.lifetime.end.p0(i64 8, ptr %6) #2
  br label %75

75:                                               ; preds = %73, %16
  %76 = load i64, ptr %3, align 8
  ret i64 %76
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
attributes #1 = { 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 #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++ TBAA"}
!9 = !{!10, !10, i64 0}
!10 = !{!"long long", !7, i64 0}
!11 = distinct !{!11, !12}
!12 = !{!"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"

@id = external dso_local local_unnamed_addr global [1010000 x i32], align 16
@l = external dso_local local_unnamed_addr global [1010000 x i32], align 16
@r = external dso_local local_unnamed_addr global [1010000 x i32], align 16
@sum = external dso_local local_unnamed_addr global [1010000 x i64], align 16

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: none, inaccessiblemem: none) uwtable
define dso_local noundef i64 @_Z6travelii(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = icmp slt i32 %1, 0
  br i1 %3, label %.loopexit, label %.preheader

.preheader:                                       ; preds = %2, %13
  %.02243 = phi i64 [ %22, %13 ], [ 0, %2 ]
  %.02342 = phi i32 [ %24, %13 ], [ %0, %2 ]
  %.02641 = phi i32 [ %27, %13 ], [ 0, %2 ]
  %.02740 = phi i32 [ %26, %13 ], [ %0, %2 ]
  %4 = sext i32 %.02342 to i64
  %5 = getelementptr inbounds [1010000 x i32], ptr @id, i64 0, i64 %4
  %6 = load i32, ptr %5, align 4, !tbaa !5
  %7 = sext i32 %.02740 to i64
  %8 = getelementptr inbounds [1010000 x i32], ptr @id, i64 0, i64 %7
  %9 = load i32, ptr %8, align 4, !tbaa !5
  %10 = icmp eq i32 %.02342, 0
  %11 = icmp eq i32 %.02740, 0
  %or.cond = or i1 %10, %11
  %12 = icmp sgt i32 %6, %9
  %or.cond32 = select i1 %or.cond, i1 true, i1 %12
  br i1 %or.cond32, label %.loopexit, label %13

13:                                               ; preds = %.preheader
  %14 = sext i32 %9 to i64
  %15 = getelementptr inbounds [1010000 x i64], ptr @sum, i64 0, i64 %14
  %16 = load i64, ptr %15, align 8, !tbaa !9
  %17 = add nsw i32 %6, -1
  %18 = sext i32 %17 to i64
  %19 = getelementptr inbounds [1010000 x i64], ptr @sum, i64 0, i64 %18
  %20 = load i64, ptr %19, align 8, !tbaa !9
  %21 = add i64 %16, %.02243
  %22 = sub i64 %21, %20
  %23 = getelementptr inbounds [1010000 x i32], ptr @l, i64 0, i64 %4
  %24 = load i32, ptr %23, align 4, !tbaa !5
  %25 = getelementptr inbounds [1010000 x i32], ptr @r, i64 0, i64 %7
  %26 = load i32, ptr %25, align 4, !tbaa !5
  %27 = add nuw i32 %.02641, 1
  %exitcond.not = icmp eq i32 %.02641, %1
  br i1 %exitcond.not, label %.loopexit, label %.preheader, !llvm.loop !11

.loopexit:                                        ; preds = %.preheader, %13, %2
  %.0 = phi i64 [ 0, %2 ], [ %22, %13 ], [ %.02243, %.preheader ]
  ret i64 %.0
}

attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, 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 = !{!"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 = distinct !{!11, !12}
!12 = !{!"llvm.loop.mustprogress"}


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